aboutsummaryrefslogtreecommitdiff
path: root/Sources/SwiftLVGL/Widgets
diff options
context:
space:
mode:
Diffstat (limited to 'Sources/SwiftLVGL/Widgets')
-rw-r--r--Sources/SwiftLVGL/Widgets/LVGLButton.swift36
-rw-r--r--Sources/SwiftLVGL/Widgets/LVGLLabel.swift27
-rw-r--r--Sources/SwiftLVGL/Widgets/LVGLSlider.swift34
-rw-r--r--Sources/SwiftLVGL/Widgets/LVGLSpinner.swift30
-rw-r--r--Sources/SwiftLVGL/Widgets/LVGLSwitch.swift40
5 files changed, 156 insertions, 11 deletions
diff --git a/Sources/SwiftLVGL/Widgets/LVGLButton.swift b/Sources/SwiftLVGL/Widgets/LVGLButton.swift
index a806437..62b1659 100644
--- a/Sources/SwiftLVGL/Widgets/LVGLButton.swift
+++ b/Sources/SwiftLVGL/Widgets/LVGLButton.swift
@@ -1,8 +1,38 @@
//
-// File.swift
-//
+// LVGLButton.swift
+//
//
// Created by Navan Chauhan on 8/9/24.
//
-import Foundation
+import CLVGL
+
+public protocol LVGLButtonProtocol: LVGLObjectProtocol {
+
+}
+
+public struct LVGLButton: LVGLButtonProtocol {
+ public let label: LVGLLabel
+ public var pointer: UnsafeMutablePointer<lv_obj_t>?
+
+ public init(
+ _ text: String, alignment: LVAlignment = .center, textColor: Color = .blue,
+ xSize: Int32 = 120, ySize: Int32 = 50, xOffset: Int32 = 0, yOffset: Int32 = 0,
+ eventType: LVEventCode = .all,
+ callback: @escaping (UnsafeMutablePointer<lv_event_t>?) -> Void = { _ in }
+ ) {
+ guard let button = lv_button_create(lv_screen_active()) else {
+ fatalError("Failed to create button")
+ }
+ self.pointer = button
+ self.label = LVGLLabel(
+ text, alignment: alignment, xOffset: xOffset, yOffset: yOffset
+ )
+ self.label.setParent(parentPointer: pointer!)
+ align(alignment: alignment, xOffset: xOffset, yOffset: yOffset)
+ setSize(width: xSize, height: ySize)
+ setCallback(eventType: eventType, callback)
+
+ }
+
+}
diff --git a/Sources/SwiftLVGL/Widgets/LVGLLabel.swift b/Sources/SwiftLVGL/Widgets/LVGLLabel.swift
index a806437..792144d 100644
--- a/Sources/SwiftLVGL/Widgets/LVGLLabel.swift
+++ b/Sources/SwiftLVGL/Widgets/LVGLLabel.swift
@@ -1,8 +1,31 @@
//
-// File.swift
+// LVGLLabel.swift
//
//
// Created by Navan Chauhan on 8/9/24.
//
-import Foundation
+import CLVGL
+
+public protocol LVGLLabelProtocol: LVGLObjectProtocol {
+ func setText(_ text: String)
+}
+
+public struct LVGLLabel: LVGLLabelProtocol {
+ public var pointer: UnsafeMutablePointer<lv_obj_t>?
+
+ public init(_ text: String, alignment: LVAlignment = .center, xOffset: Int32 = 0, yOffset: Int32 = 0) {
+ guard let label = lv_label_create(lv_screen_active()) else {
+ fatalError("Failed to create label")
+ }
+ self.pointer = label
+ setText(text)
+ align(alignment: alignment, xOffset: xOffset, yOffset: yOffset)
+ }
+
+ public func setText(_ text: String) {
+ text.withCString { cString in
+ lv_label_set_text(pointer, cString)
+ }
+ }
+}
diff --git a/Sources/SwiftLVGL/Widgets/LVGLSlider.swift b/Sources/SwiftLVGL/Widgets/LVGLSlider.swift
index a806437..3a7abcc 100644
--- a/Sources/SwiftLVGL/Widgets/LVGLSlider.swift
+++ b/Sources/SwiftLVGL/Widgets/LVGLSlider.swift
@@ -1,8 +1,38 @@
//
-// File.swift
+// LVGLSlider.swift
//
//
// Created by Navan Chauhan on 8/9/24.
//
-import Foundation
+import CLVGL
+
+public protocol LVGLSliderProtocol: LVGLObjectProtocol {
+ func setValue(value: Int32, enableAnimation: Bool)
+}
+
+public struct LVGLSlider: LVGLSliderProtocol {
+ public let label: LVGLLabel
+ public var pointer: UnsafeMutablePointer<lv_obj_t>?
+
+ public init(
+ _ text: String = "0%", alignment: LVAlignment = .center, xOffset: Int32 = 0,
+ yOffset: Int32 = 0
+ ) {
+ guard let slider = lv_slider_create(lv_screen_active()) else {
+ fatalError("Failed to create slider")
+ }
+ self.pointer = slider
+ self.label = LVGLLabel(text, alignment: alignment)
+
+ align(alignment: alignment, xOffset: xOffset, yOffset: yOffset)
+
+ self.label.setParent(parentPointer: slider)
+ }
+
+ public func setValue(value: Int32 = 0, enableAnimation: Bool = true) {
+
+ }
+
+}
+
diff --git a/Sources/SwiftLVGL/Widgets/LVGLSpinner.swift b/Sources/SwiftLVGL/Widgets/LVGLSpinner.swift
index a806437..9a97bb2 100644
--- a/Sources/SwiftLVGL/Widgets/LVGLSpinner.swift
+++ b/Sources/SwiftLVGL/Widgets/LVGLSpinner.swift
@@ -1,8 +1,34 @@
//
-// File.swift
+// LVGLSpinner.swift
//
//
// Created by Navan Chauhan on 8/9/24.
//
-import Foundation
+import CLVGL
+
+public protocol LVGLSpinnerProtocol: LVGLObjectProtocol {
+ func setAnimation(animationTime: UInt32, arcAngle: UInt32)
+}
+
+public struct LVGLSpinner: LVGLSpinnerProtocol {
+ public var pointer: UnsafeMutablePointer<lv_obj_t>?
+
+ public init(
+ animationTime: UInt32 = 100,
+ arcAngle: UInt32 = 200, alignment: LVAlignment = .center, xSize: UInt32 = 100,
+ ySize: UInt32 = 100
+ ) {
+ guard let spinner = lv_spinner_create(lv_screen_active()) else {
+ fatalError("Failed to create spinner")
+ }
+ self.pointer = spinner
+ align(alignment: alignment)
+ setSize(width: 100, height: 100)
+ lv_spinner_set_anim_params(spinner, 1000, 200)
+ }
+
+ public func setAnimation(animationTime: UInt32 = 1000, arcAngle: UInt32 = 200) {
+ lv_spinner_set_anim_params(pointer, animationTime, arcAngle)
+ }
+}
diff --git a/Sources/SwiftLVGL/Widgets/LVGLSwitch.swift b/Sources/SwiftLVGL/Widgets/LVGLSwitch.swift
index a806437..a3580d1 100644
--- a/Sources/SwiftLVGL/Widgets/LVGLSwitch.swift
+++ b/Sources/SwiftLVGL/Widgets/LVGLSwitch.swift
@@ -1,8 +1,44 @@
//
-// File.swift
+// LVGLSwitch.swift
//
//
// Created by Navan Chauhan on 8/9/24.
//
-import Foundation
+import CLVGL
+
+public protocol LVGLSwitchProtocol: LVGLObjectProtocol {
+ var checked: Bool { get set}
+ func isChecked() -> Bool
+ mutating func setChecked(_ to: Bool)
+}
+
+public struct LVGLSwitch: LVGLSwitchProtocol {
+ public var checked: Bool
+ public var pointer: UnsafeMutablePointer<lv_obj_t>?
+
+ public init(_ checked: Bool = false, alignment: LVAlignment = .center, xOffset: Int32 = 0, yOffset: Int32 = 0) {
+ guard let mySwitch = lv_switch_create(lv_screen_active()) else {
+ fatalError("Failed to create switch")
+ }
+ self.pointer = mySwitch
+ self.checked = checked
+
+ align(alignment: alignment, xOffset: xOffset, yOffset: yOffset)
+ setChecked(checked)
+
+ }
+
+ public func isChecked() -> Bool {
+ return checked
+ }
+
+ mutating public func setChecked(_ to: Bool) {
+ if (to) {
+ lv_obj_add_state(pointer, 1)
+ } else {
+ lv_obj_remove_state(pointer, 1)
+ }
+ self.checked = to
+ }
+}