Title: [262937] trunk/Tools
Revision
262937
Author
carlo...@webkit.org
Date
2020-06-12 01:25:58 -0700 (Fri, 12 Jun 2020)

Log Message

[GTK] MiniBrowser: stop using GtkToolbar for the search bar
https://bugs.webkit.org/show_bug.cgi?id=212817

Reviewed by Adrian Perez de Castro.

Use a GtkSearchBar instead, because GtkToolbar is no longer available in GTK4. Also use a GtkPopover for the
options menu instead of the GtkMenu.

* MiniBrowser/gtk/BrowserSearchBar.c:
(setFailedStyleForEntry):
(doSearch):
(searchEntryMenuIconPressedCallback):
(searchEntryChangedCallback):
(searchMenuCheckButtonToggledCallback):
(browserSearchBarFinalize):
(browser_search_bar_new):
(browser_search_bar_open):
(browser_search_bar_close):
(browser_search_bar_is_open):
(searchCloseButtonClickedCallback): Deleted.
(searchEntryClearIconReleasedCallback): Deleted.
* MiniBrowser/gtk/BrowserSearchBar.h:
* MiniBrowser/gtk/BrowserTab.c:
(browser_tab_start_search):
(browser_tab_stop_search):

Modified Paths

Diff

Modified: trunk/Tools/ChangeLog (262936 => 262937)


--- trunk/Tools/ChangeLog	2020-06-12 08:19:58 UTC (rev 262936)
+++ trunk/Tools/ChangeLog	2020-06-12 08:25:58 UTC (rev 262937)
@@ -1,5 +1,33 @@
 2020-06-12  Carlos Garcia Campos  <cgar...@igalia.com>
 
+        [GTK] MiniBrowser: stop using GtkToolbar for the search bar
+        https://bugs.webkit.org/show_bug.cgi?id=212817
+
+        Reviewed by Adrian Perez de Castro.
+
+        Use a GtkSearchBar instead, because GtkToolbar is no longer available in GTK4. Also use a GtkPopover for the
+        options menu instead of the GtkMenu.
+
+        * MiniBrowser/gtk/BrowserSearchBar.c:
+        (setFailedStyleForEntry):
+        (doSearch):
+        (searchEntryMenuIconPressedCallback):
+        (searchEntryChangedCallback):
+        (searchMenuCheckButtonToggledCallback):
+        (browserSearchBarFinalize):
+        (browser_search_bar_new):
+        (browser_search_bar_open):
+        (browser_search_bar_close):
+        (browser_search_bar_is_open):
+        (searchCloseButtonClickedCallback): Deleted.
+        (searchEntryClearIconReleasedCallback): Deleted.
+        * MiniBrowser/gtk/BrowserSearchBar.h:
+        * MiniBrowser/gtk/BrowserTab.c:
+        (browser_tab_start_search):
+        (browser_tab_stop_search):
+
+2020-06-12  Carlos Garcia Campos  <cgar...@igalia.com>
+
         [GTK4] Make WebDriver work
         https://bugs.webkit.org/show_bug.cgi?id=212316
 

Modified: trunk/Tools/MiniBrowser/gtk/BrowserSearchBar.c (262936 => 262937)


--- trunk/Tools/MiniBrowser/gtk/BrowserSearchBar.c	2020-06-12 08:19:58 UTC (rev 262936)
+++ trunk/Tools/MiniBrowser/gtk/BrowserSearchBar.c	2020-06-12 08:25:58 UTC (rev 262937)
@@ -28,50 +28,57 @@
 
 #if !GTK_CHECK_VERSION(3, 98, 0)
 
-static const char *searchEntryFailedStyle = "GtkEntry#searchEntry {background-color: #ff6666;}";
-
 struct _BrowserSearchBar {
-    GtkToolbar parent;
+    GtkSearchBar parent;
 
     WebKitWebView *webView;
     GtkWidget *entry;
-    GtkCssProvider *cssProvider;
     GtkWidget *prevButton;
     GtkWidget *nextButton;
-    GtkWidget *optionsMenu;
+    GMenu *optionsMenu;
     GtkWidget *caseCheckButton;
     GtkWidget *begginigWordCheckButton;
     GtkWidget *capitalAsBegginigWordCheckButton;
 };
 
