[gnome-maps] placeButtons: Refactor to make it reusable



commit 3d8bd9c53c49ff276a79c1b7c9258c3d744a6ff4
Author: James Westman <james flyingpimonster net>
Date:   Mon Dec 28 13:03:38 2020 -0600

    placeButtons: Refactor to make it reusable
    
    This will make it easier to add it to the PlaceBar later, since it won't need
    to be recreated when the selected place changes.
    
    Also removed the Buttons enum. PlaceButtons now determines which buttons to show
    on its own, based on the place type.

 data/ui/place-buttons.ui |  2 +-
 src/mapBubble.js         | 12 +++----
 src/placeBubble.js       | 15 --------
 src/placeButtons.js      | 93 ++++++++++++++++++++++++------------------------
 4 files changed, 52 insertions(+), 70 deletions(-)
---
diff --git a/data/ui/place-buttons.ui b/data/ui/place-buttons.ui
index 9260171e..bace12aa 100644
--- a/data/ui/place-buttons.ui
+++ b/data/ui/place-buttons.ui
@@ -44,7 +44,7 @@
     <child>
       <object class="GtkButton" id="sendToButton">
         <property name="name">bubble-send-to-button</property>
-        <property name="visible">False</property>
+        <property name="visible">True</property>
         <property name="can_focus">True</property>
         <property name="receives_default">False</property>
         <property name="tooltip-text" translatable="yes" comments="Translators: This is a tooltip">Share 
