-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwindow.rs
87 lines (67 loc) · 1.79 KB
/
window.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 {kernel32, user32};
use winapi::*;
//use builder::{Builder, Buildable};
use ffi::WindowHandle;
use ffi::class::{Class as WindowClass, CustomClass};
use ffi::traits::{BorrowHandle, WindowEvents, WindowData};
use winstr::WinString;
use std::borrow::Cow;
use std::boxed::FnBox;
use std::marker::PhantomData;
use std::{mem, ptr};
#[derive(Clone)]
pub struct Window {
hnd: WindowHandle<Class>,
}
impl Window {
pub fn new<T: AsRef<str>>(title: T) -> Window {
let data = Data::new(title);
let hnd = WindowHandle::create_instance(Class, data).unwrap();
Window {
hnd: hnd
}
}
pub fn add_child<W: WindowEvents, C: BorrowHandle<W>>(&mut self, child: C) -> &mut Self {
self
}
}
#[derive(Default)]
struct Data {
title: WinString,
on_create: Option<Box<FnMut(&mut Window)>>,
on_show: Option<Box<FnMut(&mut Window)>>,
}
impl Data {
fn new<T: AsRef<str>>(title: T) -> Data {
Data { title: WinString::from_str(title), .. Data::default() }
}
}
impl WindowData for Data {
fn name(&self) -> Option<&WinString> {
Some(&self.title)
}
}
struct Class;
impl WindowEvents for Class {
type Data = Data;
fn on_create(hnd: &WindowHandle<Self>) {
let cb = unsafe {
hnd.data_mut().on_create.take()
};
let mut wnd = Window { hnd: hnd.clone() };
cb.map(|mut on_create| on_create(&mut wnd));
}
fn on_show(hnd: &WindowHandle<Self>) {
let cb = unsafe {
hnd.data_mut().on_show.as_mut()
};
let mut wnd = Window { hnd: hnd.clone() };
cb.map(|on_show| (on_show)(&mut wnd));
}
}
impl CustomClass for Class {
type Events = Self;
fn name() -> &'static str {
"WinGUI Main Window"
}
}