Skip to content

Commit b4291dc

Browse files
committed
Gtk3Backend: Update ImageView impl to support HiDPI displays
1 parent 9313474 commit b4291dc

File tree

5 files changed

+41
-7
lines changed

5 files changed

+41
-7
lines changed

Sources/Gtk3/CairoSurface.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public struct CairoSurface {
2+
public let pointer: OpaquePointer
3+
}

Sources/Gtk3/Pixbuf.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,27 @@ public struct Pixbuf {
4242
)
4343
return Pixbuf(pointer: newPointer!)
4444
}
45+
46+
public func hidpiAwareScaled(
47+
toLogicalWidth logicalWidth: Int,
48+
andLogicalHeight logicalHeight: Int,
49+
for widget: Image
50+
) -> CairoSurface {
51+
// Get scaling and compute device dimensions from logical dimensions
52+
// (device dimensions being the target dimensions in terms of device
53+
// pixels, not the dimensions of the device)
54+
let scale = gtk_widget_get_scale_factor(widget.widgetPointer)
55+
let deviceWidth = logicalWidth * Int(scale)
56+
let deviceHeight = logicalHeight * Int(scale)
57+
let scaledPixbuf = self.scaled(
58+
toWidth: deviceWidth,
59+
andHeight: deviceHeight
60+
)
61+
let surface = gdk_cairo_surface_create_from_pixbuf(
62+
scaledPixbuf.pointer,
63+
scale,
64+
gtk_widget_get_window(widget.widgetPointer)
65+
)!
66+
return CairoSurface(pointer: surface)
67+
}
4568
}

Sources/Gtk3/Widgets/Image+ManualAdditions.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ extension Image {
55
gtk_image_set_from_file(castedPointer(), path)
66
}
77

8-
public func setPixbuf(_ pixbuf: OpaquePointer) {
9-
gtk_image_set_from_pixbuf(castedPointer(), pixbuf)
8+
public func setPixbuf(_ pixbuf: Pixbuf) {
9+
gtk_image_set_from_pixbuf(castedPointer(), pixbuf.pointer)
10+
}
11+
12+
public func setCairoSurface(_ surface: CairoSurface) {
13+
gtk_image_set_from_surface(castedPointer(), surface.pointer)
1014
}
1115
}

Sources/Gtk3Backend/Gtk3Backend.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,6 @@ public final class Gtk3Backend: AppBackend {
449449

450450
public func createImageView() -> Widget {
451451
let imageView = Gtk3.Image()
452-
// imageView.keepAspectRatio = false
453-
// imageView.canShrink = true
454452
return imageView
455453
}
456454

@@ -471,9 +469,13 @@ public final class Gtk3Backend: AppBackend {
471469
height: height
472470
)
473471

474-
let scaledPixbuf = pixbuf.scaled(toWidth: targetWidth, andHeight: targetHeight)
472+
let surface = pixbuf.hidpiAwareScaled(
473+
toLogicalWidth: targetWidth,
474+
andLogicalHeight: targetHeight,
475+
for: imageView
476+
)
475477

476-
imageView.setPixbuf(scaledPixbuf.pointer)
478+
imageView.setCairoSurface(surface)
477479
}
478480

479481
// private class Tables {

Sources/SwiftCrossUI/ViewGraph/ViewGraphNode.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,9 @@ public class ViewGraphNode<NodeView: View, Backend: AppBackend> {
259259
environment: viewEnvironment
260260
)
261261

262+
if !dryRun {
263+
backend.show(widget: widget)
264+
}
262265
let result = view.update(
263266
widget,
264267
children: children,
@@ -274,7 +277,6 @@ public class ViewGraphNode<NodeView: View, Backend: AppBackend> {
274277
// to false after real updates, but that's because it may get invalidated between a real
275278
// update and the next dry-run update.
276279
if !dryRun {
277-
backend.show(widget: widget)
278280
resultCache = [:]
279281
} else {
280282
resultCache[proposedSize] = result

0 commit comments

Comments
 (0)