GtkPopover on Wayland gives :hover / PRELIGHT / crossing to wrong child widget upon opening
I have a MenuButton
with a Popover
containing a Grid
of ToggleButton
s. I need to grab_focus()
on whichever of 'menu item' Button
s is currently selected. I do that on ::show
, which works... BUT!
Where things go wrong is GTK seems to automatically set CSS state :hover
(widget flag PRELIGHT
) on some arbitrary button, in my case the last one added to the child of the Popover
(or if I add my buttons in reverse, the first one in the row/column order)
This ends up looking really bad visually: my Popover
opens and shows (if keynav is visible) the active button as being focused, with a blue outline, but it also shows the random/last button as 'hovered'.
Example from my testing: item 4 is active, and has gotten the grab_focus()
, but item 8 is 'hovered':
As soon as I move the pointer, that hover goes away, as GTK puts hover back on the parent MenuButton
(while keeping the Popover
open).
This only happens if property :autohide
is TRUE
, and thus I suspect the culprit code is this bit:
[edit] It also only seems to happen on Wayland, so hopefully that provides a hint for someone more knowledgeable.
Follows a short example, which I hope suffices to show the issue. Let me know how I can help either way!
Screenshot: see the top-left button has keyboard focus, but the bottom one has the hovered state:
Code:
#include <gtk/gtk.h>
static void
on_state_flags_changed (GtkWidget *widget,
GtkStateFlags old_flags,
gpointer user_data)
{
GtkStateFlags new_flags = gtk_widget_get_state_flags (widget);
if (!((old_flags & GTK_STATE_FLAG_PRELIGHT) == 0 &&
(new_flags & GTK_STATE_FLAG_PRELIGHT) != 0))
return;
g_warning("%s", gtk_widget_get_name (widget));
}
static void
activate (GtkApplication *app,
gpointer user_data)
{
GtkWidget *window;
GtkWidget *grid;
GtkWidget *button;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window");
/* highlight the problem */
GtkCssProvider *provider = gtk_css_provider_new ();
gtk_css_provider_load_from_data (provider, "button:hover { color: red; }", -1);
gtk_style_context_add_provider_for_display (gtk_widget_get_display (window),
GTK_STYLE_PROVIDER (provider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
GtkWidget *menu_button = gtk_menu_button_new ();
gtk_window_set_child (GTK_WINDOW (window), menu_button);
GtkWidget *popover = gtk_popover_new ();
gtk_menu_button_set_popover (GTK_MENU_BUTTON (menu_button), GTK_POPOVER (popover));
// boilerplate from examples/grid-packing.c
grid = gtk_grid_new ();
gtk_popover_set_child (GTK_POPOVER (popover), grid);
button = gtk_button_new_with_label ("Button 1");
gtk_widget_set_name (GTK_WIDGET (button), "button1");
gtk_grid_attach (GTK_GRID (grid), button, 0, 0, 1, 1);
button = gtk_button_new_with_label ("Button 2");
gtk_widget_set_name (GTK_WIDGET (button), "button2");
gtk_grid_attach (GTK_GRID (grid), button, 1, 0, 1, 1);
button = gtk_button_new_with_label ("Quit");
gtk_widget_set_name (GTK_WIDGET (button), "button_quit");
g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_window_destroy), window);
g_signal_connect (button, "state-flags-changed", G_CALLBACK (on_state_flags_changed), NULL);
gtk_grid_attach (GTK_GRID (grid), button, 0, 1, 2, 1);
gtk_window_present (GTK_WINDOW (window));
}
int
main (int argc,
char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}