[gnome-weather] WeatherWidget: switch to gtk templates



commit f340a9612a7bfd78e8c5a25386605a8747cf39d6
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Fri Mar 6 20:26:06 2015 -0800

    WeatherWidget: switch to gtk templates
    
    And eliminate some crufty redundant code in the process

 data/city.ui      |   26 +++++++-------
 src/app/city.js   |   99 +++++++++++++++++++++++++----------------------------
 src/app/window.js |   10 +----
 3 files changed, 62 insertions(+), 73 deletions(-)
---
diff --git a/data/city.ui b/data/city.ui
index 1ae124e..88b6598 100644
--- a/data/city.ui
+++ b/data/city.ui
@@ -2,18 +2,18 @@
 <!-- Generated with glade 3.16.1 -->
 <interface>
   <requires lib="gtk+" version="3.0"/>
-  <object class="GtkOverlay" id="outer-box">
+  <template class="Gjs_WeatherWidget" parent="GtkFrame">
     <property name="visible">True</property>
     <property name="can_focus">False</property>
     <child>
-      <object class="GtkFrame" id="content-frame">
+      <object class="GtkFrame" id="contentFrame">
         <property name="name">weather-page-content-view</property>
         <property name="visible">True</property>
         <property name="can_focus">False</property>
         <property name="label_xalign">0</property>
         <property name="shadow_type">none</property>
         <child>
-          <object class="GtkGrid" id="outer-grid">
+          <object class="GtkGrid" id="outerGrid">
             <property name="visible">True</property>
             <property name="can_focus">False</property>
             <child>
@@ -40,7 +40,7 @@
                       </object>
                     </child>
                     <child>
-                      <object class="GtkImage" id="conditions-image">
+                      <object class="GtkImage" id="conditionsImage">
                         <property name="name">conditions-image</property>
                         <property name="visible">True</property>
                         <property name="can_focus">False</property>
@@ -55,7 +55,7 @@
                       </packing>
                     </child>
                     <child>
-                      <object class="GtkLabel" id="temperature-label">
+                      <object class="GtkLabel" id="temperatureLabel">
                         <property name="name">temperature-label</property>
                         <property name="visible">True</property>
                         <property name="can_focus">False</property>
@@ -70,7 +70,7 @@
                       </packing>
                     </child>
                     <child>
-                      <object class="GtkLabel" id="conditions-label">
+                      <object class="GtkLabel" id="conditionsLabel">
                         <property name="name">conditions-label</property>
                         <property name="visible">True</property>
                         <property name="can_focus">False</property>
@@ -98,7 +98,7 @@
                     <child>
                       <object class="GtkStackSwitcher" id="day-stack-switcher">
                         <property name="visible">True</property>
-                        <property name="stack">forecast-stack</property>
+                        <property name="stack">forecastStack</property>
                         <style>
                           <class name="osd"/>
                         </style>
@@ -119,7 +119,7 @@
                     <property name="visible">True</property>
                     <property name="can_focus">False</property>
                     <child>
-                      <object class="GtkStack" id="forecast-stack">
+                      <object class="GtkStack" id="forecastStack">
                         <property name="visible">True</property>
                         <property name="can-focus">False</property>
                         <child>
@@ -187,7 +187,7 @@
                       </packing>
                     </child>
                     <child>
-                      <object class="GtkButton" id="left-button">
+                      <object class="GtkButton" id="leftButton">
                         <property name="visible">True</property>
                         <property name="can_focus">True</property>
                         <property name="receives_default">True</property>
@@ -210,7 +210,7 @@
                       </packing>
                     </child>
                     <child>
-                      <object class="GtkButton" id="right-button">
+                      <object class="GtkButton" id="rightButton">
                         <property name="visible">True</property>
                         <property name="can_focus">True</property>
                         <property name="receives_default">True</property>
@@ -246,7 +246,7 @@
               </packing>
             </child>
             <child>
-              <object class="GtkGrid" id="time-grid">
+              <object class="GtkGrid" id="timeGrid">
                 <property name="visible">True</property>
                 <property name="can_focus">False</property>
                 <property name="halign">end</property>
@@ -270,7 +270,7 @@
                   </packing>
                 </child>
                 <child>
-                  <object class="GtkLabel" id="time-label">
+                  <object class="GtkLabel" id="timeLabel">
                     <property name="visible">True</property>
                     <property name="can_focus">False</property>
                     <property name="margin-top">15</property>
@@ -296,5 +296,5 @@
         </child>
       </object>
     </child>
-  </object>
+  </template>
 </interface>
