From 8402310c88bbc81ef3260cfd7af86febabceed86 Mon Sep 17 00:00:00 2001 From: Micah Johnston Date: Tue, 8 Dec 2020 18:37:17 -0600 Subject: [PATCH] remove Message api --- Cargo.toml | 6 ++++-- examples/open_window.rs | 30 ++++++++++++++--------------- src/lib.rs | 5 ----- src/macos/view.rs | 1 - src/macos/window.rs | 41 +++++----------------------------------- src/win/window.rs | 20 +++----------------- src/window.rs | 32 +++++-------------------------- src/x11/window.rs | 42 +++++++++-------------------------------- 8 files changed, 41 insertions(+), 136 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9c149f2..baea30f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,6 @@ license = "MIT OR Apache-2.0" [dependencies] keyboard-types = { version = "0.5.0", default-features = false } raw-window-handle = "0.3.3" -rtrb = "0.1.1" static_assertions = "1.1.0" [target.'cfg(target_os="linux")'.dependencies] @@ -31,4 +30,7 @@ winapi = { version = "0.3.8", features = ["libloaderapi", "winuser", "windef", " [target.'cfg(target_os="macos")'.dependencies] cocoa = "0.24.0" objc = "0.2.7" -uuid = { version = "0.8", features = ["v4"] } \ No newline at end of file +uuid = { version = "0.8", features = ["v4"] } + +[dev-dependencies] +rtrb = "0.1.1" diff --git a/examples/open_window.rs b/examples/open_window.rs index d4a5b38..7e4ad29 100644 --- a/examples/open_window.rs +++ b/examples/open_window.rs @@ -1,21 +1,24 @@ use std::time::Duration; -use baseview::{Event, Window, WindowHandler, WindowScalePolicy}; +use rtrb::{RingBuffer, Consumer}; +use baseview::{Event, Window, WindowHandler, WindowScalePolicy}; #[derive(Debug, Clone)] enum Message { Hello } - -struct OpenWindowExample; - +struct OpenWindowExample { + rx: Consumer, +} impl WindowHandler for OpenWindowExample { - type Message = Message; - - fn on_frame(&mut self) {} + fn on_frame(&mut self) { + while let Ok(message) = self.rx.pop() { + println!("Message: {:?}", message); + } + } fn on_event(&mut self, _window: &mut Window, event: Event) { match event { @@ -24,13 +27,8 @@ impl WindowHandler for OpenWindowExample { Event::Window(e) => println!("Window event: {:?}", e), } } - - fn on_message(&mut self, _window: &mut Window, message: Self::Message) { - println!("Message: {:?}", message); - } } - fn main() { let window_open_options = baseview::WindowOpenOptions { title: "baseview".into(), @@ -39,16 +37,18 @@ fn main() { parent: baseview::Parent::None, }; - let (mut handle, opt_app_runner) = Window::open( + let (mut tx, rx) = RingBuffer::new(128).split(); + + let (_handle, opt_app_runner) = Window::open( window_open_options, - |_| OpenWindowExample + |_| OpenWindowExample { rx } ); ::std::thread::spawn(move || { loop { ::std::thread::sleep(Duration::from_secs(5)); - if let Err(_) = handle.try_send_message(Message::Hello){ + if let Err(_) = tx.push(Message::Hello) { println!("Failed sending message"); } } diff --git a/src/lib.rs b/src/lib.rs index 4cde2e2..f81b9a0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,8 +20,6 @@ pub use window::*; pub use window_info::*; pub use window_open_options::*; -const MESSAGE_QUEUE_LEN: usize = 128; - #[derive(Debug)] pub enum Parent { None, @@ -32,9 +30,6 @@ pub enum Parent { unsafe impl Send for Parent {} pub trait WindowHandler { - type Message: Send + 'static; - fn on_frame(&mut self); fn on_event(&mut self, window: &mut Window, event: Event); - fn on_message(&mut self, window: &mut Window, message: Self::Message); } diff --git a/src/macos/view.rs b/src/macos/view.rs index 74a132c..f6a3f83 100644 --- a/src/macos/view.rs +++ b/src/macos/view.rs @@ -195,7 +195,6 @@ extern "C" fn trigger_on_frame( WindowState::from_field(this) }; - state.handle_messages(); state.trigger_frame(); } diff --git a/src/macos/window.rs b/src/macos/window.rs index 689a8c6..0436576 100644 --- a/src/macos/window.rs +++ b/src/macos/window.rs @@ -12,11 +12,10 @@ use keyboard_types::KeyboardEvent; use objc::{msg_send, runtime::Object, sel, sel_impl}; use raw_window_handle::{macos::MacOSHandle, HasRawWindowHandle, RawWindowHandle}; -use rtrb::{RingBuffer, Producer, Consumer, PushError}; use crate::{ Event, Parent, WindowHandler, WindowOpenOptions, - WindowScalePolicy, WindowInfo, MESSAGE_QUEUE_LEN + WindowScalePolicy, WindowInfo }; use super::view::create_view; @@ -41,7 +40,6 @@ pub struct Window { pub struct AppRunner; - impl AppRunner { pub fn app_run_blocking(self) { unsafe { @@ -53,27 +51,14 @@ impl AppRunner { } -pub struct WindowHandle { - message_tx: Producer, -} - - -impl WindowHandle { - pub fn try_send_message( - &mut self, - message: H::Message - ) -> Result<(), H::Message> { - self.message_tx.push(message) - .map_err(|PushError::Full(message)| message) - } -} +pub struct WindowHandle; impl Window { pub fn open( options: WindowOpenOptions, build: B - ) -> (crate::WindowHandle, Option) + ) -> (crate::WindowHandle, Option) where H: WindowHandler, B: FnOnce(&mut crate::Window) -> H, B: Send + 'static @@ -178,14 +163,10 @@ impl Window { let window_handler = build(&mut crate::Window(&mut window)); - let (message_tx, message_rx) = RingBuffer::new(MESSAGE_QUEUE_LEN) - .split(); - let window_state_arc = Arc::new(WindowState { window, window_handler, keyboard_state: KeyboardState::new(), - message_rx }); let window_state_pointer = Arc::into_raw( @@ -220,9 +201,7 @@ impl Window { ) } - let window_handle = crate::WindowHandle(WindowHandle { - message_tx - }); + let window_handle = crate::WindowHandle(WindowHandle); (window_handle, opt_app_runner) } @@ -233,11 +212,10 @@ pub(super) struct WindowState { window: Window, window_handler: H, keyboard_state: KeyboardState, - message_rx: Consumer, } -impl WindowState { +impl WindowState { /// Returns a mutable reference to a WindowState from an Objective-C field /// /// Don't use this to create two simulataneous references to a single @@ -249,15 +227,6 @@ impl WindowState { &mut *(state_ptr as *mut Self) } - pub(super) fn handle_messages(&mut self){ - while let Ok(message) = self.message_rx.pop(){ - self.window_handler.on_message( - &mut crate::Window(&mut self.window), - message - ) - } - } - pub(super) fn trigger_event(&mut self, event: Event){ self.window_handler.on_event( &mut crate::Window(&mut self.window), diff --git a/src/win/window.rs b/src/win/window.rs index 78877c5..5127935 100644 --- a/src/win/window.rs +++ b/src/win/window.rs @@ -195,19 +195,7 @@ pub struct Window { hwnd: HWND, } -pub struct WindowHandle { - // FIXME: replace this with channel sender - phantom_data: std::marker::PhantomData, -} - -impl WindowHandle { - pub fn try_send_message( - &mut self, - message: H::Message - ) -> Result<(), H::Message> { - Err(message) - } -} +pub struct WindowHandle; pub struct AppRunner { hwnd: HWND, @@ -236,7 +224,7 @@ impl Window { pub fn open( options: WindowOpenOptions, build: B - ) -> (crate::WindowHandle, Option) + ) -> (crate::WindowHandle, Option) where H: WindowHandler, B: FnOnce(&mut crate::Window) -> H, B: Send + 'static @@ -313,9 +301,7 @@ impl Window { SetWindowLongPtrA(hwnd, GWLP_USERDATA, Box::into_raw(window_state) as *const _ as _); SetTimer(hwnd, WIN_FRAME_TIMER, 15, None); - let window_handle = crate::WindowHandle(WindowHandle { - phantom_data: std::marker::PhantomData, - }); + let window_handle = crate::WindowHandle(WindowHandle); let opt_app_runner = if let crate::Parent::None = options.parent { Some(crate::AppRunner(AppRunner { hwnd })) diff --git a/src/window.rs b/src/window.rs index 22ccb7b..8bfc8c8 100644 --- a/src/window.rs +++ b/src/window.rs @@ -10,40 +10,23 @@ use crate::x11 as platform; #[cfg(target_os = "macos")] use crate::macos as platform; - pub struct AppRunner(pub(crate) platform::AppRunner); - impl AppRunner { pub fn app_run_blocking(self){ self.0.app_run_blocking(); } } - -pub struct WindowHandle( - pub(crate) platform::WindowHandle -); - - -impl WindowHandle { - pub fn try_send_message( - &mut self, - message: H::Message - ) -> Result<(), H::Message> { - self.0.try_send_message(message) - } -} - +pub struct WindowHandle(pub(crate) platform::WindowHandle); pub struct Window<'a>(pub(crate) &'a mut platform::Window); - -impl <'a>Window<'a> { +impl<'a> Window<'a> { pub fn open( options: WindowOpenOptions, build: B - ) -> (WindowHandle, Option) + ) -> (WindowHandle, Option) where H: WindowHandler, B: FnOnce(&mut Window) -> H, B: Send + 'static @@ -52,14 +35,12 @@ impl <'a>Window<'a> { } } - -unsafe impl <'a>HasRawWindowHandle for Window<'a> { +unsafe impl<'a> HasRawWindowHandle for Window<'a> { fn raw_window_handle(&self) -> RawWindowHandle { self.0.raw_window_handle() } } - // Compile-time API assertions #[doc(hidden)] mod assertions { @@ -71,14 +52,11 @@ mod assertions { } impl WindowHandler for TestWindowHandler { - type Message = (); - fn on_event(&mut self, _: &mut Window, _: Event) {} - fn on_message(&mut self, _: &mut Window, _: Self::Message) {} fn on_frame(&mut self) {} } // Assert that WindowHandle is Send even if WindowHandler isn't static_assertions::assert_not_impl_any!(TestWindowHandler: Send); - static_assertions::assert_impl_all!(WindowHandle: Send); + static_assertions::assert_impl_all!(WindowHandle: Send); } diff --git a/src/x11/window.rs b/src/x11/window.rs index abc7fbf..93d153f 100644 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -8,13 +8,12 @@ use raw_window_handle::{ HasRawWindowHandle, RawWindowHandle }; -use rtrb::{RingBuffer, Producer, Consumer, PushError}; use super::XcbConnection; use crate::{ Event, MouseButton, MouseCursor, MouseEvent, Parent, ScrollDelta, WindowEvent, WindowHandler, WindowInfo, WindowOpenOptions, - WindowScalePolicy, PhyPoint, PhySize, MESSAGE_QUEUE_LEN, + WindowScalePolicy, PhyPoint, PhySize, }; use super::keyboard::{convert_key_press_event, convert_key_release_event}; @@ -31,19 +30,8 @@ pub struct Window { new_physical_size: Option, } -pub struct WindowHandle { - message_tx: Producer, -} +pub struct WindowHandle; -impl WindowHandle { - pub fn try_send_message( - &mut self, - message: H::Message - ) -> Result<(), H::Message> { - self.message_tx.push(message) - .map_err(|PushError::Full(message)| message) - } -} pub struct AppRunner { thread: std::thread::JoinHandle<()>, @@ -61,7 +49,7 @@ impl Window { pub fn open( options: WindowOpenOptions, build: B - ) -> (crate::WindowHandle, Option) + ) -> (crate::WindowHandle, Option) where H: WindowHandler, B: FnOnce(&mut crate::Window) -> H, B: Send + 'static @@ -70,11 +58,8 @@ impl Window { let (tx, rx) = mpsc::sync_channel::(1); - let (message_tx, message_rx) = RingBuffer::new(MESSAGE_QUEUE_LEN) - .split(); - let thread = thread::spawn(move || { - if let Err(e) = Self::window_thread::(options, build, tx.clone(), message_rx) { + if let Err(e) = Self::window_thread::(options, build, tx.clone()) { let _ = tx.send(Err(e)); } }); @@ -82,9 +67,7 @@ impl Window { // FIXME: placeholder types for returning errors in the future let _ = rx.recv(); - let window_handle = crate::WindowHandle(WindowHandle { - message_tx - }); + let window_handle = crate::WindowHandle(WindowHandle); let opt_app_runner = if is_not_parented { Some(crate::AppRunner(AppRunner { thread })) @@ -95,9 +78,9 @@ impl Window { (window_handle, opt_app_runner) } - fn window_thread(options: WindowOpenOptions, build: B, + fn window_thread( + options: WindowOpenOptions, build: B, tx: mpsc::SyncSender, - message_rx: Consumer, ) -> WindowOpenResult where H: WindowHandler, B: FnOnce(&mut crate::Window) -> H, @@ -211,7 +194,7 @@ impl Window { let _ = tx.send(Ok(())); - window.run_event_loop(&mut handler, message_rx); + window.run_event_loop(&mut handler); Ok(()) } @@ -269,7 +252,7 @@ impl Window { // FIXME: poll() acts fine on linux, sometimes funky on *BSD. XCB upstream uses a define to // switch between poll() and select() (the latter of which is fine on *BSD), and we should do // the same. - fn run_event_loop(&mut self, handler: &mut H, mut message_rx: Consumer) { + fn run_event_loop(&mut self, handler: &mut H) { use nix::poll::*; let xcb_fd = unsafe { @@ -305,13 +288,6 @@ impl Window { self.drain_xcb_events(handler); } } - - while let Ok(message) = message_rx.pop(){ - handler.on_message( - &mut crate::Window(self), - message - ) - } } }