Skip to content

Commit e23a6d5

Browse files
committed
Create a Gtk3Backend for legacy systems
Currently missing window resizing support, pickers, split views, and probably more.
1 parent 608cc3b commit e23a6d5

File tree

14 files changed

+769
-52
lines changed

14 files changed

+769
-52
lines changed

Package.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ let package = Package(
104104
.library(name: "CursesBackend", type: libraryType, targets: ["CursesBackend"]),
105105
.library(name: "QtBackend", type: libraryType, targets: ["QtBackend"]),
106106
.library(name: "GtkBackend", type: libraryType, targets: ["GtkBackend"]),
107+
.library(name: "Gtk3Backend", type: libraryType, targets: ["Gtk3Backend"]),
107108
.library(name: "DefaultBackend", type: libraryType, targets: ["DefaultBackend"]),
108109
.library(name: "Gtk", type: libraryType, targets: ["Gtk"]),
109110
.executable(name: "GtkExample", targets: ["GtkExample"]),
@@ -191,6 +192,10 @@ let package = Package(
191192
name: "GtkBackend",
192193
dependencies: ["SwiftCrossUI", "Gtk", "CGtk"]
193194
),
195+
.target(
196+
name: "Gtk3Backend",
197+
dependencies: ["SwiftCrossUI", "Gtk3", "CGtk3"]
198+
),
194199
.systemLibrary(
195200
name: "CGtk",
196201
pkgConfig: "gtk4",

Sources/AppKitBackend/AppKitBackend.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,13 @@ public final class AppKitBackend: AppBackend {
230230
container.removeSubview(child)
231231
}
232232

233-
public func setBackgroundColor(ofContainer widget: Widget, to color: Color) {
233+
public func createColorableRectangle() -> Widget {
234+
let widget = NSView()
234235
widget.wantsLayer = true
236+
return widget
237+
}
238+
239+
public func setColor(ofColorableRectangle widget: Widget, to color: Color) {
235240
widget.layer?.backgroundColor = color.nsColor.cgColor
236241
}
237242

@@ -560,7 +565,19 @@ public final class AppKitBackend: AppBackend {
560565
return imageView
561566
}
562567

563-
public func updateImageView(_ imageView: Widget, rgbaData: [UInt8], width: Int, height: Int) {
568+
public func updateImageView(
569+
_ imageView: Widget,
570+
rgbaData: [UInt8],
571+
width: Int,
572+
height: Int,
573+
targetWidth: Int,
574+
targetHeight: Int,
575+
dataHasChanged: Bool
576+
) {
577+
guard dataHasChanged else {
578+
return
579+
}
580+
564581
let imageView = imageView as! NSImageView
565582
var rgbaData = rgbaData
566583
let context = CGContext(

Sources/DefaultBackend/DefaultBackend.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
#elseif canImport(GtkBackend)
55
import GtkBackend
66
public typealias DefaultBackend = GtkBackend
7-
#elseif canImport(QtBackend)
8-
import QtBackend
9-
public typealias DefaultBackend = QtBackend
7+
#elseif canImport(Gtk3Backend)
8+
import Gtk3Backend
9+
public typealias DefaultBackend = Gtk3Backend
1010
#elseif canImport(WinUIBackend)
1111
import WinUIBackend
1212
public typealias DefaultBackend = WinUIBackend
13+
#elseif canImport(QtBackend)
14+
import QtBackend
15+
public typealias DefaultBackend = QtBackend
1316
#elseif canImport(CursesBackend)
1417
import CursesBackend
1518
public typealias DefaultBackend = CursesBackend
16-
#elseif canImport(LVGLBackend)
17-
import LVGLBackend
18-
public typealias DefaultBackend = LVGLBackend
1919
#else
2020
#error("Unknown backend selected")
2121
#endif

Sources/Gtk3/Generated/Entry.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,10 @@ public class Entry: Widget, CellEditable, Editable {
892892

893893
@GObjectProperty(named: "max-length") public var maxLength: Int
894894

895+
/// The text that will be displayed in the #GtkEntry when it is empty
896+
/// and unfocused.
897+
@GObjectProperty(named: "placeholder-text") public var placeholderText: String
898+
895899
@GObjectProperty(named: "text") public var text: String
896900

897901
@GObjectProperty(named: "visibility") public var visibility: Bool

Sources/Gtk3/Pixbuf.swift

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,17 @@ import CGtk3
33
public struct Pixbuf {
44
public let pointer: OpaquePointer
55

6-
private class DestructorContext {
7-
let bufferLength: Int
6+
public init(rgbaData: [UInt8], width: Int, height: Int) {
7+
let buffer = UnsafeMutableBufferPointer<UInt8>.allocate(capacity: rgbaData.count)
8+
memcpy(buffer.baseAddress!, rgbaData, rgbaData.count)
89

9-
init(bufferLength: Int) {
10-
self.bufferLength = bufferLength
11-
}
12-
}
13-
14-
public init(rgbaData: [UInt8], width: Int, height: Int, format: Int, stride: Int) {
15-
let bufferLength = rgbaData.count
16-
let destructorContext = DestructorContext(
17-
bufferLength: bufferLength
18-
)
19-
let buffer = UnsafeMutableBufferPointer<UInt8>.allocate(capacity: bufferLength)
2010
let width = gint(width)
2111
let height = gint(height)
2212
let hasAlpha = true.toGBoolean()
2313
let bitsPerSample: gint = 8
2414
let channels: gint = 4
2515
let rowStride = channels * gint(width)
16+
2617
pointer = gdk_pixbuf_new_from_data(
2718
buffer.baseAddress,
2819
GDK_COLORSPACE_RGB,
@@ -31,15 +22,24 @@ public struct Pixbuf {
3122
width,
3223
height,
3324
rowStride,
34-
{ buffer, context in
35-
let context = Unmanaged<DestructorContext>.fromOpaque(context!).takeRetainedValue()
36-
let buffer = UnsafeMutableBufferPointer<UInt8>(
37-
start: buffer,
38-
count: context.bufferLength
39-
)
40-
buffer.deallocate()
25+
{ buffer, _ in
26+
buffer?.deallocate()
4127
},
42-
Unmanaged.passRetained(destructorContext).toOpaque()
28+
nil
29+
)
30+
}
31+
32+
private init(pointer: OpaquePointer) {
33+
self.pointer = pointer
34+
}
35+
36+
public func scaled(toWidth width: Int, andHeight height: Int) -> Pixbuf {
37+
let newPointer = gdk_pixbuf_scale_simple(
38+
pointer,
39+
gint(width),
40+
gint(height),
41+
GDK_INTERP_BILINEAR
4342
)
43+
return Pixbuf(pointer: newPointer!)
4444
}
4545
}

Sources/Gtk3/Utility/GObjectProperty.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ import CGtk3
1818
wrapped _: ReferenceWritableKeyPath<EnclosingSelf, Value>,
1919
storage storageKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Self>
2020
) -> Value {
21-
get { instance.getProperty(named: instance[keyPath: storageKeyPath].name) }
21+
get {
22+
instance.getProperty(named: instance[keyPath: storageKeyPath].name)
23+
}
2224
set {
2325
instance.setProperty(named: instance[keyPath: storageKeyPath].name, newValue: newValue)
2426
}

Sources/Gtk3/Widgets/Label+ManualAdditions.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,13 @@ extension Label {
1313
gtk_label_set_line_wrap_mode(castedPointer(), newValue.toPangoWrapMode())
1414
}
1515
}
16+
17+
public var wrap: Bool {
18+
get {
19+
self.getProperty(named: "wrap")
20+
}
21+
set {
22+
self.setProperty(named: "wrap", newValue: newValue)
23+
}
24+
}
1625
}

0 commit comments

Comments
 (0)