mirror of
https://github.com/atom/atom.git
synced 2026-01-23 13:58:08 -05:00
Add ability to suppress blur on all focusable objects in a focus context
When a view receives a 'focusout' event, we relay that to the model by calling ::blur. This is great for when users initiate the change in focus, but 'focusout' events can *also* be caused by elements being temporarily detached from the DOM. The ::suppressBlur method gives the ability to ignore blur calls during a certain operation. This is helpful, for example, when we want to detach a model and reattach it somewhere else without changing its focus state.
This commit is contained in:
@@ -3,3 +3,13 @@
|
||||
module.exports =
|
||||
class FocusContext extends Model
|
||||
@property 'focusedObject', null
|
||||
|
||||
blurSuppressionCounter: 0
|
||||
|
||||
isBlurSuppressed: ->
|
||||
@blurSuppressionCounter > 0
|
||||
|
||||
suppressBlur: (fn) ->
|
||||
@blurSuppressionCounter++
|
||||
fn()
|
||||
@blurSuppressionCounter--
|
||||
|
||||
@@ -12,8 +12,17 @@ class Focusable extends Mixin
|
||||
|
||||
focus: ->
|
||||
throw new Error("Object must be assigned a focusContext to be focus") unless @focusContext
|
||||
@focusContext.focusedObject = this
|
||||
unless @focused
|
||||
@suppressBlur =>
|
||||
@focusContext.focusedObject = this
|
||||
|
||||
blur: ->
|
||||
throw new Error("Object must be assigned a focusContext to be blurred") unless @focusContext
|
||||
@focusContext.focusedObject = null if @focused
|
||||
if @focused and not @focusContext.isBlurSuppressed()
|
||||
@focusContext.focusedObject = null
|
||||
|
||||
suppressBlur: (fn) ->
|
||||
if @focusContext?
|
||||
@focusContext.suppressBlur(fn)
|
||||
else
|
||||
fn()
|
||||
|
||||
Reference in New Issue
Block a user