Skip to content

Commit

Permalink
Don't expose WindowHandle through the public interface at all
Browse files Browse the repository at this point in the history
  • Loading branch information
DataTriny committed Sep 22, 2024
1 parent fac4a4e commit 82d8eee
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
6 changes: 3 additions & 3 deletions platforms/windows/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,22 +172,22 @@ impl Adapter {
action_handler: impl 'static + ActionHandler + Send,
) -> Self {
Self::with_wrapped_action_handler(
hwnd.into(),
hwnd,
is_window_focused,
Arc::new(ActionHandlerWrapper::new(action_handler)),
)
}

// Currently required by the test infrastructure
pub(crate) fn with_wrapped_action_handler(
hwnd: WindowHandle,
hwnd: HWND,
is_window_focused: bool,
action_handler: Arc<dyn ActionHandlerNoMut + Send + Sync>,
) -> Self {
init_uia();

let state = State::Inactive {
hwnd,
hwnd: hwnd.into(),
is_window_focused,
action_handler,
};
Expand Down
29 changes: 14 additions & 15 deletions platforms/windows/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// the LICENSE-APACHE file) or the MIT license (found in
// the LICENSE-MIT file), at your option.

use crate::window_handle::WindowHandle;
use accesskit::{ActionHandler, ActivationHandler};
use once_cell::sync::Lazy;
use std::{
Expand All @@ -21,7 +22,6 @@ use windows::{
UI::{Accessibility::*, WindowsAndMessaging::*},
},
};
use crate::window_handle::WindowHandle;

use super::{
context::{ActionHandlerNoMut, ActionHandlerWrapper},
Expand Down Expand Up @@ -54,11 +54,11 @@ struct WindowState {
adapter: RefCell<Adapter>,
}

unsafe fn get_window_state(window: WindowHandle) -> *const WindowState {
GetWindowLongPtrW(window.0, GWLP_USERDATA) as _
unsafe fn get_window_state(window: HWND) -> *const WindowState {
GetWindowLongPtrW(window, GWLP_USERDATA) as _
}

fn update_window_focus_state(window: WindowHandle, is_focused: bool) {
fn update_window_focus_state(window: HWND, is_focused: bool) {
let state = unsafe { &*get_window_state(window) };
let mut adapter = state.adapter.borrow_mut();
if let Some(events) = adapter.update_window_focus_state(is_focused) {
Expand All @@ -72,7 +72,6 @@ struct WindowCreateParams {
}

extern "system" fn wndproc(window: HWND, message: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT {
let window = window.into();
match message {
WM_NCCREATE => {
let create_struct: &CREATESTRUCTW = unsafe { &mut *(lparam.0 as *mut _) };
Expand All @@ -87,15 +86,15 @@ extern "system" fn wndproc(window: HWND, message: u32, wparam: WPARAM, lparam: L
activation_handler: RefCell::new(activation_handler),
adapter: RefCell::new(adapter),
});
unsafe { SetWindowLongPtrW(window.0, GWLP_USERDATA, Box::into_raw(state) as _) };
unsafe { DefWindowProcW(window.0, message, wparam, lparam) }
unsafe { SetWindowLongPtrW(window, GWLP_USERDATA, Box::into_raw(state) as _) };
unsafe { DefWindowProcW(window, message, wparam, lparam) }
}
WM_PAINT => {
unsafe { ValidateRect(window.0, None) }.unwrap();
unsafe { ValidateRect(window, None) }.unwrap();
LRESULT(0)
}
WM_DESTROY => {
let ptr = unsafe { SetWindowLongPtrW(window.0, GWLP_USERDATA, 0) };
let ptr = unsafe { SetWindowLongPtrW(window, GWLP_USERDATA, 0) };
if ptr != 0 {
drop(unsafe { Box::<WindowState>::from_raw(ptr as _) });
}
Expand All @@ -108,7 +107,7 @@ extern "system" fn wndproc(window: HWND, message: u32, wparam: WPARAM, lparam: L
// We need to be prepared to gracefully handle WM_GETOBJECT
// while the window is being destroyed; this can happen if
// the thread is using a COM STA.
return unsafe { DefWindowProcW(window.0, message, wparam, lparam) };
return unsafe { DefWindowProcW(window, message, wparam, lparam) };
}
let state = unsafe { &*state_ptr };
let mut adapter = state.adapter.borrow_mut();
Expand All @@ -117,7 +116,7 @@ extern "system" fn wndproc(window: HWND, message: u32, wparam: WPARAM, lparam: L
drop(activation_handler);
drop(adapter);
result.map_or_else(
|| unsafe { DefWindowProcW(window.0, message, wparam, lparam) },
|| unsafe { DefWindowProcW(window, message, wparam, lparam) },
|result| result.into(),
)
}
Expand All @@ -129,15 +128,15 @@ extern "system" fn wndproc(window: HWND, message: u32, wparam: WPARAM, lparam: L
update_window_focus_state(window, false);
LRESULT(0)
}
_ => unsafe { DefWindowProcW(window.0, message, wparam, lparam) },
_ => unsafe { DefWindowProcW(window, message, wparam, lparam) },
}
}

fn create_window(
title: &str,
activation_handler: impl 'static + ActivationHandler,
action_handler: impl 'static + ActionHandler + Send,
) -> Result<WindowHandle> {
) -> Result<HWND> {
let create_params = Box::new(WindowCreateParams {
activation_handler: Box::new(activation_handler),
action_handler: Arc::new(ActionHandlerWrapper::new(action_handler)),
Expand All @@ -163,7 +162,7 @@ fn create_window(
return Err(Error::from_win32());
}

Ok(window.into())
Ok(window)
}

pub(crate) struct Scope {
Expand Down Expand Up @@ -208,7 +207,7 @@ where

{
let mut state = window_mutex.lock().unwrap();
*state = Some(window);
*state = Some(window.into());
window_cv.notify_one();
}

Expand Down

0 comments on commit 82d8eee

Please sign in to comment.