[gnome-shell/wip/gtk-notification: 14/22] messageTray: Make addButton take a callback



commit b0c0ab3172832a91e4e8a83216f0609d612a4d09
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Sun Oct 13 23:06:09 2013 -0400

    messageTray: Make addButton take a callback
    
    This is a much simpler API for consumers to manage.

 js/ui/components/telepathyClient.js |  128 ++++++++++++----------------------
 js/ui/messageTray.js                |   37 +++++------
 js/ui/notificationDaemon.js         |   20 +++---
 js/ui/overview.js                   |    6 +-
 js/ui/status/bluetooth.js           |   46 ++++++------
 5 files changed, 96 insertions(+), 141 deletions(-)
---
diff --git a/js/ui/components/telepathyClient.js b/js/ui/components/telepathyClient.js
index 86a62bb..1522be6 100644
--- a/js/ui/components/telepathyClient.js
+++ b/js/ui/components/telepathyClient.js
@@ -1095,23 +1095,15 @@ const RoomInviteNotification = new Lang.Class({
          * for example. */
         this.addBody(_("%s is inviting you to join %s").format(inviter.get_alias(), 
channel.get_identifier()));
 
-        this.addButton('decline', _("Decline"));
-        this.addButton('accept', _("Accept"));
-
-        this.connect('action-invoked', Lang.bind(this, function(self, action) {
-            switch (action) {
-            case 'decline':
-                dispatchOp.leave_channels_async(Tp.ChannelGroupChangeReason.NONE,
-                                                '', function(src, result) {
-                    src.leave_channels_finish(result)});
-                break;
-            case 'accept':
-                dispatchOp.handle_with_time_async('', global.get_current_time(),
-                                                  function(src, result) {
-                    src.handle_with_time_finish(result)});
-                break;
-            }
-            this.destroy();
+        this.addButton(_("Decline"), Lang.bind(this, function() {
+            dispatchOp.leave_channels_async(Tp.ChannelGroupChangeReason.NONE, '', function(src, result) {
+                src.leave_channels_finish(result);
+            });
+        }));
+        this.addButton(_("Accept"), Lang.bind(this, function() {
+            dispatchOp.handle_with_time_async('', global.get_current_time(), function(src, result) {
+                src.handle_with_time_finish(result);
+            });
         }));
     }
 });
@@ -1136,24 +1128,16 @@ const AudioVideoNotification = new Lang.Class({
 
         this.setUrgency(MessageTray.Urgency.CRITICAL);
 
-        this.addButton('reject', _("Decline"));
+        this.addButton(_("Decline"), Lang.bind(this, function() {
+            dispatchOp.leave_channels_async(Tp.ChannelGroupChangeReason.NONE, '', function(src, result) {
+                src.leave_channels_finish(result);
+            });
+        }));
         /* translators: this is a button label (verb), not a noun */
-        this.addButton('answer', _("Answer"));
-
-        this.connect('action-invoked', Lang.bind(this, function(self, action) {
-            switch (action) {
-            case 'reject':
-                dispatchOp.leave_channels_async(Tp.ChannelGroupChangeReason.NONE,
-                                                '', function(src, result) {
-                    src.leave_channels_finish(result)});
-                break;
-            case 'answer':
-                dispatchOp.handle_with_time_async('', global.get_current_time(),
-                                                  function(src, result) {
-                    src.handle_with_time_finish(result)});
-                break;
-            }
-            this.destroy();
+        this.addButton(_("Answer"), Lang.bind(this, function() {
+            dispatchOp.handle_with_time_async('', global.get_current_time(), function(src, result) {
+                src.handle_with_time_finish(result);
+            });
         }));
     }
 });
@@ -1176,23 +1160,15 @@ const FileTransferNotification = new Lang.Class({
                     { customContent: true });
         this.setResident(true);
 
-        this.addButton('decline', _("Decline"));
-        this.addButton('accept', _("Accept"));
-
-        this.connect('action-invoked', Lang.bind(this, function(self, action) {
-            switch (action) {
-            case 'decline':
-                dispatchOp.leave_channels_async(Tp.ChannelGroupChangeReason.NONE,
-                                                '', function(src, result) {
-                    src.leave_channels_finish(result)});
-                break;
-            case 'accept':
-                dispatchOp.handle_with_time_async('', global.get_current_time(),
-                                                  function(src, result) {
-                    src.handle_with_time_finish(result)});
-                break;
-            }
-            this.destroy();
+        this.addButton(_("Decline"), Lang.bind(this, function() {
+            dispatchOp.leave_channels_async(Tp.ChannelGroupChangeReason.NONE, '', function(src, result) {
+                src.leave_channels_finish(result);
+            });
+        }));
+        this.addButton(_("Accept"), Lang.bind(this, function() {
+            dispatchOp.handle_with_time_async('', global.get_current_time(), function(src, result) {
+                src.handle_with_time_finish(result);
+            });
         }));
     }
 });
@@ -1239,27 +1215,20 @@ const SubscriptionRequestNotification = new Lang.Class({
 
         this.addActor(layout);
 
-        this.addButton('decline', _("Decline"));
-        this.addButton('accept', _("Accept"));
-
-        this.connect('action-invoked', Lang.bind(this, function(self, action) {
-            switch (action) {
-            case 'decline':
-                contact.remove_async(function(src, result) {
-                    src.remove_finish(result)});
-                break;
-            case 'accept':
-                // Authorize the contact and request to see his status as well
-                contact.authorize_publication_async(function(src, result) {
-                    src.authorize_publication_finish(result)});
-
-                contact.request_subscription_async('', function(src, result) {
-                    src.request_subscription_finish(result)});
-                break;
-            }
+        this.addButton(_("Decline"), Lang.bind(this, function() {
+            contact.remove_async(function(src, result) {
+                src.remove_finish(result);
+            });
+        }));
+        this.addButton(_("Accept"), Lang.bind(this, function() {
+            // Authorize the contact and request to see his status as well
+            contact.authorize_publication_async(function(src, result) {
+                src.authorize_publication_finish(result);
+            });
 
-            // rely on _subscriptionStatesChangedCb to destroy the
-            // notification
+            contact.request_subscription_async('', function(src, result) {
+                src.request_subscription_finish(result);
+            });
         }));
 
         this._changedId = contact.connect('subscription-states-changed',
@@ -1358,18 +1327,11 @@ const AccountNotification = new Lang.Class({
 
         this._account = account;
 
-        this.addButton('view', _("View account"));
-
-        this.connect('action-invoked', Lang.bind(this, function(self, action) {
-            switch (action) {
-            case 'view':
-                let cmd = 'empathy-accounts --select-account=' +
-                          account.get_path_suffix();
-                let app_info = Gio.app_info_create_from_commandline(cmd, null, 0);
-                app_info.launch([], global.create_app_launch_context());
-                break;
-            }
-            this.destroy();
+        this.addButton(_("View account"), Lang.bind(this, function() {
+            let cmd = 'empathy-accounts --select-account=' +
+                account.get_path_suffix();
+            let app_info = Gio.app_info_create_from_commandline(cmd, null, 0);
+            app_info.launch([], global.create_app_launch_context());
         }));
 
         this._enabledId = account.connect('notify::enabled',
diff --git a/js/ui/messageTray.js b/js/ui/messageTray.js
index 865e169..d2fe70c 100644
--- a/js/ui/messageTray.js
+++ b/js/ui/messageTray.js
@@ -835,7 +835,7 @@ const Notification = new Lang.Class({
         }
     },
 
-    addButtonFull: function(id, button) {
+    addButtonFull: function(button, callback) {
         if (!this._buttonBox) {
             let box = new St.BoxLayout({ style_class: 'notification-actions' });
             this.setActionArea(box, { x_expand: false,
@@ -848,27 +848,36 @@ const Notification = new Lang.Class({
         }
 
         this._buttonBox.add(button);
-        button.connect('clicked', Lang.bind(this, this._onActionInvoked, id));
+        button.connect('clicked', Lang.bind(this, function() {
+            callback();
+
+            if (!this.resident) {
+                // We don't hide a resident notification when the user invokes one of its actions,
+                // because it is common for such notifications to update themselves with new
+                // information based on the action. We'd like to display the updated information
+                // in place, rather than pop-up a new notification.
+                this.emit('done-displaying');
+                this.destroy();
+            }
+        }));
 
         this.updated();
         return button;
     },
 
     // addButton:
-    // @id: the action ID
     // @label: the label for the action's button
+    // @callback: the callback for the action
     //
     // Adds a button with the given @label to the notification. All
     // action buttons will appear in a single row at the bottom of
     // the notification.
-    //
-    // If the button is clicked, the notification will emit the
-    // %action-invoked signal with @id as a parameter
-    addButton: function(id, label) {
+    addButton: function(label, callback) {
         let button = new St.Button({ style_class: 'notification-button',
                                      label: label,
                                      can_focus: true });
-        return this.addButtonFull(id, button);
+
+        return this.addButtonFull(button, callback);
     },
 
     setUrgency: function(urgency) {
@@ -1111,18 +1120,6 @@ const Notification = new Lang.Class({
         this.actor.add_style_class_name('notification-unexpanded');
     },
 
-    _onActionInvoked: function(actor, mouseButtonClicked, id) {
-        this.emit('action-invoked', id);
-        if (!this.resident) {
-            // We don't hide a resident notification when the user invokes one of its actions,
-            // because it is common for such notifications to update themselves with new
-            // information based on the action. We'd like to display the updated information
-            // in place, rather than pop-up a new notification.
-            this.emit('done-displaying');
-            this.destroy();
-        }
-    },
-
     _onClicked: function() {
         this.emit('clicked');
         // We hide all types of notifications once the user clicks on them because the common
diff --git a/js/ui/notificationDaemon.js b/js/ui/notificationDaemon.js
index 0b0c257..8960b49 100644
--- a/js/ui/notificationDaemon.js
+++ b/js/ui/notificationDaemon.js
@@ -365,10 +365,6 @@ const NotificationDaemon = new Lang.Class({
                     }
                     this._emitNotificationClosed(ndata.id, notificationClosedReason);
                 }));
-            notification.connect('action-invoked', Lang.bind(this,
-                function(n, actionId) {
-                    this._emitActionInvoked(ndata.id, actionId);
-                }));
         }
 
         // Mark music notifications so they can be shown in the screen shield
@@ -406,14 +402,16 @@ const NotificationDaemon = new Lang.Class({
             let useActionIcons = (hints['action-icons'] == true);
 
             for (let i = 0; i < actions.length - 1; i += 2) {
-                let [actionId, title] = [actions[i], [actions[i+1]];
+                let [actionId, title] = [actions[i], actions[i+1]];
+
                 if (actionId == 'default') {
-                    notification.connect('clicked', Lang.bind(this,
-                        function() {
-                            this._emitActionInvoked(ndata.id, "default");
-                        }));
+                    notification.connect('clicked', Lang.bind(this, function() {
+                        this._emitActionInvoked(ndata.id, "default");
+                    }));
                 } else {
-                    notification.addButtonFull(actionId, this._makeButton(id, label, useActionIcons));
+                    notification.addButtonFull(this._makeButton(id, label, useActionIcons), Lang.bind(this, 
function() {
+                        this._emitActionInvoked(ndata.id, actionId);
+                    }));
                 }
             }
         }
@@ -635,7 +633,7 @@ const Source = new Lang.Class({
         this.parent(title);
     },
 
-    open: function(notification) {
+    open: function() {
         this.openApp();
         this.destroyNonResidentNotifications();
     },
diff --git a/js/ui/overview.js b/js/ui/overview.js
index 69563a6..1cae42b 100644
--- a/js/ui/overview.js
+++ b/js/ui/overview.js
@@ -78,10 +78,8 @@ const ShellInfo = new Lang.Class({
         }
 
         this._undoCallback = undoCallback;
-        if (undoCallback) {
-            notification.addButton('system-undo', _("Undo"));
-            notification.connect('action-invoked', Lang.bind(this, this._onUndoClicked));
-        }
+        if (undoCallback)
+            notification.addButton(_("Undo"), Lang.bind(this, this._onUndoClicked));
 
         this._source.notify(notification);
     }
diff --git a/js/ui/status/bluetooth.js b/js/ui/status/bluetooth.js
index 718a866..2928dba 100644
--- a/js/ui/status/bluetooth.js
+++ b/js/ui/status/bluetooth.js
@@ -207,44 +207,44 @@ const PinNotification = new Lang.Class({
             let key = event.get_key_symbol();
             if (key == Clutter.KEY_Return) {
                 if (this._canActivateOkButton())
-                    this.emit('action-invoked', 'ok');
+                    this._ok();
                 return true;
             } else if (key == Clutter.KEY_Escape) {
-                this.emit('action-invoked', 'cancel');
+                this._cancel();
                 return true;
             }
             return false;
         }));
         this.addActor(this._entry);
 
-        let okButton = this.addButton('ok', _("OK"));
-        this.addButton('cancel', _("Cancel"));
+        let okButton = this.addButton(_("OK"), Lang.bind(this, this._ok));
+        this.addButton(_("Cancel"), Lang.bind(this, this._cancel));
 
         okButton.reactive = this._canActivateOkButton();
         this._entry.clutter_text.connect('text-changed', Lang.bind(this, function() {
             okButton.reactive = this._canActivateOkButton();
         }));
+    },
 
-        this.connect('action-invoked', Lang.bind(this, function(self, action) {
-            if (action == 'ok') {
-                if (this._numeric) {
-                    let num = parseInt(this._entry.text);
-                    if (isNaN(num)) {
-                        // user reply was empty, or was invalid
-                        // cancel the operation
-                        num = -1;
-                    }
-                    this._applet.agent_reply_passkey(this._devicePath, num);
-                } else
-                    this._applet.agent_reply_pincode(this._devicePath, this._entry.text);
-            } else {
-                if (this._numeric)
-                    this._applet.agent_reply_passkey(this._devicePath, -1);
-                else
-                    this._applet.agent_reply_pincode(this._devicePath, null);
+    _ok: function() {
+        if (this._numeric) {
+            let num = parseInt(this._entry.text);
+            if (isNaN(num)) {
+                // user reply was empty, or was invalid
+                // cancel the operation
+                num = -1;
             }
-            this.destroy();
-        }));
+            this._applet.agent_reply_passkey(this._devicePath, num);
+        } else {
+            this._applet.agent_reply_pincode(this._devicePath, this._entry.text);
+        }
+    },
+
+    _cancel: function() {
+        if (this._numeric)
+            this._applet.agent_reply_passkey(this._devicePath, -1);
+        else
+            this._applet.agent_reply_pincode(this._devicePath, null);
     },
 
     _canActivateOkButton: function() {


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