Dismiss Notifications with Keyboard


#1

Is there a way to dismiss “sticky” notifications (that appear top right of the widow, red for “Error” for example) using the keyboard? I’ve had a dig but can’t find a command. Thanks!


#2

You can create a command that does this. Put this in your ~/.atom/init.coffee:

atom.commands.add 'atom-workspace', 'custom-commands:dismiss-notifications', ->
  atom.notifications.getNotifications().forEach (notification) ->
    notification.dismiss()
  atom.notifications.clear()

Then you can create a keybinding for the command. Put this in ~/.atom/keymap.cson

'atom-workspace':
  'ctrl-shift-d': 'custom-commands:dismiss-notifications'

Replace the key combo with whatever you want.


#3

I wonder if I can do it with the escape key, which is the obvious key.


#4

Works for me if I just replace ctrl-shift-d with escape

EDIT - although it does break most uses of escape like closing the find/keybinding resolver panels etc.


#5

Thanks, @olmokramer - that works great!

I’ve only tried the ctrl+shift+d option though I agree with @mark_hahn ‘escape’ would be the obvious option, but perhaps there is no way (yet) to restrict a keybinding to a context? i.e if notification is present use it to close it, otherwise move on to the next likely use. I don’t have the chops to work out the answer.


#6

You can restrict a keybinding to a context. Keybindings consist of three components:

'<context>':
  '<keybinding>': '<command>'

The context is a CSS selector - atom-workspace in the example above - so you can’t perform checks like you describe at the context definition (or the command would only trigger if a notification was focused, requiring you to use the mouse anyway). You can, however, perform such checks in the command code.

This improved version dismisses all notifications that haven’t been dismissed yet. Then it checks if it has dismissed more than 0 notifications, if not it dispatches the core:cancel command on the atom-workspace. This should cover most cases where things break.

atom.commands.add 'atom-workspace', 'custom-commands:dismiss-notifications', (e) ->
  dismissedCounter = 0
  atom.notifications.getNotifications().forEach (notification) ->
    unless notification.dismissed
      notification.dismiss()
      dismissedCounter++
  if dismissedCounter is 0
    atom.commands.dispatch e.currentTarget, 'core:cancel'

And in keymap.cson

'atom-workspace':
  'escape': 'custom-commands:dismiss-notifications'

#7

That is how my various popups work. I put a listener for any keypress from the workspace and check the value (e.which) for escape (27). If I see an escape I close my popup and then stop propagation of the event.

This has worked so far. First escape closes my popup and second closes find/replace.

Maybe I can do the equivalent for the notification popups. I might have to do it in a package.


#8

Our messages crossed. I’ll try your solution.


#9

How does Esc close Find/Replace? Perhaps the same command could dismiss the error popup, too? Do we really need to be able to dismiss just the error popup, keeping Find/Replace open? After all, Cmd-Shift-F brings Find/Replace back.

Esc FTW!


#10

Of course! Haha I feel silly now.

Just add to init.coffee:

atom.commands.add 'atom-workspace', 'core:cancel', ->
  atom.notifications.getNotifications().forEach (notification) ->
    notification.dismiss()

#11

Is there a reason not to make this a core behavior? To me, it’s a no-brainer that this is the right behavior. But perhaps I’m overlooking something.


#12

As long as it doesn’t cause issues with other stuff, I’d think that this would be a great pull request for the atom/notifications repository.


#13

I was thinking core’s NotificationManager


#14

Sure … that works too :laughing:


#15

Silly? Trust me, my ignorance is far far greater!


#16

Thanks for the replies. I do think it would be good core behavior.
That said the solution above works great for me (and I learned something which is cool), so thanks!


#17

PR submitted:


#18

If you would like to change the location to the bottom right you can put this code in your styles.less file:

atom-notifications {
  top: auto;
}