-
Notifications
You must be signed in to change notification settings - Fork 134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
app: [macos] improve focus #93
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -217,6 +217,13 @@ static void invalidateCharacterCoordinates(CFTypeRef viewRef) { | |
} | ||
} | ||
} | ||
|
||
static void setFocus(CFTypeRef windowRef, CFTypeRef viewRef) { | ||
NSWindow *window = (__bridge NSWindow *)windowRef; | ||
NSView *view = (__bridge NSView *)viewRef; | ||
[window makeFirstResponder:view]; | ||
} | ||
|
||
*/ | ||
import "C" | ||
|
||
|
@@ -246,8 +253,9 @@ type window struct { | |
cursor pointer.Cursor | ||
pointerBtns pointer.Buttons | ||
|
||
scale float32 | ||
config Config | ||
scale float32 | ||
config Config | ||
focused bool | ||
} | ||
|
||
// viewMap is the mapping from Cocoa NSViews to Go windows. | ||
|
@@ -524,6 +532,9 @@ func gio_onMouse(view, evt C.CFTypeRef, cdir C.int, cbtn C.NSInteger, x, y, dx, | |
typ = pointer.Release | ||
w.pointerBtns &^= btn | ||
case C.MOUSE_DOWN: | ||
if !w.focused { | ||
C.setFocus(w.window, w.view) | ||
} | ||
typ = pointer.Press | ||
w.pointerBtns |= btn | ||
act, ok := w.w.ActionAt(pos) | ||
|
@@ -563,6 +574,16 @@ func gio_onFocus(view C.CFTypeRef, focus C.int) { | |
w.SetCursor(w.cursor) | ||
} | ||
|
||
//export gio_onResponder | ||
func gio_onResponder(view C.CFTypeRef, first C.int) { | ||
// That function is called when some child view becomes first responder. | ||
w := mustView(view) | ||
w.focused = first == 1 | ||
if w.w.d != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without that check it crashes. I'm not sure exactly why. I also tried to wrapper it into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the crash dump? Maybe we can figure out why. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need to check again, but that is something like: "sending events before drivers is ready". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought so, which is why I commented "perhaps the SetDriver is called too late". You can try moving that call earlier, or you may have to construct a window in a two-step process: first create the window, then make it visible/key/whatever. |
||
w.w.Event(key.FocusEvent{Focus: w.focused}) | ||
} | ||
Comment on lines
+582
to
+584
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe unrelated, add it as new PR? |
||
} | ||
|
||
//export gio_onChangeScreen | ||
func gio_onChangeScreen(view C.CFTypeRef, did uint64) { | ||
w := mustView(view) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about
gio_onFocus
,windowDidBecomeKey
,windowDidResignKey
? It seems to me they should be removed by this change. If not, why not?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think each one fits one purpose. The Responder seems to be related to the current window, and Key is global. In the end:
Seems that in some conditions, both will notify.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So in total, Key is what we want for StageInactive, and Responder is what we want to use for focus, right?