[epiphany] location-entry: Always select whole text on focus



commit 460d5d90ed8d4d9b51571b133a9383b6f04e2e11
Author: Jan-Michael Brummer <jan brummer tabos org>
Date:   Wed Jun 3 17:47:50 2020 +0200

    location-entry: Always select whole text on focus
    
    Updates focus handling:
    
    CTRL + l, mouse click and touch selects whole text on activation
    On focus out, deselect whole text
    
    This patch integrates a MR previously proposed by Jonathan Kang.

 lib/widgets/ephy-location-entry.c | 51 +++++++++++++++++++++++++++++++++------
 1 file changed, 43 insertions(+), 8 deletions(-)
---
diff --git a/lib/widgets/ephy-location-entry.c b/lib/widgets/ephy-location-entry.c
index 978febbd5..4c8ea0a36 100644
--- a/lib/widgets/ephy-location-entry.c
+++ b/lib/widgets/ephy-location-entry.c
@@ -68,6 +68,7 @@ struct _EphyLocationEntry {
   GtkCssProvider *css_provider;
 
   gboolean reader_mode_active;
+  gboolean button_release_is_blocked;
 
   char *saved_text;
   char *jump_tab;
@@ -114,6 +115,44 @@ static void schedule_dns_prefetch (EphyLocationEntry *entry,
 G_DEFINE_TYPE_WITH_CODE (EphyLocationEntry, ephy_location_entry, GTK_TYPE_OVERLAY,
                          G_IMPLEMENT_INTERFACE (EPHY_TYPE_TITLE_WIDGET,
                                                 ephy_location_entry_title_widget_interface_init))
+static gboolean
+entry_button_release (GtkWidget *widget,
+                      GdkEvent  *event,
+                      gpointer   user_data)
+{
+  EphyLocationEntry *entry = EPHY_LOCATION_ENTRY (user_data);
+
+  if (((GdkEventButton *)event)->button != GDK_BUTTON_PRIMARY)
+    return GDK_EVENT_PROPAGATE;
+
+  gtk_editable_select_region (GTK_EDITABLE (entry->url_entry), 0, -1);
+
+  g_signal_handlers_block_by_func (widget, G_CALLBACK (entry_button_release), entry);
+  entry->button_release_is_blocked = TRUE;
+
+  return GDK_EVENT_STOP;
+}
+
+static gboolean
+entry_focus_out_event (GtkWidget *widget,
+                       GdkEvent  *event,
+                       gpointer   user_data)
+{
+  EphyLocationEntry *entry = EPHY_LOCATION_ENTRY (user_data);
+
+  if (((GdkEventButton *)event)->button != GDK_BUTTON_PRIMARY)
+    return GDK_EVENT_PROPAGATE;
+
+  /* Unselect. */
+  gtk_editable_select_region (GTK_EDITABLE (entry->url_entry), 0, 0);
+
+  if (entry->button_release_is_blocked) {
+    g_signal_handlers_unblock_by_func (widget, G_CALLBACK (entry_button_release), entry);
+    entry->button_release_is_blocked = FALSE;
+  }
+
+  return GDK_EVENT_PROPAGATE;
+}
 
 static void
 editable_changed_cb (GtkEditable       *editable,
@@ -1045,6 +1084,8 @@ ephy_location_entry_construct_contents (EphyLocationEntry *entry)
   g_signal_connect (entry->url_entry, "suggestion-activated",
                     G_CALLBACK (ephy_location_entry_suggestion_activated), entry);
 
+  g_signal_connect (entry->url_entry, "button-release-event", G_CALLBACK (entry_button_release), entry);
+  g_signal_connect (entry->url_entry, "focus-out-event", G_CALLBACK (entry_focus_out_event), entry);
 
   controller = dzl_shortcut_controller_find (entry->url_entry);
   dzl_shortcut_controller_add_command_callback (controller,
@@ -1071,6 +1112,7 @@ ephy_location_entry_init (EphyLocationEntry *le)
 
   le->user_changed = FALSE;
   le->block_update = FALSE;
+  le->button_release_is_blocked = FALSE;
   le->saved_text = NULL;
 
   ephy_location_entry_construct_contents (le);
@@ -1274,14 +1316,7 @@ ephy_location_entry_reset (EphyLocationEntry *entry)
 void
 ephy_location_entry_focus (EphyLocationEntry *entry)
 {
-  GtkWidget *toplevel, *widget = GTK_WIDGET (entry->url_entry);
-
-  toplevel = gtk_widget_get_toplevel (widget);
-
-  gtk_editable_select_region (GTK_EDITABLE (entry->url_entry),
-                              0, -1);
-  gtk_window_set_focus (GTK_WINDOW (toplevel),
-                        widget);
+  gtk_widget_grab_focus (GTK_WIDGET (entry->url_entry));
 }
 
 void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]