Skip to content

Commit

Permalink
macos: move is_pointer_locked to the window
Browse files Browse the repository at this point in the history
  • Loading branch information
eyelash committed Sep 18, 2024
1 parent ec8b437 commit d608e73
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions gral_macos.m
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ @interface GralWindow: NSWindow<NSWindowDelegate> {
@public
struct gral_window_interface interface;
void *user_data;
BOOL is_pointer_locked;
}
@end
@implementation GralWindow
Expand All @@ -413,10 +414,7 @@ - (void)windowDidResignKey:(NSNotification *)notification {
}
@end

@interface GralView: NSView<NSTextInputClient> {
@public
BOOL is_pointer_locked;
}
@interface GralView: NSView<NSTextInputClient>
@end
@implementation GralView
- (BOOL)acceptsFirstResponder {
Expand Down Expand Up @@ -448,7 +446,7 @@ - (void)mouseExited:(NSEvent *)event {
}
- (void)mouseMoved:(NSEvent *)event {
GralWindow *window = (GralWindow *)[self window];
if (is_pointer_locked) {
if (window->is_pointer_locked) {
window->interface.mouse_move_relative([event deltaX], [event deltaY], window->user_data);
}
else {
Expand Down Expand Up @@ -576,10 +574,10 @@ - (void)doCommandBySelector:(SEL)selector {
];
window->interface = *interface;
window->user_data = user_data;
window->is_pointer_locked = NO;
[window setDelegate:window];
[window setTitle:[NSString stringWithUTF8String:title]];
GralView *view = [[GralView alloc] init];
view->is_pointer_locked = NO;
NSTrackingArea *trackingArea = [[NSTrackingArea alloc]
initWithRect:CGRectMake(0, 0, width, height)
options:NSTrackingMouseEnteredAndExited|NSTrackingMouseMoved|NSTrackingActiveAlways|NSTrackingInVisibleRect
Expand Down Expand Up @@ -653,16 +651,16 @@ void gral_window_warp_cursor(struct gral_window *window_, float x, float y) {
CGWarpMouseCursorPosition(point);
}

void gral_window_lock_pointer(struct gral_window *window) {
GralView *view = (GralView *)[(GralWindow *)window contentView];
void gral_window_lock_pointer(struct gral_window *window_) {
GralWindow *window = (GralWindow *)window_;
CGAssociateMouseAndMouseCursorPosition(NO);
view->is_pointer_locked = YES;
window->is_pointer_locked = YES;
}

void gral_window_unlock_pointer(struct gral_window *window) {
GralView *view = (GralView *)[(GralWindow *)window contentView];
void gral_window_unlock_pointer(struct gral_window *window_) {
GralWindow *window = (GralWindow *)window_;
CGAssociateMouseAndMouseCursorPosition(YES);
view->is_pointer_locked = NO;
window->is_pointer_locked = NO;
}

void gral_window_show_open_file_dialog(struct gral_window *window, void (*callback)(char const *file, void *user_data), void *user_data) {
Expand Down

0 comments on commit d608e73

Please sign in to comment.