-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.c3
76 lines (63 loc) · 2.43 KB
/
window.c3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import std::os::macos::objc, objc;
import std::io;
import std::math;
bool running = true;
fn void main()
{
NSObject object = objc::new("NSObject");
object.add_method("windowShouldClose:", &on_close)!!;
NSApplication app = NSApplication { .cls = objc::getClass("NSApplication") };
app.sharedApplication();
app.setActivationPolicy(ApplicationActivationPolicy.REGULAR);
NSWindow win = objc::new("NSWindow");
win.alloc();
ulong window_style = WindowStyleMask.CLOSABLE.val | WindowStyleMask.MINIATURIZABLE.val | BackingStore.BUFFERED.val | WindowStyleMask.TITLED.val | WindowStyleMask.RESIZABLE.val;
win.initWithContentRect(NSRect {{200.0, 200.0}, {200.0, 200.0}}, window_style, window_style, false);
NSObject delegate = objc::new("NSObject");
delegate.alloc_class_pair("WindowDelegate", 0);
delegate.add_ivar("NSWindow", $sizeof(win), math::log2($sizeof(win)), "L")!!;
delegate.add_method("windowWillResize:toSize:", &resize_window, "{NSSize=ff}@:{NSSize=ff}")!!;
delegate.alloc();
delegate.init();
delegate.setInstanceVariable("NSWindow", win);
win.setDelegate(delegate);
win.makeKeyAndOrderFront(null);
win.setIsVisible(true);
app.activateIgnoringOtherApps(true);
app.finishLaunching();
while(running) {
NSAutoreleasePool pool = objc::new("NSAutoreleasePool");
pool.alloc();
pool.init();
NSString loop = objc::new("NSString");
loop.stringWithUTF8String("kCFRunLoopDefaultMode");
NSDate until;
// NSDate until = objc::new("NSDate");
// until.distantFuture();
NSEvent event = app.nextEventMatchingMask(long.max, until, loop, true);
int type = event.get_type();
NSPoint pos = event.locationInWindow();
if (type != 0) {
io::printfn("Event [type=%s location={%d, %d} modifierFlags={%s}]",
objc::event_type_from(type)!!,
pos.x, pos.y,
objc::modifier_to_str(event.get_modifierFlags()));
}
win.sendEvent(event);
app.updateWindows();
pool.release();
}
}
fn bool on_close(ObjcId self)
{
io::printn("closing window..");
if (catch self.get_ivar("NSWindow")) return true;
running = false;
return true;
}
fn NSSize resize_window(ObjcId self, NSSize frame)
{
if (catch self.get_ivar("NSWindow")) return frame;
io::printfn("window resized to %f %f", frame.width, frame.height);
return frame;
}