1
0
Fork 0

Merge pull request #59 from greatest-ape/unify-platforms

Unify platform APIs with wrapper Window and WindowHandle
This commit is contained in:
glowcoil 2020-11-28 11:58:15 -06:00 committed by GitHub
commit 307e82c7b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 98 additions and 35 deletions

View file

@ -2,26 +2,21 @@ use raw_window_handle::RawWindowHandle;
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
mod win; mod win;
#[cfg(target_os = "windows")]
pub use win::*;
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
mod x11; mod x11;
#[cfg(target_os = "linux")]
pub use crate::x11::*;
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
mod macos; mod macos;
#[cfg(target_os = "macos")]
pub use macos::*;
mod event; mod event;
mod keyboard; mod keyboard;
mod mouse_cursor; mod mouse_cursor;
mod window;
mod window_info; mod window_info;
mod window_open_options; mod window_open_options;
pub use event::*; pub use event::*;
pub use mouse_cursor::MouseCursor; pub use mouse_cursor::MouseCursor;
pub use window::*;
pub use window_info::*; pub use window_info::*;
pub use window_open_options::*; pub use window_open_options::*;

View file

@ -56,9 +56,12 @@ impl WindowHandle {
} }
impl Window { impl Window {
pub fn open<H, B>(options: WindowOpenOptions, build: B) -> WindowHandle pub fn open<H, B>(
options: WindowOpenOptions,
build: B
) -> crate::WindowHandle
where H: WindowHandler, where H: WindowHandler,
B: FnOnce(&mut Window) -> H, B: FnOnce(&mut crate::Window) -> H,
B: Send + 'static B: Send + 'static
{ {
let _pool = unsafe { NSAutoreleasePool::new(nil) }; let _pool = unsafe { NSAutoreleasePool::new(nil) };
@ -155,7 +158,7 @@ impl Window {
}, },
}; };
let window_handler = build(&mut window); let window_handler = build(&mut crate::Window(&mut window));
let window_state_arc = Arc::new(WindowState { let window_state_arc = Arc::new(WindowState {
window, window,
@ -195,7 +198,7 @@ impl Window {
) )
} }
WindowHandle crate::WindowHandle(WindowHandle)
} }
} }
@ -220,7 +223,10 @@ impl <H: WindowHandler>WindowState<H> {
} }
pub(super) fn trigger_event(&mut self, event: Event){ pub(super) fn trigger_event(&mut self, event: Event){
self.window_handler.on_event(&mut self.window, event); self.window_handler.on_event(
&mut crate::Window(&mut self.window),
event
);
} }
pub(super) fn trigger_frame(&mut self){ pub(super) fn trigger_frame(&mut self){

View file

@ -73,6 +73,7 @@ unsafe extern "system" fn wnd_proc<H: WindowHandler>(
if !win_ptr.is_null() { if !win_ptr.is_null() {
let window_state = &*(win_ptr as *const RefCell<WindowState<H>>); let window_state = &*(win_ptr as *const RefCell<WindowState<H>>);
let mut window = Window { hwnd }; let mut window = Window { hwnd };
let mut window = crate::Window(&mut window);
match msg { match msg {
WM_MOUSEMOVE => { WM_MOUSEMOVE => {
@ -175,9 +176,12 @@ impl WindowHandle {
} }
impl Window { impl Window {
pub fn open<H, B>(options: WindowOpenOptions, build: B) -> WindowHandle pub fn open<H, B>(
options: WindowOpenOptions,
build: B
) -> crate::WindowHandle
where H: WindowHandler, where H: WindowHandler,
B: FnOnce(&mut Window) -> H, B: FnOnce(&mut crate::Window) -> H,
B: Send + 'static B: Send + 'static
{ {
unsafe { unsafe {
@ -240,9 +244,7 @@ impl Window {
); );
// todo: manage error ^ // todo: manage error ^
let mut window = Window { hwnd }; let handler = build(&mut crate::Window(&mut Window { hwnd }));
let handler = build(&mut window);
let window_state = Box::new(RefCell::new(WindowState { let window_state = Box::new(RefCell::new(WindowState {
window_class, window_class,
@ -253,7 +255,7 @@ impl Window {
SetWindowLongPtrA(hwnd, GWLP_USERDATA, Box::into_raw(window_state) as *const _ as _); SetWindowLongPtrA(hwnd, GWLP_USERDATA, Box::into_raw(window_state) as *const _ as _);
SetTimer(hwnd, 4242, 13, None); SetTimer(hwnd, 4242, 13, None);
WindowHandle { hwnd } crate::WindowHandle(WindowHandle { hwnd })
} }
} }
} }

45
src/window.rs Normal file
View file

@ -0,0 +1,45 @@
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
use crate::WindowHandler;
use crate::window_open_options::WindowOpenOptions;
#[cfg(target_os = "windows")]
use crate::win as platform;
#[cfg(target_os = "linux")]
use crate::x11 as platform;
#[cfg(target_os = "macos")]
use crate::macos as platform;
pub struct WindowHandle(pub(crate) platform::WindowHandle);
impl WindowHandle {
pub fn app_run_blocking(self){
self.0.app_run_blocking();
}
}
pub struct Window<'a>(pub(crate) &'a mut platform::Window);
impl <'a>Window<'a> {
pub fn open<H, B>(
options: WindowOpenOptions,
build: B
) -> WindowHandle
where H: WindowHandler,
B: FnOnce(&mut Window) -> H,
B: Send + 'static
{
platform::Window::open::<H, B>(options, build)
}
}
unsafe impl <'a>HasRawWindowHandle for Window<'a> {
fn raw_window_handle(&self) -> RawWindowHandle {
self.0.raw_window_handle()
}
}

View file

@ -44,9 +44,12 @@ impl WindowHandle {
type WindowOpenResult = Result<(), ()>; type WindowOpenResult = Result<(), ()>;
impl Window { impl Window {
pub fn open<H, B>(options: WindowOpenOptions, build: B) -> WindowHandle pub fn open<H, B>(
options: WindowOpenOptions,
build: B
) -> crate::WindowHandle
where H: WindowHandler, where H: WindowHandler,
B: FnOnce(&mut Window) -> H, B: FnOnce(&mut crate::Window) -> H,
B: Send + 'static B: Send + 'static
{ {
let (tx, rx) = mpsc::sync_channel::<WindowOpenResult>(1); let (tx, rx) = mpsc::sync_channel::<WindowOpenResult>(1);
@ -60,13 +63,13 @@ impl Window {
// FIXME: placeholder types for returning errors in the future // FIXME: placeholder types for returning errors in the future
let _ = rx.recv(); let _ = rx.recv();
WindowHandle { thread } crate::WindowHandle(WindowHandle { thread })
} }
fn window_thread<H, B>(options: WindowOpenOptions, build: B, fn window_thread<H, B>(options: WindowOpenOptions, build: B,
tx: mpsc::SyncSender<WindowOpenResult>) -> WindowOpenResult tx: mpsc::SyncSender<WindowOpenResult>) -> WindowOpenResult
where H: WindowHandler, where H: WindowHandler,
B: FnOnce(&mut Window) -> H, B: FnOnce(&mut crate::Window) -> H,
B: Send + 'static B: Send + 'static
{ {
// Connect to the X server // Connect to the X server
@ -173,7 +176,7 @@ impl Window {
new_physical_size: None, new_physical_size: None,
}; };
let mut handler = build(&mut window); let mut handler = build(&mut crate::Window(&mut window));
let _ = tx.send(Ok(())); let _ = tx.send(Ok(()));
@ -222,9 +225,12 @@ impl Window {
self.window_info.scale() self.window_info.scale()
); );
handler.on_event(self, Event::Window( let window_info = self.window_info;
WindowEvent::Resized(self.window_info)
)) handler.on_event(
&mut crate::Window(self),
Event::Window(WindowEvent::Resized(window_info))
)
} }
} }
@ -313,7 +319,10 @@ impl Window {
.unwrap_or(xcb::NONE); .unwrap_or(xcb::NONE);
if wm_delete_window == data32[0] { if wm_delete_window == data32[0] {
handler.on_event(self, Event::Window(WindowEvent::WillClose)); handler.on_event(
&mut crate::Window(self),
Event::Window(WindowEvent::WillClose)
);
// FIXME: handler should decide whether window stays open or not // FIXME: handler should decide whether window stays open or not
self.event_loop_running = false; self.event_loop_running = false;
@ -342,7 +351,7 @@ impl Window {
let logical_pos = physical_pos.to_logical(&self.window_info); let logical_pos = physical_pos.to_logical(&self.window_info);
handler.on_event( handler.on_event(
self, &mut crate::Window(self),
Event::Mouse(MouseEvent::CursorMoved { Event::Mouse(MouseEvent::CursorMoved {
position: logical_pos, position: logical_pos,
}), }),
@ -357,7 +366,7 @@ impl Window {
match detail { match detail {
4 => { 4 => {
handler.on_event( handler.on_event(
self, &mut crate::Window(self),
Event::Mouse(MouseEvent::WheelScrolled(ScrollDelta::Lines { Event::Mouse(MouseEvent::WheelScrolled(ScrollDelta::Lines {
x: 0.0, x: 0.0,
y: 1.0, y: 1.0,
@ -366,7 +375,7 @@ impl Window {
} }
5 => { 5 => {
handler.on_event( handler.on_event(
self, &mut crate::Window(self),
Event::Mouse(MouseEvent::WheelScrolled(ScrollDelta::Lines { Event::Mouse(MouseEvent::WheelScrolled(ScrollDelta::Lines {
x: 0.0, x: 0.0,
y: -1.0, y: -1.0,
@ -375,7 +384,10 @@ impl Window {
} }
detail => { detail => {
let button_id = mouse_id(detail); let button_id = mouse_id(detail);
handler.on_event(self, Event::Mouse(MouseEvent::ButtonPressed(button_id))); handler.on_event(
&mut crate::Window(self),
Event::Mouse(MouseEvent::ButtonPressed(button_id))
);
} }
} }
} }
@ -386,7 +398,10 @@ impl Window {
if detail != 4 && detail != 5 { if detail != 4 && detail != 5 {
let button_id = mouse_id(detail); let button_id = mouse_id(detail);
handler.on_event(self, Event::Mouse(MouseEvent::ButtonReleased(button_id))); handler.on_event(
&mut crate::Window(self),
Event::Mouse(MouseEvent::ButtonReleased(button_id))
);
} }
} }
@ -397,7 +412,7 @@ impl Window {
let event = unsafe { xcb::cast_event::<xcb::KeyPressEvent>(&event) }; let event = unsafe { xcb::cast_event::<xcb::KeyPressEvent>(&event) };
handler.on_event( handler.on_event(
self, &mut crate::Window(self),
Event::Keyboard(convert_key_press_event(&event)) Event::Keyboard(convert_key_press_event(&event))
); );
} }
@ -406,7 +421,7 @@ impl Window {
let event = unsafe { xcb::cast_event::<xcb::KeyReleaseEvent>(&event) }; let event = unsafe { xcb::cast_event::<xcb::KeyReleaseEvent>(&event) };
handler.on_event( handler.on_event(
self, &mut crate::Window(self),
Event::Keyboard(convert_key_release_event(&event)) Event::Keyboard(convert_key_release_event(&event))
); );
} }