diff options
author | Navan Chauhan <navanchauhan@gmail.com> | 2024-08-09 14:55:07 -0600 |
---|---|---|
committer | Navan Chauhan <navanchauhan@gmail.com> | 2024-08-09 14:55:07 -0600 |
commit | d3769010aa3a2975167e97f12da8cc0d8091438f (patch) | |
tree | 9304ab3d40cb9d955a8d8cdfcd4e850dd69015ed | |
parent | c736b833f360576bc0c436bb65eebc4ce05b708a (diff) |
swift-format
-rw-r--r-- | Package.swift | 61 | ||||
-rw-r--r-- | Sources/SwiftLVGL/Core/LVGLObject.swift | 244 | ||||
-rw-r--r-- | Sources/SwiftLVGL/Core/LVGLObjectProtocol.swift | 50 | ||||
-rw-r--r-- | Sources/SwiftLVGL/Enums/Color.swift | 18 | ||||
-rw-r--r-- | Sources/SwiftLVGL/Enums/LVAlignment.swift | 46 | ||||
-rw-r--r-- | Sources/SwiftLVGL/Enums/LVEventCode.swift | 149 | ||||
-rw-r--r-- | Sources/SwiftLVGL/Enums/LVGLDimension.swift | 4 | ||||
-rw-r--r-- | Sources/SwiftLVGL/Utilities/CallbackStore.swift | 19 | ||||
-rw-r--r-- | Sources/SwiftLVGL/Widgets/LVGLButton.swift | 40 | ||||
-rw-r--r-- | Sources/SwiftLVGL/Widgets/LVGLLabel.swift | 30 | ||||
-rw-r--r-- | Sources/SwiftLVGL/Widgets/LVGLSlider.swift | 41 | ||||
-rw-r--r-- | Sources/SwiftLVGL/Widgets/LVGLSpinner.swift | 36 | ||||
-rw-r--r-- | Sources/SwiftLVGL/Widgets/LVGLSwitch.swift | 41 | ||||
-rw-r--r-- | Sources/SwiftLVGLDemo/Main.swift | 50 |
14 files changed, 417 insertions, 412 deletions
diff --git a/Package.swift b/Package.swift index af094a6..6126286 100644 --- a/Package.swift +++ b/Package.swift @@ -1,41 +1,42 @@ // swift-tools-version: 5.10 import PackageDescription -let lvConfPath: String = Context.environment["LV_CONF_PATH"] ?? "\(Context.packageDirectory)/Sources/CLVGL/lv_conf.h" +let lvConfPath: String = + Context.environment["LV_CONF_PATH"] ?? "\(Context.packageDirectory)/Sources/CLVGL/lv_conf.h" #if os(macOS) -let sdlCFlags = [Context.environment["SDL_INCLUDE_PATH"] ?? "-I/opt/homebrew/include"] + let sdlCFlags = [Context.environment["SDL_INCLUDE_PATH"] ?? "-I/opt/homebrew/include"] #else -let sdlCFlags = [Context.environment["SDL_INCLUDE_PATH"] ?? ""] + let sdlCFlags = [Context.environment["SDL_INCLUDE_PATH"] ?? ""] #endif let package = Package( - name: "SwiftLVGL", - products: [ + name: "SwiftLVGL", + products: [ .library(name: "CLVGL", targets: ["CLVGL"]), - .library(name: "SwiftLVGL", targets: ["SwiftLVGL"]), - ], - targets: [ - .target( - name: "CLVGL", - dependencies: [], - exclude: ["lvgl/demos", "lvgl/examples", "lvgl/tests"], - cSettings: [ - .headerSearchPath("lvgl"), - .headerSearchPath("."), - .define("LV_CONF_INCLUDE_SIMPLE"), - .define("LV_CONF_PATH", to: lvConfPath), - .unsafeFlags(sdlCFlags) - ], - linkerSettings: [.unsafeFlags(["-L/opt/homebrew/lib", "-lSDL2"])] - ), - .target( - name: "SwiftLVGL", - dependencies: ["CLVGL"] - ), - .executableTarget( - name: "SwiftLVGLDemo", - dependencies: ["SwiftLVGL"] - ) - ] + .library(name: "SwiftLVGL", targets: ["SwiftLVGL"]), + ], + targets: [ + .target( + name: "CLVGL", + dependencies: [], + exclude: ["lvgl/demos", "lvgl/examples", "lvgl/tests"], + cSettings: [ + .headerSearchPath("lvgl"), + .headerSearchPath("."), + .define("LV_CONF_INCLUDE_SIMPLE"), + .define("LV_CONF_PATH", to: lvConfPath), + .unsafeFlags(sdlCFlags), + ], + linkerSettings: [.unsafeFlags(["-L/opt/homebrew/lib", "-lSDL2"])] + ), + .target( + name: "SwiftLVGL", + dependencies: ["CLVGL"] + ), + .executableTarget( + name: "SwiftLVGLDemo", + dependencies: ["SwiftLVGL"] + ), + ] ) diff --git a/Sources/SwiftLVGL/Core/LVGLObject.swift b/Sources/SwiftLVGL/Core/LVGLObject.swift index 0d23f93..8eb3a09 100644 --- a/Sources/SwiftLVGL/Core/LVGLObject.swift +++ b/Sources/SwiftLVGL/Core/LVGLObject.swift @@ -7,126 +7,126 @@ import CLVGL -public extension LVGLObjectProtocol { - - func setPosition(x: Int32) { - lv_obj_set_x(pointer, x) - } - - func setPosition(y: Int32) { - lv_obj_set_y(pointer, y) - } - - func setPosition(x: Int32, y: Int32) { - lv_obj_set_pos(pointer, x, y) - } - - func setSize(width: Int32) { - lv_obj_set_width(pointer, width) - } - - func setSize(height: Int32) { - lv_obj_set_height(pointer, height) - } - - func setSize(width: Int32, height: Int32) { - lv_obj_set_size(pointer, width, height) - } - - func getDimension(dimension: LVGLDimension) -> Int32 { - switch dimension { - case .width: - return lv_obj_get_width(pointer) - case .height: - return lv_obj_get_height(pointer) - } - } - - func getContentDimension(dimension: LVGLDimension) -> Int32 { - switch dimension { - case .width: - return lv_obj_get_content_width(pointer) - case .height: - return lv_obj_get_content_height(pointer) - } - } - - func getSelfDimension(dimension: LVGLDimension) -> Int32 { - switch dimension { - case .width: - return lv_obj_get_self_width(pointer) - case .height: - return lv_obj_get_self_height(pointer) - } - } - - func setContentSize(width: Int32, height: Int32) { - lv_obj_set_content_width(pointer, width) - lv_obj_set_content_height(pointer, height) - } - - func setContentSize(width: Int32) { - lv_obj_set_content_width(pointer, width) - } - - func setContentSize(height: Int32) { - lv_obj_set_content_height(pointer, height) - } - - func align(alignment: LVAlignment) { - lv_obj_set_align(pointer, alignment.rawValue) - } - - func align(alignment: LVAlignment, xOffset: Int32, yOffset: Int32) { - lv_obj_align(pointer, alignment.rawValue, xOffset, yOffset) - } - - func center() { - lv_obj_center(pointer) - } - - func getParent() -> UnsafeMutablePointer<lv_obj_t>? { - let parentPointer: UnsafeMutablePointer<lv_obj_t>? = lv_obj_get_parent(pointer) - return parentPointer - } - - func setParent(parentPointer: UnsafeMutablePointer<lv_obj_t>) { - lv_obj_set_parent(pointer, parentPointer) - } - - func setCallback( - eventType: LVEventCode, - _ callback: @escaping (UnsafeMutablePointer<lv_event_t>?) -> Void - ) { - callbackStore[UnsafeMutableRawPointer(pointer!)] = callback - - lv_obj_add_event_cb( - pointer, - { cCallback($0) }, - eventType.toLVEventCode(), - nil - ) - } - - func removeCallback() { - callbackStore.removeValue(forKey: UnsafeMutableRawPointer(pointer!)) - } - - mutating func delete() { - if pointer == nil { - print("Pointer already exists") - print("This will be a fatal error in the future") - } else { - lv_obj_delete(pointer) - pointer = nil - - } - } - - func exists() -> Bool { - if pointer == nil { - return false - } - return true - } +extension LVGLObjectProtocol { + + public func setPosition(x: Int32) { + lv_obj_set_x(pointer, x) + } + + public func setPosition(y: Int32) { + lv_obj_set_y(pointer, y) + } + + public func setPosition(x: Int32, y: Int32) { + lv_obj_set_pos(pointer, x, y) + } + + public func setSize(width: Int32) { + lv_obj_set_width(pointer, width) + } + + public func setSize(height: Int32) { + lv_obj_set_height(pointer, height) + } + + public func setSize(width: Int32, height: Int32) { + lv_obj_set_size(pointer, width, height) + } + + public func getDimension(dimension: LVGLDimension) -> Int32 { + switch dimension { + case .width: + return lv_obj_get_width(pointer) + case .height: + return lv_obj_get_height(pointer) + } + } + + public func getContentDimension(dimension: LVGLDimension) -> Int32 { + switch dimension { + case .width: + return lv_obj_get_content_width(pointer) + case .height: + return lv_obj_get_content_height(pointer) + } + } + + public func getSelfDimension(dimension: LVGLDimension) -> Int32 { + switch dimension { + case .width: + return lv_obj_get_self_width(pointer) + case .height: + return lv_obj_get_self_height(pointer) + } + } + + public func setContentSize(width: Int32, height: Int32) { + lv_obj_set_content_width(pointer, width) + lv_obj_set_content_height(pointer, height) + } + + public func setContentSize(width: Int32) { + lv_obj_set_content_width(pointer, width) + } + + public func setContentSize(height: Int32) { + lv_obj_set_content_height(pointer, height) + } + + public func align(alignment: LVAlignment) { + lv_obj_set_align(pointer, alignment.rawValue) + } + + public func align(alignment: LVAlignment, xOffset: Int32, yOffset: Int32) { + lv_obj_align(pointer, alignment.rawValue, xOffset, yOffset) + } + + public func center() { + lv_obj_center(pointer) + } + + public func getParent() -> UnsafeMutablePointer<lv_obj_t>? { + let parentPointer: UnsafeMutablePointer<lv_obj_t>? = lv_obj_get_parent(pointer) + return parentPointer + } + + public func setParent(parentPointer: UnsafeMutablePointer<lv_obj_t>) { + lv_obj_set_parent(pointer, parentPointer) + } + + public func setCallback( + eventType: LVEventCode, + _ callback: @escaping (UnsafeMutablePointer<lv_event_t>?) -> Void + ) { + callbackStore[UnsafeMutableRawPointer(pointer!)] = callback + + lv_obj_add_event_cb( + pointer, + { cCallback($0) }, + eventType.toLVEventCode(), + nil + ) + } + + public func removeCallback() { + callbackStore.removeValue(forKey: UnsafeMutableRawPointer(pointer!)) + } + + public mutating func delete() { + if pointer == nil { + print("Pointer already exists") + print("This will be a fatal error in the future") + } else { + lv_obj_delete(pointer) + pointer = nil + + } + } + + public func exists() -> Bool { + if pointer == nil { + return false + } + return true + } } diff --git a/Sources/SwiftLVGL/Core/LVGLObjectProtocol.swift b/Sources/SwiftLVGL/Core/LVGLObjectProtocol.swift index 47bfe21..06277bb 100644 --- a/Sources/SwiftLVGL/Core/LVGLObjectProtocol.swift +++ b/Sources/SwiftLVGL/Core/LVGLObjectProtocol.swift @@ -1,6 +1,6 @@ // // LVGLObjectProtocol.swift -// +// // // Created by Navan Chauhan on 8/9/24. // @@ -8,39 +8,39 @@ import CLVGL public protocol LVGLObjectProtocol { - var pointer: UnsafeMutablePointer<lv_obj_t>? { get set } + var pointer: UnsafeMutablePointer<lv_obj_t>? { get set } - func setPosition(x: Int32) - func setPosition(y: Int32) - func setPosition(x: Int32, y: Int32) + func setPosition(x: Int32) + func setPosition(y: Int32) + func setPosition(x: Int32, y: Int32) - func setSize(width: Int32, height: Int32) - func setSize(height: Int32) - func setSize(width: Int32) + func setSize(width: Int32, height: Int32) + func setSize(height: Int32) + func setSize(width: Int32) - func getDimension(dimension: LVGLDimension) -> Int32 - func getContentDimension(dimension: LVGLDimension) -> Int32 - func getSelfDimension(dimension: LVGLDimension) -> Int32 + func getDimension(dimension: LVGLDimension) -> Int32 + func getContentDimension(dimension: LVGLDimension) -> Int32 + func getSelfDimension(dimension: LVGLDimension) -> Int32 - func setContentSize(width: Int32, height: Int32) - func setContentSize(width: Int32) - func setContentSize(height: Int32) + func setContentSize(width: Int32, height: Int32) + func setContentSize(width: Int32) + func setContentSize(height: Int32) - func align(alignment: LVAlignment) - func align(alignment: LVAlignment, xOffset: Int32, yOffset: Int32) - func center() + func align(alignment: LVAlignment) + func align(alignment: LVAlignment, xOffset: Int32, yOffset: Int32) + func center() - func getParent() -> UnsafeMutablePointer<lv_obj_t>? - func setParent(parentPointer: UnsafeMutablePointer<lv_obj_t>) + func getParent() -> UnsafeMutablePointer<lv_obj_t>? + func setParent(parentPointer: UnsafeMutablePointer<lv_obj_t>) - func setCallback( - eventType: LVEventCode, _ callback: @escaping (UnsafeMutablePointer<lv_event_t>?) -> Void) - func removeCallback() + func setCallback( + eventType: LVEventCode, _ callback: @escaping (UnsafeMutablePointer<lv_event_t>?) -> Void) + func removeCallback() - mutating func delete() - func exists() -> Bool + mutating func delete() + func exists() -> Bool - /* + /* TODO: func refreshSize() -> bool // lv_obj_refr_size func setLayout(layout: UInt32) // lv_obj_set_layout diff --git a/Sources/SwiftLVGL/Enums/Color.swift b/Sources/SwiftLVGL/Enums/Color.swift index 82f4d0b..1f1208e 100644 --- a/Sources/SwiftLVGL/Enums/Color.swift +++ b/Sources/SwiftLVGL/Enums/Color.swift @@ -1,18 +1,18 @@ // // Color.swift -// +// // // Created by Navan Chauhan on 8/9/24. // public enum Color: UInt32 { - case blue = 0x003a57 - case white = 0xffffff - case red = 0xff0000 - case green = 0x00ff00 - case black = 0x000000 + case blue = 0x003a57 + case white = 0xffffff + case red = 0xff0000 + case green = 0x00ff00 + case black = 0x000000 - func toHex() -> UInt32 { - return self.rawValue - } + func toHex() -> UInt32 { + return self.rawValue + } } diff --git a/Sources/SwiftLVGL/Enums/LVAlignment.swift b/Sources/SwiftLVGL/Enums/LVAlignment.swift index c880734..be8378b 100644 --- a/Sources/SwiftLVGL/Enums/LVAlignment.swift +++ b/Sources/SwiftLVGL/Enums/LVAlignment.swift @@ -1,31 +1,31 @@ // // LVAlignment.swift -// +// // // Created by Navan Chauhan on 8/9/24. // public enum LVAlignment: UInt8 { - case `default` = 0 - case topLeft = 1 - case topMid = 2 - case topRight = 3 - case bottomLeft = 4 - case bottomMid = 5 - case bottomRight = 6 - case leftMid = 7 - case rightMid = 8 - case center = 9 - case outTopLeft = 10 - case outTopMid = 11 - case outTopRight = 12 - case outBottomLeft = 13 - case outBottomMid = 14 - case outBottomRight = 15 - case outLeftTop = 16 - case outLeftMid = 17 - case outLeftBottom = 18 - case outRightTop = 19 - case outRightMid = 20 - case outRightBottom = 21 + case `default` = 0 + case topLeft = 1 + case topMid = 2 + case topRight = 3 + case bottomLeft = 4 + case bottomMid = 5 + case bottomRight = 6 + case leftMid = 7 + case rightMid = 8 + case center = 9 + case outTopLeft = 10 + case outTopMid = 11 + case outTopRight = 12 + case outBottomLeft = 13 + case outBottomMid = 14 + case outBottomRight = 15 + case outLeftTop = 16 + case outLeftMid = 17 + case outLeftBottom = 18 + case outRightTop = 19 + case outRightMid = 20 + case outRightBottom = 21 } diff --git a/Sources/SwiftLVGL/Enums/LVEventCode.swift b/Sources/SwiftLVGL/Enums/LVEventCode.swift index 1275cb7..8f4f7c9 100644 --- a/Sources/SwiftLVGL/Enums/LVEventCode.swift +++ b/Sources/SwiftLVGL/Enums/LVEventCode.swift @@ -1,6 +1,6 @@ // // LVEventCode.swift -// +// // // Created by Navan Chauhan on 8/9/24. // @@ -8,81 +8,80 @@ import CLVGL public enum LVEventCode: Int { - case all = 0 - case pressed - case pressing - case pressLost - case shortClicked - case longPressed - case longPressedRepeat - case clicked - case released - case scrollBegin - case scrollThrowBegin - case scrollEnd - case scroll - case gesture - case key - case rotary - case focused - case defocused - case leave - case hitTest - case indevReset - case hoverOver - case hoverLeave - case coverCheck - case refreshExtDrawSize - case drawMainBegin - case drawMain - case drawMainEnd - case drawPostBegin - case drawPost - case drawPostEnd - case drawTaskAdded - case valueChanged - case insert - case refresh - case ready - case cancel - case create - case delete - case childChanged - case childCreated - case childDeleted - case screenUnloadStart - case screenLoadStart - case screenLoaded - case screenUnloaded - case sizeChanged - case styleChanged - case layoutChanged - case getSelfSize - case invalidateArea - case resolutionChanged - case colorFormatChanged - case refreshRequest - case refreshStart - case refreshReady - case renderStart - case renderReady - case flushStart - case flushFinish - case flushWaitStart - case flushWaitFinish - case vsync - case preprocess - case last = 1000 + case all = 0 + case pressed + case pressing + case pressLost + case shortClicked + case longPressed + case longPressedRepeat + case clicked + case released + case scrollBegin + case scrollThrowBegin + case scrollEnd + case scroll + case gesture + case key + case rotary + case focused + case defocused + case leave + case hitTest + case indevReset + case hoverOver + case hoverLeave + case coverCheck + case refreshExtDrawSize + case drawMainBegin + case drawMain + case drawMainEnd + case drawPostBegin + case drawPost + case drawPostEnd + case drawTaskAdded + case valueChanged + case insert + case refresh + case ready + case cancel + case create + case delete + case childChanged + case childCreated + case childDeleted + case screenUnloadStart + case screenLoadStart + case screenLoaded + case screenUnloaded + case sizeChanged + case styleChanged + case layoutChanged + case getSelfSize + case invalidateArea + case resolutionChanged + case colorFormatChanged + case refreshRequest + case refreshStart + case refreshReady + case renderStart + case renderReady + case flushStart + case flushFinish + case flushWaitStart + case flushWaitFinish + case vsync + case preprocess + case last = 1000 - func toLVEventCode() -> lv_event_code_t { - return lv_event_code_t(rawValue: UInt32(self.rawValue)) // UInt16 on Pi Pico - } + func toLVEventCode() -> lv_event_code_t { + return lv_event_code_t(rawValue: UInt32(self.rawValue)) // UInt16 on Pi Pico + } } - -public extension UnsafeMutablePointer<lv_event_t> { - var eventCode: LVEventCode? { - let rawValue = Int(lv_event_get_code(self).rawValue) - return LVEventCode(rawValue: rawValue) - } +extension UnsafeMutablePointer<lv_event_t> { + public var eventCode: LVEventCode? { + let rawValue = Int(lv_event_get_code(self).rawValue) + return LVEventCode(rawValue: rawValue) + } } diff --git a/Sources/SwiftLVGL/Enums/LVGLDimension.swift b/Sources/SwiftLVGL/Enums/LVGLDimension.swift index b224fff..21ae04c 100644 --- a/Sources/SwiftLVGL/Enums/LVGLDimension.swift +++ b/Sources/SwiftLVGL/Enums/LVGLDimension.swift @@ -1,10 +1,10 @@ // // LVGLDimension.swift -// +// // // Created by Navan Chauhan on 8/9/24. // public enum LVGLDimension { - case width, height + case width, height } diff --git a/Sources/SwiftLVGL/Utilities/CallbackStore.swift b/Sources/SwiftLVGL/Utilities/CallbackStore.swift index 879bcd8..a45f61a 100644 --- a/Sources/SwiftLVGL/Utilities/CallbackStore.swift +++ b/Sources/SwiftLVGL/Utilities/CallbackStore.swift @@ -1,20 +1,21 @@ // // CallbackStore.swift -// +// // // Created by Navan Chauhan on 8/9/24. // import CLVGL -public var callbackStore: [UnsafeMutableRawPointer: (UnsafeMutablePointer<lv_event_t>?) -> Void] = [:] +public var callbackStore: [UnsafeMutableRawPointer: (UnsafeMutablePointer<lv_event_t>?) -> Void] = + [:] public func cCallback(_ event: UnsafeMutablePointer<lv_event_t>?) { - guard let event = event else { return } - let target = lv_event_get_target(event) - if let targetPointer = UnsafeMutableRawPointer(target), - let callback = callbackStore[targetPointer] - { - callback(event) - } + guard let event = event else { return } + let target = lv_event_get_target(event) + if let targetPointer = UnsafeMutableRawPointer(target), + let callback = callbackStore[targetPointer] + { + callback(event) + } } diff --git a/Sources/SwiftLVGL/Widgets/LVGLButton.swift b/Sources/SwiftLVGL/Widgets/LVGLButton.swift index 62b1659..694b764 100644 --- a/Sources/SwiftLVGL/Widgets/LVGLButton.swift +++ b/Sources/SwiftLVGL/Widgets/LVGLButton.swift @@ -12,27 +12,27 @@ 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) + 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 792144d..070da16 100644 --- a/Sources/SwiftLVGL/Widgets/LVGLLabel.swift +++ b/Sources/SwiftLVGL/Widgets/LVGLLabel.swift @@ -1,6 +1,6 @@ // // LVGLLabel.swift -// +// // // Created by Navan Chauhan on 8/9/24. // @@ -8,24 +8,26 @@ import CLVGL public protocol LVGLLabelProtocol: LVGLObjectProtocol { - func setText(_ text: String) + func setText(_ text: String) } public struct LVGLLabel: LVGLLabelProtocol { - public var pointer: UnsafeMutablePointer<lv_obj_t>? + 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 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) - } + 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 3a7abcc..c397b4e 100644 --- a/Sources/SwiftLVGL/Widgets/LVGLSlider.swift +++ b/Sources/SwiftLVGL/Widgets/LVGLSlider.swift @@ -1,6 +1,6 @@ // // LVGLSlider.swift -// +// // // Created by Navan Chauhan on 8/9/24. // @@ -8,31 +8,30 @@ import CLVGL public protocol LVGLSliderProtocol: LVGLObjectProtocol { - func setValue(value: Int32, enableAnimation: Bool) + 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 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) - public func setValue(value: Int32 = 0, enableAnimation: Bool = true) { + 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 9a97bb2..2650b07 100644 --- a/Sources/SwiftLVGL/Widgets/LVGLSpinner.swift +++ b/Sources/SwiftLVGL/Widgets/LVGLSpinner.swift @@ -1,6 +1,6 @@ // // LVGLSpinner.swift -// +// // // Created by Navan Chauhan on 8/9/24. // @@ -8,27 +8,27 @@ import CLVGL public protocol LVGLSpinnerProtocol: LVGLObjectProtocol { - func setAnimation(animationTime: UInt32, arcAngle: UInt32) + func setAnimation(animationTime: UInt32, arcAngle: UInt32) } public struct LVGLSpinner: LVGLSpinnerProtocol { - public var pointer: UnsafeMutablePointer<lv_obj_t>? + 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 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) - } + 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 a3580d1..c2c4263 100644 --- a/Sources/SwiftLVGL/Widgets/LVGLSwitch.swift +++ b/Sources/SwiftLVGL/Widgets/LVGLSwitch.swift @@ -1,6 +1,6 @@ // // LVGLSwitch.swift -// +// // // Created by Navan Chauhan on 8/9/24. // @@ -8,18 +8,21 @@ import CLVGL public protocol LVGLSwitchProtocol: LVGLObjectProtocol { - var checked: Bool { get set} - func isChecked() -> Bool - mutating func setChecked(_ to: Bool) + 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 var checked: Bool + public var pointer: UnsafeMutablePointer<lv_obj_t>? - public init(_ checked: Bool = false, alignment: LVAlignment = .center, xOffset: Int32 = 0, yOffset: Int32 = 0) { + 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") + fatalError("Failed to create switch") } self.pointer = mySwitch self.checked = checked @@ -27,18 +30,18 @@ public struct LVGLSwitch: LVGLSwitchProtocol { align(alignment: alignment, xOffset: xOffset, yOffset: yOffset) setChecked(checked) - } + } - public func isChecked() -> Bool { - return 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 + mutating public func setChecked(_ to: Bool) { + if to { + lv_obj_add_state(pointer, 1) + } else { + lv_obj_remove_state(pointer, 1) } + self.checked = to + } } diff --git a/Sources/SwiftLVGLDemo/Main.swift b/Sources/SwiftLVGLDemo/Main.swift index 2e53d4d..66d369c 100644 --- a/Sources/SwiftLVGLDemo/Main.swift +++ b/Sources/SwiftLVGLDemo/Main.swift @@ -3,31 +3,31 @@ import SwiftLVGL @main struct Main { - static func main() { - lv_init() - let _ = lv_sdl_window_create(480, 480) - let _ = lv_sdl_mouse_create() - - var myCounter: Int = 0 - - let label = LVGLLabel("", alignment: .bottomMid) - let button = LVGLButton("Click Me", eventType: .pressed) { event in - if let event = event, event.eventCode == .pressed { - myCounter += 1 - label.setText("You clicked the button \(myCounter) times") - } - } - let _ = LVGLButton("Shift Button", alignment: .bottomRight, eventType: .pressed) { event in - button.align(alignment: .leftMid) - } - - let _ = LVGLSlider("", alignment: .topMid, yOffset: 50) + static func main() { + lv_init() + let _ = lv_sdl_window_create(480, 480) + let _ = lv_sdl_mouse_create() - let _ = LVGLSwitch(alignment: .rightMid) - - while true { - lv_timer_handler() - usleep(5000) - } + var myCounter: Int = 0 + + let label = LVGLLabel("", alignment: .bottomMid) + let button = LVGLButton("Click Me", eventType: .pressed) { event in + if let event = event, event.eventCode == .pressed { + myCounter += 1 + label.setText("You clicked the button \(myCounter) times") + } + } + let _ = LVGLButton("Shift Button", alignment: .bottomRight, eventType: .pressed) { event in + button.align(alignment: .leftMid) + } + + let _ = LVGLSlider("", alignment: .topMid, yOffset: 50) + + let _ = LVGLSwitch(alignment: .rightMid) + + while true { + lv_timer_handler() + usleep(5000) } + } } |