rename AppWindow -> WindowHandler
This commit is contained in:
parent
057324e688
commit
c51aea5c12
|
@ -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) {}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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<A: AppWindow>(
|
||||
pub fn open<H: WindowHandler>(
|
||||
options: WindowOpenOptions,
|
||||
app_message_rx: mpsc::Receiver<A::AppMessage>,
|
||||
app_message_rx: mpsc::Receiver<H::Message>,
|
||||
) {
|
||||
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);
|
||||
|
|
|
@ -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<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 {
|
||||
WIN_FRAME_TIMER => {}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
unsafe extern "system" fn wnd_proc<A: AppWindow>(
|
||||
unsafe extern "system" fn wnd_proc<H: WindowHandler>(
|
||||
hwnd: HWND,
|
||||
msg: UINT,
|
||||
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;
|
||||
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 };
|
||||
|
||||
match msg {
|
||||
|
@ -77,7 +77,7 @@ unsafe extern "system" fn wnd_proc<A: AppWindow>(
|
|||
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<A: AppWindow>(
|
|||
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<A: AppWindow>(
|
|||
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
|
||||
let class_name = format!("Baseview-{}", generate_guid()).as_ptr() as *const i8;
|
||||
|
||||
let wnd_class = WNDCLASSA {
|
||||
style: CS_OWNDC,
|
||||
lpfnWndProc: Some(wnd_proc::<A>),
|
||||
lpfnWndProc: Some(wnd_proc::<H>),
|
||||
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<A> {
|
||||
struct WindowState<H> {
|
||||
window_class: ATOM,
|
||||
scaling: Option<f64>, // 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<A: AppWindow>(
|
||||
pub fn open<H: WindowHandler>(
|
||||
options: WindowOpenOptions,
|
||||
app_message_rx: mpsc::Receiver<A::AppMessage>,
|
||||
app_message_rx: mpsc::Receiver<H::Message>,
|
||||
) {
|
||||
unsafe {
|
||||
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 ^
|
||||
|
||||
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));
|
||||
|
|
|
@ -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<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
|
||||
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<A: AppWindow>(window: &mut Window, app_window: &mut A) {
|
||||
fn run_event_loop<H: WindowHandler>(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<A: AppWindow>(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::<xcb::MotionNotifyEvent>(&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<A: AppWindow>(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<A: AppWindow>(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::<xcb::KeyPressEvent>(&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::<xcb::KeyReleaseEvent>(&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);
|
||||
|
|
Loading…
Reference in a new issue