Skip to content

Commit

Permalink
Mac: Window Resize Support
Browse files Browse the repository at this point in the history
  • Loading branch information
CardealRusso authored Oct 19, 2024
1 parent ee2933d commit 251c697
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/fenster/fenster_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@
extern id const NSDefaultRunLoopMode;
extern id const NSApp;

static void fenster_window_resize(id v, SEL s, id note) {
(void)s;
struct fenster *f = (struct fenster *)objc_getAssociatedObject(v, "fenster");
CGSize size = msg(CGSize, msg(id, note, "object"), "contentViewBounds").size;

size_t new_size = size.width * size.height * sizeof(uint32_t);
uint32_t *new_buf = realloc(f->buf, new_size);
if (!new_buf) return;

f->buf = new_buf;
f->width = size.width;
f->height = size.height;
}

static void fenster_draw_rect(id v, SEL s, CGRect r) {
(void)r, (void)s;
struct fenster *f = (struct fenster *)objc_getAssociatedObject(v, "fenster");
Expand All @@ -42,6 +56,8 @@ static BOOL fenster_should_close(id v, SEL s, id w) {
}

FENSTER_API int fenster_open(struct fenster *f) {
f->buf = malloc(f->width * f->height * sizeof(uint32_t));
if (!f->buf) return -1;
msg(id, cls("NSApplication"), "sharedApplication");
msg1(void, NSApp, "setActivationPolicy:", NSInteger, 0);
f->wnd = msg4(id, msg(id, cls("NSWindow"), "alloc"),
Expand Down Expand Up @@ -69,11 +85,18 @@ FENSTER_API int fenster_open(struct fenster *f) {
msg1(void, f->wnd, "makeKeyAndOrderFront:", id, nil);
msg(void, f->wnd, "center");
msg1(void, NSApp, "activateIgnoringOtherApps:", BOOL, YES);
class_addMethod(c, sel_getUid("windowDidResize:"), (IMP)fenster_window_resize, "v@:@");
msg2(void, msg(id, cls("NSNotificationCenter"), "defaultCenter"),
"addObserver:selector:name:object:", id, v,
SEL, sel_getUid("windowDidResize:"),
id, msg(id, cls("NSString"), "stringWithUTF8String:", "NSWindowDidResizeNotification"),
id, f->wnd);
return 0;
}

FENSTER_API void fenster_close(struct fenster *f) {
msg(void, f->wnd, "close");
free(f->buf);
msg(void, f->wnd, "close");
}

// clang-format off
Expand Down

0 comments on commit 251c697

Please sign in to comment.