diff --git a/examples/open_window.rs b/examples/open_window.rs index 49cff6f..4a6f546 100644 --- a/examples/open_window.rs +++ b/examples/open_window.rs @@ -1,6 +1,6 @@ use std::sync::mpsc; -use baseview::{Event, Window}; +use baseview::{Event, Window, WindowHandler}; fn main() { let window_open_options = baseview::WindowOpenOptions { @@ -19,8 +19,8 @@ fn main() { struct MyProgram {} -impl baseview::AppWindow for MyProgram { - type AppMessage = (); +impl WindowHandler for MyProgram { + type Message = (); fn build(window: &mut Window) -> Self { Self {} @@ -69,5 +69,5 @@ impl baseview::AppWindow for MyProgram { } } - fn on_app_message(&mut self, window: &mut Window, _message: Self::AppMessage) {} + fn on_message(&mut self, window: &mut Window, _message: Self::Message) {} } diff --git a/src/lib.rs b/src/lib.rs index a962ba4..c4ed84f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,12 +33,12 @@ pub struct WindowOpenOptions<'a> { pub parent: Parent, } -pub trait AppWindow { - type AppMessage; +pub trait WindowHandler { + type Message; fn build(window: &mut Window) -> Self; fn draw(&mut self, window: &mut Window); fn on_event(&mut self, window: &mut Window, event: Event); - fn on_app_message(&mut self, window: &mut Window, message: Self::AppMessage); + fn on_message(&mut self, window: &mut Window, message: Self::Message); } diff --git a/src/macos/window.rs b/src/macos/window.rs index 2213f3e..d142884 100644 --- a/src/macos/window.rs +++ b/src/macos/window.rs @@ -11,7 +11,7 @@ use cocoa::foundation::{NSAutoreleasePool, NSPoint, NSRect, NSSize, NSString}; use raw_window_handle::{macos::MacOSHandle, HasRawWindowHandle, RawWindowHandle}; -use crate::{AppWindow, MouseScroll, WindowOpenOptions}; +use crate::{MouseScroll, WindowHandler, WindowOpenOptions}; pub struct Window { ns_window: id, @@ -19,9 +19,9 @@ pub struct Window { } impl Window { - pub fn open( + pub fn open( options: WindowOpenOptions, - app_message_rx: mpsc::Receiver, + app_message_rx: mpsc::Receiver, ) { unsafe { let _pool = NSAutoreleasePool::new(nil); @@ -49,12 +49,9 @@ impl Window { let ns_view = NSView::alloc(nil).init(); ns_window.setContentView_(ns_view); - let mut window = Window { - ns_window, - ns_view, - }; + let mut window = Window { ns_window, ns_view }; - let app_window = A::build(&mut window); + let handler = H::build(&mut window); let current_app = NSRunningApplication::currentApplication(nil); current_app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps); diff --git a/src/win/window.rs b/src/win/window.rs index 89ad38b..b735daf 100644 --- a/src/win/window.rs +++ b/src/win/window.rs @@ -19,7 +19,7 @@ use std::sync::mpsc; use raw_window_handle::{windows::WindowsHandle, HasRawWindowHandle, RawWindowHandle}; -use crate::{AppWindow, Event, Parent::WithParent, WindowInfo, WindowOpenOptions}; +use crate::{Event, Parent::WithParent, WindowHandler, WindowInfo, WindowOpenOptions}; unsafe fn message_box(title: &str, msg: &str) { let title = (title.to_owned() + "\0").as_ptr() as *const i8; @@ -48,14 +48,14 @@ unsafe fn generate_guid() -> String { const WIN_FRAME_TIMER: usize = 4242; -unsafe fn handle_timer(window_state: &RefCell>, timer_id: usize) { +unsafe fn handle_timer(window_state: &RefCell>, timer_id: usize) { match timer_id { WIN_FRAME_TIMER => {} _ => (), } } -unsafe extern "system" fn wnd_proc( +unsafe extern "system" fn wnd_proc( hwnd: HWND, msg: UINT, wparam: WPARAM, @@ -68,7 +68,7 @@ unsafe extern "system" fn wnd_proc( let win_ptr = GetWindowLongPtrA(hwnd, GWLP_USERDATA) as *const c_void; if !win_ptr.is_null() { - let window_state = &*(win_ptr as *const RefCell>); + let window_state = &*(win_ptr as *const RefCell>); let mut window = Window { hwnd }; match msg { @@ -77,7 +77,7 @@ unsafe extern "system" fn wnd_proc( let y = ((lparam >> 16) & 0xFFFF) as i32; window_state .borrow_mut() - .app_window + .handler .on_event(&mut window, Event::CursorMotion(x, y)); return 0; } @@ -91,7 +91,7 @@ unsafe extern "system" fn wnd_proc( WM_CLOSE => { window_state .borrow_mut() - .app_window + .handler .on_event(&mut window, Event::WillClose); return DefWindowProcA(hwnd, msg, wparam, lparam); } @@ -102,13 +102,13 @@ unsafe extern "system" fn wnd_proc( return DefWindowProcA(hwnd, msg, wparam, lparam); } -unsafe fn register_wnd_class() -> ATOM { +unsafe fn register_wnd_class() -> ATOM { // We generate a unique name for the new window class to prevent name collisions let class_name = format!("Baseview-{}", generate_guid()).as_ptr() as *const i8; let wnd_class = WNDCLASSA { style: CS_OWNDC, - lpfnWndProc: Some(wnd_proc::), + lpfnWndProc: Some(wnd_proc::), hInstance: null_mut(), lpszClassName: class_name, cbClsExtra: 0, @@ -126,10 +126,10 @@ unsafe fn unregister_wnd_class(wnd_class: ATOM) { UnregisterClassA(wnd_class as _, null_mut()); } -struct WindowState { +struct WindowState { window_class: ATOM, scaling: Option, // DPI scale, 96.0 is "default". - app_window: A, + handler: H, } pub struct Window { @@ -137,14 +137,14 @@ pub struct Window { } impl Window { - pub fn open( + pub fn open( options: WindowOpenOptions, - app_message_rx: mpsc::Receiver, + app_message_rx: mpsc::Receiver, ) { unsafe { let title = (options.title.to_owned() + "\0").as_ptr() as *const i8; - let window_class = register_wnd_class::(); + let window_class = register_wnd_class::(); // todo: manage error ^ let mut flags = WS_POPUPWINDOW @@ -190,12 +190,12 @@ impl Window { let mut window = Window { hwnd }; - let app_window = A::build(&mut window); + let handler = H::build(&mut window); let window_state = Rc::new(RefCell::new(WindowState { window_class, scaling: None, - app_window, + handler, })); let win = Rc::new(RefCell::new(window)); diff --git a/src/x11/window.rs b/src/x11/window.rs index 5d3a896..4b7694b 100644 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -3,9 +3,7 @@ use std::os::raw::{c_ulong, c_void}; use std::sync::mpsc; use super::XcbConnection; -use crate::{ - AppWindow, Event, MouseButtonID, MouseScroll, Parent, WindowOpenOptions, -}; +use crate::{Event, MouseButtonID, MouseScroll, Parent, WindowHandler, WindowOpenOptions}; use raw_window_handle::{unix::XlibHandle, HasRawWindowHandle, RawWindowHandle}; @@ -16,7 +14,10 @@ pub struct Window { } impl Window { - pub fn open(options: WindowOpenOptions, app_message_rx: mpsc::Receiver) { + pub fn open( + options: WindowOpenOptions, + app_message_rx: mpsc::Receiver, + ) { // Convert the parent to a X11 window ID if we're given one let parent = match options.parent { Parent::None => None, @@ -102,9 +103,9 @@ impl Window { scaling, }; - let mut app_window = A::build(&mut window); + let mut handler = H::build(&mut window); - run_event_loop(&mut window, &mut app_window); + run_event_loop(&mut window, &mut handler); } } @@ -119,7 +120,7 @@ unsafe impl HasRawWindowHandle for Window { } // Event loop -fn run_event_loop(window: &mut Window, app_window: &mut A) { +fn run_event_loop(window: &mut Window, handler: &mut H) { loop { // somehow poll app_message_rx for messages at the same time @@ -149,17 +150,17 @@ fn run_event_loop(window: &mut Window, app_window: &mut A) { match event_type { xcb::EXPOSE => { - app_window.draw(window); + handler.draw(window); } xcb::MOTION_NOTIFY => { let event = unsafe { xcb::cast_event::(&event) }; let detail = event.detail(); if detail != 4 && detail != 5 { - app_window.on_event(window, Event::CursorMotion( - event.event_x() as i32, - event.event_y() as i32, - )); + handler.on_event( + window, + Event::CursorMotion(event.event_x() as i32, event.event_y() as i32), + ); } } xcb::BUTTON_PRESS => { @@ -168,20 +169,26 @@ fn run_event_loop(window: &mut Window, app_window: &mut A) { match detail { 4 => { - app_window.on_event(window, Event::MouseScroll(MouseScroll { - x_delta: 0.0, - y_delta: 1.0, - })); + handler.on_event( + window, + Event::MouseScroll(MouseScroll { + x_delta: 0.0, + y_delta: 1.0, + }), + ); } 5 => { - app_window.on_event(window, Event::MouseScroll(MouseScroll { - x_delta: 0.0, - y_delta: -1.0, - })); + handler.on_event( + window, + Event::MouseScroll(MouseScroll { + x_delta: 0.0, + y_delta: -1.0, + }), + ); } detail => { let button_id = mouse_id(detail); - app_window.on_event(window, Event::MouseDown(button_id)); + handler.on_event(window, Event::MouseDown(button_id)); } } } @@ -191,20 +198,20 @@ fn run_event_loop(window: &mut Window, app_window: &mut A) { if detail != 4 && detail != 5 { let button_id = mouse_id(detail); - app_window.on_event(window, Event::MouseUp(button_id)); + handler.on_event(window, Event::MouseUp(button_id)); } } xcb::KEY_PRESS => { let event = unsafe { xcb::cast_event::(&event) }; let detail = event.detail(); - app_window.on_event(window, Event::KeyDown(detail)); + handler.on_event(window, Event::KeyDown(detail)); } xcb::KEY_RELEASE => { let event = unsafe { xcb::cast_event::(&event) }; let detail = event.detail(); - app_window.on_event(window, Event::KeyUp(detail)); + handler.on_event(window, Event::KeyUp(detail)); } _ => { println!("Unhandled event type: {:?}", event_type);