1
0
Fork 0

Merge pull request #38 from glowcoil/master

Refactor: call DispatchMessage in event loop and combine handle_message with wnd_proc
This commit is contained in:
william light 2020-09-07 22:15:26 +02:00 committed by GitHub
commit 17c673957c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 83 deletions

View file

@ -1,41 +0,0 @@
use std::sync::{Arc, Mutex};
use winapi::shared::minwindef::{LPARAM, LRESULT, UINT, WPARAM};
use crate::{AppWindow, Window};
use winapi::um::winuser::{DefWindowProcA, WM_MOUSEMOVE, WM_PAINT, WM_TIMER};
const WIN_FRAME_TIMER: usize = 4242;
unsafe fn handle_timer<A: AppWindow>(win: &Arc<Mutex<Window<A>>>, timer_id: usize) {
match timer_id {
WIN_FRAME_TIMER => {}
_ => (),
}
}
pub(crate) unsafe fn handle_message<A: AppWindow>(
win: &Arc<Mutex<Window<A>>>,
message: UINT,
wparam: WPARAM,
lparam: LPARAM,
) -> LRESULT {
let hwnd;
{
hwnd = win.lock().unwrap().hwnd;
}
match message {
WM_MOUSEMOVE => {
let x = (lparam & 0xFFFF) as i32;
let y = ((lparam >> 16) & 0xFFFF) as i32;
win.lock().unwrap().handle_mouse_motion(x, y);
0
}
WM_TIMER => {
handle_timer(win, wparam);
0
}
WM_PAINT => 0,
_ => DefWindowProcA(hwnd, message, wparam, lparam),
}
}

View file

@ -1,4 +1,3 @@
mod event;
mod window; mod window;
pub use window::*; pub use window::*;

View file

@ -1,24 +1,23 @@
extern crate winapi; use winapi::shared::guiddef::GUID;
use winapi::shared::minwindef::{ATOM, FALSE, LPARAM, LRESULT, UINT, WPARAM};
use std::ptr::null_mut; use winapi::shared::windef::{HWND, RECT};
use std::sync::mpsc; use winapi::um::combaseapi::CoCreateGuid;
use winapi::um::winuser::{
use self::winapi::shared::guiddef::GUID; AdjustWindowRectEx, CreateWindowExA, DefWindowProcA, DestroyWindow, DispatchMessageA,
use self::winapi::shared::minwindef::{ATOM, FALSE, LPARAM, LRESULT, UINT, WPARAM}; GetMessageA, GetWindowLongPtrA, MessageBoxA, PostMessageA, RegisterClassA, SetTimer,
use self::winapi::shared::windef::{HWND, RECT}; SetWindowLongPtrA, TranslateMessage, UnregisterClassA, CS_OWNDC, GWLP_USERDATA, MB_ICONERROR,
use self::winapi::um::combaseapi::CoCreateGuid; MB_OK, MB_TOPMOST, MSG, WM_CREATE, WM_MOUSEMOVE, WM_PAINT, WM_SHOWWINDOW, WM_TIMER, WNDCLASSA,
use self::winapi::um::winuser::{ WS_CAPTION, WS_CHILD, WS_CLIPSIBLINGS, WS_MAXIMIZEBOX, WS_MINIMIZEBOX, WS_POPUPWINDOW,
AdjustWindowRectEx, CreateWindowExA, DefWindowProcA, DestroyWindow, GetMessageA, WS_SIZEBOX, WS_VISIBLE,
GetWindowLongPtrA, MessageBoxA, PostMessageA, RegisterClassA, SetTimer, SetWindowLongPtrA,
TranslateMessage, UnregisterClassA, CS_OWNDC, GWLP_USERDATA, MB_ICONERROR, MB_OK, MB_TOPMOST,
MSG, WM_CREATE, WM_SHOWWINDOW, WNDCLASSA, WS_CAPTION, WS_CHILD, WS_CLIPSIBLINGS,
WS_MAXIMIZEBOX, WS_MINIMIZEBOX, WS_POPUPWINDOW, WS_SIZEBOX, WS_VISIBLE,
}; };
use self::winapi::ctypes::c_void; use std::ffi::c_void;
use super::event::handle_message; use std::ptr::null_mut;
use std::sync::mpsc;
use std::rc::Rc;
use std::cell::RefCell;
use crate::{AppWindow, Event, Parent::WithParent, RawWindow, WindowInfo, WindowOpenOptions}; use crate::{AppWindow, Event, Parent::WithParent, RawWindow, WindowInfo, WindowOpenOptions};
use std::sync::{Arc, Mutex};
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;
@ -45,36 +44,49 @@ unsafe fn generate_guid() -> String {
) )
} }
const WIN_FRAME_TIMER: usize = 4242;
unsafe fn handle_timer<A: AppWindow>(win: &RefCell<Window<A>>, timer_id: usize) {
match timer_id {
WIN_FRAME_TIMER => {}
_ => (),
}
}
unsafe extern "system" fn wnd_proc<A: AppWindow>( unsafe extern "system" fn wnd_proc<A: AppWindow>(
hwnd: HWND, hwnd: HWND,
msg: UINT, msg: UINT,
wparam: WPARAM, wparam: WPARAM,
lparam: LPARAM, lparam: LPARAM,
) -> LRESULT { ) -> LRESULT {
let win_ptr = GetWindowLongPtrA(hwnd, GWLP_USERDATA) as *const c_void; if msg == WM_CREATE {
match msg {
WM_CREATE => {
PostMessageA(hwnd, WM_SHOWWINDOW, 0, 0); PostMessageA(hwnd, WM_SHOWWINDOW, 0, 0);
0 return 0;
} }
_ => {
let win_ptr = GetWindowLongPtrA(hwnd, GWLP_USERDATA) as *const c_void;
if !win_ptr.is_null() { if !win_ptr.is_null() {
let win: Arc<Mutex<Window<A>>> = let win = &*(win_ptr as *const RefCell<Window<A>>);
Arc::from_raw(win_ptr as *mut Mutex<Window<A>>);
let ret = handle_message(&win, msg, wparam, lparam); match msg {
WM_MOUSEMOVE => {
// todo: need_reconfigure thing? let x = (lparam & 0xFFFF) as i32;
let y = ((lparam >> 16) & 0xFFFF) as i32;
// If we don't do this, the Arc will be dropped and we'll get a crash. win.borrow_mut().handle_mouse_motion(x, y);
let _ = Arc::into_raw(win); return 0;
}
return ret; WM_TIMER => {
handle_timer(&win, wparam);
return 0;
}
WM_PAINT => {
return 0;
}
_ => {}
}
} }
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<A: AppWindow>() -> ATOM {
@ -181,10 +193,9 @@ impl<A: AppWindow> Window<A> {
scaling: None, scaling: None,
}; };
let win = Arc::new(Mutex::new(window)); let win = Rc::new(RefCell::new(window));
let win_p = Arc::clone(&win);
SetWindowLongPtrA(hwnd, GWLP_USERDATA, Arc::into_raw(win) as *const _ as _); SetWindowLongPtrA(hwnd, GWLP_USERDATA, Rc::into_raw(win) as *const _ as _);
SetTimer(hwnd, 4242, 13, None); SetTimer(hwnd, 4242, 13, None);
@ -197,7 +208,7 @@ impl<A: AppWindow> Window<A> {
break; break;
} }
TranslateMessage(&mut msg); TranslateMessage(&mut msg);
handle_message(&win_p, msg.message, msg.wParam, msg.lParam); DispatchMessageA(&mut msg);
} }
} }
} }