Merge pull request #49 from RustAudio/build-closure
replace `WindowHandler::build()` with closure passed to `Window::open()`
This commit is contained in:
commit
e3171da665
|
@ -1,14 +1,10 @@
|
||||||
use baseview::{Event, Window, WindowHandler};
|
use baseview::{Event, Window, WindowHandler};
|
||||||
|
|
||||||
struct MyProgram {}
|
struct OpenWindowExample;
|
||||||
|
|
||||||
impl WindowHandler for MyProgram {
|
impl WindowHandler for OpenWindowExample {
|
||||||
type Message = ();
|
type Message = ();
|
||||||
|
|
||||||
fn build(_window: &mut Window) -> Self {
|
|
||||||
Self {}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn on_frame(&mut self) {}
|
fn on_frame(&mut self) {}
|
||||||
|
|
||||||
fn on_event(&mut self, _window: &mut Window, event: Event) {
|
fn on_event(&mut self, _window: &mut Window, event: Event) {
|
||||||
|
@ -30,6 +26,6 @@ fn main() {
|
||||||
parent: baseview::Parent::None,
|
parent: baseview::Parent::None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let handle = Window::open::<MyProgram>(window_open_options);
|
let handle = Window::open(window_open_options, |_| OpenWindowExample);
|
||||||
handle.app_run_blocking();
|
handle.app_run_blocking();
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,8 +42,6 @@ pub struct WindowOpenOptions {
|
||||||
pub trait WindowHandler {
|
pub trait WindowHandler {
|
||||||
type Message;
|
type Message;
|
||||||
|
|
||||||
fn build(window: &mut Window) -> Self;
|
|
||||||
|
|
||||||
fn on_frame(&mut self);
|
fn on_frame(&mut self);
|
||||||
fn on_event(&mut self, window: &mut Window, event: Event);
|
fn on_event(&mut self, window: &mut Window, event: Event);
|
||||||
fn on_message(&mut self, window: &mut Window, message: Self::Message);
|
fn on_message(&mut self, window: &mut Window, message: Self::Message);
|
||||||
|
|
|
@ -33,7 +33,11 @@ impl WindowHandle {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Window {
|
impl Window {
|
||||||
pub fn open<H: WindowHandler>(options: WindowOpenOptions) -> WindowHandle {
|
pub fn open<H, B>(options: WindowOpenOptions, build: B) -> WindowHandle
|
||||||
|
where H: WindowHandler,
|
||||||
|
B: FnOnce(&mut Window) -> H,
|
||||||
|
B: Send + 'static
|
||||||
|
{
|
||||||
unsafe {
|
unsafe {
|
||||||
let _pool = NSAutoreleasePool::new(nil);
|
let _pool = NSAutoreleasePool::new(nil);
|
||||||
|
|
||||||
|
@ -59,7 +63,7 @@ impl Window {
|
||||||
|
|
||||||
let mut window = Window { ns_window, ns_view };
|
let mut window = Window { ns_window, ns_view };
|
||||||
|
|
||||||
let handler = H::build(&mut window);
|
let handler = build(&mut window);
|
||||||
|
|
||||||
// FIXME: only do this in the unparented case
|
// FIXME: only do this in the unparented case
|
||||||
let current_app = NSRunningApplication::currentApplication(nil);
|
let current_app = NSRunningApplication::currentApplication(nil);
|
||||||
|
|
|
@ -166,7 +166,11 @@ impl WindowHandle {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Window {
|
impl Window {
|
||||||
pub fn open<H: WindowHandler>(options: WindowOpenOptions) -> WindowHandle {
|
pub fn open<H, B>(options: WindowOpenOptions, build: B) -> WindowHandle
|
||||||
|
where H: WindowHandler,
|
||||||
|
B: FnOnce(&mut Window) -> H,
|
||||||
|
B: Send + 'static
|
||||||
|
{
|
||||||
unsafe {
|
unsafe {
|
||||||
let title = (options.title.to_owned() + "\0").as_ptr() as *const i8;
|
let title = (options.title.to_owned() + "\0").as_ptr() as *const i8;
|
||||||
|
|
||||||
|
@ -222,7 +226,7 @@ impl Window {
|
||||||
|
|
||||||
let mut window = Window { hwnd };
|
let mut window = Window { hwnd };
|
||||||
|
|
||||||
let handler = H::build(&mut window);
|
let handler = build(&mut window);
|
||||||
|
|
||||||
let window_state = Rc::new(RefCell::new(WindowState {
|
let window_state = Rc::new(RefCell::new(WindowState {
|
||||||
window_class,
|
window_class,
|
||||||
|
|
|
@ -41,11 +41,15 @@ impl WindowHandle {
|
||||||
type WindowOpenResult = Result<(), ()>;
|
type WindowOpenResult = Result<(), ()>;
|
||||||
|
|
||||||
impl Window {
|
impl Window {
|
||||||
pub fn open<H: WindowHandler>(options: WindowOpenOptions) -> WindowHandle {
|
pub fn open<H, B>(options: WindowOpenOptions, build: B) -> WindowHandle
|
||||||
|
where H: WindowHandler,
|
||||||
|
B: FnOnce(&mut Window) -> H,
|
||||||
|
B: Send + 'static
|
||||||
|
{
|
||||||
let (tx, rx) = mpsc::sync_channel::<WindowOpenResult>(1);
|
let (tx, rx) = mpsc::sync_channel::<WindowOpenResult>(1);
|
||||||
|
|
||||||
let thread = thread::spawn(move || {
|
let thread = thread::spawn(move || {
|
||||||
if let Err(e) = Self::window_thread::<H>(options, tx.clone()) {
|
if let Err(e) = Self::window_thread::<H, B>(options, build, tx.clone()) {
|
||||||
let _ = tx.send(Err(e));
|
let _ = tx.send(Err(e));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -56,9 +60,12 @@ impl Window {
|
||||||
WindowHandle { thread }
|
WindowHandle { thread }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn window_thread<H: WindowHandler>(
|
fn window_thread<H, B>(options: WindowOpenOptions, build: B,
|
||||||
options: WindowOpenOptions, tx: mpsc::SyncSender<WindowOpenResult>,
|
tx: mpsc::SyncSender<WindowOpenResult>) -> WindowOpenResult
|
||||||
) -> WindowOpenResult {
|
where H: WindowHandler,
|
||||||
|
B: FnOnce(&mut Window) -> H,
|
||||||
|
B: Send + 'static
|
||||||
|
{
|
||||||
// Connect to the X server
|
// Connect to the X server
|
||||||
// FIXME: baseview error type instead of unwrap()
|
// FIXME: baseview error type instead of unwrap()
|
||||||
let xcb_connection = XcbConnection::new().unwrap();
|
let xcb_connection = XcbConnection::new().unwrap();
|
||||||
|
@ -164,7 +171,7 @@ impl Window {
|
||||||
new_size: None
|
new_size: None
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut handler = H::build(&mut window);
|
let mut handler = build(&mut window);
|
||||||
|
|
||||||
let _ = tx.send(Ok(()));
|
let _ = tx.send(Ok(()));
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue