[gnome-maps/wip/fix-geoloc: 9/11] MainWindow: more robust track toggle



commit 2295a097c3ee3bfb7687164734d31d7c77d3175c
Author: Mattias Bengtsson <mattias jc bengtsson gmail com>
Date:   Thu Jul 11 04:07:28 2013 +0200

    MainWindow: more robust track toggle
    
    Make the track user location toggle just represent a GSettingsAction.
    This makes the code a bit cleaner and also the app a bit more robus in
    case of a user fiddling with gsettings while Maps is running.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=704537

 src/main-window.ui |    1 +
 src/mainWindow.js  |   62 ++++++++++++++++++++++++++--------------------------
 src/mapView.js     |    1 +
 3 files changed, 33 insertions(+), 31 deletions(-)
---
diff --git a/src/main-window.ui b/src/main-window.ui
index b5082ed..4911841 100644
--- a/src/main-window.ui
+++ b/src/main-window.ui
@@ -40,6 +40,7 @@
                                <property name="visible">True</property>
                                <property name="can_focus">True</property>
                                <property name="symbolic-icon-name">find-location-symbolic</property>
+                <property name="action_name">win.track-user-location</property>
                          </object>
                          <packing>
                                <property name="pack_type">start</property>
diff --git a/src/mainWindow.js b/src/mainWindow.js
index fd9a850..26cd3fb 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -70,37 +70,20 @@ const MainWindow = new Lang.Class({
 
         this.mapView = new MapView.MapView();
 
-        let trackUserLocation = Application.settings.get_boolean('track-user-location');
-
-        let onViewMoved = Lang.bind(this,
-            function () {
-                if (!this.mapView.userLocationVisible())
-                    toggle.active = false;
-            });
-
-        // Disable animation for goto animation on startup only
-        let animateGotoUserLocation = !trackUserLocation;
-        toggle.connect('toggled', Lang.bind(this,
-            function() {
-                if (this._onViewMovedId > 0) {
-                    this.mapView.disconnect(this._onViewMovedId);
-                    this._onViewMovedId = 0;
-                }
-
-                if (toggle.active) {
-                    let goneToUserLocationId = this.mapView.connect('gone-to-user-location', Lang.bind(this,
-                        function () {
-                            this.mapView.disconnect(goneToUserLocationId);
-                            this._onViewMovedId = this.mapView.connect('view-moved', onViewMoved);
-                        }));
-                    this.mapView.gotoUserLocation(animateGotoUserLocation);
-                    if (!animateGotoUserLocation)
-                        animateGotoUserLocation = true;
-                }
-
-                Application.settings.set_boolean('track-user-location', toggle.active);
-            }));
-        toggle.active = trackUserLocation;
+        if(Application.settings.get_boolean('track-user-location'))
+            this.mapView.gotoUserLocation(false);
+
+        this._connectMapMove();
+        this.mapView.connect('going-to-user-location',
+                             this._disconnectMapMove.bind(this));
+        this.mapView.connect('gone-to-user-location',
+                             this._connectMapMove.bind(this));
+        
+        Application.settings.connect('changed::track-user-location', (function() {
+            if(Application.settings.get_boolean('track-user-location')) {
+                this.mapView.gotoUserLocation(true);
+            }
+        }).bind(this));
 
         grid.add(this.mapView);
 
@@ -127,6 +110,8 @@ const MainWindow = new Lang.Class({
                 signalHandlers: { activate: this._onMapTypeActivate }
             }
         ], this);
+        this.window.add_action(
+            Application.settings.create_action('track-user-location'));
     },
 
     _saveWindowGeometry: function() {
@@ -182,6 +167,21 @@ const MainWindow = new Lang.Class({
             }));
     },
 
+    _connectMapMove: function() {
+        if(!this._viewMovedId) {
+            this._viewMovedId = this.mapView.connect('view-moved', (function() {
+                if (!this.mapView.userLocationVisible())
+                    Application.settings.set_boolean('track-user-location', false);
+            }).bind(this));
+        }
+    },
+    _disconnectMapMove: function() {
+        if(this._viewMovedId) {
+            this.mapView.disconnect(this._viewMovedId);
+            this._viewMovedId = undefined;
+        }
+    },
+    
     _onWindowStateEvent: function(widget, event) {
         let window = widget.get_window();
         let state = window.get_state();
diff --git a/src/mapView.js b/src/mapView.js
index 04f3515..b39d4b4 100644
--- a/src/mapView.js
+++ b/src/mapView.js
@@ -126,6 +126,7 @@ const MapView = new Lang.Class({
     },
 
     gotoUserLocation: function(animate) {
+        this.emit('going-to-user-location');
         this._userLocation.once("gone-to",
                                 this.emit.bind(this, 'gone-to-user-location'));
         this._userLocation.goTo(animate);


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