Detect when a search bar appears and disappears

,

Hi, I’m developing an application using Gtk4 and I’ve included a search bar and a toggle button to initiate the search.

I’ve set it up so that the search bar appears and disappears when the button emits the clicked signal, which means that the button appears “pressed” or “toggled” while the search bar is visible.

However, the user may start searching without pressing the button, e.g., by pressing Ctrl+f and I want this to also caused the search button to appear “toggled”, so I need to capture the search bar appearing and going away and call gtk_toggle_button_set_active() accordingly.

As far as I can tell, there is no signal to detect when the search bar appears and disappears, the best I can think of is calling gtk_search_bar_get_search_mode(), on the entry’s changed signal.

Is there a better way to do this?

Hi,

If you want the togglebutton state to always stay in sync with the search bar visibility, you can just bind their properties:

g_object_bind_property (searchbar, "search-mode-enabled", togglebutton, "active", G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE);
1 Like

It seems you’re capturing the Ctrl+f to show the search bar, why don’t you do it in that callback?

I was using Ctrl+f as an example, I have also enabled “type to search”, so I wanted a single way of intercepting the search bar appearing/disappearing.