diff --git a/Sources/Core/Model/Enumerations/WrapMode.swift b/Sources/Core/Model/Enumerations/WrapMode.swift new file mode 100644 index 0000000..f643afc --- /dev/null +++ b/Sources/Core/Model/Enumerations/WrapMode.swift @@ -0,0 +1,22 @@ +import CAdw + +/// Wrap modes for `TextView`/`TextEditor` +public enum WrapMode: GtkWrapMode, RawRepresentable { + case none // GTK_WRAP_NONE + case char // GTK_WRAP_CHAR + case word // GTK_WRAP_WORD + case wordChar // GTK_WRAP_WORD_CHAR + + public var rawValue: GtkWrapMode { + switch self { + case .none: GTK_WRAP_NONE + case .char: GTK_WRAP_CHAR + case .word: GTK_WRAP_WORD + case .wordChar: GTK_WRAP_WORD_CHAR + } + } + + public init?(rawValue: GtkWrapMode) { + return nil + } +} diff --git a/Sources/Core/Model/Extensions/GtkWrapMode.swift b/Sources/Core/Model/Extensions/GtkWrapMode.swift new file mode 100644 index 0000000..9aeb0d1 --- /dev/null +++ b/Sources/Core/Model/Extensions/GtkWrapMode.swift @@ -0,0 +1,9 @@ +import CAdw + +/// Add ExpressibleByIntegerLiteral conformance to make GtkWrapMode usable as +// a RawValue in an enum. +extension GtkWrapMode: @retroactive ExpressibleByIntegerLiteral { + public init(integerLiteral value: Int) { + self.init(UInt32(value)) + } +} diff --git a/Sources/Core/View/TextEditor.swift b/Sources/Core/View/TextEditor.swift index 28a5571..91ad7db 100644 --- a/Sources/Core/View/TextEditor.swift +++ b/Sources/Core/View/TextEditor.swift @@ -19,6 +19,8 @@ public struct TextView: AdwaitaWidget { var padding = 0 /// The edges affected by the padding. var paddingEdges: Set = [] + /// The (word) wrap mode used when rendering the text. + var wrapMode: WrapMode = .none /// Initialize a text editor. /// - Parameter text: The editor's content. @@ -74,9 +76,23 @@ public struct TextView: AdwaitaWidget { if paddingEdges.contains(.trailing) { gtk_text_view_set_right_margin(storage.opaquePointer?.cast(), padding.cInt) } + // update the wrapping mode + gtk_text_view_set_wrap_mode(storage.opaquePointer?.cast(), wrapMode.rawValue) } } + /// Set the wrapMode for the text view. + /// + /// Available wrap modes are `none`, `char`, `word`, `wordChar`. Please refer to the + /// corresponding `GtkWrapMode` documentation for the details on how they work. + /// + /// - Parameter mode: The `WrapMode` to set. + public func wrapMode(_ mode: WrapMode) -> Self { + var newSelf = self + newSelf.wrapMode = mode + return newSelf + } + /// Get the text view's content. /// - Parameter buffer: The text view's buffer. /// - Returns: The content.