[libadwaita/wip/exalm/view-switcher-narrow: 2/2] view-switcher-title: Hide switcher for narrow windows




commit 17293a9a754ffb37e9fb5889baa636e7f03972b2
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Mon Nov 15 16:02:58 2021 +0500

    view-switcher-title: Hide switcher for narrow windows
    
    Well, apparently we can't get away from this. At least automate and
    encapsulate it.
    
    Fixes https://gitlab.gnome.org/GNOME/libadwaita/-/issues/14

 src/adw-view-switcher-title.c | 58 ++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 55 insertions(+), 3 deletions(-)
---
diff --git a/src/adw-view-switcher-title.c b/src/adw-view-switcher-title.c
index bfcfca4e..a5fe9e48 100644
--- a/src/adw-view-switcher-title.c
+++ b/src/adw-view-switcher-title.c
@@ -88,6 +88,8 @@ struct _AdwViewSwitcherTitle
   AdwViewSwitcher *narrow_view_switcher;
 
   gboolean view_switcher_enabled;
+  gboolean is_window_narrow;
+
   GtkSelectionModel *pages;
 };
 
@@ -104,7 +106,7 @@ update_view_switcher_visible (AdwViewSwitcherTitle *self)
   if (!self->squeezer)
     return;
 
-  if (self->view_switcher_enabled && self->pages) {
+  if (!self->is_window_narrow && self->view_switcher_enabled && self->pages) {
     guint i, n;
 
     n = g_list_model_get_n_items (G_LIST_MODEL (self->pages));
@@ -187,8 +189,9 @@ adw_view_switcher_title_set_property (GObject      *object,
 }
 
 static void
-adw_view_switcher_title_dispose (GObject *object) {
-  AdwViewSwitcherTitle *self = (AdwViewSwitcherTitle *)object;
+adw_view_switcher_title_dispose (GObject *object)
+{
+  AdwViewSwitcherTitle *self = ADW_VIEW_SWITCHER_TITLE (object);
 
   if (self->pages)
     g_signal_handlers_disconnect_by_func (self->pages, G_CALLBACK (update_view_switcher_visible), self);
@@ -199,6 +202,52 @@ adw_view_switcher_title_dispose (GObject *object) {
   G_OBJECT_CLASS (adw_view_switcher_title_parent_class)->dispose (object);
 }
 
+static gboolean
+check_window_width (AdwViewSwitcherTitle *self)
+{
+  GtkRoot *root = gtk_widget_get_root (GTK_WIDGET (self));
+  int width = gtk_widget_get_width (GTK_WIDGET (root));
+
+  self->is_window_narrow = width <= 360;
+  update_view_switcher_visible (self);
+
+  return G_SOURCE_REMOVE;
+}
+
+static void
+notify_surface_width_cb (AdwViewSwitcherTitle *self)
+{
+  g_idle_add (G_SOURCE_FUNC (check_window_width), self);
+}
+
+static void
+adw_view_switcher_title_realize (GtkWidget *widget)
+{
+  AdwViewSwitcherTitle *self = ADW_VIEW_SWITCHER_TITLE (widget);
+  GdkSurface *surface;
+
+  GTK_WIDGET_CLASS (adw_view_switcher_title_parent_class)->realize (widget);
+
+  surface = gtk_native_get_surface (gtk_widget_get_native (widget));
+
+  g_signal_connect_swapped (surface, "notify::width", G_CALLBACK (notify_surface_width_cb), self);
+
+  notify_surface_width_cb (self);
+}
+
+static void
+adw_view_switcher_title_unrealize (GtkWidget *widget)
+{
+  AdwViewSwitcherTitle *self = ADW_VIEW_SWITCHER_TITLE (widget);
+  GdkSurface *surface;
+
+  surface = gtk_native_get_surface (gtk_widget_get_native (widget));
+
+  g_signal_handlers_disconnect_by_func (surface, notify_surface_width_cb, self);
+
+  GTK_WIDGET_CLASS (adw_view_switcher_title_parent_class)->unrealize (widget);
+}
+
 static void
 adw_view_switcher_title_class_init (AdwViewSwitcherTitleClass *klass)
 {
@@ -209,6 +258,9 @@ adw_view_switcher_title_class_init (AdwViewSwitcherTitleClass *klass)
   object_class->get_property = adw_view_switcher_title_get_property;
   object_class->set_property = adw_view_switcher_title_set_property;
 
+  widget_class->realize = adw_view_switcher_title_realize;
+  widget_class->unrealize = adw_view_switcher_title_unrealize;
+
   /**
    * AdwViewSwitcherTitle:stack: (attributes org.gtk.Property.get=adw_view_switcher_title_get_stack 
org.gtk.Property.set=adw_view_switcher_title_set_stack)
    *


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