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