[gnome-clocks/bilelmoussaoui/redesign: 9/10] Refactoring: Stop passing the headerbar to each panel



commit 860645b025d1d499484d46cd20c82d99c3649f8a
Author: Bilal Elmoussaoui <bil elmoussaoui gmail com>
Date:   Thu Sep 19 00:34:07 2019 +0200

    Refactoring: Stop passing the headerbar to each panel
    
    For now, Clock passes a headerbar to each panel.
    Replace that with an actions_widget that we can add to a stack we putt on the headerbar

 data/ui/window.ui  | 125 +++++++++++++++++++++++++----------------------------
 src/alarm.vala     |  37 ++--------------
 src/clock.vala     |  23 +++++++---
 src/stopwatch.vala |   5 +--
 src/timer.vala     |  11 ++---
 src/widgets.vala   |   4 +-
 src/window.vala    |  54 +++++++++++------------
 src/world.vala     |  53 ++++-------------------
 8 files changed, 122 insertions(+), 190 deletions(-)
---
diff --git a/data/ui/window.ui b/data/ui/window.ui
index 17882e0..d03740e 100644
--- a/data/ui/window.ui
+++ b/data/ui/window.ui
@@ -7,13 +7,26 @@
     <property name="can_focus">False</property>
     <property name="title" translatable="yes">Clocks</property>
     <child type="titlebar">
-      <object class="ClocksHeaderBar" id="header_bar">
+      <object class="GtkHeaderBar" id="header_bar">
         <property name="visible">True</property>
+        <property name="can_focus">False</property>
         <property name="hexpand">True</property>
         <property name="vexpand">False</property>
-        <style>
-          <class name="titlebar"/>
-        </style>
+        <property name="show_close_button">True</property>
+        <child>
+          <object class="GtkStack" id="headerbar_actions_stack">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="hhomogeneous">False</property>
+            <property name="vhomogeneous">False</property>
+            <child>
+              <placeholder/>
+            </child>
+          </object>
+          <packing>
+            <property name="position">1</property>
+          </packing>
+        </child>
         <child type="title">
           <object class="HdySqueezer" id="squeezer">
             <property name="visible">True</property>
@@ -62,16 +75,17 @@
               </object>
             </child>
           </object>
-          <packing>
-            <property name="pack_type">start</property>
-          </packing>
         </child>
         <child>
           <object class="GtkMenuButton" id="menu_button">
+            <property name="can_focus">False</property>
+            <property name="receives_default">False</property>
             <property name="valign">center</property>
-            <property name="menu_model">primary-menu</property>
             <property name="action_name">win.show-primary-menu</property>
             <property name="direction">none</property>
+            <child>
+              <placeholder/>
+            </child>
             <style>
               <class name="image-button"/>
             </style>
@@ -80,67 +94,46 @@
             <property name="pack_type">end</property>
           </packing>
         </child>
+        <style>
+          <class name="titlebar"/>
+        </style>
       </object>
     </child>
     <child>
-
-              <object class="GtkBox">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="orientation">vertical</property>
-                <child>
-                  <object class="GtkStack" id="stack">
-                    <property name="visible">True</property>
-                    <property name="can_focus">False</property>
-                    <property name="hexpand">True</property>
-                    <property name="vexpand">True</property>
-                    <property name="homogeneous">False</property>
-                    <child>
-                      <placeholder/>
-                    </child>
-                  </object>
-                  <packing>
-                    <property name="expand">True</property>
-                    <property name="fill">True</property>
-                    <property name="position">0</property>
-                  </packing>
-                </child>
-                <child>
-                  <object class="HdyViewSwitcherBar" id="switcher_bar">
-                    <property name="visible">True</property>
-                    <property name="can_focus">False</property>
-                    <property name="stack">stack</property>
-                    <property name="reveal">True</property>
-                  </object>
-                  <packing>
-                    <property name="expand">False</property>
-                    <property name="fill">True</property>
-                    <property name="position">1</property>
-                  </packing>
-                </child>
-              </object>
-              <packing>
-                <property name="expand">True</property>
-                <property name="fill">True</property>
-                <property name="position">0</property>
-              </packing>
+      <object class="GtkBox">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="orientation">vertical</property>
+        <child>
+          <object class="GtkStack" id="stack">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="hexpand">True</property>
+            <property name="vexpand">True</property>
+            <child>
+              <placeholder/>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">True</property>
+            <property name="fill">True</property>
+            <property name="position">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="HdyViewSwitcherBar" id="switcher_bar">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="stack">stack</property>
+            <property name="reveal">True</property>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="position">1</property>
+          </packing>
+        </child>
+      </object>
     </child>
   </template>
