From f095d38978e15e680ad48b41c28308784d378575 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Fri, 5 Dec 2014 12:01:59 -0800 Subject: [PATCH 1/3] Add dismissing to notifications --- spec/notification-spec.coffee | 29 +++++++++++++++++++++++++++++ src/config.coffee | 2 +- src/keymap-extensions.coffee | 2 +- src/notification.coffee | 17 +++++++++++++++-- 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/spec/notification-spec.coffee b/spec/notification-spec.coffee index c564edc89..0ff4599a7 100644 --- a/spec/notification-spec.coffee +++ b/spec/notification-spec.coffee @@ -16,3 +16,32 @@ describe "Notification", -> it "returns the icon specified", -> notification = new Notification('error', 'message!', icon: 'my-icon') expect(notification.getIcon()).toBe 'my-icon' + + describe "dismissing notifications", -> + describe "when the notfication is dismissable", -> + it "calls a callback when the notification is dismissed", -> + dismissedSpy = jasmine.createSpy() + notification = new Notification('error', 'message', dismissable: true) + notification.onDidDismiss dismissedSpy + + expect(notification.isDismissable()).toBe true + expect(notification.isDismissed()).toBe false + + notification.dismiss() + + expect(dismissedSpy).toHaveBeenCalled() + expect(notification.isDismissed()).toBe true + + describe "when the notfication is not dismissable", -> + it "does nothing when ::dismiss() is called", -> + dismissedSpy = jasmine.createSpy() + notification = new Notification('error', 'message') + notification.onDidDismiss dismissedSpy + + expect(notification.isDismissable()).toBe false + expect(notification.isDismissed()).toBe true + + notification.dismiss() + + expect(dismissedSpy).not.toHaveBeenCalled() + expect(notification.isDismissed()).toBe true diff --git a/src/config.coffee b/src/config.coffee index 33c8d2393..fb4e717d6 100644 --- a/src/config.coffee +++ b/src/config.coffee @@ -740,7 +740,7 @@ class Config notifyFailure: (errorMessage, error) -> message = "#{errorMessage}" detail = error.stack - atom.notifications.addError(message, {detail, closable: true}) + atom.notifications.addError(message, {detail, dismissable: true}) console.error message console.error detail diff --git a/src/keymap-extensions.coffee b/src/keymap-extensions.coffee index d0237c432..a6e6bcddc 100644 --- a/src/keymap-extensions.coffee +++ b/src/keymap-extensions.coffee @@ -25,7 +25,7 @@ KeymapManager::loadUserKeymap = -> KeymapManager::subscribeToFileReadFailure = -> this.onDidFailToReadFile (error) -> - atom.notifications.addError('Failed to load keymap.cson', {detail: error.stack, closable: true}) + atom.notifications.addError('Failed to load keymap.cson', {detail: error.stack, dismissable: true}) # This enables command handlers registered via jQuery to call # `.abortKeyBinding()` on the `jQuery.Event` object passed to the handler. diff --git a/src/notification.coffee b/src/notification.coffee index 531c7fb43..b772cc65b 100644 --- a/src/notification.coffee +++ b/src/notification.coffee @@ -1,9 +1,16 @@ +{Emitter} = require 'event-kit' # Experimental: This will likely change, do not use. module.exports = class Notification constructor: (@type, @message, @options={}) -> + @emitter = new Emitter @timestamp = new Date() + @dismissed = true + @dismissed = false if @isDismissable() + + onDidDismiss: (callback) -> + @emitter.on 'did-dismiss', callback getOptions: -> @options @@ -20,8 +27,14 @@ class Notification and @getType() == other.getType() \ and @getDetail() == other.getDetail() - isClosable: -> - !!@options.closable + dismiss: -> + return unless @isDismissable() + @dismissed = true + @emitter.emit 'did-dismiss' + + isDismissed: -> @dismissed + + isDismissable: -> !!@options.dismissable getIcon: -> return @options.icon if @options.icon? From 5cf97db07c26b7ed51ac45c76d93c53d3e9e2a09 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Fri, 5 Dec 2014 13:42:55 -0800 Subject: [PATCH 2/3] Add getNotifications() --- src/notification-manager.coffee | 6 ++++++ src/notification.coffee | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/notification-manager.coffee b/src/notification-manager.coffee index 499905667..3e47c11e8 100644 --- a/src/notification-manager.coffee +++ b/src/notification-manager.coffee @@ -42,3 +42,9 @@ class NotificationManager @notifications.push(notification) @emitter.emit('did-add-notification', notification) notification + + ### + Section: Getting Notifications + ### + + getNotifications: -> @notifications diff --git a/src/notification.coffee b/src/notification.coffee index b772cc65b..bc0261ad8 100644 --- a/src/notification.coffee +++ b/src/notification.coffee @@ -28,7 +28,7 @@ class Notification and @getDetail() == other.getDetail() dismiss: -> - return unless @isDismissable() + return unless @isDismissable() and not @isDismissed() @dismissed = true @emitter.emit 'did-dismiss' From 33ee1cb0def52b3ec5e27f7a3a8766737f1c6795 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Fri, 5 Dec 2014 14:29:00 -0800 Subject: [PATCH 3/3] Upgrade notifications to use new APIs --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d43902fc2..1d64594ff 100644 --- a/package.json +++ b/package.json @@ -98,7 +98,7 @@ "link": "0.28.0", "markdown-preview": "0.110.0", "metrics": "0.39.0", - "notifications": "0.7.0", + "notifications": "0.8.0", "open-on-github": "0.31.0", "package-generator": "0.33.0", "release-notes": "0.36.0",