[gnome-maps/wip/notifications2: 2/3] Notifications: Add static notifications



commit d6c2e7c36dc09de067e2d0d236e251b3e8015e8e
Author: Mattias Bengtsson <mattias jc bengtsson gmail com>
Date:   Sat Feb 15 06:33:25 2014 +0100

    Notifications: Add static notifications
    
    Add support for static notifications. These are pre-defined
    notifications that are cached and reused.

 src/notification.js        |   51 ++++++++++++++++++++++++++++++++++++++++++++
 src/notificationManager.js |   25 +++++++++++++++++++++
 2 files changed, 76 insertions(+), 0 deletions(-)
---
diff --git a/src/notification.js b/src/notification.js
index ee21cd1..56fe5fe 100644
--- a/src/notification.js
+++ b/src/notification.js
@@ -79,3 +79,54 @@ const Plain = new Lang.Class({
         this._ui.body.add(label);
     }
 });
+
+const WithButton = new Lang.Class({
+    Name: 'WithButton',
+    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: WithButton,
+
+    _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: WithButton,
+
+    _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  : { name: "NO_NETWORK",  Class: NoNetwork },
+    NO_LOCATION : { name: "NO_LOCATION", Class: NoLocation }
+};
diff --git a/src/notificationManager.js b/src/notificationManager.js
index 9edef21..986c40a 100644
--- a/src/notificationManager.js
+++ b/src/notificationManager.js
@@ -31,6 +31,7 @@ const NotificationManager = new Lang.Class({
 
     _init: function(overlay) {
         this._overlay = overlay;
+        this._cache = {};
     },
 
     showMessage: function (msg) {
@@ -39,5 +40,29 @@ const NotificationManager = new Lang.Class({
                              notification.destroy.bind(notification));
         this._overlay.add_overlay(notification);
         notification.reveal();
+    },
+
+    // Shows a static (reusable) notification
+    showNotification: function(notificationType) {
+        let notification = this._getNotification(notificationType);
+        if(!notification.get_parent())
+            this._overlay.add_overlay(notification);
+        notification.reveal();
+    },
+
+    _getNotification: function(notificationType) {
+        if(!this._cache.hasOwnProperty(notificationType.name)) {
+            this._createNotification(notificationType);
+        }
+        return this._cache[notificationType.name];
+    },
+
+    _createNotification: function(notificationType) {
+        let notification = new notificationType.Class();
+        notification.connect('dismissed', (function() {
+            this._overlay.remove(notification);
+        }).bind(this));
+
+        this._cache[notificationType.name] = notification;
     }
 });


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