-G_DEFINE_TYPE(BrowserSearchBar, browser_search_bar, GTK_TYPE_TOOLBAR)
+G_DEFINE_TYPE(BrowserSearchBar, browser_search_bar, GTK_TYPE_SEARCH_BAR)
 
 static void setFailedStyleForEntry(BrowserSearchBar *searchBar, gboolean failedSearch)
 {
-    gtk_css_provider_load_from_data(searchBar->cssProvider, failedSearch ? searchEntryFailedStyle : "", -1, NULL);
+    if (failedSearch)
+        gtk_style_context_add_class(gtk_widget_get_style_context(searchBar->entry), "search-failed");
+    else
+        gtk_style_context_remove_class(gtk_widget_get_style_context(searchBar->entry), "search-failed");
 }
 
 static void doSearch(BrowserSearchBar *searchBar)
 {
     GtkEntry *entry = GTK_ENTRY(searchBar->entry);
-
     if (!gtk_entry_get_text_length(entry)) {
+        setFailedStyleForEntry(searchBar, FALSE);
         webkit_find_controller_search_finish(webkit_web_view_get_find_controller(searchBar->webView));
-        gtk_entry_set_icon_from_icon_name(entry, GTK_ENTRY_ICON_SECONDARY, NULL);
-        setFailedStyleForEntry(searchBar, FALSE);
         return;
     }
 
-    if (!gtk_entry_get_icon_name(entry, GTK_ENTRY_ICON_SECONDARY))
-        gtk_entry_set_icon_from_icon_name(entry, GTK_ENTRY_ICON_SECONDARY, "edit-clear");
-
     WebKitFindOptions options = WEBKIT_FIND_OPTIONS_WRAP_AROUND;
-    if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(searchBar->caseCheckButton)))
+    GActionGroup *actionGroup = gtk_widget_get_action_group(GTK_WIDGET(searchBar), "find");
+    GAction *action = "" "case-sensitive");
+    GVariant *state = g_action_get_state(action);
+    if (!g_variant_get_boolean(state))
         options |= WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE;
-    if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(searchBar->begginigWordCheckButton)))
+    g_variant_unref(state);
+
+    action = "" "beginning-word");
+    state = g_action_get_state(action);
+    if (g_variant_get_boolean(state))
         options |= WEBKIT_FIND_OPTIONS_AT_WORD_STARTS;
-    if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(searchBar->capitalAsBegginigWordCheckButton)))
+    g_variant_unref(state);
+
+    action = "" "capital-as-beginning-word");
+    state = g_action_get_state(action);
+    if (g_variant_get_boolean(state))
         options |= WEBKIT_FIND_OPTIONS_TREAT_MEDIAL_CAPITAL_AS_WORD_START;
+    g_variant_unref(state);
 
     const gchar *text = gtk_entry_get_text(entry);
     webkit_find_controller_search(webkit_web_view_get_find_controller(searchBar->webView), text, options, G_MAXUINT);
@@ -87,24 +94,20 @@
     webkit_find_controller_search_previous(webkit_web_view_get_find_controller(searchBar->webView));
 }
 
-static void searchCloseButtonClickedCallback(BrowserSearchBar *searchBar)
-{
-    browser_search_bar_close(searchBar);
-}
-
 static void searchEntryMenuIconPressedCallback(BrowserSearchBar *searchBar, GtkEntryIconPosition iconPosition, GdkEvent *event)
 {
-    if (iconPosition == GTK_ENTRY_ICON_PRIMARY)
-        gtk_menu_popup_at_pointer(GTK_MENU(searchBar->optionsMenu), event);
-}
+    if (iconPosition != GTK_ENTRY_ICON_PRIMARY)
+        return;
 
