Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
25 changes: 23 additions & 2 deletions app/os_macos.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Copy link
Contributor

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?

Copy link
Contributor Author

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:

  • If you minimize the window: the ResignKey is triggered, but resignFirstResponder is not.
  • If you click in another child window: the ResignKey is not triggered, but the resignFirstResponder will be triggered.

Seems that in some conditions, both will notify.

Copy link
Contributor

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:

* If you minimize the window: the ResignKey is triggered, but resignFirstResponder is not.

* If you click in another child window: the ResignKey is not triggered, but the resignFirstResponder will be triggered.

Seems that in some conditions, both will notify.

So in total, Key is what we want for StageInactive, and Responder is what we want to use for focus, right?

// That function is called when some child view becomes first responder.
w := mustView(view)
w.focused = first == 1
if w.w.d != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't check w.w.d for nil elsewhere, why here? If it's really necessary, it smells like a different problem, perhaps that SetDriver is called too late.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 w.run() (the main thread stuff), and still crashing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the crash dump? Maybe we can figure out why.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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".

Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
Expand Down
8 changes: 8 additions & 0 deletions app/os_macos.m
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ - (NSRect)firstRectForCharacterRange:(NSRange)rng
r = [self convertRect:r toView:nil];
return [[self window] convertRectToScreen:r];
}
- (BOOL) becomeFirstResponder {
gio_onResponder((__bridge CFTypeRef)self, 1);
return [super becomeFirstResponder];
}
- (BOOL) resignFirstResponder {
gio_onResponder((__bridge CFTypeRef)self, 0);
return [super resignFirstResponder];
}
@end

// Delegates are weakly referenced from their peers. Nothing
Expand Down