Skip to content

Commit

Permalink
Re-focus a parent window when dialog closes
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Mar 7, 2019
1 parent f190523 commit fb853b7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
2 changes: 2 additions & 0 deletions dialog/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func (d *dialog) closed() {
if !d.responded && d.callback != nil {
d.callback(false)
}

d.win.RequestFocus()
}

func (d *dialog) setButtons(buttons fyne.CanvasObject) {
Expand Down
6 changes: 6 additions & 0 deletions driver/gl/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ func (w *window) sizeOnScreen() (int, int) {
return viewWidth, viewHeight
}

func (w *window) RequestFocus() {
runOnMainAsync(func() {
w.viewport.Focus()
})
}

func (w *window) Resize(size fyne.Size) {
runOnMainAsync(func() {
scale := w.canvas.Scale()
Expand Down
18 changes: 16 additions & 2 deletions test/testwindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type testWindow struct {
fullScreen bool
fixedSize bool
padded bool
focused bool
onClosed func()

canvas fyne.Canvas
Expand Down Expand Up @@ -44,6 +45,14 @@ func (w *testWindow) Resize(fyne.Size) {
// no-op
}

func (w *testWindow) RequestFocus() {
for _, win := range windows {
win.(*testWindow).focused = false
}

w.focused = true
}

func (w *testWindow) FixedSize() bool {
return w.fixedSize
}
Expand Down Expand Up @@ -72,18 +81,23 @@ func (w *testWindow) SetOnClosed(closed func()) {
w.onClosed = closed
}

func (w *testWindow) Show() {}
func (w *testWindow) Show() {
w.RequestFocus()
}

func (w *testWindow) Clipboard() fyne.Clipboard {
return w.clipboard
}

func (w *testWindow) Hide() {}
func (w *testWindow) Hide() {
w.focused = false
}

func (w *testWindow) Close() {
if w.onClosed != nil {
w.onClosed()
}
w.focused = false

windowsMutex.Lock()
i := 0
Expand Down
5 changes: 5 additions & 0 deletions window.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ type Window interface {
// platform constraints.
Resize(Size)

// RequestFocus attempts to raise and focus this window.
// This should only be called when you are sure the user would want this window
// to steal focus from any current focused window.
RequestFocus()

// FixedSize returns whether or not this window should disable resizing.
FixedSize() bool
// SetFixedSize sets a hint that states whether the window should be a fixed
Expand Down

0 comments on commit fb853b7

Please sign in to comment.