-
Notifications
You must be signed in to change notification settings - Fork 3
/
mod.rs
108 lines (85 loc) · 2.92 KB
/
mod.rs
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
extern crate cocoa;
use crate::platform::cocoa::view::*;
use crate::window::{Size, WindowImpl};
use core::ffi::c_void;
use cocoa::appkit::{ NSWindow, NSView };
use cocoa::base::{ id, nil, YES };
use cocoa::foundation::{ NSString, NSSize };
mod view;
mod util;
pub struct PlatformWindow {
pub window: id,
pub view: id,
}
pub struct WindowState {
pub name: String,
pub event_cb: Box<Fn(crate::event::Event)>,
}
impl PlatformWindow {
// fn get_window_state<'a>(&self) -> &'a WindowState {
// unsafe {
// let delegate: id = msg_send![self.window, delegate];
// let ivar: *const c_void = *(&*delegate).get_ivar("windowState");
// &*(ivar as *const WindowState)
// }
// }
fn set_window_state(&self, state: WindowState) {
let mut state = Box::new(state);
let state_ptr: *mut WindowState = &mut *state;
unsafe {
let delegate: id = msg_send![self.window, delegate];
(&mut *delegate).set_ivar("windowState", state_ptr as *mut ::std::os::raw::c_void);
std::mem::forget(state);
}
}
}
impl WindowImpl for PlatformWindow {
fn attach(parent: *mut c_void) -> PlatformWindow {
unsafe {
// todo: structure this setup code properly.
// window
let window:id = util::get_window_id(parent as id);
window.setAcceptsMouseMovedEvents_(YES);
window.makeKeyAndOrderFront_(nil);
window.setOpaque_(YES);
window.center();
// content view
let content_view = window.contentView();
// main view
let view: id = msg_send![view_class(), new];
window.setDelegate_(view);
window.makeFirstResponder_(view);
NSView::initWithFrame_(view, NSView::frame(content_view as id));
content_view.addSubview_(view);
let red = msg_send![class!(NSColor), redColor];
view.setBackgroundColor_(red);
util::resize_view_to_window(view);
PlatformWindow {
window: window,
view: view,
}
}
}
fn add_events_hook(&mut self, events: Box<Fn(crate::event::Event)>) {
let state = WindowState{ event_cb: events, name: "Rob".to_string() };
self.set_window_state(state);
}
fn resize(&mut self, size: Size) {
let nssize = NSSize::new(size.width as f64, size.height as f64);
unsafe {
let mut frame = NSWindow::frame(self.window);
frame.size = nssize;
self.window.setFrame_display_(frame, YES);
util::resize_view_to_window(self.view);
}
}
fn set_title(&mut self, title: &str) {
unsafe {
let nstitle = NSString::alloc(nil).init_str(title);
self.window.setTitle_(nstitle);
}
}
}
impl Drop for PlatformWindow {
fn drop(&mut self) { }
}