-static void searchEntryClearIconReleasedCallback(BrowserSearchBar *searchBar, GtkEntryIconPosition iconPosition)
-{
-    if (iconPosition == GTK_ENTRY_ICON_SECONDARY)
-        gtk_entry_set_text(GTK_ENTRY(searchBar->entry), "");
+    GtkWidget *popover = gtk_popover_new_from_model(searchBar->entry, G_MENU_MODEL(searchBar->optionsMenu));
+    GdkRectangle rect;
+    gtk_entry_get_icon_area(GTK_ENTRY(searchBar->entry), GTK_ENTRY_ICON_PRIMARY, &rect);
+    gtk_popover_set_pointing_to(GTK_POPOVER(popover), &rect);
+    gtk_popover_set_position(GTK_POPOVER(popover), GTK_POS_BOTTOM);
+    gtk_popover_popup(GTK_POPOVER(popover));
 }
 
-static void searchEntryChangedCallback(GtkEntry *entry, BrowserSearchBar *searchBar)
+static void searchEntryChangedCallback(GtkSearchEntry *entry, BrowserSearchBar *searchBar)
 {
     doSearch(searchBar);
 }
@@ -124,9 +127,10 @@
     searchNext(BROWSER_SEARCH_BAR(userData));
 }
 
-static void searchMenuCheckButtonToggledCallback(BrowserSearchBar *searchBar)
+static void searchMenuCheckButtonToggledCallback(GSimpleAction *action, GVariant *state, gpointer userData)
 {
-    doSearch(searchBar);
+    g_simple_action_set_state(action, state);
+    doSearch(BROWSER_SEARCH_BAR(userData));
 }
 
 static void findControllerFailedToFindTextCallback(BrowserSearchBar *searchBar)
@@ -142,6 +146,9 @@
 static const GActionEntry actions[] = {
     { "next", searchNextButtonCallback, NULL, NULL, NULL, { 0 } },
     { "previous", searchPreviousButtonCallback, NULL, NULL, NULL, { 0 } },
+    { "case-sensitive", NULL, NULL, "false", searchMenuCheckButtonToggledCallback, { 0 } },
+    { "beginning-word", NULL, NULL, "false", searchMenuCheckButtonToggledCallback, { 0 } },
+    { "capital-as-beginning-word", NULL, NULL, "false", searchMenuCheckButtonToggledCallback, { 0 } }
 };
 
 static void browser_search_bar_init(BrowserSearchBar *searchBar)
@@ -151,71 +158,50 @@
     gtk_widget_insert_action_group(GTK_WIDGET(searchBar), "find", G_ACTION_GROUP(actionGroup));
     g_object_unref(actionGroup);
 
-    gtk_widget_set_hexpand(GTK_WIDGET(searchBar), TRUE);
+    GtkWidget *hBox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
+    GtkStyleContext *styleContext = gtk_widget_get_style_context(hBox);
+    gtk_style_context_add_class(styleContext, GTK_STYLE_CLASS_LINKED);
+    gtk_style_context_add_class(styleContext, GTK_STYLE_CLASS_RAISED);
 
-    GtkToolItem *toolItem = gtk_tool_item_new();
-    gtk_tool_item_set_expand(toolItem, TRUE);
-    gtk_toolbar_insert(GTK_TOOLBAR(searchBar), toolItem, 0);
+    searchBar->entry = gtk_search_entry_new();
+    gtk_entry_set_icon_activatable(GTK_ENTRY(searchBar->entry), GTK_ENTRY_ICON_PRIMARY, TRUE);
+    gtk_entry_set_icon_sensitive(GTK_ENTRY(searchBar->entry), GTK_ENTRY_ICON_PRIMARY, TRUE);
+    gtk_entry_set_icon_tooltip_text(GTK_ENTRY(searchBar->entry), GTK_ENTRY_ICON_PRIMARY, "Search options");
+    gtk_box_pack_start(GTK_BOX(hBox), searchBar->entry, TRUE, TRUE, 0);
+    gtk_widget_show(searchBar->entry);
 
