The GtkSnapshot mask will flash

录屏 2025-07-26 22-29-37

Any suggestions for improvement?


/*
	.mask_bg._level_up_1._level_up_2._level_up_3 {
		background-color: black;
	}
*/

// all code
{
	gtk_snapshot_push_mask( snapshot, GSK_MASK_MODE_INVERTED_ALPHA );
	{
		gtk_widget_add_css_class( mask_target, "mask_bg" );
		gtk_widget_add_css_class( mask_target, "_level_up_1" );
		gtk_widget_add_css_class( mask_target, "_level_up_2" );
		gtk_widget_add_css_class( mask_target, "_level_up_3" );

		gtk_widget_snapshot_child( widget, mask_target, snapshot );

		gtk_widget_remove_css_class( mask_target, "mask_bg" );
		gtk_widget_remove_css_class( mask_target, "_level_up_1" );
		gtk_widget_remove_css_class( mask_target, "_level_up_2" );
		gtk_widget_remove_css_class( mask_target, "_level_up_3" );
	}
	gtk_snapshot_pop( snapshot );
	{
		for ( GtkWidget* child = gtk_widget_get_first_child( widget ); child;
		      child            = gtk_widget_get_next_sibling( child ) ) {
			if ( child != mask_target )
				gtk_widget_snapshot_child( widget, child, snapshot );
		}
	}
	gtk_snapshot_pop( snapshot );

	gtk_widget_queue_draw(mask_target );
	gtk_widget_snapshot_child( widget, mask_target, snapshot );
}

Got the solution but can’t understand it.

Call gtk_widget_queue_draw before calling gtk_widget_snapshot_child for mask_target.

Hi,

I don’t understand why mask_target is shapshot twice in your example. What are you trying to achieve?

Also, there is no need to call gtk_widget_queue_draw() on the child widget. gtk_widget_snapshot_child() should be enough.

Hi, have you seen my gif?

mask_target will animate in place. It will cover the bottom element. But it is semi-transparent.
So I need to erase the content of the area where mask_target is. Then draw it.

截图 2025-07-27 19-44-56
截图 2025-07-27 19-44-53
截图 2025-07-27 19-44-49

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.