Skip to content

Commit 629ad25

Browse files
committed
Fix GtkBackend window resizing (delay handler to not mess with Gtk rendering)
1 parent cebd011 commit 629ad25

File tree

4 files changed

+12
-25
lines changed

4 files changed

+12
-25
lines changed

Sources/Gtk/Widgets/CustomRootWidget.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,7 @@ public class CustomRootWidget: Widget {
5252

5353
public func setResizeHandler(_ handler: @escaping (Size) -> Void) {
5454
let box = SignalBox1<Size> { size in
55-
// Ensure that the handler can't mess with the current update.
56-
// Otherwise if it goes and updates the whole view hierarchy then
57-
// Gtk gets annoyed cause everything kinda gets pulled out from
58-
// under it.
59-
DispatchQueue.main.async {
60-
handler(size)
61-
}
55+
handler(size)
6256
}
6357
gtk_custom_root_widget_set_resize_callback(
6458
castedPointer(),

Sources/GtkBackend/GtkBackend.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ public final class GtkBackend: AppBackend {
109109
) {
110110
let child = window.getChild() as! CustomRootWidget
111111
child.setResizeHandler { size in
112-
action(SIMD2(size.width, size.height))
112+
self.runInMainThread {
113+
action(SIMD2(size.width, size.height))
114+
}
113115
}
114116
}
115117

Sources/GtkCustomWidgets/gtk_custom_root_widget.c

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ void gtk_custom_root_widget_measure(
2828
switch (orientation) {
2929
case GTK_ORIENTATION_HORIZONTAL:
3030
*minimum = root_widget->minimum_width;
31-
*natural = root_widget->natural_width;
31+
*natural = 0;
3232
break;
3333
case GTK_ORIENTATION_VERTICAL:
3434
*minimum = root_widget->minimum_height;
35-
*natural = root_widget->natural_height;
35+
*natural = 0;
3636
break;
3737
}
3838
}
@@ -46,19 +46,14 @@ void gtk_custom_root_widget_allocate(
4646
GtkCustomRootWidget *root_widget = GTK_CUSTOM_ROOT_WIDGET(widget);
4747
gtk_widget_allocate(root_widget->child, width, height, 0, NULL);
4848

49-
if (!root_widget->has_been_allocated) {
50-
if (width == root_widget->natural_width && height == root_widget->natural_height) {
51-
root_widget->allocated_width = width;
52-
root_widget->allocated_height = height;
53-
return;
54-
}
55-
} else if (width == root_widget->allocated_width && height == root_widget->allocated_height) {
49+
root_widget->has_been_allocated = true;
50+
51+
if (width == root_widget->allocated_width && height == root_widget->allocated_height) {
5652
return;
5753
}
5854

5955
root_widget->allocated_width = width;
6056
root_widget->allocated_height = height;
61-
root_widget->has_been_allocated = true;
6257

6358
if (root_widget->resize_callback != NULL) {
6459
Size size = { .width = width, .height = height };
@@ -73,8 +68,6 @@ GtkWidget *gtk_custom_root_widget_new(void) {
7368
widget->resize_callback_data = NULL;
7469
widget->minimum_width = 0;
7570
widget->minimum_height = 0;
76-
widget->natural_width = 0;
77-
widget->natural_height = 0;
7871
widget->allocated_width = 0;
7972
widget->allocated_height = 0;
8073
widget->has_been_allocated = false;
@@ -88,12 +81,12 @@ void gtk_custom_root_widget_set_child(GtkCustomRootWidget *self, GtkWidget *chil
8881
}
8982

9083
void gtk_custom_root_widget_get_size(GtkCustomRootWidget *widget, gint *width, gint *height) {
91-
if (widget->has_been_allocated || widget->natural_width == 0 || widget->natural_height == 0) {
84+
if (widget->has_been_allocated) {
9285
*width = widget->allocated_width;
9386
*height = widget->allocated_height;
9487
} else {
95-
*width = widget->natural_width;
96-
*height = widget->natural_height;
88+
*width = 0;
89+
*height = 0;
9790
}
9891
}
9992

Sources/GtkCustomWidgets/include/gtk_custom_root_widget.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ typedef struct _GtkCustomRootWidget {
1919

2020
gint minimum_width;
2121
gint minimum_height;
22-
gint natural_width;
23-
gint natural_height;
2422
gint allocated_width;
2523
gint allocated_height;
2624
gboolean has_been_allocated;

0 commit comments

Comments
 (0)