From 74287b8bd294541e8ae4d62ad6a401094c28c653 Mon Sep 17 00:00:00 2001 From: Evan Hu Date: Mon, 16 Nov 2026 02:03:29 +0900 Subject: [PATCH 1/3] fix(windows): commit IME composition before blur --- docs/architecture/text.md | 2 +- internal/platform/windows/input.go | 3 +- .../platform/windows/testhost_native_test.go | 98 +++++++++++++++++++ 3 files changed, 101 insertions(+), 2 deletions(-) diff --git a/docs/architecture/text.md b/docs/architecture/text.md index 7ae9b3cd8..2afca90a6 100644 --- a/docs/architecture/text.md +++ b/docs/architecture/text.md @@ -84,7 +84,7 @@ A final layout answers where its carets are, as NSH's `TextEngine` hit testing d - `InputConfig.Submit` resolves `SubmitBehavior` with `Multiline` as NSH's `InputConfig::submit()`, and backends read only it: single lines blur and submit by default, multiline inputs break the line. - Shift, Option or Alt with Return always break a multiline input's line; a Return that commits a composition never submits. - The platform answers `MaxLength` and Return natively, without the engine. -- `internal/platform/field` keeps what every Go backend reports the same way: edit counts, selections, keys, compositions, and a `Value` held while an input method composes. Android's `InputField` does the same in Java. +- `internal/platform/field` keeps what every Go backend reports the same way: edit counts, selections, keys, compositions, and a `Value` held while an input method composes. Android's `InputField` does the same in Java. Windows completes a composition before a field loses focus, so its committed `OnChange` and `OnCompositionChange{Composing:false}` precede `OnBlur`. - Selections are UTF-16 offsets. `SelectedRange` and `Ref.SetSelection` send `ItemSelection` after the batch's value. - `PlaceholderColor` and `SelectionColor` are inherited text styles. `CaretColor`, which is not, is the caret's and it takes the selection color without one; iOS tints both with it, and Android's runtime, which has one color for them, does too. An input without a `SelectionColor` takes its theme's `Selection` and, without a `CaretColor`, its `Accent`. An opaque `SelectionColor` highlights at 30%, a translucent one as it is (`InputConfig.Highlight`). `HideCaret` hides it. On macOS the focused input gives the app its theme's `Accent` through AppKit's private `_setAccentColor:`, where the system's accent is Multicolor, so input methods draw their candidates and marked text in it; a theme change reactivates the input context, the only point input methods read it at, unless they are composing. A host's app keeps its own accent. - `OnCaretMove` reports the caret's bounds in the input while it has the focus, and the core adds them in the surface for `Overlay.AnchorBounds`. Each platform reads them from its own editor: diff --git a/internal/platform/windows/input.go b/internal/platform/windows/input.go index da4add79c..6b265714e 100644 --- a/internal/platform/windows/input.go +++ b/internal/platform/windows/input.go @@ -582,7 +582,8 @@ func (w *window) setFocus(in *textInput) { } w.restore = nil if old != nil { - w.notifyComposition(cpsCancel) + // Losing the focus commits what the IME composes, as Windows' own edit controls do. ImmNotifyIME delivers the result while old still has the focus, so its change and the composition's end precede the blur. + w.notifyComposition(cpsComplete) old.focused = false old.drag = dragNone old.model.composition = nil diff --git a/internal/platform/windows/testhost_native_test.go b/internal/platform/windows/testhost_native_test.go index 0ee2ec059..bbe30ae2e 100644 --- a/internal/platform/windows/testhost_native_test.go +++ b/internal/platform/windows/testhost_native_test.go @@ -3,10 +3,15 @@ package windows import ( + "errors" + "fmt" + "strings" "testing" "time" "github.com/nsgo-dev/nsgo/core" + "github.com/nsgo-dev/nsgo/internal/driver" + "github.com/nsgo-dev/nsgo/internal/testhost" ) // TestTestHostChoosesNativeMenus chooses a menu bar item and an item of a @@ -69,3 +74,96 @@ func TestTestHostChoosesNativeMenus(t *testing.T) { t.Error("the context menu's item was not chosen") } } + +// TestTestHostCommitsCompositionBeforeBlur moves the focus from a field while an input method composes in it, by a click on another field and from the app, and checks that the field reports the committed text, then the composition's end, then its blur; a click in the composing field itself commits without a blur. A machine without an input method skips it. +func TestTestHostCommitsCompositionBeforeBlur(t *testing.T) { + if testing.Short() { + t.Skip("opens a window") + } + for by, want := range map[string]string{"click": "focus,change,end,blur", "app": "focus,change,end,blur", "same": "focus,change,end"} { + t.Run(by, func(t *testing.T) { + title := "nsgo-ime-blur-" + by + var root *core.Scope + second := &core.Ref{} + app := func(cx *core.Scope) core.Node { + root = cx + value, events := core.NewSignal(cx, ""), core.NewSignal(cx, "") + add := func(event string) { + events.Update(func(old string) string { + if old == "" { + return event + } + return old + "," + event + }) + } + cx.Effect(func() { cx.SetTitle(fmt.Sprintf("%s|%s|%s", title, value.Get(), events.Get())) }) + field := core.Style{Width: driver.Pt(300), Height: driver.Pt(30)} + return core.View{Style: core.Style{Width: driver.Pt(320), Height: driver.Pt(120), Padding: driver.All(10), Gap: 10}, Children: []core.Node{ + core.Input{Value: value.Get, AutoFocus: true, Style: field, + OnChange: func(v string) { value.Set(v); add("change") }, + OnCompositionChange: func(c core.Composition) { + if c.Composing { + add("composition") + } else { + add("end") + } + }, + OnFocus: func() { add("focus") }, + OnBlur: func() { add("blur") }}, + core.Input{Ref: second, Style: field}, + }} + } + hwnd := startApp(t, title, app, 320, 120) + waitForTitle(t, hwnd, title+"||focus", 5*time.Second) + var ts *testSurface + onMain(t, hwnd, func(w *window) { ts = &testSurface{w: w} }) + if err := ts.Compose("ni"); err != nil { + if errors.Is(err, testhost.ErrNoInputMethod) { + t.Skip(err) + } + t.Fatal(err) + } + waitForTitle(t, hwnd, title+"||focus,composition", 5*time.Second) + switch by { + case "click": + if err := ts.Press(driver.Point{X: 20, Y: 65}, 0); err != nil { + t.Fatal(err) + } + case "same": + if err := ts.Press(driver.Point{X: 20, Y: 25}, 0); err != nil { + t.Fatal(err) + } + case "app": + root.Post(second.Focus) + } + order := func() (string, bool) { + parts := strings.SplitN(windowText(hwnd), "|", 3) + if len(parts) != 3 { + return "", false + } + var events []string + for _, ev := range strings.Split(parts[2], ",") { + if ev != "composition" { + events = append(events, ev) + } + } + return strings.Join(events, ","), parts[1] != "" + } + deadline := time.Now().Add(5 * time.Second) + for { + if got, committed := order(); committed && got == want { + break + } + if time.Now().After(deadline) { + t.Fatalf("window title = %q, want the committed text and %s", windowText(hwnd), want) + } + time.Sleep(20 * time.Millisecond) + } + // Nothing follows, such as a late blur. + time.Sleep(200 * time.Millisecond) + if got, _ := order(); got != want { + t.Fatalf("events %s after %s", got, want) + } + }) + } +} From a901b6d8cd1f6c50c9ec2c2ea32a5b082d53cc24 Mon Sep 17 00:00:00 2001 From: Evan Hu Date: Mon, 16 Nov 2026 13:57:47 +0900 Subject: [PATCH 2/3] fix(windows): commit the composition of a field that stops taking text A field turning disabled or read-only while the IME composed in it dropped the composition: the new config made insert refuse the IME result. It now completes the composition first, while the old config still takes the text, so the committed OnChange precedes the end and, for a disabled field, the blur. --- internal/platform/windows/input.go | 4 ++++ internal/platform/windows/testhost_native_test.go | 12 +++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/internal/platform/windows/input.go b/internal/platform/windows/input.go index 6b265714e..8f3669f91 100644 --- a/internal/platform/windows/input.go +++ b/internal/platform/windows/input.go @@ -1115,6 +1115,10 @@ func (in *textInput) configure(c *driver.InputConfig) { in.scroll = 0 } releaseAll(&in.hint) + // A field that stops taking text, disabled or read-only, first commits what the IME composes in it, while its old config still lets the result in, as the blur below would for an editable field; the browser and AppKit keep that text too. + if in.focused && in.editable() && (c.Disabled || c.Readonly) { + in.w.notifyComposition(cpsComplete) + } in.config = *c in.fs.Config = *c in.setSize(float32(c.Content.Size.Width), float32(c.Content.Size.Height)) diff --git a/internal/platform/windows/testhost_native_test.go b/internal/platform/windows/testhost_native_test.go index bbe30ae2e..b4fa6bdaf 100644 --- a/internal/platform/windows/testhost_native_test.go +++ b/internal/platform/windows/testhost_native_test.go @@ -75,19 +75,21 @@ func TestTestHostChoosesNativeMenus(t *testing.T) { } } -// TestTestHostCommitsCompositionBeforeBlur moves the focus from a field while an input method composes in it, by a click on another field and from the app, and checks that the field reports the committed text, then the composition's end, then its blur; a click in the composing field itself commits without a blur. A machine without an input method skips it. +// TestTestHostCommitsCompositionBeforeBlur moves the focus from a field while an input method composes in it, by a click on another field, from the app and by disabling the field, and checks that the field reports the committed text, then the composition's end, then its blur; a click in the composing field itself and making it read-only commit without a blur. A machine without an input method skips it. func TestTestHostCommitsCompositionBeforeBlur(t *testing.T) { if testing.Short() { t.Skip("opens a window") } - for by, want := range map[string]string{"click": "focus,change,end,blur", "app": "focus,change,end,blur", "same": "focus,change,end"} { + for by, want := range map[string]string{"click": "focus,change,end,blur", "app": "focus,change,end,blur", "disable": "focus,change,end,blur", "same": "focus,change,end", "readonly": "focus,change,end"} { t.Run(by, func(t *testing.T) { title := "nsgo-ime-blur-" + by var root *core.Scope second := &core.Ref{} + var disabled, readonly *core.Signal[bool] app := func(cx *core.Scope) core.Node { root = cx value, events := core.NewSignal(cx, ""), core.NewSignal(cx, "") + disabled, readonly = core.NewSignal(cx, false), core.NewSignal(cx, false) add := func(event string) { events.Update(func(old string) string { if old == "" { @@ -99,7 +101,7 @@ func TestTestHostCommitsCompositionBeforeBlur(t *testing.T) { cx.Effect(func() { cx.SetTitle(fmt.Sprintf("%s|%s|%s", title, value.Get(), events.Get())) }) field := core.Style{Width: driver.Pt(300), Height: driver.Pt(30)} return core.View{Style: core.Style{Width: driver.Pt(320), Height: driver.Pt(120), Padding: driver.All(10), Gap: 10}, Children: []core.Node{ - core.Input{Value: value.Get, AutoFocus: true, Style: field, + core.Input{Value: value.Get, AutoFocus: true, Disabled: disabled.Get, ReadonlyFunc: readonly.Get, Style: field, OnChange: func(v string) { value.Set(v); add("change") }, OnCompositionChange: func(c core.Composition) { if c.Composing { @@ -135,6 +137,10 @@ func TestTestHostCommitsCompositionBeforeBlur(t *testing.T) { } case "app": root.Post(second.Focus) + case "disable": + root.Post(func() { disabled.Set(true) }) + case "readonly": + root.Post(func() { readonly.Set(true) }) } order := func() (string, bool) { parts := strings.SplitN(windowText(hwnd), "|", 3) From 82fdcb9a07b7787adf1d898f9acb7e26fba466be Mon Sep 17 00:00:00 2001 From: Evan Hu Date: Mon, 16 Nov 2026 14:26:33 +0900 Subject: [PATCH 3/3] docs(text): state what each platform does with a composition on blur The architecture page now says, per backend, whether a composition is kept and in which order its end and the blur arrive, as checked with nsgotest on macOS, Linux, the web, Android and Windows; iOS is read from the code. The roadmap records the two gaps found: Android reports the blur before the composition ends, and Linux keeps only what the input method commits. --- docs/architecture/text.md | 8 +++++++- docs/roadmap.md | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/architecture/text.md b/docs/architecture/text.md index 2afca90a6..cdf80ffa8 100644 --- a/docs/architecture/text.md +++ b/docs/architecture/text.md @@ -84,7 +84,13 @@ A final layout answers where its carets are, as NSH's `TextEngine` hit testing d - `InputConfig.Submit` resolves `SubmitBehavior` with `Multiline` as NSH's `InputConfig::submit()`, and backends read only it: single lines blur and submit by default, multiline inputs break the line. - Shift, Option or Alt with Return always break a multiline input's line; a Return that commits a composition never submits. - The platform answers `MaxLength` and Return natively, without the engine. -- `internal/platform/field` keeps what every Go backend reports the same way: edit counts, selections, keys, compositions, and a `Value` held while an input method composes. Android's `InputField` does the same in Java. Windows completes a composition before a field loses focus, so its committed `OnChange` and `OnCompositionChange{Composing:false}` precede `OnBlur`. +- `internal/platform/field` keeps what every Go backend reports the same way: edit counts, selections, keys, compositions, and a `Value` held while an input method composes. Android's `InputField` does the same in Java. +- An input that loses the focus while an input method composes ends the composition, `OnCompositionChange{Composing:false}`, before its `OnBlur` everywhere but Android, and keeps the composed text everywhere but Linux: + - Windows completes the IMM32 composition (`CPS_COMPLETE`) as the field loses the focus, whether a click, the app (`Ref.Focus`, `Ref.Blur`), a modal or alert, hiding or UI Automation moves it, so the committed `OnChange` comes before the end and the blur. A field that turns disabled or read-only while composing commits first in the same way, before it stops taking text. + - macOS and iOS keep the editor's marked text, already in the field's value and reported by `OnChange` as it was marked, as AppKit and UIKit unmark it while the editor resigns. On macOS an input method's own session is not yet ended and committed as NSH's `commit_marked` does (the roadmap's input methods on macOS). + - The web keeps what the browser commits as the element blurs: Chrome fires `compositionend` with the composed text in the value, and the backend reports it before the end. + - Android's `InputField` clears the composing span as it loses the focus, keeping its text, already reported by `OnChange` as it was composed; the view's focus listener reports the blur first, so `OnBlur` precedes the composition's end there. + - On Linux the preedit is never in a GTK 4 editor's text, and the backend ends the composition as the editor loses the focus: what the input method commits as GTK tells it of the focus-out reaches the field as an edit, and a preedit it drops is gone, so without such a commit the app hears only the end and the blur. - Selections are UTF-16 offsets. `SelectedRange` and `Ref.SetSelection` send `ItemSelection` after the batch's value. - `PlaceholderColor` and `SelectionColor` are inherited text styles. `CaretColor`, which is not, is the caret's and it takes the selection color without one; iOS tints both with it, and Android's runtime, which has one color for them, does too. An input without a `SelectionColor` takes its theme's `Selection` and, without a `CaretColor`, its `Accent`. An opaque `SelectionColor` highlights at 30%, a translucent one as it is (`InputConfig.Highlight`). `HideCaret` hides it. On macOS the focused input gives the app its theme's `Accent` through AppKit's private `_setAccentColor:`, where the system's accent is Multicolor, so input methods draw their candidates and marked text in it; a theme change reactivates the input context, the only point input methods read it at, unless they are composing. A host's app keeps its own accent. - `OnCaretMove` reports the caret's bounds in the input while it has the focus, and the core adds them in the surface for `Overlay.AnchorBounds`. Each platform reads them from its own editor: diff --git a/docs/roadmap.md b/docs/roadmap.md index 1cf4bb6eb..cff4ba69d 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -351,6 +351,7 @@ These items wait for community contributions, and a change to one backend record | Pasted HTML | `api-naming.md` (`PasteEvent`) | `onPaste`'s `html` holds the clipboard's HTML on Windows (`CF_HTML`), Linux (`text/html`) and Android (`ClipData.Item.getHtmlText`), as it does on macOS, iOS and the web | | Windows' own text box | `windows.md`, #1475 | Windows' inputs edit through a windowless RichEdit (`ITextServices2`) drawn with Direct2D into the view, so editing, undo, spell checking, password masking and input methods through TSF are the system's, where the backend has an editor of its own now | | Input methods on macOS | `macos.md`, #1507, #1502 | Text an input method still composes, such as a whole message under live conversion, is committed and reported as its input loses the focus, and the candidates open beside the caret of an editor's point-wide sink rather than at the screen's corner | +| Compositions on blur on Android and Linux | `text.md` | Android's `InputField` ends its composition before the view's focus listener reports the blur, so `OnCompositionChange{Composing:false}` precedes `OnBlur` as on the other platforms, with `classes.dex` rebuilt; and an IBus or Fcitx input method on Linux is checked to commit what it composes as a GTK 4 editor loses the focus, or the backend commits the preedit itself | | Pastes past `maxLength` on iOS | `pitfalls.md` (iOS, UIKit and Mac Catalyst), #1647 | A paste longer than an input's `maxLength` is clipped, inserted and undone without UIKit's crash at the next undo, which NSH found when the delegate refuses or replaces a paste on iOS 26, checked in the simulator | | Sharing on Windows, Linux and the web | — | `Share` shows Windows' share UI (`IDataTransferManagerInterop::ShowShareUIForWindow` with the files as `StorageFile`s), the desktop portal's or a Linux file manager's where one exists, and the web's `navigator.share` for text and files the app holds as bytes, rather than answering `NoShareSheet` | | Photos on the desktops | `services.md` (Taking a photo) | `TakePhoto` shows a camera on macOS (an `AVCaptureSession` sheet, or Continuity Camera's import from an iPhone), Windows (`CameraCaptureUI`) and Linux (the desktop portal's `Camera` with a PipeWire stream), rather than failing with `ErrNoCamera` |