[gnome-maps/wip/gtk-builder] MainWindow: use more GtkBuilder



commit 88227fa4eb63f640ca22808e6460c2d71608229b
Author: Mattias Bengtsson <mattias jc bengtsson gmail com>
Date:   Mon Jun 17 22:13:35 2013 +0200

    MainWindow: use more GtkBuilder
    
    Move the appwindow into the ui-file and load it with Utils.getUIObject.

 src/application.js |    6 ++--
 src/main-window.ui |   79 +++++++++++++++++++++++++++++-----------------------
 src/mainWindow.js  |   77 +++++++++++++++++++++++---------------------------
 src/utils.js       |    8 ++++-
 4 files changed, 89 insertions(+), 81 deletions(-)
---
diff --git a/src/application.js b/src/application.js
index 10ec1dc..fa9c733 100644
--- a/src/application.js
+++ b/src/application.js
@@ -59,7 +59,7 @@ const Application = new Lang.Class({
     },
 
     _onQuitActivate: function() {
-        this._mainWindow.window.destroy();
+        this._mainWindow.ui.appWindow.destroy();
     },
 
     _initAppMenu: function() {
@@ -97,12 +97,12 @@ const Application = new Lang.Class({
             return;
 
         this._mainWindow = new MainWindow.MainWindow(this);
-        this._mainWindow.window.connect('destroy', Lang.bind(this, this._onWindowDestroy));
+        this._mainWindow.ui.appWindow.connect('destroy', Lang.bind(this, this._onWindowDestroy));
     },
 
     vfunc_activate: function() {
         this._createWindow();
-        this._mainWindow.window.present();
+        this._mainWindow.ui.appWindow.present();
     },
 
     _onWindowDestroy: function(window) {
diff --git a/src/main-window.ui b/src/main-window.ui
index 2e4b4d3..b5082ed 100644
--- a/src/main-window.ui
+++ b/src/main-window.ui
@@ -15,42 +15,51 @@
       </item>
     </section>
   </menu>
-  <object class="GtkGrid" id="window-content">
-    <property name="visible">True</property>
-    <property name="orientation">vertical</property>
+  <object class="GtkApplicationWindow" id="app-window">
+    <property name="width_request">600</property>
+    <property name="height_request">500</property>
+    <property name="window_position">center</property>
+    <property name="hide_titlebar_when_maximized">True</property>
+    <property name="title" translatable="yes">Maps</property>
     <child>
-      <object class="GdHeaderBar" id="header-bar">
+         <object class="GtkGrid" id="window-content">
         <property name="visible">True</property>
-        <property name="can_focus">False</property>
-        <child type="title">
-          <object class="GdTaggedEntry" id="search-entry">
-            <property name="visible">True</property>
-            <property name="width_request">500</property>
-          </object>
-        </child>
-        <child>
-          <object class="GdHeaderToggleButton" id="track-user-button">
-            <property name="visible">True</property>
-            <property name="can_focus">True</property>
-            <property name="symbolic-icon-name">find-location-symbolic</property>
-          </object>
-          <packing>
-            <property name="pack_type">start</property>
-          </packing>
-        </child>
-        <child>
-          <object class="GdHeaderMenuButton" id="layer-menu-button">
-            <property name="visible">True</property>
-            <property name="can_focus">True</property>
-            <property name="symbolic-icon-name">emblem-system-symbolic</property>
-            <property name="menu-model">map-type-menu</property>
-            <property name="action_name">win.map-type-menu</property>
-          </object>
-          <packing>
-            <property name="pack_type">end</property>
-          </packing>
-        </child>
-      </object>
-    </child>
+       <property name="orientation">vertical</property>
+               <child>
+                 <object class="GdHeaderBar" id="header-bar">
+                       <property name="visible">True</property>
+                       <property name="can_focus">False</property>
+                       <child type="title">
+                         <object class="GdTaggedEntry" id="search-entry">
+                               <property name="visible">True</property>
+                               <property name="width_request">500</property>
+                         </object>
+                       </child>
+                       <child>
+                         <object class="GdHeaderToggleButton" id="track-user-button">
+                               <property name="visible">True</property>
+                               <property name="can_focus">True</property>
+                               <property name="symbolic-icon-name">find-location-symbolic</property>
+                         </object>
+                         <packing>
+                               <property name="pack_type">start</property>
+                         </packing>
+                       </child>
+                       <child>
+                         <object class="GdHeaderMenuButton" id="layer-menu-button">
+                               <property name="visible">True</property>
+                               <property name="can_focus">True</property>
+                               <property name="symbolic-icon-name">emblem-system-symbolic</property>
+                               <property name="menu-model">map-type-menu</property>
+                               <property name="action_name">win.map-type-menu</property>
+                         </object>
+                         <packing>
+                               <property name="pack_type">end</property>
+                         </packing>
+                       </child>
+                 </object>
+               </child>
+         </object>
+       </child>
   </object>
 </interface>
diff --git a/src/mainWindow.js b/src/mainWindow.js
index 6cb3ee5..63c4662 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -47,15 +47,13 @@ const MainWindow = new Lang.Class({
 
     _init: function(app) {
         this._configureId = 0;
-
-        this.window = new Gtk.ApplicationWindow({ application: app,
-                                                  width_request: _WINDOW_MIN_WIDTH,
-                                                  height_request: _WINDOW_MIN_HEIGHT,
-                                                  window_position: Gtk.WindowPosition.CENTER,
-                                                  hide_titlebar_when_maximized: true,
-                                                  title: _("Maps") });
-
-        Utils.initActions(this.window, [
+        this.ui = Utils.getUIObject('main-window', ['app-window',
+                                                    'window-content',
+                                                    'search-entry',
+                                                    'track-user-button']);
+        this.ui.appWindow.application = app;
+        
+        Utils.initActions(this.ui.appWindow, [
             { 
                 properties: { name: 'about' }, 
                 signalHandlers: { activate: this._onAboutActivate }
@@ -81,8 +79,8 @@ const MainWindow = new Lang.Class({
             let width = size.get_child_value(0);
             let height = size.get_child_value(1);
 
-            this.window.set_default_size(width.get_int32(),
-                                         height.get_int32());
+            this.ui.appWindow.set_default_size(width.get_int32(),
+                                               height.get_int32());
         }
 
         let position = Application.settings.get_value('window-position');
@@ -90,52 +88,46 @@ const MainWindow = new Lang.Class({
             let x = position.get_child_value(0);
             let y = position.get_child_value(1);
 
-            this.window.move(x.get_int32(),
-                             y.get_int32());
+            this.ui.appWindow.move(x.get_int32(),
+                                   y.get_int32());
         }
 
         if (Application.settings.get_boolean('window-maximized'))
-            this.window.maximize();
+            this.ui.appWindow.maximize();
 
-        this.window.connect('delete-event',
-                            Lang.bind(this, this._quit));
-        this.window.connect('configure-event',
-                            Lang.bind(this, this._onConfigureEvent));
-        this.window.connect('window-state-event',
-                            Lang.bind(this, this._onWindowStateEvent));
+        this.ui.appWindow.connect('delete-event',
+                                  Lang.bind(this, this._quit));
+        this.ui.appWindow.connect('configure-event',
+                                  Lang.bind(this, this._onConfigureEvent));
+        this.ui.appWindow.connect('window-state-event',
+                                  Lang.bind(this, this._onWindowStateEvent));
 
-        let builder = new Gtk.Builder();
-        builder.add_from_resource('/org/gnome/maps/main-window.ui');
 
-        let grid = builder.get_object('window-content');
-        grid.set_orientation (Gtk.Orientation.VERTICAL);
-        this.window.add(grid);
-
-        this._searchEntry = builder.get_object('search-entry');
-        this._searchEntry.connect('activate', Lang.bind(this, this._onSearchActivate));
+        this.ui.windowContent.set_orientation (Gtk.Orientation.VERTICAL);
+        
+        this.ui.searchEntry.connect('activate',
+                                    Lang.bind(this, this._onSearchActivate));
 
         this.mapView = new MapView.MapView();
 
         let trackUserLocation = Application.settings.get_boolean('track-user-location');
 
-        let toggle = builder.get_object('track-user-button');
-
         let onViewMoved = Lang.bind(this,
             function () {
                 if (!this.mapView.userLocationVisible())
-                    toggle.active = false;
+                    this.ui.trackUserButton.active = false;
             });
 
         // Disable animation for goto animation on startup only
         let animateGotoUserLocation = !trackUserLocation;
-        toggle.connect('toggled', Lang.bind(this,
+        this.ui.trackUserButton.connect('toggled', Lang.bind(this,
             function() {
                 if (this._onViewMovedId > 0) {
                     this.mapView.disconnect(this._onViewMovedId);
                     this._onViewMovedId = 0;
                 }
 
-                if (toggle.active) {
+                if (this.ui.trackUserButton.active) {
                     let goneToUserLocationId = this.mapView.connect('gone-to-user-location', Lang.bind(this,
                         function () {
                             this.mapView.disconnect(goneToUserLocationId);
@@ -146,28 +138,29 @@ const MainWindow = new Lang.Class({
                         animateGotoUserLocation = true;
                 }
 
-                Application.settings.set_boolean('track-user-location', toggle.active);
+                Application.settings.set_boolean('track-user-location',
+                                                 this.ui.trackUserButton.active);
             }));
-        toggle.active = trackUserLocation;
+        this.ui.trackUserButton.active = trackUserLocation;
 
-        grid.add(this.mapView);
+        this.ui.windowContent.add(this.mapView);
 
-        grid.show_all();
+        this.ui.windowContent.show_all();
     },
 
     _saveWindowGeometry: function() {
-        let window = this.window.get_window();
+        let window = this.ui.appWindow.get_window();
         let state = window.get_state();
 
         if (state & Gdk.WindowState.MAXIMIZED)
             return;
 
         // GLib.Variant.new() can handle arrays just fine
-        let size = this.window.get_size();
+        let size = this.ui.appWindow.get_size();
         let variant = GLib.Variant.new ('ai', size);
         Application.settings.set_value('window-size', variant);
 
-        let position = this.window.get_position();
+        let position = this.ui.appWindow.get_position();
         variant = GLib.Variant.new ('ai', position);
         Application.settings.set_value('window-position', variant);
     },
@@ -197,7 +190,7 @@ const MainWindow = new Lang.Class({
     },
 
     _onSearchActivate: function() {
-        let string = this._searchEntry.get_text();
+        let string = this.ui.searchEntry.get_text();
 
         this.mapView.geocodeSearch(string);
     },
@@ -243,7 +236,7 @@ const MainWindow = new Lang.Class({
         aboutDialog.wrap_license = true;
 
         aboutDialog.modal = true;
-        aboutDialog.transient_for = this.window;
+        aboutDialog.transient_for = this.ui.appWindow;
 
         aboutDialog.show();
         aboutDialog.connect('response', function() {
diff --git a/src/utils.js b/src/utils.js
index 2da13ce..6aefc0b 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -138,12 +138,18 @@ function CreateActorFromImageFile(path) {
     }
 }
 
+function dashedToCamelCase(name) {
+    return name.replace(/(-.)/g,function(x){
+        return x[1].toUpperCase();
+    });
+}
+
 function getUIObject(res, ids) {
     let builder = new Gtk.Builder();
     builder.add_from_resource('/org/gnome/maps/' + res + '.ui');
     let ret = {};
     ids.forEach(function(id) {
-        ret[id] = builder.get_object(id);
+        ret[dashedToCamelCase(id)] = builder.get_object(id);
     });
     return ret;
 }


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