Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions winio-ui-win32/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@ sync-unsafe-cell = { version = "0.1", optional = true }
embed-resource = "3"

[features]
ignore-class-conflict = []
dark-mode = ["dep:slim-detours-sys", "dep:sync-unsafe-cell"]
60 changes: 36 additions & 24 deletions winio-ui-win32/src/ui/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,25 @@ use std::{

use compio::driver::syscall;
use inherit_methods_macro::inherit_methods;
use widestring::U16CString;
use windows_sys::{
Win32::{
Foundation::{ERROR_INVALID_HANDLE, HWND, LPARAM, LRESULT, POINT, SetLastError, WPARAM},
Graphics::Gdi::{GetStockObject, MapWindowPoints, WHITE_BRUSH},
System::LibraryLoader::GetModuleHandleW,
UI::{
Input::KeyboardAndMouse::{EnableWindow, IsWindowEnabled},
WindowsAndMessaging::{
CW_USEDEFAULT, CloseWindow, CreateWindowExW, DestroyWindow, GWL_EXSTYLE, GWL_STYLE,
GetClientRect, GetParent, GetWindowLongPtrW, GetWindowRect, GetWindowTextLengthW,
GetWindowTextW, HICON, HWND_DESKTOP, ICON_BIG, IDC_ARROW, IMAGE_ICON,
IsWindowVisible, LR_DEFAULTCOLOR, LR_DEFAULTSIZE, LR_SHARED, LoadCursorW,
LoadImageW, RegisterClassExW, SW_HIDE, SW_SHOW, SWP_NOMOVE, SWP_NOSIZE,
SWP_NOZORDER, SendMessageW, SetWindowLongPtrW, SetWindowPos, SetWindowTextW,
ShowWindow, WM_CLOSE, WM_MOVE, WM_SETICON, WM_SIZE, WNDCLASSEXW, WS_CHILDWINDOW,
WS_OVERLAPPEDWINDOW,
},
use widestring::{U16CStr, U16CString, u16cstr};
#[cfg(feature = "ignore-class-conflict")]
use windows_sys::Win32::Foundation::ERROR_CLASS_ALREADY_EXISTS;
use windows_sys::Win32::{
Foundation::{ERROR_INVALID_HANDLE, HWND, LPARAM, LRESULT, POINT, SetLastError, WPARAM},
Graphics::Gdi::{GetStockObject, MapWindowPoints, WHITE_BRUSH},
System::LibraryLoader::GetModuleHandleW,
UI::{
Input::KeyboardAndMouse::{EnableWindow, IsWindowEnabled},
WindowsAndMessaging::{
CW_USEDEFAULT, CloseWindow, CreateWindowExW, DestroyWindow, GWL_EXSTYLE, GWL_STYLE,
GetClientRect, GetParent, GetWindowLongPtrW, GetWindowRect, GetWindowTextLengthW,
GetWindowTextW, HICON, HWND_DESKTOP, ICON_BIG, IDC_ARROW, IMAGE_ICON, IsWindowVisible,
LR_DEFAULTCOLOR, LR_DEFAULTSIZE, LR_SHARED, LoadCursorW, LoadImageW, RegisterClassExW,
SW_HIDE, SW_SHOW, SWP_NOMOVE, SWP_NOSIZE, SWP_NOZORDER, SendMessageW,
SetWindowLongPtrW, SetWindowPos, SetWindowTextW, ShowWindow, WM_CLOSE, WM_MOVE,
WM_SETICON, WM_SIZE, WNDCLASSEXW, WS_CHILDWINDOW, WS_OVERLAPPEDWINDOW,
},
},
w,
};
use winio_handle::{AsRawWindow, AsWindow, BorrowedWindow, RawWindow};
use winio_primitive::{Point, Size};
Expand Down Expand Up @@ -321,7 +319,8 @@ impl AsRawWindow for Widget {
}
}

pub(crate) const WINDOW_CLASS_NAME: *const u16 = w!("XamlWindow");
pub(crate) const WINDOW_CLASS_NAME: &U16CStr =
u16cstr!(concat!("WinioWindowVersion", env!("CARGO_PKG_VERSION")));

fn register() {
unsafe {
Expand All @@ -338,10 +337,18 @@ fn register() {
hCursor: unsafe { LoadCursorW(null_mut(), IDC_ARROW) },
hbrBackground: unsafe { GetStockObject(WHITE_BRUSH) },
lpszMenuName: null(),
lpszClassName: WINDOW_CLASS_NAME,
lpszClassName: WINDOW_CLASS_NAME.as_ptr(),
hIconSm: null_mut(),
};
syscall!(BOOL, unsafe { RegisterClassExW(&cls) }).unwrap();
match syscall!(BOOL, unsafe { RegisterClassExW(&cls) }) {
Ok(_) => {}
#[cfg(feature = "ignore-class-conflict")]
Err(e) if e.raw_os_error() == Some(ERROR_CLASS_ALREADY_EXISTS as _) => {
// The class is already registered. We choose to ignore this error,
// and hope that the class is still valid.
}
Err(e) => panic!("{e:?}"),
}
}

static REGISTER: Once = Once::new();
Expand All @@ -361,13 +368,18 @@ impl Window {
register_once();
let handle = if let Some(parent) = parent {
Widget::new(
WINDOW_CLASS_NAME,
WINDOW_CLASS_NAME.as_ptr(),
WS_OVERLAPPEDWINDOW | WS_CHILDWINDOW,
0,
parent.as_window().as_raw_window(),
)
} else {
Widget::new(WINDOW_CLASS_NAME, WS_OVERLAPPEDWINDOW, 0, null_mut())
Widget::new(
WINDOW_CLASS_NAME.as_ptr(),
WS_OVERLAPPEDWINDOW,
0,
null_mut(),
)
};
let this = Self { handle };
unsafe { window_use_dark_mode(this.as_raw_window()) };
Expand Down
1 change: 1 addition & 0 deletions winio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ enable_log = ["compio/enable_log", "compio-log/enable_log"]

nightly = ["compio/nightly", "cyper/nightly"]
win32-dark-mode = ["winio-ui-win32/dark-mode"]
win32-ignore-class-conflict = ["winio-ui-win32/ignore-class-conflict"]
objc-static = ["winio-ui-app-kit/objc-static"]