Add dismissing to notifications

This commit is contained in:
Ben Ogle
2014-12-05 12:01:59 -08:00
parent 218f6ab629
commit f095d38978
4 changed files with 46 additions and 4 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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.

View File

@@ -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?