-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathview.rs
87 lines (74 loc) · 2.04 KB
/
view.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
use core_graphics::geometry::CGRect;
use objc::Message;
use objc::runtime::Class;
use objc_id::{Id, ShareId};
use objc_foundation::INSObject;
use {NoSyncSend, UIColor};
pub trait IUIView : INSObject {
fn with_frame(frame: CGRect) -> Id<Self> {
assert_main_thread!();
let cls = Self::class();
unsafe {
let obj: *mut Self = msg_send![cls, alloc];
let obj: *mut Self = msg_send![obj, initWithFrame:frame];
Id::from_retained_ptr(obj)
}
}
fn frame(&self) -> CGRect {
assert_main_thread!();
unsafe {
msg_send![self, frame]
}
}
fn set_frame(&self, frame: CGRect) {
assert_main_thread!();
unsafe {
msg_send![self, setFrame:frame]
}
}
fn add_subview<T: IUIView>(&self, view: ShareId<T>) {
assert_main_thread!();
unsafe {
msg_send![self, addSubview:&*view]
}
}
fn remove_from_superview(&self) {
assert_main_thread!();
unsafe {
msg_send![self, removeFromSuperview]
}
}
fn background_color(&self) -> ShareId<UIColor> {
assert_main_thread!();
unsafe {
let obj: *mut UIColor = msg_send![self, backgroundColor];
ShareId::from_ptr(obj)
}
}
fn set_background_color(&self, color: ShareId<UIColor>) {
assert_main_thread!();
unsafe {
msg_send![self, setBackgroundColor:&*color]
}
}
}
pub struct UIView {
_marker: NoSyncSend,
}
unsafe impl Message for UIView { }
impl INSObject for UIView {
fn class() -> &'static Class {
Class::get("UIView").unwrap()
}
// Redefine new to only allow constructing on the main thread
fn new() -> Id<Self> {
assert_main_thread!();
let cls = Self::class();
unsafe {
let obj: *mut Self = msg_send![cls, alloc];
let obj: *mut Self = msg_send![obj, init];
Id::from_retained_ptr(obj)
}
}
}
impl IUIView for UIView { }