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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
extern crate libc;
extern crate iup_sys;
macro_rules! assert_kiss_running (
() => (
assert!(
::KISS_RUNNING.load(::std::sync::atomic::Ordering::Acquire),
"No KISS-UI widget methods may be called before `kiss_ui::show_gui()` is invoked or after it returns!"
)
)
);
#[macro_use]
pub mod widget;
#[macro_use]
pub mod utils;
mod attrs;
#[macro_use]
pub mod callback;
pub mod base;
pub mod button;
pub mod container;
pub mod dialog;
pub mod image;
pub mod progress;
pub mod text;
pub mod timer;
use std::borrow::Borrow;
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::ptr;
use std::rc::Rc;
use std::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT, Ordering};
use base::BaseWidget;
use dialog::Dialog;
use widget::Widget;
use utils::cstr::AsCStr;
use widget_prelude::IUPPtr;
mod widget_prelude {
pub use widget::{Widget, IUPWidget, Destroy, WidgetStr};
pub type IUPPtr = *mut ::iup_sys::Ihandle;
}
pub mod prelude {
pub use base::BaseWidget;
pub use dialog::Dialog;
pub use container::Orientation;
pub use callback::{CallbackStatus, OnClick, OnShow, OnValueChange};
pub use widget::{Widget, Destroy};
}
static KISS_RUNNING: AtomicBool = ATOMIC_BOOL_INIT;
thread_local! { static CONTEXT: KISSContext = KISSContext::default() }
#[derive(Default)]
struct KISSContext {
widget_store: RefCell<HashMap<String, BaseWidget>>,
borrowed_strs: RefCell<HashMap<IUPPtr, HashMap<&'static str, Rc<Cell<usize>>>>>,
}
impl KISSContext {
fn assert_str_not_borrowed(widget: IUPPtr, str_: &'static str) {
assert_kiss_running!();
let is_borrowed = CONTEXT.with(|context|
context.borrowed_strs.borrow()
.get(&widget)
.and_then(|widget_strs|
widget_strs.get(str_)
.map(|refcount| refcount.get() != 0)
)
.unwrap_or(false)
);
assert!(
!is_borrowed,
"Cannot update the value of a string property of a widget if it's been previously borrowed!"
);
}
fn str_refcount(widget: IUPPtr, str_: &'static str) -> Rc<Cell<usize>> {
assert_kiss_running!();
CONTEXT.with(|context|
context.borrowed_strs.borrow_mut()
.entry(widget).or_insert_with(HashMap::new)
.entry(str_).or_insert_with(|| Rc::new(Cell::new(0)))
.clone()
)
}
fn store_widget<N: Into<String>, W: Widget>(name: N, widget: W) -> Option<BaseWidget> {
CONTEXT.with(|context|
context.widget_store.borrow_mut()
.insert(name.into(), widget.to_base())
)
}
fn load_widget<N: Borrow<str>>(name: &N) -> Option<BaseWidget> {
CONTEXT.with(|context|
context.widget_store.borrow().get(name.borrow()).cloned()
)
}
unsafe fn clear() {
CONTEXT.with(|context| {
context.widget_store.borrow_mut().clear();
context.borrowed_strs.borrow_mut().clear();
})
}
}
pub fn show_gui<F>(init_fn: F) where F: FnOnce() -> Dialog + Send {
assert!(
!KISS_RUNNING.compare_and_swap(false, true, Ordering::SeqCst),
"KISS-UI may only be running (in `kiss_ui::show_gui()`) in one thread at a time!"
);
unsafe {
assert!(iup_sys::IupOpen(ptr::null(), ptr::null()) == 0);
iup_sys::IupSetGlobal(::attrs::UTF8_MODE.as_cstr(), ::attrs::values::YES.as_cstr());
}
init_fn().show();
unsafe {
iup_sys::IupMainLoop();
iup_sys::IupClose();
KISSContext::clear();
}
KISS_RUNNING.store(false, Ordering::SeqCst);
}