diff options
author | Navan Chauhan <navanchauhan@gmail.com> | 2024-08-09 16:12:27 -0600 |
---|---|---|
committer | Navan Chauhan <navanchauhan@gmail.com> | 2024-08-09 16:12:27 -0600 |
commit | e5618778691851dc5bfc91741f4a61697ce2b07e (patch) | |
tree | 90a9a00ac563658a852cd253e83d6e4aef53ae49 | |
parent | e3eb01acd7e39b89e8161693ce7f16babddbce46 (diff) |
add documentation
-rw-r--r-- | Sources/SwiftLVGL/Core/LVGLObject.swift | 59 | ||||
-rw-r--r-- | Sources/SwiftLVGL/Core/LVGLObjectProtocol.swift | 93 | ||||
-rw-r--r-- | Sources/SwiftLVGL/Utilities/CallbackStore.swift | 9 | ||||
-rw-r--r-- | Sources/SwiftLVGL/Widgets/LVGLButton.swift | 33 | ||||
-rw-r--r-- | Sources/SwiftLVGL/Widgets/LVGLLabel.swift | 18 |
5 files changed, 190 insertions, 22 deletions
diff --git a/Sources/SwiftLVGL/Core/LVGLObject.swift b/Sources/SwiftLVGL/Core/LVGLObject.swift index 8eb3a09..efaad5b 100644 --- a/Sources/SwiftLVGL/Core/LVGLObject.swift +++ b/Sources/SwiftLVGL/Core/LVGLObject.swift @@ -7,32 +7,52 @@ import CLVGL +/// Extension providing default implementations for `LVGLObjectProtocol`. extension LVGLObjectProtocol { + /// Sets the x-coordinate of the object's position. + /// - Parameter x: The new x-coordinate. public func setPosition(x: Int32) { lv_obj_set_x(pointer, x) } + /// Sets the y-coordinate of the object's position. + /// - Parameter y: The new y-coordinate. public func setPosition(y: Int32) { lv_obj_set_y(pointer, y) } + /// Sets both x and y coordinates of the object's position. + /// - Parameters: + /// - x: The new x-coordinate. + /// - y: The new y-coordinate. public func setPosition(x: Int32, y: Int32) { lv_obj_set_pos(pointer, x, y) } + /// Sets the width of the object. + /// - Parameter width: The new width. public func setSize(width: Int32) { lv_obj_set_width(pointer, width) } + /// Sets the height of the object. + /// - Parameter height: The new height. public func setSize(height: Int32) { lv_obj_set_height(pointer, height) } + /// Sets both width and height of the object. + /// - Parameters: + /// - width: The new width. + /// - height: The new height. public func setSize(width: Int32, height: Int32) { lv_obj_set_size(pointer, width, height) } + /// Gets a specific dimension of the object. + /// - Parameter dimension: The dimension to retrieve. + /// - Returns: The value of the specified dimension. public func getDimension(dimension: LVGLDimension) -> Int32 { switch dimension { case .width: @@ -42,6 +62,9 @@ extension LVGLObjectProtocol { } } + /// Gets a specific content dimension of the object. + /// - Parameter dimension: The dimension to retrieve. + /// - Returns: The value of the specified content dimension. public func getContentDimension(dimension: LVGLDimension) -> Int32 { switch dimension { case .width: @@ -51,6 +74,9 @@ extension LVGLObjectProtocol { } } + /// Gets a specific self dimension of the object. + /// - Parameter dimension: The dimension to retrieve. + /// - Returns: The value of the specified self dimension. public func getSelfDimension(dimension: LVGLDimension) -> Int32 { switch dimension { case .width: @@ -60,43 +86,67 @@ extension LVGLObjectProtocol { } } + /// Sets both width and height of the object's content. + /// - Parameters: + /// - width: The new content width. + /// - height: The new content height. public func setContentSize(width: Int32, height: Int32) { lv_obj_set_content_width(pointer, width) lv_obj_set_content_height(pointer, height) } + /// Sets the width of the object's content. + /// - Parameter width: The new content width. public func setContentSize(width: Int32) { lv_obj_set_content_width(pointer, width) } + /// Sets the height of the object's content. + /// - Parameter height: The new content height. public func setContentSize(height: Int32) { lv_obj_set_content_height(pointer, height) } + /// Aligns the object according to the specified alignment. + /// - Parameter alignment: The alignment to apply. public func align(alignment: LVAlignment) { lv_obj_set_align(pointer, alignment.rawValue) } + /// Aligns the object with an offset. + /// - Parameters: + /// - alignment: The alignment to apply. + /// - xOffset: The x-offset from the aligned position. + /// - yOffset: The y-offset from the aligned position. public func align(alignment: LVAlignment, xOffset: Int32, yOffset: Int32) { lv_obj_align(pointer, alignment.rawValue, xOffset, yOffset) } + /// Centers the object within its parent. public func center() { lv_obj_center(pointer) } + /// Gets the parent object. + /// - Returns: A pointer to the parent object, or nil if there is no parent. public func getParent() -> UnsafeMutablePointer<lv_obj_t>? { let parentPointer: UnsafeMutablePointer<lv_obj_t>? = lv_obj_get_parent(pointer) return parentPointer } + /// Sets the parent of the object. + /// - Parameter parentPointer: A pointer to the new parent object. public func setParent(parentPointer: UnsafeMutablePointer<lv_obj_t>) { lv_obj_set_parent(pointer, parentPointer) } + /// Sets a callback for a specific event type. + /// - Parameters: + /// - eventType: The type of event to listen for. + /// - callback: The callback function to be called when the event occurs. public func setCallback( eventType: LVEventCode, - _ callback: @escaping (UnsafeMutablePointer<lv_event_t>?) -> Void + callback: @escaping (UnsafeMutablePointer<lv_event_t>?) -> Void ) { callbackStore[UnsafeMutableRawPointer(pointer!)] = callback @@ -108,21 +158,24 @@ extension LVGLObjectProtocol { ) } + /// Removes the previously set callback. public func removeCallback() { callbackStore.removeValue(forKey: UnsafeMutableRawPointer(pointer!)) } + /// Deletes the object. public mutating func delete() { if pointer == nil { - print("Pointer already exists") + print("Pointer already deleted") print("This will be a fatal error in the future") } else { lv_obj_delete(pointer) pointer = nil - } } + /// Checks if the object still exists. + /// - Returns: `true` if the object exists, `false` otherwise. public func exists() -> Bool { if pointer == nil { return false diff --git a/Sources/SwiftLVGL/Core/LVGLObjectProtocol.swift b/Sources/SwiftLVGL/Core/LVGLObjectProtocol.swift index 06277bb..69ffa58 100644 --- a/Sources/SwiftLVGL/Core/LVGLObjectProtocol.swift +++ b/Sources/SwiftLVGL/Core/LVGLObjectProtocol.swift @@ -7,52 +7,121 @@ import CLVGL +/// A protocol that defines the interface for LVGL objects. public protocol LVGLObjectProtocol { + /// The pointer to the underlying LVGL object. var pointer: UnsafeMutablePointer<lv_obj_t>? { get set } + /// Sets the x-coordinate of the object's position. + /// - Parameter x: The new x-coordinate. func setPosition(x: Int32) + + /// Sets the y-coordinate of the object's position. + /// - Parameter y: The new y-coordinate. func setPosition(y: Int32) + + /// Sets both x and y coordinates of the object's position. + /// - Parameters: + /// - x: The new x-coordinate. + /// - y: The new y-coordinate. func setPosition(x: Int32, y: Int32) + /// Sets the size of the object. + /// - Parameters: + /// - width: The new width. + /// - height: The new height. func setSize(width: Int32, height: Int32) + + /// Sets the height of the object. + /// - Parameter height: The new height. func setSize(height: Int32) + + /// Sets the width of the object. + /// - Parameter width: The new width. func setSize(width: Int32) + /// Gets a specific dimension of the object. + /// - Parameter dimension: The dimension to retrieve. + /// - Returns: The value of the specified dimension. func getDimension(dimension: LVGLDimension) -> Int32 + + /// Gets a specific content dimension of the object. + /// - Parameter dimension: The dimension to retrieve. + /// - Returns: The value of the specified content dimension. func getContentDimension(dimension: LVGLDimension) -> Int32 + + /// Gets a specific self dimension of the object. + /// - Parameter dimension: The dimension to retrieve. + /// - Returns: The value of the specified self dimension. func getSelfDimension(dimension: LVGLDimension) -> Int32 + /// Sets the content size of the object. + /// - Parameters: + /// - width: The new content width. + /// - height: The new content height. func setContentSize(width: Int32, height: Int32) + + /// Sets the content width of the object. + /// - Parameter width: The new content width. func setContentSize(width: Int32) + + /// Sets the content height of the object. + /// - Parameter height: The new content height. func setContentSize(height: Int32) + /// Aligns the object according to the specified alignment. + /// - Parameter alignment: The alignment to apply. func align(alignment: LVAlignment) + + /// Aligns the object with an offset. + /// - Parameters: + /// - alignment: The alignment to apply. + /// - xOffset: The x-offset from the aligned position. + /// - yOffset: The y-offset from the aligned position. func align(alignment: LVAlignment, xOffset: Int32, yOffset: Int32) + + /// Centers the object within its parent. func center() + /// Gets the parent object. + /// - Returns: A pointer to the parent object, or nil if there is no parent. func getParent() -> UnsafeMutablePointer<lv_obj_t>? + + /// Sets the parent of the object. + /// - Parameter parentPointer: A pointer to the new parent object. func setParent(parentPointer: UnsafeMutablePointer<lv_obj_t>) + /// Sets a callback for a specific event type. + /// - Parameters: + /// - eventType: The type of event to listen for. + /// - callback: The callback function to be called when the event occurs. func setCallback( - eventType: LVEventCode, _ callback: @escaping (UnsafeMutablePointer<lv_event_t>?) -> Void) + eventType: LVEventCode, + callback: @escaping (UnsafeMutablePointer<lv_event_t>?) -> Void) + + /// Removes the previously set callback. func removeCallback() + /// Deletes the object. mutating func delete() + + /// Checks if the object still exists. + /// - Returns: `true` if the object exists, `false` otherwise. func exists() -> Bool /* - TODO: - func refreshSize() -> bool // lv_obj_refr_size - func setLayout(layout: UInt32) // lv_obj_set_layout - func isLayoutPositioned() -> bool // lv_obj_is_layout_positioned - func setLayoutAsDirty() // lv_obj_mark_layout_as_dirty - func updateLayout() // lv_obj_update_layout - func align(to: UnsafeMutablePointer<lv_obj_t>? = nil, alignment: LVAlignment, xOffset: Int32, yOffset: Int32) // lv_obj_align_to - func copyCoords(area to: UnsafeMutablePointer<lv_area_t>) // lv_obj_get_coords + TODO: + func refreshSize() -> bool // lv_obj_refr_size + func setLayout(layout: UInt32) // lv_obj_set_layout + func isLayoutPositioned() -> bool // lv_obj_is_layout_positioned + func setLayoutAsDirty() // lv_obj_mark_layout_as_dirty + func updateLayout() // lv_obj_update_layout + func align(to: UnsafeMutablePointer<lv_obj_t>? = nil, alignment: LVAlignment, xOffset: Int32, yOffset: Int32) // lv_obj_align_to + func copyCoords(area to: UnsafeMutablePointer<lv_area_t>) // lv_obj_get_coords - // Get Coords lv_obj_get_x, lv_obj_get_x2, lv_obj_get_y, lv_obj_get_y2, lv_obj_get_x_aligned, lv_obj_get_y_aligned + // Get Coords lv_obj_get_x, lv_obj_get_x2, lv_obj_get_y, lv_obj_get_y2, lv_obj_get_x_aligned, lv_obj_get_y_aligned - and a few more... + and a few more... - */ + */ } diff --git a/Sources/SwiftLVGL/Utilities/CallbackStore.swift b/Sources/SwiftLVGL/Utilities/CallbackStore.swift index a45f61a..524d44c 100644 --- a/Sources/SwiftLVGL/Utilities/CallbackStore.swift +++ b/Sources/SwiftLVGL/Utilities/CallbackStore.swift @@ -7,9 +7,18 @@ import CLVGL +/// A global dictionary that stores callback functions for LVGL objects. +/// +/// The key is a raw pointer to the LVGL object, and the value is the callback function. public var callbackStore: [UnsafeMutableRawPointer: (UnsafeMutablePointer<lv_event_t>?) -> Void] = [:] +/// A C-compatible callback function that bridges LVGL events to Swift callbacks. +/// +/// This function is used as the actual callback for LVGL events. It looks up the appropriate +/// Swift callback in the `callbackStore` based on the event target, and then calls that callback. +/// +/// - Parameter event: A pointer to the LVGL event structure. public func cCallback(_ event: UnsafeMutablePointer<lv_event_t>?) { guard let event = event else { return } let target = lv_event_get_target(event) diff --git a/Sources/SwiftLVGL/Widgets/LVGLButton.swift b/Sources/SwiftLVGL/Widgets/LVGLButton.swift index 694b764..7b63f43 100644 --- a/Sources/SwiftLVGL/Widgets/LVGLButton.swift +++ b/Sources/SwiftLVGL/Widgets/LVGLButton.swift @@ -11,13 +11,33 @@ public protocol LVGLButtonProtocol: LVGLObjectProtocol { } +/// A struct representing an LVGL button object. public struct LVGLButton: LVGLButtonProtocol { + /// The label associated with the button. public let label: LVGLLabel + + /// The pointer to the underlying LVGL object. public var pointer: UnsafeMutablePointer<lv_obj_t>? + /// Creates a new LVGL button. + /// - Parameters: + /// - text: The text to be displayed on the button. + /// - alignment: The alignment of the button. Defaults to `.center`. + /// - textColor: The color of the button text. Defaults to `.blue`. + /// - xSize: The width of the button. Defaults to 120. + /// - ySize: The height of the button. Defaults to 50. + /// - xOffset: The x-offset of the button. Defaults to 0. + /// - yOffset: The y-offset of the button. Defaults to 0. + /// - eventType: The type of event to listen for. Defaults to `.all`. + /// - callback: The callback function to be called when the specified event occurs. Defaults to an empty closure. public init( - _ text: String, alignment: LVAlignment = .center, textColor: Color = .blue, - xSize: Int32 = 120, ySize: Int32 = 50, xOffset: Int32 = 0, yOffset: Int32 = 0, + _ 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 } ) { @@ -26,13 +46,14 @@ public struct LVGLButton: LVGLButtonProtocol { } self.pointer = button self.label = LVGLLabel( - text, alignment: alignment, xOffset: xOffset, yOffset: yOffset + 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) - + setCallback(eventType: eventType, callback: callback) } - } diff --git a/Sources/SwiftLVGL/Widgets/LVGLLabel.swift b/Sources/SwiftLVGL/Widgets/LVGLLabel.swift index 070da16..d393612 100644 --- a/Sources/SwiftLVGL/Widgets/LVGLLabel.swift +++ b/Sources/SwiftLVGL/Widgets/LVGLLabel.swift @@ -7,15 +7,29 @@ import CLVGL +/// A protocol that defines the interface for LVGL label objects. public protocol LVGLLabelProtocol: LVGLObjectProtocol { + /// Sets the text of the label. + /// - Parameter text: The new text to be displayed on the label. func setText(_ text: String) } +/// A struct representing an LVGL label object. public struct LVGLLabel: LVGLLabelProtocol { + /// The pointer to the underlying LVGL object. public var pointer: UnsafeMutablePointer<lv_obj_t>? + /// Creates a new LVGL label. + /// - Parameters: + /// - text: The initial text to be displayed on the label. + /// - alignment: The alignment of the label. Defaults to `.center`. + /// - xOffset: The x-offset of the label. Defaults to 0. + /// - yOffset: The y-offset of the label. Defaults to 0. public init( - _ text: String, alignment: LVAlignment = .center, xOffset: Int32 = 0, yOffset: Int32 = 0 + _ 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") @@ -25,6 +39,8 @@ public struct LVGLLabel: LVGLLabelProtocol { align(alignment: alignment, xOffset: xOffset, yOffset: yOffset) } + /// Sets the text of the label. + /// - Parameter text: The new text to be displayed on the label. public func setText(_ text: String) { text.withCString { cString in lv_label_set_text(pointer, cString) |