Skip to content

Commit cc104be

Browse files
committed
[Gtk4] Fix Composite background not propagating
Snapshot background before snapshoting children to actually propagate the background if not overriden. Fixes one of the most notable problems on Gtk 4 - backgrounds being all white making components look out of place, not distinguish text fields from the rest and so on.
1 parent 53a4f88 commit cc104be

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Composite.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,43 @@ void deregister () {
522522
if (socketHandle != 0) display.removeWidget (socketHandle);
523523
}
524524

525+
@Override
526+
void snapshotBackground (long handle, long snapshot) {
527+
if ((state & OBSCURED) != 0) return;
528+
/*
529+
* Draw the effective background before children are snapshotted.
530+
*
531+
* SWTFixed widget has no CSS background rule, thus obtain a Cairo
532+
* context from the snapshot and delegate to drawBackground(), which
533+
* paints the background image or color directly via Cairo.
534+
*
535+
* Skip when an explicit background color has been set (state & BACKGROUND):
536+
* GTK4 renders the CSS provider background automatically before calling
537+
* the snapshot vfunc.
538+
*/
539+
Control control = findBackgroundControl ();
540+
boolean draw = control != null && control.backgroundImage != null;
541+
if (!draw && (state & CANVAS) != 0) {
542+
draw = (state & BACKGROUND) == 0;
543+
}
544+
if (!draw) return;
545+
546+
if (control == null) control = this;
547+
GtkAllocation allocation = new GtkAllocation();
548+
GTK.gtk_widget_get_allocation(handle, allocation);
549+
int width = (state & ZERO_WIDTH) != 0 ? 0 : allocation.width;
550+
int height = (state & ZERO_HEIGHT) != 0 ? 0 : allocation.height;
551+
long rect = Graphene.graphene_rect_alloc();
552+
Graphene.graphene_rect_init(rect, 0, 0, width, height);
553+
long cairo = GTK4.gtk_snapshot_append_cairo(snapshot, rect);
554+
if (cairo != 0) {
555+
drawBackground(control, 0, cairo, 0, 0, width, height);
556+
Cairo.cairo_destroy(cairo);
557+
}
558+
Graphene.graphene_rect_free(rect);
559+
}
560+
561+
525562
/**
526563
* Fills the interior of the rectangle specified by the arguments,
527564
* with the receiver's background.

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Display.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1716,14 +1716,17 @@ static long rendererClassInitProc (long g_class, long class_data) {
17161716

17171717
void snapshotDrawProc(long handle, long snapshot) {
17181718
Display display = getCurrent ();
1719+
Widget widget = display.getWidget (handle);
1720+
// Draw background before children so it appears behind them
1721+
if (widget != null) widget.snapshotBackground(handle, snapshot);
1722+
17191723
long child = GTK4.gtk_widget_get_first_child(handle);
17201724
// Propagate the snapshot down the widget tree first
17211725
while (child != 0) {
17221726
GTK4.gtk_widget_snapshot_child(handle, child, snapshot);
17231727
child = GTK4.gtk_widget_get_next_sibling(child);
17241728
}
17251729
// Draw custom paint on top of children
1726-
Widget widget = display.getWidget (handle);
17271730
if (widget != null) widget.snapshotToDraw(handle, snapshot);
17281731
}
17291732

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Widget.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,6 +2281,17 @@ long sizeRequestProc (long handle, long arg0, long user_data) {
22812281
return 0;
22822282
}
22832283

2284+
/**
2285+
* Renders the widget background during a GTK4 snapshot. Called before
2286+
* children are snapshotted so the background appears behind them.
2287+
* Subclasses can override to perform background rendering.
2288+
*
2289+
* @param handle the widget receiving the snapshot
2290+
* @param snapshot the actual GtkSnapshot
2291+
*/
2292+
void snapshotBackground (long handle, long snapshot) {
2293+
}
2294+
22842295
/**
22852296
* Converts an incoming snapshot into a gtk_draw() call, complete with
22862297
* a Cairo context.

0 commit comments

Comments
 (0)