[nautilus/antoniof/hide-path-border-on-small-windows: 5/8] container-max-width: Add width-maximized property



commit 28295528ae34960378f6906ab24eaa6e7a92585e
Author: António Fernandes <antoniof gnome org>
Date:   Wed Aug 1 21:57:25 2018 +0100

    container-max-width: Add width-maximized property
    
    It may be useful when we want to change look or behavior depending on
    whether the container has grown to maximum width.
    
    The next commit will make use of this.

 src/nautilus-container-max-width.c | 80 +++++++++++++++++++++++++++++++++++++-
 src/nautilus-container-max-width.h |  3 +-
 2 files changed, 81 insertions(+), 2 deletions(-)
---
diff --git a/src/nautilus-container-max-width.c b/src/nautilus-container-max-width.c
index 3ed8c8116..ef6fb8792 100644
--- a/src/nautilus-container-max-width.c
+++ b/src/nautilus-container-max-width.c
@@ -6,6 +6,9 @@ struct _NautilusContainerMaxWidth
 {
     GtkBin parent_instance;
     guint max_width;
+
+    gboolean width_maximized;
+    guint change_width_maximized_idle_id;
 };
 
 G_DEFINE_TYPE (NautilusContainerMaxWidth, nautilus_container_max_width, GTK_TYPE_BIN)
@@ -14,6 +17,7 @@ enum
 {
     PROP_0,
     PROP_MAX_WIDTH,
+    PROP_WIDTH_MAXIMIZED,
     N_PROPS
 };
 
@@ -31,6 +35,12 @@ nautilus_container_max_width_get_max_width (NautilusContainerMaxWidth *self)
     return self->max_width;
 }
 
+gboolean
+nautilus_container_max_width_get_width_maximized (NautilusContainerMaxWidth *self)
+{
+    return self->width_maximized;
+}
+
 NautilusContainerMaxWidth *
 nautilus_container_max_width_new (void)
 {
@@ -40,6 +50,13 @@ nautilus_container_max_width_new (void)
 static void
 nautilus_container_max_width_finalize (GObject *object)
 {
+    NautilusContainerMaxWidth *self = NAUTILUS_CONTAINER_MAX_WIDTH (object);
+
+    if (self->change_width_maximized_idle_id != 0)
+    {
+        g_source_remove (self->change_width_maximized_idle_id);
+    }
+
     G_OBJECT_CLASS (nautilus_container_max_width_parent_class)->finalize (object);
 }
 
@@ -59,6 +76,12 @@ nautilus_container_max_width_get_property (GObject    *object,
         }
         break;
 
+        case PROP_WIDTH_MAXIMIZED:
+        {
+            g_value_set_boolean (value, self->width_maximized);
+        }
+        break;
+
         default:
         {
             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -177,6 +200,47 @@ get_preferred_width_for_height (GtkWidget *widget,
     get_preferred_width (widget, minimum_size, natural_size);
 }
 
+static gboolean
+change_width_maximized_idle_callback (gpointer userdata)
+{
+    NautilusContainerMaxWidth *self = userdata;
+
+    self->change_width_maximized_idle_id = 0;
+
+    self->width_maximized = !self->width_maximized;
+    g_object_notify (G_OBJECT (self), "width-maximized");
+
+    return G_SOURCE_REMOVE;
+}
+
+static void
+on_size_allocate (GtkWidget    *widget,
+                  GdkRectangle *allocation,
+                  gpointer      userdata)
+{
+    NautilusContainerMaxWidth *self = NAUTILUS_CONTAINER_MAX_WIDTH (widget);
+    gboolean is_width_maximized;
+
+    is_width_maximized = self->max_width == -1 ? FALSE : allocation->width >= self->max_width;
+
+    if (self->width_maximized != is_width_maximized)
+    {
+        /* The handlers of the "notify::width-maximized" signal may trigger
+         * a reallocation, which shouldn't happen at this point because we are
+         * still in size_allocate phase. So, change the property on idle*/
+        if (self->change_width_maximized_idle_id == 0)
+        {
+            self->change_width_maximized_idle_id = g_idle_add (change_width_maximized_idle_callback, self);
+        }
+    }
+    else if (self->change_width_maximized_idle_id != 0)
+    {
+        /* This was going to change self->width_maximized, let's cancel it. */
+        g_source_remove (self->change_width_maximized_idle_id);
+        self->change_width_maximized_idle_id = 0;
+    }
+}
+
 static void
 constructed (GObject *obj)
 {
@@ -187,6 +251,13 @@ constructed (GObject *obj)
     /* We want our parent to gives our preferred width */
     gtk_widget_set_halign (GTK_WIDGET (self), GTK_ALIGN_CENTER);
     self->max_width = -1;
+
+    /* We want to know when the container has grown to its max width */
+    self->width_maximized = FALSE;
+    g_signal_connect (GTK_WIDGET (self),
+                      "size-allocate",
+                      G_CALLBACK (on_size_allocate),
+                      NULL);
 }
 
 static void
@@ -215,8 +286,15 @@ nautilus_container_max_width_class_init (NautilusContainerMaxWidthClass *klass)
                                                        G_MAXINT,
                                                        0,
                                                        G_PARAM_READWRITE));
-}
 
+    g_object_class_install_property (object_class,
+                                     PROP_WIDTH_MAXIMIZED,
+                                     g_param_spec_boolean ("width-maximized",
+                                                           "Width maximized",
+                                                           "Whether the container is at the max width",
+                                                           FALSE,
+                                                           G_PARAM_READABLE));
+}
 static void
 nautilus_container_max_width_init (NautilusContainerMaxWidth *self)
 {
diff --git a/src/nautilus-container-max-width.h b/src/nautilus-container-max-width.h
index 19621f9dc..3373416d5 100644
--- a/src/nautilus-container-max-width.h
+++ b/src/nautilus-container-max-width.h
@@ -14,5 +14,6 @@ NautilusContainerMaxWidth *nautilus_container_max_width_new (void);
 void nautilus_container_max_width_set_max_width (NautilusContainerMaxWidth *self,
                                                  guint                      max_width);
 guint nautilus_container_max_width_get_max_width (NautilusContainerMaxWidth *self);
+gboolean nautilus_container_max_width_get_width_maximized (NautilusContainerMaxWidth *self);
 
-G_END_DECLS
\ No newline at end of file
+G_END_DECLS


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