Skip to content

Commit

Permalink
move focus gained/lost notification from canvas to FocusManager
Browse files Browse the repository at this point in the history
  • Loading branch information
toaster committed Sep 12, 2020
1 parent 8d380dc commit a3be02b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 40 deletions.
15 changes: 15 additions & 0 deletions internal/app/focus.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,22 @@ func NewFocusManager(c fyne.CanvasObject) *FocusManager {
func (f *FocusManager) Focus(obj fyne.Focusable) {
f.Lock()
defer f.Unlock()

if f.focused == obj {
return
}

if dis, ok := obj.(fyne.Disableable); ok && dis.Disabled() {
obj = nil
}

if f.focused != nil {
f.focused.FocusLost()
}
f.focused = obj
if obj != nil {
obj.FocusGained()
}
}

// Focused returns the currently focused object or nil if none.
Expand Down
51 changes: 11 additions & 40 deletions internal/driver/glfw/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,58 +162,23 @@ func (c *glCanvas) Refresh(obj fyne.CanvasObject) {
}

func (c *glCanvas) Focus(obj fyne.Focusable) {
c.RLock()
mgr := c.focusMgr
c.RUnlock()
focused := mgr.Focused()

if focused == obj || obj == nil {
return
}

if dis, ok := obj.(fyne.Disableable); ok && dis.Disabled() {
c.Unfocus()
return
}

if focused != nil {
focused.FocusLost()
}

mgr.Focus(obj)
obj.FocusGained()
c.focusManager().Focus(obj)
}

func (c *glCanvas) FocusNext() {
c.RLock()
mgr := c.focusMgr
c.RUnlock()
mgr.FocusNext()
c.focusManager().FocusNext()
}

func (c *glCanvas) FocusPrevious() {
c.RLock()
mgr := c.focusMgr
c.RUnlock()
mgr.FocusPrevious()
c.focusManager().FocusPrevious()
}

func (c *glCanvas) Unfocus() {
c.RLock()
mgr := c.focusMgr
c.RUnlock()
focused := mgr.Focused()

if focused != nil {
mgr.Focus(nil)
focused.FocusLost()
}
c.focusManager().Focus(nil)
}

func (c *glCanvas) Focused() fyne.Focusable {
c.RLock()
defer c.RUnlock()
return c.focusMgr.Focused()
return c.focusManager().Focused()
}

func (c *glCanvas) Resize(size fyne.Size) {
Expand Down Expand Up @@ -418,6 +383,12 @@ func (c *glCanvas) ensureMinSize() bool {
return windowNeedsMinSizeUpdate
}

func (c *glCanvas) focusManager() *app.FocusManager {
c.RLock()
defer c.RUnlock()
return c.focusMgr
}

func (c *glCanvas) isDirty() bool {
c.dirtyMutex.Lock()
defer c.dirtyMutex.Unlock()
Expand Down

0 comments on commit a3be02b

Please sign in to comment.