-    GtkBox *hBox = GTK_BOX(gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5));
-    gtk_box_set_homogeneous(hBox, TRUE);
-    gtk_container_add(GTK_CONTAINER(toolItem), GTK_WIDGET(hBox));
+    GtkCssProvider *cssProvider = gtk_css_provider_new();
+    gtk_css_provider_load_from_data(cssProvider, ".search-failed { background-color: #ff6666; }", -1, NULL);
+    gtk_style_context_add_provider(gtk_widget_get_style_context(searchBar->entry), GTK_STYLE_PROVIDER(cssProvider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+    g_object_unref(cssProvider);
 
-    gtk_box_pack_start(hBox, gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0), TRUE, TRUE, 0);
-
-    searchBar->entry = gtk_entry_new();
-    gtk_widget_set_name(searchBar->entry, "searchEntry");
-    gtk_entry_set_placeholder_text(GTK_ENTRY(searchBar->entry), "Search");
-    gtk_entry_set_icon_from_icon_name(GTK_ENTRY(searchBar->entry), GTK_ENTRY_ICON_PRIMARY, "edit-find");
-    gtk_box_pack_start(hBox, searchBar->entry, TRUE, TRUE, 0);
-
-    searchBar->cssProvider = gtk_css_provider_new();
-    gtk_style_context_add_provider(gtk_widget_get_style_context(searchBar->entry), GTK_STYLE_PROVIDER(searchBar->cssProvider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
-
-    GtkBox *hBoxButtons = GTK_BOX(gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0));
-    gtk_box_pack_start(hBox, GTK_WIDGET(hBoxButtons), TRUE, TRUE, 0);
-
-    searchBar->prevButton = gtk_button_new_from_icon_name("go-up", GTK_ICON_SIZE_SMALL_TOOLBAR);
+    searchBar->prevButton = gtk_button_new_from_icon_name("go-up-symbolic", GTK_ICON_SIZE_MENU);
     GtkButton *button = GTK_BUTTON(searchBar->prevButton);
     gtk_actionable_set_action_name(GTK_ACTIONABLE(button), "find.previous");
-    gtk_button_set_relief(button, GTK_RELIEF_NONE);
     gtk_widget_set_focus_on_click(searchBar->prevButton, FALSE);
-    gtk_box_pack_start(hBoxButtons, searchBar->prevButton, FALSE, FALSE, 0);
+    gtk_box_pack_start(GTK_BOX(hBox), searchBar->prevButton, FALSE, FALSE, 0);
+    gtk_widget_show(searchBar->prevButton);
 
-    searchBar->nextButton = gtk_button_new_from_icon_name("go-down", GTK_ICON_SIZE_SMALL_TOOLBAR);
+    searchBar->nextButton = gtk_button_new_from_icon_name("go-down-symbolic", GTK_ICON_SIZE_MENU);
     button = GTK_BUTTON(searchBar->nextButton);
     gtk_actionable_set_action_name(GTK_ACTIONABLE(button), "find.next");
-    gtk_button_set_relief(button, GTK_RELIEF_NONE);
     gtk_widget_set_focus_on_click(searchBar->nextButton, FALSE);
-    gtk_box_pack_start(hBoxButtons, searchBar->nextButton, FALSE, FALSE, 0);
+    gtk_box_pack_start(GTK_BOX(hBox), searchBar->nextButton, FALSE, FALSE, 0);
+    gtk_widget_show(searchBar->nextButton);
 
-    GtkWidget *closeButton = gtk_button_new_from_icon_name("window-close", GTK_ICON_SIZE_SMALL_TOOLBAR);
-    gtk_button_set_relief(GTK_BUTTON(closeButton), GTK_RELIEF_NONE);
-    gtk_widget_set_focus_on_click(closeButton, FALSE);
-    gtk_box_pack_end(hBoxButtons, closeButton, FALSE, FALSE, 0);
+    gtk_container_add(GTK_CONTAINER(searchBar), hBox);
+    gtk_widget_show(hBox);
+    gtk_search_bar_connect_entry(GTK_SEARCH_BAR(searchBar), GTK_ENTRY(searchBar->entry));
+    gtk_widget_show(GTK_WIDGET(searchBar));
 
-    searchBar->optionsMenu = g_object_ref_sink(gtk_menu_new());
+    searchBar->optionsMenu = g_menu_new();
+    g_menu_append(searchBar->optionsMenu, "Ca_se sensitive", "find.case-sensitive");
+    g_menu_append(searchBar->optionsMenu, "Only at the _beginning of words", "find.beginning-word");
+    g_menu_append(searchBar->optionsMenu, "Capital _always as beginning of word", "find.capital-as-beginning-word");
 
-    searchBar->caseCheckButton = gtk_check_menu_item_new_with_mnemonic("Ca_se sensitive");
-    gtk_container_add(GTK_CONTAINER(searchBar->optionsMenu), searchBar->caseCheckButton);
-
-    searchBar->begginigWordCheckButton = gtk_check_menu_item_new_with_mnemonic("Only at the _beginning of words");
-    gtk_container_add(GTK_CONTAINER(searchBar->optionsMenu), searchBar->begginigWordCheckButton);
-
-    searchBar->capitalAsBegginigWordCheckButton = gtk_check_menu_item_new_with_mnemonic("Capital _always as beginning of word");
-    gtk_container_add(GTK_CONTAINER(searchBar->optionsMenu), searchBar->capitalAsBegginigWordCheckButton);
-
-    g_signal_connect_swapped(closeButton, "clicked", G_CALLBACK(searchCloseButtonClickedCallback), searchBar);
     g_signal_connect_swapped(searchBar->entry, "icon-press", G_CALLBACK(searchEntryMenuIconPressedCallback), searchBar);
-    g_signal_connect_swapped(searchBar->entry, "icon-release", G_CALLBACK(searchEntryClearIconReleasedCallback), searchBar);
-    g_signal_connect_after(searchBar->entry, "changed", G_CALLBACK(searchEntryChangedCallback), searchBar);
+    g_signal_connect_after(searchBar->entry, "search-changed", G_CALLBACK(searchEntryChangedCallback), searchBar);
     g_signal_connect_swapped(searchBar->entry, "activate", G_CALLBACK(searchEntryActivatedCallback), searchBar);
-    g_signal_connect_swapped(searchBar->caseCheckButton, "toggled", G_CALLBACK(searchMenuCheckButtonToggledCallback), searchBar);
-    g_signal_connect_swapped(searchBar->begginigWordCheckButton, "toggled", G_CALLBACK(searchMenuCheckButtonToggledCallback), searchBar);
-    g_signal_connect_swapped(searchBar->capitalAsBegginigWordCheckButton, "toggled", G_CALLBACK(searchMenuCheckButtonToggledCallback), searchBar);
-
-    gtk_widget_show_all(GTK_WIDGET(toolItem));
-    gtk_widget_show_all(searchBar->optionsMenu);
 }
 
 static void browserSearchBarFinalize(GObject *gObject)
@@ -227,11 +213,6 @@
         searchBar->webView = NULL;
     }
 
