Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ type Config struct {
// text input and input methods behave as before. Handled and Repaint apply
// as for OnKey.
OnModifierTap func(event gfx.ModifierTapEvent) gfx.EventResult
// OnFocusLost is called when the window loses the keyboard to another window
// or application, after the keys that were down are released. It is how an
// application saves or hides what only makes sense while it has the user's
// attention. Its Repaint asks for a new frame. Native windows only.
OnFocusLost func() gfx.EventResult
// OnScroll receives normalized pixel deltas and the hovered hit target.
// Handled suppresses the window's default canvas scroll offset.
OnScroll func(event gfx.ScrollEvent) gfx.EventResult
Expand Down
38 changes: 38 additions & 0 deletions app/focus_lost_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//go:build darwin || windows || linux

package app

import (
"testing"

"github.com/vivaui/viva/gfx"
)

func TestFocusLostIsReportedAfterKeysAreReleased(t *testing.T) {
var keys []gfx.KeyEvent
lost := 0
heldWhenLost := -1
driver, err := NewDriver(Config{
Width: 320, Height: 180,
Paint: func(canvas *gfx.Canvas, _ gfx.Size) {},
OnKey: func(event gfx.KeyEvent) gfx.EventResult { keys = append(keys, event); return gfx.EventResult{} },
OnFocusLost: func() gfx.EventResult {
lost++
heldWhenLost = len(keys)
return gfx.EventResult{Repaint: true}
},
}, nil)
if err != nil {
t.Fatal(err)
}
t.Cleanup(driver.Close)
if err := driver.LoseFocus(); err != nil {
t.Fatal(err)
}
if lost != 1 {
t.Fatalf("OnFocusLost called %d times, want 1", lost)
}
if heldWhenLost != len(keys) {
t.Fatalf("OnFocusLost ran before the held keys were released: %d of %d key events", heldWhenLost, len(keys))
}
}
20 changes: 14 additions & 6 deletions app/painter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ type painter struct {
onScroll func(gfx.ScrollEvent) gfx.EventResult
// onModifierTap and tap recognize a modifier pressed and released on its own.
onModifierTap func(gfx.ModifierTapEvent) gfx.EventResult
tap input.ModifierTap
onFileDrop func(gfx.FileDropEvent) gfx.EventResult
onResize func(gfx.Size)
// onFocusLost hears that the window lost the keyboard (Config.OnFocusLost).
onFocusLost func() gfx.EventResult
tap input.ModifierTap
onFileDrop func(gfx.FileDropEvent) gfx.EventResult
onResize func(gfx.Size)
// scale is the display's device pixels per logical pixel, zero when the platform
// does not report it (see platform.Size): the canvas then keeps the scale it was given.
scale float32
Expand Down Expand Up @@ -150,6 +152,7 @@ func newPainterWithClock(canvas *gfx.Canvas, config *Config, clock func() time.D
onShortcut: config.OnShortcut,
onScroll: config.OnScroll,
onModifierTap: config.OnModifierTap,
onFocusLost: config.OnFocusLost,
onFileDrop: config.OnFileDrop,
onResize: config.OnResize,
textInput: config.TextInput,
Expand Down Expand Up @@ -604,17 +607,22 @@ func (painter *painter) releaseKeys() bool {

// focusLostHandler is what a window hands the platform for the loss of the
// keyboard: it releases the keys that were down, since their releases will go
// to whatever window has the keyboard now.
// to whatever window has the keyboard now, and tells the application.
func (painter *painter) focusLostHandler() platform.FocusLostHandler {
if painter.onKey == nil && painter.onModifierTap == nil {
if painter.onKey == nil && painter.onModifierTap == nil && painter.onFocusLost == nil {
return nil
}
return painter.focusLost
}

func (painter *painter) focusLost() (platform.Frame, error) {
painter.tap.Cancel()
if !painter.releaseKeys() {
repaint := painter.releaseKeys()
if painter.onFocusLost != nil {
response := painter.onFocusLost()
repaint = painter.applyEventResult(response) || response.Repaint || repaint
}
if !repaint {
return platform.Frame{}, nil
}
return painter.repaint()
Expand Down