From 6a00786a3b7d8c3d5e5efba9bbf1a692f05e131a Mon Sep 17 00:00:00 2001 From: lick <2188718831@qq.com> Date: Wed, 11 Nov 2026 00:45:05 -0800 Subject: [PATCH] app: report the window losing the keyboard (Config.OnFocusLost) Native windows already hear the loss of focus to release held keys; the application now hears it too, after the release, so it can save or hide what only makes sense while the window has the user's attention. Co-Authored-By: Claude Opus 5.5 --- app/app.go | 5 +++++ app/focus_lost_test.go | 38 ++++++++++++++++++++++++++++++++++++++ app/painter.go | 20 ++++++++++++++------ 3 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 app/focus_lost_test.go diff --git a/app/app.go b/app/app.go index b99a44099..ef3b32b3f 100644 --- a/app/app.go +++ b/app/app.go @@ -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 diff --git a/app/focus_lost_test.go b/app/focus_lost_test.go new file mode 100644 index 000000000..3d8198025 --- /dev/null +++ b/app/focus_lost_test.go @@ -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)) + } +} diff --git a/app/painter.go b/app/painter.go index 79a24c6ef..21b40f422 100644 --- a/app/painter.go +++ b/app/painter.go @@ -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 @@ -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, @@ -604,9 +607,9 @@ 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 @@ -614,7 +617,12 @@ func (painter *painter) focusLostHandler() platform.FocusLostHandler { 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()