-    if (searchBar->cssProvider) {
-        g_object_unref(searchBar->cssProvider);
-        searchBar->cssProvider = NULL;
-    }
-
     if (searchBar->optionsMenu) {
         g_object_unref(searchBar->optionsMenu);
         searchBar->optionsMenu = NULL;
@@ -253,6 +234,7 @@
 
     GtkWidget *searchBar = GTK_WIDGET(g_object_new(BROWSER_TYPE_SEARCH_BAR, NULL));
     BROWSER_SEARCH_BAR(searchBar)->webView = g_object_ref(webView);
+    gtk_search_bar_set_show_close_button(GTK_SEARCH_BAR(searchBar), TRUE);
 
     WebKitFindController *controller = webkit_web_view_get_find_controller(webView);
     g_signal_connect_swapped(controller, "failed-to-find-text", G_CALLBACK(findControllerFailedToFindTextCallback), searchBar);
@@ -265,13 +247,7 @@
 {
     g_return_if_fail(BROWSER_IS_SEARCH_BAR(searchBar));
 
-    GtkEntry *entry = GTK_ENTRY(searchBar->entry);
-
-    gtk_widget_show(GTK_WIDGET(searchBar));
-    gtk_widget_grab_focus(GTK_WIDGET(entry));
-    gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
-    if (gtk_entry_get_text_length(entry))
-        doSearch(searchBar);
+    gtk_search_bar_set_search_mode(GTK_SEARCH_BAR(searchBar), TRUE);
 }
 
 void browser_search_bar_close(BrowserSearchBar *searchBar)
@@ -278,9 +254,15 @@
 {
     g_return_if_fail(BROWSER_IS_SEARCH_BAR(searchBar));
 
-    gtk_widget_hide(GTK_WIDGET(searchBar));
-    WebKitFindController *controller = webkit_web_view_get_find_controller(searchBar->webView);
-    webkit_find_controller_search_finish(controller);
+    gtk_search_bar_set_search_mode(GTK_SEARCH_BAR(searchBar), FALSE);
 }
 
+gboolean browser_search_bar_is_open(BrowserSearchBar *searchBar)
+{
+    g_return_val_if_fail(BROWSER_IS_SEARCH_BAR(searchBar), FALSE);
+
+    GtkWidget *revealer = gtk_bin_get_child(GTK_BIN(searchBar));
+    return gtk_revealer_get_reveal_child(GTK_REVEALER(revealer));
+}
+
 #endif

Modified: trunk/Tools/MiniBrowser/gtk/BrowserSearchBar.h (262936 => 262937)


--- trunk/Tools/MiniBrowser/gtk/BrowserSearchBar.h	2020-06-12 08:19:58 UTC (rev 262936)
+++ trunk/Tools/MiniBrowser/gtk/BrowserSearchBar.h	2020-06-12 08:25:58 UTC (rev 262937)
@@ -44,7 +44,7 @@
 typedef struct _BrowserSearchBarClass  BrowserSearchBarClass;
 
 struct _BrowserSearchBarClass {
-    GtkToolbarClass parent_class;
+    GtkSearchBarClass parent_class;
 };
 
 GType browser_search_bar_get_type(void);
@@ -52,7 +52,9 @@
 GtkWidget *browser_search_bar_new(WebKitWebView *);
 void browser_search_bar_open(BrowserSearchBar *);
 void browser_search_bar_close(BrowserSearchBar *);
+gboolean browser_search_bar_is_open(BrowserSearchBar *);
 
+
 G_END_DECLS
 
 #endif

Modified: trunk/Tools/MiniBrowser/gtk/BrowserTab.c (262936 => 262937)


--- trunk/Tools/MiniBrowser/gtk/BrowserTab.c	2020-06-12 08:19:58 UTC (rev 262936)
+++ trunk/Tools/MiniBrowser/gtk/BrowserTab.c	2020-06-12 08:25:58 UTC (rev 262937)
@@ -625,7 +625,7 @@
 {
     g_return_if_fail(BROWSER_IS_TAB(tab));
 #if !GTK_CHECK_VERSION(3, 98, 0)
-    if (!gtk_widget_get_visible(GTK_WIDGET(tab->searchBar)))
+    if (!browser_search_bar_is_open(tab->searchBar))
         browser_search_bar_open(tab->searchBar);
 #endif
 }
@@ -634,7 +634,7 @@
 {
     g_return_if_fail(BROWSER_IS_TAB(tab));
 #if !GTK_CHECK_VERSION(3, 98, 0)
-    if (gtk_widget_get_visible(GTK_WIDGET(tab->searchBar)))
+    if (browser_search_bar_is_open(tab->searchBar))
         browser_search_bar_close(tab->searchBar);
 #endif
 }
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to