diff --git a/src/app/city.js b/src/app/city.js
index 939933c..d8c11c2 100644
--- a/src/app/city.js
+++ b/src/app/city.js
@@ -33,6 +33,13 @@ const SCROLLING_ANIMATION_TIME = 400000; //us
 const WeatherWidget = new Lang.Class({
     Name: 'WeatherWidget',
     Extends: Gtk.Frame,
+    Template: 'resource:///org/gnome/Weather/Application/city.ui',
+    InternalChildren: ['contentFrame', 'outerGrid', 'conditionsImage',
+                       'temperatureLabel', 'conditionsLabel',
+                       'timeLabel', 'timeGrid', 'forecastStack',
+                       'leftButton', 'rightButton',
+                       'forecast-today-grid', 'forecast-tomorrow-grid',
+                       'forecast-today', 'forecast-tomorrow'],
 
     _init: function(params) {
         params = Params.fill(params, { shadow_type: Gtk.ShadowType.NONE,
@@ -42,48 +49,28 @@ const WeatherWidget = new Lang.Class({
         this._currentStyle = null;
         this._info = null;
 
-        let builder = new Gtk.Builder();
-        builder.add_from_resource('/org/gnome/Weather/Application/city.ui');
-
-        let outerBox = builder.get_object('outer-box');
-        this._contentFrame = builder.get_object('content-frame');
-        this._outerGrid = builder.get_object('outer-grid');
-        this._wForecastFrame = builder.get_object('weekly-forecast-frame');
-        this._icon = builder.get_object('conditions-image');
-        this._temperature = builder.get_object('temperature-label');
-        this._conditions = builder.get_object('conditions-label');
-        this.timeLabel = builder.get_object('time-label');
-        this.timeGrid = builder.get_object('time-grid');
-
         this._weeklyForecasts = new WForecast.WeeklyForecastFrame();
         this._outerGrid.attach(this._weeklyForecasts, 1, 0, 1, 2);
 
         this._forecasts = { };
-        this._hadjustments = { };
 
         for (let t of ['today', 'tomorrow']) {
             let box = new Forecast.ForecastBox({ hexpand: false });
 
             this._forecasts[t] = box;
-            builder.get_object('forecast-' + t + '-grid').add(box);
+            this['_forecast_' + t + '_grid'].add(box);
 
-            let fsw = builder.get_object('forecast-' + t);
+            let fsw = this['_forecast_' + t];
             let hscrollbar = fsw.get_hscrollbar();
             hscrollbar.set_opacity(0.0);
             hscrollbar.hide();
             let hadjustment = fsw.get_hadjustment();
             hadjustment.connect('changed', Lang.bind(this, this._syncLeftRightButtons));
             hadjustment.connect('value-changed', Lang.bind(this, this._syncLeftRightButtons));
-
-            this._hadjustments[t] = hadjustment;
         }
 
-        this._dayStack = builder.get_object('forecast-stack');
-        this._leftButton = builder.get_object('left-button');
-        this._rightButton = builder.get_object('right-button');
-
-        this._dayStack.connect('notify::visible-child', Lang.bind(this, function() {
-            let visible_child = this._dayStack.visible_child;
+        this._forecastStack.connect('notify::visible-child', Lang.bind(this, function() {
+            let visible_child = this._forecastStack.visible_child;
             if (visible_child == null)
                 return; // can happen at destruction
 
@@ -100,24 +87,22 @@ const WeatherWidget = new Lang.Class({
         this._tickId = 0;
 
         this._leftButton.connect('clicked', Lang.bind(this, function() {
-            let hadjustment = this._dayStack.visible_child.get_hadjustment();
+            let hadjustment = this._forecastStack.visible_child.get_hadjustment();
             let target = hadjustment.value - hadjustment.page_size;
 
             this._beginScrollAnimation(target);
         }));
 
         this._rightButton.connect('clicked', Lang.bind(this, function() {
-            let hadjustment = this._dayStack.visible_child.get_hadjustment();
+            let hadjustment = this._forecastStack.visible_child.get_hadjustment();
             let target = hadjustment.value + hadjustment.page_size;
 
             this._beginScrollAnimation(target);
         }));
-
-        this.add(outerBox);
     },
 
     _syncLeftRightButtons: function() {
-        let hadjustment = this._dayStack.visible_child.get_hadjustment();
+        let hadjustment = this._forecastStack.visible_child.get_hadjustment();
         if ((hadjustment.get_upper() - hadjustment.get_lower()) == hadjustment.page_size) {
             this._leftButton.set_sensitive(false);
             this._rightButton.set_sensitive(false);
@@ -146,7 +131,7 @@ const WeatherWidget = new Lang.Class({
     },
 
     _animate: function(target, start, end) {
-        let hadjustment = this._dayStack.visible_child.get_hadjustment();
+        let hadjustment = this._forecastStack.visible_child.get_hadjustment();
         let value = hadjustment.value;
         let t = 1.0;
         let now = this.get_frame_clock().get_frame_time();
@@ -173,23 +158,31 @@ const WeatherWidget = new Lang.Class({
         }
     },
 
-    _get_style_class: function(info) {
+    _getStyleClass: function(info) {
         let icon = info.get_icon_name();
         let name = icon.replace(/(-\d{3})/, "");
         return name;
     },
 
+    setTimeVisible: function(visible) {
+        this._timeGrid.visible = visible;
+    },
+
+    setTime: function(time) {
+        this._timeLabel.label = time;
+    },
+
     update: function(info) {
         this._info = info;
 
-        this._conditions.label = Util.getWeatherConditions(info);
-        this._temperature.label = info.get_temp_summary();
+        this._conditionsLabel.label = Util.getWeatherConditions(info);
+        this._temperatureLabel.label = info.get_temp_summary();
 
-        this._icon.icon_name = info.get_symbolic_icon_name();
+        this._conditionsImage.icon_name = info.get_symbolic_icon_name();
         let context = this._contentFrame.get_style_context();
         if (this._currentStyle)
             context.remove_class(this._currentStyle);
-        this._currentStyle = this._get_style_class(info);
+        this._currentStyle = this._getStyleClass(info);
         context.add_class(this._currentStyle);
 
         let forecasts = info.get_forecast_list();
@@ -224,8 +217,8 @@ const WeatherView = new Lang.Class({
                                         name: "loading-label" }));
         this.add_named(loadingPage, 'loading');
 
-        this.infoPage = new WeatherWidget();
-        this.add_named(this.infoPage, 'info');
+        this._infoPage = new WeatherWidget();
+        this.add_named(this._infoPage, 'info');
 
         this._info = null;
         this._updateId = 0;
@@ -233,7 +226,7 @@ const WeatherView = new Lang.Class({
         this.connect('destroy', Lang.bind(this, this._onDestroy));
 
         this._wallClock = new Gnome.WallClock();
-        this._clockHandlerId = null;
+        this._clockHandlerId = 0;
     },
 
     get info() {
@@ -245,7 +238,7 @@ const WeatherView = new Lang.Class({
             this._info.disconnect(this._updateId);
             this._updateId = 0;
 
-            this.infoPage.clear();
+            this._infoPage.clear();
         }
 
         this._info = info;
@@ -268,25 +261,34 @@ const WeatherView = new Lang.Class({
     update: function() {
         this.visible_child_name = 'loading';
         this._spinner.start();
-        this.infoPage.clear();
+        this._infoPage.clear();
 
         getApp().model.updateInfo(this._info);
     },
 
     _onUpdate: function(info) {
-        this.infoPage.clear();
-        this.infoPage.update(info);
+        this._infoPage.clear();
+        this._infoPage.update(info);
         this._updateTime();
         this._spinner.stop();
         this.visible_child_name = 'info';
     },
 
-    connectClock: function() {
-        this._clockHandlerId = this._wallClock.connect('notify::clock', Lang.bind(this, this._updateTime));
+    setTimeVisible: function(visible) {
+        if (this._clockHandlerId && !visible) {
+            this._wallClock.disconnect(this._clockHandlerId);
+            this._clockHandlerId = 0;
+        }
+
+        if (!this._clockHandlerId && visible) {
+            this._clockHandlerId = this._wallClock.connect('notify::clock', Lang.bind(this, 
this._updateTime));
+        }
+
+        this._infoPage.setTimeVisible(visible);
     },
 
     _updateTime: function() {
-        this.infoPage.timeLabel.label = this._getTime();
+        this._infoPage.setTime(this._getTime());
     },
 
     _getTime: function() {
@@ -297,12 +299,5 @@ const WeatherView = new Lang.Class({
             return dt.format(_("%H:%M"));
         }
         return null;
-    },
-
-    disconnectClock: function() {
-        if (this._clockHandlerId) {
-            this._wallClock.disconnect(this._clockHandlerId);
-            this._clockHandlerId = null;
-        }
     }
 });
diff --git a/src/app/window.js b/src/app/window.js
index 945a33d..d634c83 100644
--- a/src/app/window.js
+++ b/src/app/window.js
@@ -155,7 +155,7 @@ const MainWindow = new Lang.Class({
     },
 
     showSearch: function(text) {
-        this._cityView.disconnectClock();
+        this._cityView.setTimeVisible(false);
         this._stack.set_visible_child(this._searchView);
         this._goToPage(Page.SEARCH);
         this._searchEntry.text = text;
@@ -184,19 +184,13 @@ const MainWindow = new Lang.Class({
 
         this.currentInfo = info;
         this._cityView.info = info;
-        this._cityView.disconnectClock();
 
         let isCurrentTimezone = false;
         let currentLocation = this.application.currentLocationController.currentLocation;
         if (currentLocation) {
             isCurrentTimezone = currentLocation.get_timezone().get_tzid() == 
info.location.get_timezone().get_tzid();
         }
-        if (isCurrentTimezone) {
-            this._cityView.infoPage.timeGrid.hide();
-        } else {
-            this._cityView.connectClock();
-            this._cityView.infoPage.timeGrid.show();
-        }
+        this._cityView.setTimeVisible(!isCurrentTimezone);
 
         this._worldView.refilter();
         this._stack.set_visible_child(this._cityView);


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