-  <menu id="primary-menu">
-    <section>
-      <item>
-        <attribute name="label" translatable="yes">_Keyboard Shortcuts</attribute>
-        <attribute name="action">win.show-help-overlay</attribute>
-      </item>
-      <item>
-        <attribute name="label" translatable="yes">_Help</attribute>
-        <attribute name="action">win.help</attribute>
-      </item>
-      <item>
-        <attribute name="label" translatable="yes">_About Clocks</attribute>
-        <attribute name="action">win.about</attribute>
-      </item>
-    </section>
-  </menu>
-
 </interface>
diff --git a/src/alarm.vala b/src/alarm.vala
index 4d81f12..8cc67c4 100644
--- a/src/alarm.vala
+++ b/src/alarm.vala
@@ -584,7 +584,6 @@ private class RingingPanel : Gtk.Grid {
 public class Face : Gtk.Stack, Clocks.Clock {
     public string label { get; construct set; }
     public string icon_name { get; construct set; }
-    public HeaderBar header_bar { get; construct set; }
     public PanelId panel_id { get; construct set; }
 
     private ContentStore alarms;
@@ -596,11 +595,11 @@ public class Face : Gtk.Stack, Clocks.Clock {
     private ContentView content_view;
     [GtkChild]
     private RingingPanel ringing_panel;
+    public Gtk.Widget? header_actions_widget{ get; set; }
 
-    public Face (HeaderBar header_bar) {
+    public Face () {
         Object (label: _("Alarm"),
                 icon_name: "alarm-symbolic",
-                header_bar: header_bar,
                 panel_id: PanelId.ALARM);
 
         alarms = new ContentStore ();
@@ -629,16 +628,13 @@ public class Face : Gtk.Stack, Clocks.Clock {
 
         new_button = new Gtk.Button.from_icon_name ("list-add-symbolic", Gtk.IconSize.BUTTON);
         new_button.valign = Gtk.Align.CENTER;
-        new_button.no_show_all = true;
         new_button.action_name = "win.new";
-        header_bar.pack_start (new_button);
+        header_actions_widget = new_button;
 
         content_view.bind_model (alarms, (item) => {
             return new Tile ((Item)item);
         });
 
-        content_view.set_header_bar (header_bar);
-
         load ();
         show_all ();
 
@@ -682,15 +678,6 @@ public class Face : Gtk.Stack, Clocks.Clock {
        reset_view ();
     }
 
-    [GtkCallback]
-    private void visible_child_changed () {
-        if (visible_child == empty_view || visible_child == content_view) {
-            header_bar.mode = HeaderBar.Mode.NORMAL;
-        } else if (visible_child == ringing_panel) {
-            header_bar.mode = HeaderBar.Mode.STANDALONE;
-        }
-    }
-
     private void load () {
         alarms.deserialize (settings.get_value ("alarms"), Item.deserialize);
     }
@@ -726,7 +713,6 @@ public class Face : Gtk.Stack, Clocks.Clock {
 
     private void reset_view () {
         visible_child = alarms.get_n_items () == 0 ? empty_view : content_view;
-        request_header_bar_update ();
     }
 
     public void activate_new () {
@@ -754,23 +740,6 @@ public class Face : Gtk.Stack, Clocks.Clock {
     public bool escape_pressed () {
         return content_view.escape_pressed ();
     }
-
-    public void update_header_bar () {
-        switch (header_bar.mode) {
-        case HeaderBar.Mode.NORMAL:
-            new_button.show ();
-            content_view.update_header_bar ();
-            break;
-        case HeaderBar.Mode.SELECTION:
-            content_view.update_header_bar ();
-            break;
-        case HeaderBar.Mode.STANDALONE:
-            header_bar.title = ringing_panel.alarm.name;
-            break;
-        default:
-            assert_not_reached ();
-        }
-    }
 }
 
 } // namespace Alarm
diff --git a/src/clock.vala b/src/clock.vala
index cc8a018..97c8248 100644
--- a/src/clock.vala
+++ b/src/clock.vala
@@ -22,7 +22,22 @@ public enum PanelId {
     WORLD,
     ALARM,
     STOPWATCH,
-    TIMER,
+    TIMER;
+
+    public string to_string() {
+        switch (this) {
+            case WORLD:
+                return "world";
+            case ALARM:
+                return "alarm";
+            case STOPWATCH:
+                return "stopwatch";
+            case TIMER:
+                return "timer";
+            default:
+                assert_not_reached();
+        }
+    }
 }
 
 public const int N_PANELS = 4;
@@ -30,8 +45,8 @@ public const int N_PANELS = 4;
 public interface Clock : GLib.Object {
     public abstract string label { get; protected construct set; }
     public abstract string icon_name { get; protected construct set; }
-    public abstract HeaderBar header_bar { get; protected construct set; }
     public abstract PanelId panel_id { get; protected construct set; }
+    public abstract Gtk.Widget? header_actions_widget { get; protected set; default=null; }
 
     public virtual void activate_new () {
     }
@@ -49,10 +64,6 @@ public interface Clock : GLib.Object {
     public virtual void back () {
     }
 
-    public signal void request_header_bar_update ();
-
-    public virtual void update_header_bar () {
-    }
 }
 
 } // namespace Clocks
diff --git a/src/stopwatch.vala b/src/stopwatch.vala
index d3a7d75..88169a4 100644
--- a/src/stopwatch.vala
+++ b/src/stopwatch.vala
@@ -57,7 +57,6 @@ public class Face : Gtk.Box, Clocks.Clock {
 
     public string label { get; construct set; }
     public string icon_name { get; construct set; }
-    public HeaderBar header_bar { get; construct set; }
     public PanelId panel_id { get; construct set; }
 
     public State state { get; private set; default = State.RESET; }
@@ -76,11 +75,11 @@ public class Face : Gtk.Box, Clocks.Clock {
     private Gtk.ScrolledWindow laps_scrollwin;
     [GtkChild]
     private Gtk.ListBox laps_list;
+    public Gtk.Widget? header_actions_widget { get; set; default=null; }
 
-    public Face (HeaderBar header_bar) {
+    public Face () {
         Object (label: _("Stopwatch"),
                 icon_name: "stopwatch-symbolic",
-                header_bar: header_bar,
                 panel_id: PanelId.STOPWATCH);
 
         timer = new GLib.Timer ();
diff --git a/src/timer.vala b/src/timer.vala
index 597fd1e..4aa9954 100644
--- a/src/timer.vala
+++ b/src/timer.vala
@@ -86,7 +86,7 @@ public class Item : Object, ContentItem {
 
     public Item (GLib.Time timer, string name) {
         Object (name: name);
-        timer = timer;
+        this.timer = timer;
     }
 }
 
@@ -339,21 +339,20 @@ public class Face : Gtk.Stack, Clocks.Clock {
     public string label { get; construct set; }
     public string icon_name { get; construct set; }
 
-    public HeaderBar header_bar { get; construct set; }
     public PanelId panel_id { get; construct set; }
 
     [GtkChild]
     private ContentView content_view;
+    public Gtk.Widget? header_actions_widget{ get; set; }
 
     private Utils.Bell bell;
     private GLib.Notification notification;
     private ContentStore timers;
     private GLib.Settings settings;
 
-    public Face(HeaderBar header_bar) {
+    public Face() {
         Object (label: _("Timer"),
                 icon_name: "timer-symbolic",
-                header_bar: header_bar,
                 panel_id: PanelId.TIMER);
 
         bell = new Utils.Bell ("complete");
@@ -379,12 +378,10 @@ public class Face : Gtk.Stack, Clocks.Clock {
             return new Row((Item)item);
         });
 
-        content_view.set_header_bar (header_bar);
-
         var new_button = new Gtk.Button.from_icon_name ("list-add-symbolic", Gtk.IconSize.BUTTON);
         new_button.valign = Gtk.Align.CENTER;
         new_button.clicked.connect(on_add_clicked);
-        header_bar.pack_start (new_button);
+        header_actions_widget = new_button;
     }
 
     private void add_timer_item (Item item) {
diff --git a/src/widgets.vala b/src/widgets.vala
index de79c5d..dde0cfb 100644
--- a/src/widgets.vala
+++ b/src/widgets.vala
@@ -323,8 +323,8 @@ public class ContentView : Gtk.Bin {
         var scrolled_window = new Gtk.ScrolledWindow (null, null);
 
         var column = new Hdy.Column();
-        column.set_maximum_width(700);
-        column.set_linear_growth_width(700);
+        column.set_maximum_width(800);
+        column.set_linear_growth_width(800);
 
         column.add (list_box);
 
diff --git a/src/window.vala b/src/window.vala
index 2186343..4a9d92a 100644
--- a/src/window.vala
+++ b/src/window.vala
@@ -33,7 +33,10 @@ public class Window : Gtk.ApplicationWindow {
     };
 
     [GtkChild]
-    private HeaderBar header_bar;
+    private Gtk.HeaderBar header_bar;
+    [GtkChild]
+    private Gtk.Stack headerbar_actions_stack;
+
     [GtkChild]
     private Gtk.Stack stack;
     [GtkChild]
@@ -77,22 +80,24 @@ public class Window : Gtk.ApplicationWindow {
 
         panels = new Gtk.Widget[N_PANELS];
 
-        panels[PanelId.WORLD] = new World.Face (header_bar);
-        panels[PanelId.ALARM] =  new Alarm.Face (header_bar);
-        panels[PanelId.STOPWATCH] = new Stopwatch.Face (header_bar);
-        panels[PanelId.TIMER] = new Timer.Face (header_bar);
+        panels[PanelId.WORLD] = new World.Face ();
+        panels[PanelId.ALARM] =  new Alarm.Face ();
+        panels[PanelId.STOPWATCH] = new Stopwatch.Face ();
+        panels[PanelId.TIMER] = new Timer.Face ();
 
         var world = (World.Face)panels[PanelId.WORLD];
         var alarm = (Alarm.Face)panels[PanelId.ALARM];
         var stopwatch = (Stopwatch.Face)panels[PanelId.STOPWATCH];
         var timer = (Timer.Face)panels[PanelId.TIMER];
 
-        foreach (var clock in panels) {
-            stack.add_titled (clock, ((Clock)clock).label, ((Clock)clock).label);
-            stack.child_set_property(clock, "icon-name",  ((Clock)clock).icon_name);
-            ((Clock)clock).request_header_bar_update.connect (() => {
-                update_header_bar ();
-            });
+        foreach (var panel in panels) {
+            stack.add_titled (panel, ((Clock)panel).label, ((Clock)panel).label);
+            stack.child_set_property(panel, "icon-name", ((Clock)panel).icon_name);
+            var header_actions_widget = ((Clock)panel).header_actions_widget;
+            if (header_actions_widget != null) {
+                headerbar_actions_stack.add_named (header_actions_widget,
+                                                    ((Clock)panel).panel_id.to_string());
+            }
         }
 
         var stack_id = stack.notify["visible-child"].connect (() => {
@@ -101,19 +106,14 @@ public class Window : Gtk.ApplicationWindow {
             update_header_bar ();
         });
 
-        var header_bar_id = header_bar.notify["mode"].connect (() => {
-            update_header_bar ();
-        });
         this.size_allocate.connect((widget, allocation) => {
-            switcher_bar.set_reveal(allocation.width < 450);
+            switcher_bar.set_reveal(allocation.width < 500);
             squeezer.set_child_enabled(title_wide_switcher, allocation.width > 800);
-            squeezer.set_child_enabled(title_narrow_switcher, allocation.width > 450);
-            squeezer.set_child_enabled(title_text, allocation.width <= 450);
+            squeezer.set_child_enabled(title_narrow_switcher, allocation.width > 500);
+            squeezer.set_child_enabled(title_text, allocation.width <= 500);
         });
 
         stack.destroy.connect(() => {
-            header_bar.disconnect (header_bar_id);
-            header_bar_id = 0;
             stack.disconnect (stack_id);
             stack_id = 0;
         });
@@ -289,20 +289,18 @@ public class Window : Gtk.ApplicationWindow {
     }
 
     private void update_header_bar () {
-
         var clock = (Clock) stack.visible_child;
+        var panel_id = clock.panel_id.to_string();
+        if (headerbar_actions_stack.get_child_by_name(panel_id) != null) {
+            headerbar_actions_stack.visible_child_name = clock.panel_id.to_string();
+            headerbar_actions_stack.show();
+        } else {
+            headerbar_actions_stack.hide();
+        }
         if (clock != null) {
             settings.set_enum ("panel-id", clock.panel_id);
-            clock.update_header_bar ();
             ((Gtk.Widget) clock).grab_focus ();
         }
-
-        if (header_bar.mode == HeaderBar.Mode.NORMAL) {
-            // header_bar.custom_title = stack_switcher;
-            menu_button.show ();
-        }
-
-        header_bar.set_show_close_button (header_bar.mode != HeaderBar.Mode.SELECTION);
     }
 }
 
diff --git a/src/world.vala b/src/world.vala
index 15c0937..34f24ea 100644
--- a/src/world.vala
+++ b/src/world.vala
@@ -321,7 +321,6 @@ private class LocationDialog : Gtk.Dialog {
 public class Face : Gtk.Stack, Clocks.Clock {
     public string label { get; construct set; }
     public string icon_name { get; construct set; }
-    public HeaderBar header_bar { get; construct set; }
     public PanelId panel_id { get; construct set; }
 
     private ContentStore locations;
@@ -343,11 +342,11 @@ public class Face : Gtk.Stack, Clocks.Clock {
     private Gtk.Label standalone_sunrise_label;
     [GtkChild]
     private Gtk.Label standalone_sunset_label;
+    public Gtk.Widget? header_actions_widget{ get; set; }
 
-    public Face (HeaderBar header_bar) {
+    public Face () {
         Object (label: _("World"),
                 icon_name: "globe-symbolic",
-                header_bar: header_bar,
                 panel_id: PanelId.WORLD,
                 transition_type: Gtk.StackTransitionType.CROSSFADE);
 
@@ -364,28 +363,27 @@ public class Face : Gtk.Stack, Clocks.Clock {
             return 0;
         });
 
+        header_actions_widget = new Gtk.Stack();
+
         new_button = new Gtk.Button.from_icon_name ("list-add-symbolic", Gtk.IconSize.BUTTON);
         new_button.valign = Gtk.Align.CENTER;
-        new_button.no_show_all = true;
         new_button.action_name = "win.new";
-        header_bar.pack_start (new_button);
+        ((Gtk.Stack)header_actions_widget).add_named (new_button, "add");
 
         back_button = new Gtk.Button ();
         var back_button_image = new Gtk.Image.from_icon_name ("go-previous-symbolic", Gtk.IconSize.MENU);
         back_button.valign = Gtk.Align.CENTER;
         back_button.set_image (back_button_image);
-        back_button.no_show_all = true;
         back_button.clicked.connect (() => {
             reset_view ();
+            ((Gtk.Stack)header_actions_widget).visible_child_name = "add";
         });
-        header_bar.pack_start (back_button);
+        ((Gtk.Stack)header_actions_widget).add_named (back_button, "back");
 
         content_view.bind_model (locations, (item) => {
             return new Tile ((Item)item);
         });
 
-        content_view.set_header_bar (header_bar);
-
         load ();
         show_all ();
 
@@ -412,18 +410,11 @@ public class Face : Gtk.Stack, Clocks.Clock {
         });
     }
 
+
     [GtkCallback]
     private void item_activated (ContentItem item) {
         show_standalone ((Item) item);
-    }
-
-    [GtkCallback]
-    private void visible_child_changed () {
-        if (visible_child == empty_view || visible_child == content_view) {
-            header_bar.mode = HeaderBar.Mode.NORMAL;
-        } else if (visible_child == standalone) {
-            header_bar.mode = HeaderBar.Mode.STANDALONE;
-        }
+        ((Gtk.Stack)header_actions_widget).visible_child_name = "back";
     }
 
     private void update_standalone () {
@@ -534,32 +525,6 @@ public class Face : Gtk.Stack, Clocks.Clock {
     public void reset_view () {
         standalone_location = null;
         visible_child = locations.get_n_items () == 0 ? empty_view : content_view;
-        request_header_bar_update ();
-    }
-
-    public void update_header_bar () {
-        switch (header_bar.mode) {
-        case HeaderBar.Mode.NORMAL:
-            header_bar.title = _("Clocks");
-            header_bar.subtitle = null;
-            new_button.show ();
-            content_view.update_header_bar ();
-            break;
-        case HeaderBar.Mode.SELECTION:
-            content_view.update_header_bar ();
-            break;
-        case HeaderBar.Mode.STANDALONE:
-            if (standalone_location.state_name != null) {
-                header_bar.title = "%s, %s".printf (standalone_location.city_name, 
standalone_location.state_name);
-            } else {
-                header_bar.title = standalone_location.city_name;
-            }
-            header_bar.subtitle = standalone_location.country_name;
-            back_button.show ();
-            break;
-        default:
-            assert_not_reached ();
-        }
     }
 }
 


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