location</property>
diff --git a/src/mapBubble.js b/src/mapBubble.js
index 879916bf..0ab299bb 100644
--- a/src/mapBubble.js
+++ b/src/mapBubble.js
@@ -52,9 +52,6 @@ class MapBubble extends Gtk.Popover {
         params.transitions_enabled = false;
         delete params.mapView;
 
-        let buttonFlags = params.buttons || Button.NONE;
-        delete params.buttons;
-
         params.modal = false;
 
         super._init(params);
@@ -80,9 +77,7 @@ class MapBubble extends Gtk.Popover {
         this._contactAvatar = ui.contactAvatar;
         this._addressLabel = ui.addressLabel;
 
-        ui.placeButtons.visible = !!buttonFlags;
-        let placeButtons = new PlaceButtons.PlaceButtons({ buttonFlags,
-                                                           place: this._place,
+        let placeButtons = new PlaceButtons.PlaceButtons({ place: this._place,
                                                            mapView: this._mapView })
         ui.placeButtons.add(placeButtons);
 
@@ -92,8 +87,11 @@ class MapBubble extends Gtk.Popover {
 
             /* hide the normal button area */
             ui.placeButtons.visible = false;
+
             /* show the top-end-corner share button instead */
-            placeButtons.initSendToButton(ui.sendToButtonAlt, buttonFlags & PlaceButtons.Button.CHECK_IN);
+            ui.sendToButtonAlt.visible = true;
+            placeButtons.initSendToButton(ui.sendToButtonAlt);
+
             /* adjust some margins */
             ui.titleBox.margin = 12;
             ui.titleBox.marginStart = 18;
diff --git a/src/placeBubble.js b/src/placeBubble.js
index 7538a866..c7dcb353 100644
--- a/src/placeBubble.js
+++ b/src/placeBubble.js
@@ -28,7 +28,6 @@ const Pango = imports.gi.Pango;
 const Format = imports.format;
 
 const Application = imports.application;
-const ContactPlace = imports.contactPlace;
 const MapBubble = imports.mapBubble;
 const Overpass = imports.overpass;
 const Place = imports.place;
@@ -54,20 +53,6 @@ var PlaceBubble = GObject.registerClass({
 }, class PlaceBubble extends MapBubble.MapBubble {
 
     _init(params) {
-        params.buttons = (PlaceButtons.Button.ROUTE |
-                          PlaceButtons.Button.SEND_TO);
-
-        if (params.place.store)
-            params.buttons |= PlaceButtons.Button.FAVORITE;
-
-        if (!(params.place instanceof ContactPlace.ContactPlace) && params.place.osm_id)
-            params.buttons |= PlaceButtons.Button.EDIT_ON_OSM;
-
-        if (params.place.isUserLocation) {
-            params.buttons |= PlaceButtons.Button.CHECK_IN;
-            params.buttons &= ~PlaceButtons.Button.ROUTE;
-        }
-
         super._init(params);
 
         this.loading = true;
diff --git a/src/placeButtons.js b/src/placeButtons.js
index fa0ace5f..cee2aa64 100644
--- a/src/placeButtons.js
+++ b/src/placeButtons.js
@@ -24,21 +24,13 @@ const GObject = imports.gi.GObject;
 const Gtk = imports.gi.Gtk;
 
 const Application = imports.application;
+const ContactPlace = imports.contactPlace;
 const OSMAccountDialog = imports.osmAccountDialog;
 const OSMEditDialog = imports.osmEditDialog;
 const OSMUtils = imports.osmUtils;
 const PlaceStore = imports.placeStore;
 const SendToDialog = imports.sendToDialog;
 
-var Button = {
-    NONE: 0,
-    ROUTE: 2,
-    SEND_TO: 4,
-    FAVORITE: 8,
-    CHECK_IN: 16,
-    EDIT_ON_OSM: 32,
-};
-
 var PlaceButtons = GObject.registerClass({
     Template: 'resource:///org/gnome/Maps/ui/place-buttons.ui',
     InternalChildren: [ 'routeButton',
@@ -48,10 +40,7 @@ var PlaceButtons = GObject.registerClass({
                         'favoriteButtonImage' ],
 }, class PlaceButtons extends Gtk.Box {
     _init(params) {
-        let buttonFlags = params.buttonFlags;
-        delete params.buttonFlags;
-
-        this._place = params.place;
+        let place = params.place;
         delete params.place;
 
         this._mapView = params.mapView;
@@ -59,62 +48,61 @@ var PlaceButtons = GObject.registerClass({
 
         super._init(params);
 
-        if (buttonFlags & Button.ROUTE)
-            this._initRouteButton(this._routeButton);
-        if (buttonFlags & Button.SEND_TO)
-            this.initSendToButton(this._sendToButton, buttonFlags & Button.CHECK_IN);
-        if (buttonFlags & Button.FAVORITE)
-            this._initFavoriteButton(this._favoriteButton, this._favoriteButtonImage);
-        if (buttonFlags & Button.EDIT_ON_OSM)
-            this._initEditButton(this._editButton);
+        this._initSignals();
+
+        this.place = place;
+    }
+
+    get place() {
+        return this._place;
     }
 
-    initSendToButton(button, showCheckIn) {
-        button.visible = true;
+    set place(newPlace) {
+        this._place = newPlace;
+
+        this._updateFavoriteButton(!!this._place.store);
+
+        this._editButton.visible = (!(this._place instanceof ContactPlace.ContactPlace) &&
+                                    this._place.osm_id);
+
+        this._routeButton.visible = !this._place.isCurrentLocation;
+
+        this._showCheckIn = !!this._place.isCurrentLocation;
+    }
+
+    initSendToButton(button) {
         button.connect('clicked', () => {
             let dialog = new SendToDialog.SendToDialog({ transient_for: this.get_toplevel(),
                                                          modal: true,
                                                          mapView: this._mapView,
                                                          place: this._place,
-                                                         showCheckIn });
+                                                         showCheckIn: this._showCheckIn });
             dialog.connect('response', () => dialog.destroy());
             dialog.show();
         });
     }
 
-    _initFavoriteButton(button, image) {
+    _initSignals() {
         let placeStore = Application.placeStore;
-        button.visible = true;
-
-        if (placeStore.exists(this._place,
-                              PlaceStore.PlaceType.FAVORITE)) {
-            image.icon_name = 'starred-symbolic';
-        } else {
-            image.icon_name = 'non-starred-symbolic';
-        }
+        let query = Application.routeQuery;
 
-        button.connect('clicked', () => {
+        this._favoriteButton.connect('clicked', () => {
             if (placeStore.exists(this._place,
                                   PlaceStore.PlaceType.FAVORITE)) {
-                image.icon_name = 'non-starred-symbolic';
+                this._favoriteButtonImage.icon_name = 'non-starred-symbolic';
                 placeStore.removePlace(this._place,
                                        PlaceStore.PlaceType.FAVORITE);
             } else {
-                image.icon_name = 'starred-symbolic';
+                this._favoriteButtonImage.icon_name = 'starred-symbolic';
                 placeStore.addPlace(this._place,
                                     PlaceStore.PlaceType.FAVORITE);
             }
         });
-    }
 
-    _initRouteButton(button) {
-        let query = Application.routeQuery;
-        let from = query.points[0];
-        let to = query.points[query.points.length - 1];
-
-        button.visible = true;
+        this._routeButton.connect('clicked', () => {
+            let from = query.points[0];
+            let to = query.points[query.points.length - 1];
 
-        button.connect('clicked', () => {
             query.freeze_notify();
             query.reset();
             Application.routingDelegator.reset();
@@ -125,11 +113,22 @@ var PlaceButtons = GObject.registerClass({
 
             query.thaw_notify();
         });
+
+        this._editButton.connect('clicked', this._onEditClicked.bind(this));
+
+        this.initSendToButton(this._sendToButton);
     }
 
-    _initEditButton(button) {
-        button.visible = true;
-        button.connect('clicked', this._onEditClicked.bind(this));
+    _updateFavoriteButton(visible) {
+        let placeStore = Application.placeStore;
+        this._favoriteButton.visible = visible;
+
+        if (placeStore.exists(this._place,
+                              PlaceStore.PlaceType.FAVORITE)) {
+            this._favoriteButtonImage.icon_name = 'starred-symbolic';
+        } else {
+            this._favoriteButtonImage.icon_name = 'non-starred-symbolic';
+        }
     }
 
     _onEditClicked() {


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