[gnome-builder/wip/gtk4-port] libide/webkit: bind enabled state for actions



commit 18ceee047de217c28ffe6d28044e8ee6420d09fb
Author: Christian Hergert <chergert redhat com>
Date:   Fri Jun 3 15:37:34 2022 -0700

    libide/webkit: bind enabled state for actions

 src/libide/webkit/ide-webkit-page.c  | 33 ++++++++++++++++++++++++++++-----
 src/libide/webkit/ide-webkit-page.ui |  4 +++-
 2 files changed, 31 insertions(+), 6 deletions(-)
---
diff --git a/src/libide/webkit/ide-webkit-page.c b/src/libide/webkit/ide-webkit-page.c
index ee5068209..b49d665a9 100644
--- a/src/libide/webkit/ide-webkit-page.c
+++ b/src/libide/webkit/ide-webkit-page.c
@@ -116,9 +116,9 @@ transform_cairo_surface_to_gicon (GBinding     *binding,
 }
 
 static void
-toolbar_notify_visible_cb (IdeWebkitPage *self,
-                           GParamSpec    *pspec,
-                           GtkWidget     *toolbar)
+on_toolbar_notify_visible_cb (IdeWebkitPage *self,
+                              GParamSpec    *pspec,
+                              GtkWidget     *toolbar)
 {
   g_assert (IDE_IS_WEBKIT_PAGE (self));
   g_assert (GTK_IS_WIDGET (toolbar));
@@ -249,6 +249,25 @@ on_back_forward_list_changed_cb (IdeWebkitPage             *self,
   IDE_EXIT;
 }
 
+static void
+ide_webkit_page_update_reload (IdeWebkitPage *self)
+{
+  IdeWebkitPagePrivate *priv = ide_webkit_page_get_instance_private (self);
+  const char *uri;
+  gboolean loading;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_WEBKIT_PAGE (self));
+
+  loading = webkit_web_view_is_loading (priv->web_view);
+  uri = webkit_web_view_get_uri (priv->web_view);
+
+  set_action_enabled (self, "reload", !loading && !ide_str_empty0 (uri));
+
+  IDE_EXIT;
+}
+
 static void
 ide_webkit_page_constructed (GObject *object)
 {
@@ -328,7 +347,8 @@ ide_webkit_page_class_init (IdeWebkitPageClass *klass)
   gtk_widget_class_bind_template_child_private (widget_class, IdeWebkitPage, toolbar);
   gtk_widget_class_bind_template_child_private (widget_class, IdeWebkitPage, url_bar);
   gtk_widget_class_bind_template_child_private (widget_class, IdeWebkitPage, web_view);
-  gtk_widget_class_bind_template_callback (widget_class, toolbar_notify_visible_cb);
+  gtk_widget_class_bind_template_callback (widget_class, on_toolbar_notify_visible_cb);
+  gtk_widget_class_bind_template_callback (widget_class, ide_webkit_page_update_reload);
 
   g_type_ensure (WEBKIT_TYPE_WEB_VIEW);
   g_type_ensure (IDE_TYPE_URL_BAR);
@@ -364,7 +384,10 @@ ide_webkit_page_init (IdeWebkitPage *self)
                            G_CALLBACK (on_back_forward_list_changed_cb),
                            self,
                            G_CONNECT_SWAPPED);
-  on_back_forward_list_changed_cb (self, NULL, NULL, list);
+
+  set_action_enabled (self, "go-forward", FALSE);
+  set_action_enabled (self, "go-back", FALSE);
+  set_action_enabled (self, "reload", FALSE);
 }
 
 IdeWebkitPage *
diff --git a/src/libide/webkit/ide-webkit-page.ui b/src/libide/webkit/ide-webkit-page.ui
index 7bf4232e2..d534f2503 100644
--- a/src/libide/webkit/ide-webkit-page.ui
+++ b/src/libide/webkit/ide-webkit-page.ui
@@ -10,6 +10,8 @@
           <object class="WebKitWebView" id="web_view">
             <property name="hexpand">true</property>
             <property name="vexpand">true</property>
+            <signal name="notify::is-loading" handler="ide_webkit_page_update_reload" swapped="true" 
object="IdeWebkitPage"/>
+            <signal name="notify::uri" handler="ide_webkit_page_update_reload" swapped="true" 
object="IdeWebkitPage"/>
           </object>
         </child>
         <child>
@@ -17,7 +19,7 @@
         </child>
         <child>
           <object class="GtkCenterBox" id="toolbar">
-            <signal name="notify::visible" handler="toolbar_notify_visible_cb" swapped="true" 
object="IdeWebkitPage"/>
+            <signal name="notify::visible" handler="on_toolbar_notify_visible_cb" swapped="true" 
object="IdeWebkitPage"/>
             <property name="orientation">horizontal</property>
             <style>
               <class name="toolbar"/>


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