Skip to content

Commit 101cf1a

Browse files
committed
app,io,widget: [android/macos] fix focus issues
This patch is intended to fix two issues related to focus and software-keyboard. The focus is now reseted if it's lost due to external event, such as clicking on non-Gio window/activity/fragment. The software-keyboard will now re-open when clicked, except if it's ReadOnly. On macOS it will request focus again if the focus is lose due to another View in the same Window. However, currently, it's done by using ShowTextInput. Fixes: https://todo.sr.ht/~eliasnaur/gio/591 Signed-off-by: inkeliz <[email protected]>
1 parent e6da07a commit 101cf1a

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

app/os_macos.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,11 @@ static void invalidateCharacterCoordinates(CFTypeRef viewRef) {
297297
}
298298
}
299299
}
300+
301+
static void setFocus(CFTypeRef viewRef) {
302+
NSView *view = (__bridge NSView *)viewRef;
303+
[view.window makeFirstResponder:view];
304+
}
300305
*/
301306
import "C"
302307

@@ -514,7 +519,11 @@ func (w *window) EditorStateChanged(old, new editorState) {
514519
}
515520
}
516521

517-
func (w *window) ShowTextInput(show bool) {}
522+
func (w *window) ShowTextInput(show bool) {
523+
if show && !w.config.Focused {
524+
C.setFocus(w.view)
525+
}
526+
}
518527

519528
func (w *window) SetInputHint(_ key.InputHint) {}
520529

app/os_macos.m

+16-4
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,17 @@ - (void)windowDidChangeScreen:(NSNotification *)notification {
4747
}
4848
- (void)windowDidBecomeKey:(NSNotification *)notification {
4949
NSWindow *window = (NSWindow *)[notification object];
50-
GioView *view = (GioView *)window.contentView;
51-
gio_onFocus(view.handle, 1);
50+
GioView *view = (GioView *)window.contentView;
51+
if ([window firstResponder] == view) {
52+
gio_onFocus(view.handle, 1);
53+
}
5254
}
5355
- (void)windowDidResignKey:(NSNotification *)notification {
5456
NSWindow *window = (NSWindow *)[notification object];
55-
GioView *view = (GioView *)window.contentView;
56-
gio_onFocus(view.handle, 0);
57+
GioView *view = (GioView *)window.contentView;
58+
if ([window firstResponder] == view) {
59+
gio_onFocus(view.handle, 0);
60+
}
5761
}
5862
@end
5963

@@ -205,6 +209,14 @@ - (void)applicationDidHide:(NSNotification *)notification {
205209
- (void)dealloc {
206210
gio_onDestroy(self.handle);
207211
}
212+
- (BOOL) becomeFirstResponder {
213+
gio_onFocus(self.handle, 1);
214+
return [super becomeFirstResponder];
215+
}
216+
- (BOOL) resignFirstResponder {
217+
gio_onFocus(self.handle, 0);
218+
return [super resignFirstResponder];
219+
}
208220
@end
209221

210222
// Delegates are weakly referenced from their peers. Nothing

io/input/router.go

+8
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,14 @@ func (q *Router) changeState(e event.Event, state inputState, evts []taggedEvent
567567
e.event = de
568568
}
569569
}
570+
for i := range evts {
571+
e := &evts[i]
572+
if fe, ok := e.event.(key.FocusEvent); ok {
573+
if !fe.Focus {
574+
state.keyState.focus = nil
575+
}
576+
}
577+
}
570578
// Initialize the first change to contain the current state
571579
// and events that are bound for the current frame.
572580
if len(q.changes) == 0 {

widget/editor.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ func (e *Editor) processPointerEvent(gtx layout.Context, ev event.Event) (Editor
289289
Y: int(math.Round(float64(evt.Position.Y))),
290290
})
291291
gtx.Execute(key.FocusCmd{Tag: e})
292+
if !e.ReadOnly {
293+
gtx.Execute(key.SoftKeyboardCmd{Show: true})
294+
}
292295
if e.scroller.State() != gesture.StateFlinging {
293296
e.scrollCaret = true
294297
}
@@ -395,7 +398,7 @@ func (e *Editor) processKey(gtx layout.Context) (EditorEvent, bool) {
395398
case key.FocusEvent:
396399
// Reset IME state.
397400
e.ime.imeState = imeState{}
398-
if ke.Focus {
401+
if ke.Focus && !e.ReadOnly {
399402
gtx.Execute(key.SoftKeyboardCmd{Show: true})
400403
}
401404
case key.Event:

0 commit comments

Comments
 (0)