1
0
Fork 0

rename AppWindow -> WindowHandler

This commit is contained in:
Micah Johnston 2020-09-07 21:36:54 -05:00
parent 057324e688
commit c51aea5c12
5 changed files with 58 additions and 54 deletions

View file

@ -1,6 +1,6 @@
use std::sync::mpsc; use std::sync::mpsc;
use baseview::{Event, Window}; use baseview::{Event, Window, WindowHandler};
fn main() { fn main() {
let window_open_options = baseview::WindowOpenOptions { let window_open_options = baseview::WindowOpenOptions {
@ -19,8 +19,8 @@ fn main() {
struct MyProgram {} struct MyProgram {}
impl baseview::AppWindow for MyProgram { impl WindowHandler for MyProgram {
type AppMessage = (); type Message = ();
fn build(window: &mut Window) -> Self { fn build(window: &mut Window) -> Self {
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) {}
} }

View file

@ -33,12 +33,12 @@ pub struct WindowOpenOptions<'a> {
pub parent: Parent, pub parent: Parent,
} }
pub trait AppWindow { pub trait WindowHandler {
type AppMessage; type Message;
fn build(window: &mut Window) -> Self; fn build(window: &mut Window) -> Self;
fn draw(&mut self, window: &mut Window); fn draw(&mut self, window: &mut Window);
fn on_event(&mut self, window: &mut Window, event: Event); 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);
} }

View file

@ -11,7 +11,7 @@ use cocoa::foundation::{NSAutoreleasePool, NSPoint, NSRect, NSSize, NSString};
use raw_window_handle::{macos::MacOSHandle, HasRawWindowHandle, RawWindowHandle}; use raw_window_handle::{macos::MacOSHandle, HasRawWindowHandle, RawWindowHandle};
use crate::{AppWindow, MouseScroll, WindowOpenOptions}; use crate::{MouseScroll, WindowHandler, WindowOpenOptions};
pub struct Window { pub struct Window {
ns_window: id, ns_window: id,
@ -19,9 +19,9 @@ pub struct Window {
} }
impl Window { impl Window {
pub fn open<A: AppWindow>( pub fn open<H: WindowHandler>(
options: WindowOpenOptions, options: WindowOpenOptions,
app_message_rx: mpsc::Receiver<A::AppMessage>, app_message_rx: mpsc::Receiver<H::Message>,
) { ) {
unsafe { unsafe {
let _pool = NSAutoreleasePool::new(nil); let _pool = NSAutoreleasePool::new(nil);
@ -49,12 +49,9 @@ impl Window {
let ns_view = NSView::alloc(nil).init(); let ns_view = NSView::alloc(nil).init();
ns_window.setContentView_(ns_view); ns_window.setContentView_(ns_view);
let mut window = Window { let mut window = Window { ns_window, ns_view };
ns_window,
ns_view,
};
let app_window = A::build(&mut window); let handler = H::build(&mut window);
let current_app = NSRunningApplication::currentApplication(nil); let current_app = NSRunningApplication::currentApplication(nil);
current_app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps); current_app.activateWithOptions_(NSApplicationActivateIgnoringOtherApps);

View file

@ -19,7 +19,7 @@ use std::sync::mpsc;
use raw_window_handle::{windows::WindowsHandle, HasRawWindowHandle, RawWindowHandle}; 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) { unsafe fn message_box(title: &str, msg: &str) {
let title = (title.to_owned() + "\0").as_ptr() as *const i8; 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; const WIN_FRAME_TIMER: usize = 4242;
unsafe fn handle_timer<A: AppWindow>(window_state: &RefCell<WindowState<A>>, timer_id: usize) { unsafe fn handle_timer<H: WindowHandler>(window_state: &RefCell<WindowState<H>>, timer_id: usize) {
match timer_id { match timer_id {
WIN_FRAME_TIMER => {} WIN_FRAME_TIMER => {}
_ => (), _ => (),
} }
} }
unsafe extern "system" fn wnd_proc<A: AppWindow>( unsafe extern "system" fn wnd_proc<H: WindowHandler>(
hwnd: HWND, hwnd: HWND,
msg: UINT, msg: UINT,
wparam: WPARAM, wparam: WPARAM,
@ -68,7 +68,7 @@ unsafe extern "system" fn wnd_proc<A: AppWindow>(
let win_ptr = GetWindowLongPtrA(hwnd, GWLP_USERDATA) as *const c_void; let win_ptr = GetWindowLongPtrA(hwnd, GWLP_USERDATA) as *const c_void;
if !win_ptr.is_null() { if !win_ptr.is_null() {
let window_state = &*(win_ptr as *const RefCell<WindowState<A>>); let window_state = &*(win_ptr as *const RefCell<WindowState<H>>);
let mut window = Window { hwnd }; let mut window = Window { hwnd };
match msg { match msg {
@ -77,7 +77,7 @@ unsafe extern "system" fn wnd_proc<A: AppWindow>(
let y = ((lparam >> 16) & 0xFFFF) as i32; let y = ((lparam >> 16) & 0xFFFF) as i32;
window_state window_state
.borrow_mut() .borrow_mut()
.app_window .handler
.on_event(&mut window, Event::CursorMotion(x, y)); .on_event(&mut window, Event::CursorMotion(x, y));
return 0; return 0;
} }
@ -91,7 +91,7 @@ unsafe extern "system" fn wnd_proc<A: AppWindow>(
WM_CLOSE => { WM_CLOSE => {
window_state window_state
.borrow_mut() .borrow_mut()
.app_window .handler
.on_event(&mut window, Event::WillClose); .on_event(&mut window, Event::WillClose);
return DefWindowProcA(hwnd, msg, wparam, lparam); return DefWindowProcA(hwnd, msg, wparam, lparam);
} }
@ -102,13 +102,13 @@ unsafe extern "system" fn wnd_proc<A: AppWindow>(
return DefWindowProcA(hwnd, msg, wparam, lparam); return DefWindowProcA(hwnd, msg, wparam, lparam);
} }
unsafe fn register_wnd_class<A: AppWindow>() -> ATOM { unsafe fn register_wnd_class<H: WindowHandler>() -> ATOM {
// We generate a unique name for the new window class to prevent name collisions // 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 class_name = format!("Baseview-{}", generate_guid()).as_ptr() as *const i8;
let wnd_class = WNDCLASSA { let wnd_class = WNDCLASSA {
style: CS_OWNDC, style: CS_OWNDC,
lpfnWndProc: Some(wnd_proc::<A>), lpfnWndProc: Some(wnd_proc::<H>),
hInstance: null_mut(), hInstance: null_mut(),
lpszClassName: class_name, lpszClassName: class_name,
cbClsExtra: 0, cbClsExtra: 0,
@ -126,10 +126,10 @@ unsafe fn unregister_wnd_class(wnd_class: ATOM) {
UnregisterClassA(wnd_class as _, null_mut()); UnregisterClassA(wnd_class as _, null_mut());
} }
struct WindowState<A> { struct WindowState<H> {
window_class: ATOM, window_class: ATOM,
scaling: Option<f64>, // DPI scale, 96.0 is "default". scaling: Option<f64>, // DPI scale, 96.0 is "default".
app_window: A, handler: H,
} }
pub struct Window { pub struct Window {
@ -137,14 +137,14 @@ pub struct Window {
} }
impl Window { impl Window {
pub fn open<A: AppWindow>( pub fn open<H: WindowHandler>(
options: WindowOpenOptions, options: WindowOpenOptions,
app_message_rx: mpsc::Receiver<A::AppMessage>, app_message_rx: mpsc::Receiver<H::Message>,
) { ) {
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;
let window_class = register_wnd_class::<A>(); let window_class = register_wnd_class::<H>();
// todo: manage error ^ // todo: manage error ^
let mut flags = WS_POPUPWINDOW let mut flags = WS_POPUPWINDOW
@ -190,12 +190,12 @@ impl Window {
let mut window = Window { hwnd }; 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 { let window_state = Rc::new(RefCell::new(WindowState {
window_class, window_class,
scaling: None, scaling: None,
app_window, handler,
})); }));
let win = Rc::new(RefCell::new(window)); let win = Rc::new(RefCell::new(window));

View file

@ -3,9 +3,7 @@ use std::os::raw::{c_ulong, c_void};
use std::sync::mpsc; use std::sync::mpsc;
use super::XcbConnection; use super::XcbConnection;
use crate::{ use crate::{Event, MouseButtonID, MouseScroll, Parent, WindowHandler, WindowOpenOptions};
AppWindow, Event, MouseButtonID, MouseScroll, Parent, WindowOpenOptions,
};
use raw_window_handle::{unix::XlibHandle, HasRawWindowHandle, RawWindowHandle}; use raw_window_handle::{unix::XlibHandle, HasRawWindowHandle, RawWindowHandle};
@ -16,7 +14,10 @@ pub struct Window {
} }
impl Window { impl Window {
pub fn open<A: AppWindow>(options: WindowOpenOptions, app_message_rx: mpsc::Receiver<A::AppMessage>) { pub fn open<H: WindowHandler>(
options: WindowOpenOptions,
app_message_rx: mpsc::Receiver<H::Message>,
) {
// Convert the parent to a X11 window ID if we're given one // Convert the parent to a X11 window ID if we're given one
let parent = match options.parent { let parent = match options.parent {
Parent::None => None, Parent::None => None,
@ -102,9 +103,9 @@ impl Window {
scaling, 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 // Event loop
fn run_event_loop<A: AppWindow>(window: &mut Window, app_window: &mut A) { fn run_event_loop<H: WindowHandler>(window: &mut Window, handler: &mut H) {
loop { loop {
// somehow poll app_message_rx for messages at the same time // somehow poll app_message_rx for messages at the same time
@ -149,17 +150,17 @@ fn run_event_loop<A: AppWindow>(window: &mut Window, app_window: &mut A) {
match event_type { match event_type {
xcb::EXPOSE => { xcb::EXPOSE => {
app_window.draw(window); handler.draw(window);
} }
xcb::MOTION_NOTIFY => { xcb::MOTION_NOTIFY => {
let event = unsafe { xcb::cast_event::<xcb::MotionNotifyEvent>(&event) }; let event = unsafe { xcb::cast_event::<xcb::MotionNotifyEvent>(&event) };
let detail = event.detail(); let detail = event.detail();
if detail != 4 && detail != 5 { if detail != 4 && detail != 5 {
app_window.on_event(window, Event::CursorMotion( handler.on_event(
event.event_x() as i32, window,
event.event_y() as i32, Event::CursorMotion(event.event_x() as i32, event.event_y() as i32),
)); );
} }
} }
xcb::BUTTON_PRESS => { xcb::BUTTON_PRESS => {
@ -168,20 +169,26 @@ fn run_event_loop<A: AppWindow>(window: &mut Window, app_window: &mut A) {
match detail { match detail {
4 => { 4 => {
app_window.on_event(window, Event::MouseScroll(MouseScroll { handler.on_event(
x_delta: 0.0, window,
y_delta: 1.0, Event::MouseScroll(MouseScroll {
})); x_delta: 0.0,
y_delta: 1.0,
}),
);
} }
5 => { 5 => {
app_window.on_event(window, Event::MouseScroll(MouseScroll { handler.on_event(
x_delta: 0.0, window,
y_delta: -1.0, Event::MouseScroll(MouseScroll {
})); x_delta: 0.0,
y_delta: -1.0,
}),
);
} }
detail => { detail => {
let button_id = mouse_id(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<A: AppWindow>(window: &mut Window, app_window: &mut A) {
if detail != 4 && detail != 5 { if detail != 4 && detail != 5 {
let button_id = mouse_id(detail); 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 => { xcb::KEY_PRESS => {
let event = unsafe { xcb::cast_event::<xcb::KeyPressEvent>(&event) }; let event = unsafe { xcb::cast_event::<xcb::KeyPressEvent>(&event) };
let detail = event.detail(); let detail = event.detail();
app_window.on_event(window, Event::KeyDown(detail)); handler.on_event(window, Event::KeyDown(detail));
} }
xcb::KEY_RELEASE => { xcb::KEY_RELEASE => {
let event = unsafe { xcb::cast_event::<xcb::KeyReleaseEvent>(&event) }; let event = unsafe { xcb::cast_event::<xcb::KeyReleaseEvent>(&event) };
let detail = event.detail(); 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); println!("Unhandled event type: {:?}", event_type);