[gnome-maps/wip/notifications] WIP redo it a bit



commit 281a02d141ddec82581475729d8164842280db68
Author: Mattias Bengtsson <mattias jc bengtsson gmail com>
Date:   Fri Feb 14 05:30:26 2014 +0100

    WIP redo it a bit

 src/application.js  |    5 ++
 src/mainWindow.js   |   20 +-----
 src/notification.js |  171 ++++++++++++++++++++++++++++++++++++++++++++-------
 src/notification.ui |   15 ++---
 4 files changed, 163 insertions(+), 48 deletions(-)
---
diff --git a/src/application.js b/src/application.js
index de4cdfc..9a65901 100644
--- a/src/application.js
+++ b/src/application.js
@@ -35,6 +35,8 @@ const GLib = imports.gi.GLib;
 const Main = imports.main;
 const Format = imports.format;
 const MainWindow = imports.mainWindow;
+const Notification = imports.notification;
+
 const Utils = imports.utils;
 const Path = imports.path;
 const Settings = imports.settings;
@@ -42,6 +44,7 @@ const Settings = imports.settings;
 // used globally
 let application = null;
 let settings = null;
+let notificationManager = null;
 
 const Application = new Lang.Class({
     Name: 'Application',
@@ -98,6 +101,8 @@ const Application = new Lang.Class({
 
     vfunc_activate: function() {
         this._createWindow();
+        notificationManager = new Notification.Manager(this._mainWindow.getOverlay());
+        notificationManager.showNotification(Notification.Type.NO_NETWORK);
         this._mainWindow.window.present();
     },
 
diff --git a/src/mainWindow.js b/src/mainWindow.js
index ed66ab7..66af000 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -89,16 +89,8 @@ const MainWindow = new Lang.Class({
         this._windowContent.show_all();
     },
 
-    showNotification: function(msg, buttonLabel, callback) {
-        let notification = new Notification.Notification(msg, buttonLabel),
-            id;
-
-        if(callback) {
-            notification.connect('button-clicked', callback);
-        }
-
-        this._windowContent.add_overlay(notification);
-        notification.reveal();
+    getOverlay: function() {
+        return this._windowContent;
     },
 
     _initPlaces: function() {
@@ -354,11 +346,7 @@ const MainWindow = new Lang.Class({
     },
 
     _onGotoUserLocationActivate: function() {
-        this.showNotification("Turn on location services to find your location",
-                              "Turn On",
-                              function() {
-                                  log("Turning on location service!");
-                              });
+        Application.notificationManager.showNotification(Notification.Type.NO_LOCATION);
         log(this._windowContent.get_children().length);
         if (this.mapView.geoclue.userSetLocation) {
             Utils.once(this.mapView.geoclue,
@@ -379,7 +367,7 @@ const MainWindow = new Lang.Class({
     _onMapTypeActivate: function(action, value) {
         action.set_state(value);
         let [mapType, len] = value.get_string();
-        this.showNotification("Changed base layer to " + mapType);
+        Application.notificationManager.showMessage("Changed base layer to " + mapType);
         this.mapView.setMapType(MapView.MapType[mapType]);
     },
 
diff --git a/src/notification.js b/src/notification.js
index d580280..4c88a50 100644
--- a/src/notification.js
+++ b/src/notification.js
@@ -26,42 +26,167 @@ const Mainloop = imports.mainloop;
 const Lang = imports.lang;
 const Utils = imports.utils;
 
-const SECOND = 1000;
-
 const Notification = new Lang.Class({
     Name: 'Notification',
     Extends: Gtk.Revealer,
+    Abstract: true,
 
-    _init: function(msg, buttonLabel) {
+    _init: function(params) {
         this.parent({ visible: true,
                       halign: Gtk.Align.CENTER,
                       valign: Gtk.Align.START });
-        let ui = Utils.getUIObject('notification', [ 'frame',
-                                                     'button',
-                                                     'notification',
-                                                     'dismiss-button']);
-        ui.notification.label = msg;
-        if(buttonLabel) {
-            ui.button.show();
-            ui.button.label = buttonLabel;
-            ui.button.connect('clicked', (function() {
-                this.emit('button-clicked');
-            }).bind(this));
-        }
-        ui.dismissButton.connect('clicked', this.dismiss.bind(this));
-        this.add(ui.frame);
+
+        this._ui = Utils.getUIObject('notification', [ 'frame',
+                                                       'body',
+                                                       'dismiss-button']);
+
+        this._ui.dismissButton.connect('clicked', this.dismiss.bind(this));
+        this.add(this._ui.frame);
     },
 
     reveal: function() {
-        this.set_reveal_child(true);
+        this._setRevealAndEmit(true, 'revealed');
     },
 
     dismiss: function() {
-        this.set_reveal_child(false);
-        Mainloop.timeout_add(this.transition_duration, (function() {
-            this.destroy();
-            return false;
-        }).bind(this));
+        this._setRevealAndEmit(false, 'dismissed');
+    },
+
+    _setRevealAndEmit: function(state, signal) {
+        // We only want to send a dismissed / shown -signal
+        // if there is an actual change in revealed state.
+        if(state !== this.child_revealed) {
+            this.set_reveal_child(state);
+            Mainloop.timeout_add(this.transition_duration, (function() {
+                this.emit(signal);
+                return false;
+            }).bind(this));
+        }
     }
 });
 Utils.addSignalMethods(Notification.prototype);
+
+const Message = new Lang.Class({
+    Name: 'Message',
+    Extends: Notification,
+
+    _init: function(msg) {
+        this.parent();
+        let label = new Gtk.Label({ visible : true,
+                                    hexpand : true,
+                                    halign  : Gtk.Align.START,
+                                    label   : msg });
+        this._ui.body.add(label);
+    }
+});
+
+const Action = new Lang.Class({
+    Name: 'Action',
+    Extends: Notification,
+    Abstract: true,
+
+    _init: function(msg, buttonLabel, callback) {
+        this.parent();
+        let label = new Gtk.Label({ visible : true,
+                                    hexpand : true,
+                                    halign  : Gtk.Align.START,
+                                    label   : msg });
+        let button = new Gtk.Button({ visible : true,
+                                      label   : buttonLabel });
+        button.connect('clicked', callback);
+
+        this._ui.body.add(label);
+        this._ui.body.add(button);
+    }
+});
+
+const NoNetwork = new Lang.Class({
+    Name: 'NoNetwork',
+    Extends: Action,
+
+    _init: function() {
+        this.parent("Maps need a working network connection to function properly",
+                    "Turn On",
+                    (function() {
+                        log("TODO: connect to network here...");
+                    }));
+    }
+});
+
+const NoLocation = new Lang.Class({
+    Name: 'NoLocation',
+    Extends: Action,
+
+    _init: function() {
+        this.parent("Turn on location services to find your location",
+                    "Turn On",
+                    (function() {
+                        log("TODO: turn on location services here...");
+                    }));
+    }
+});
+
+const Type = {
+    NO_NETWORK  : "NO_NETWORK",
+    NO_LOCATION : "NO_LOCATION"
+};
+
+const Manager = new Lang.Class({
+    Name: 'Manager',
+
+    _init: function(overlay) {
+        this._overlay = overlay;
+        this._notifications = {};
+        for(let type in Type) {
+            this._notifications[type] = undefined;
+        }
+    },
+
+    showMessage: function (msg) {
+        let notification = new Message(msg);
+        notification.connect('dismissed',
+                             notification.destroy.bind(notification));
+        this._overlay.add_overlay(notification);
+        notification.reveal();
+    },
+
+    showNotification: function(type) {
+        if(!Type.hasOwnProperty(type)) {
+            Utils.log("Error: tried to show notification of type "
+                      + type +
+                      " but it isn't implemented.");
+            return;
+        }
+        let notification = this._getNotification(type);
+        if(!notification.get_parent())
+            this._overlay.add_overlay(notification);
+        notification.reveal();
+    },
+
+    _getNotification: function(type) {
+        if(!this._notifications[type]) {
+            this._notifications[type] = this._createNotification(type);
+        }
+        return this._notifications[type];
+    },
+
+    _createNotification: function (type) {
+        let notification;
+        switch(type) {
+        case Type.NO_NETWORK:
+            notification = new NoNetwork();
+            break;
+        case Type.NO_LOCATION:
+            notification = new NoLocation();
+            break;
+
+        default: return undefined;
+        }
+
+        notification.connect('dismissed', (function() {
+            this._overlay.remove(notification);
+        }).bind(this));
+
+        return notification;
+    }
+});
diff --git a/src/notification.ui b/src/notification.ui
index 5e31e47..7b41d37 100644
--- a/src/notification.ui
+++ b/src/notification.ui
@@ -6,22 +6,19 @@
       <class name="app-notification"/>
     </style>
     <child>
-      <object class="GtkBox" id="box">
+      <object class="GtkGrid">
         <property name="visible">True</property>
         <property name="margin-start">10</property>
         <property name="margin-end">10</property>
-        <property name="spacing">20</property>
         <property name="width_request">600</property>
+        <property name="hexpand">True</property>
+        <property name="vexpand">True</property>
         <child>
-          <object class="GtkLabel" id="notification">
+          <object class="GtkGrid" id="body">
             <property name="visible">True</property>
             <property name="hexpand">True</property>
-            <property name="halign">start</property>
-          </object>
-        </child>
-        <child>
-          <object class="GtkButton" id="button">
-            <property name="visible">False</property>
+            <property name="vexpand">True</property>
+            <property name="margin-end">20</property>
           </object>
         </child>
         <child>


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