Skip to content
Draft
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
2 changes: 1 addition & 1 deletion docs/architecture/text.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion internal/platform/windows/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
98 changes: 98 additions & 0 deletions internal/platform/windows/testhost_native_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
})
}
}
Loading