Add WrapMode to TextView/TextEditor
Some checks failed
SwiftLint / SwiftLint (pull_request) Has been cancelled

This commit is contained in:
ml 2025-08-24 17:21:16 +02:00
parent daa922d33b
commit 0af3864058
3 changed files with 47 additions and 0 deletions

View File

@ -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
}
}

View File

@ -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))
}
}

View File

@ -19,6 +19,8 @@ public struct TextView: AdwaitaWidget {
var padding = 0
/// The edges affected by the padding.
var paddingEdges: Set<Edge> = []
/// 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.