Ability to force a theme on Windows (#1666)
This commit is contained in:
parent
5700359a61
commit
6ddee9a8ac
9 changed files with 63 additions and 37 deletions
|
|
@ -14,6 +14,8 @@ use winapi::{
|
|||
um::{libloaderapi, uxtheme, winuser},
|
||||
};
|
||||
|
||||
use crate::window::Theme;
|
||||
|
||||
lazy_static! {
|
||||
static ref WIN10_BUILD_VERSION: Option<DWORD> = {
|
||||
// FIXME: RtlGetVersion is a documented windows API,
|
||||
|
|
@ -70,11 +72,14 @@ lazy_static! {
|
|||
static ref LIGHT_THEME_NAME: Vec<u16> = widestring("");
|
||||
}
|
||||
|
||||
/// Attempt to set dark mode on a window, if necessary.
|
||||
/// Returns true if dark mode was set, false if not.
|
||||
pub fn try_dark_mode(hwnd: HWND) -> bool {
|
||||
/// Attempt to set a theme on a window, if necessary.
|
||||
/// Returns the theme that was picked
|
||||
pub fn try_theme(hwnd: HWND, preferred_theme: Option<Theme>) -> Theme {
|
||||
if *DARK_MODE_SUPPORTED {
|
||||
let is_dark_mode = should_use_dark_mode();
|
||||
let is_dark_mode = match preferred_theme {
|
||||
Some(theme) => theme == Theme::Dark,
|
||||
None => should_use_dark_mode(),
|
||||
};
|
||||
|
||||
let theme_name = if is_dark_mode {
|
||||
DARK_THEME_NAME.as_ptr()
|
||||
|
|
@ -84,10 +89,12 @@ pub fn try_dark_mode(hwnd: HWND) -> bool {
|
|||
|
||||
let status = unsafe { uxtheme::SetWindowTheme(hwnd, theme_name as _, std::ptr::null()) };
|
||||
|
||||
status == S_OK && set_dark_mode_for_window(hwnd, is_dark_mode)
|
||||
} else {
|
||||
false
|
||||
if status == S_OK && set_dark_mode_for_window(hwnd, is_dark_mode) {
|
||||
return Theme::Dark;
|
||||
}
|
||||
}
|
||||
|
||||
Theme::Light
|
||||
}
|
||||
|
||||
fn set_dark_mode_for_window(hwnd: HWND, is_dark_mode: bool) -> bool {
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ use crate::{
|
|||
event_loop::{ControlFlow, EventLoopClosed, EventLoopWindowTarget as RootELW},
|
||||
monitor::MonitorHandle as RootMonitorHandle,
|
||||
platform_impl::platform::{
|
||||
dark_mode::try_dark_mode,
|
||||
dark_mode::try_theme,
|
||||
dpi::{become_dpi_aware, dpi_to_scale_factor, enable_non_client_dpi_scaling},
|
||||
drop_handler::FileDropHandler,
|
||||
event::{self, handle_extended_keys, process_key_params, vkey_to_winit_vkey},
|
||||
|
|
@ -1874,20 +1874,20 @@ unsafe extern "system" fn public_window_callback<T: 'static>(
|
|||
winuser::WM_SETTINGCHANGE => {
|
||||
use crate::event::WindowEvent::ThemeChanged;
|
||||
|
||||
let is_dark_mode = try_dark_mode(window);
|
||||
let mut window_state = subclass_input.window_state.lock();
|
||||
let changed = window_state.is_dark_mode != is_dark_mode;
|
||||
let preferred_theme = subclass_input.window_state.lock().preferred_theme;
|
||||
|
||||
if changed {
|
||||
use crate::window::Theme::*;
|
||||
let theme = if is_dark_mode { Dark } else { Light };
|
||||
if preferred_theme == None {
|
||||
let new_theme = try_theme(window, preferred_theme);
|
||||
let mut window_state = subclass_input.window_state.lock();
|
||||
|
||||
window_state.is_dark_mode = is_dark_mode;
|
||||
mem::drop(window_state);
|
||||
subclass_input.send_event(Event::WindowEvent {
|
||||
window_id: RootWindowId(WindowId(window)),
|
||||
event: ThemeChanged(theme),
|
||||
});
|
||||
if window_state.current_theme != new_theme {
|
||||
window_state.current_theme = new_theme;
|
||||
mem::drop(window_state);
|
||||
subclass_input.send_event(Event::WindowEvent {
|
||||
window_id: RootWindowId(WindowId(window)),
|
||||
event: ThemeChanged(new_theme),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
commctrl::DefSubclassProc(window, msg, wparam, lparam)
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ pub use self::icon::WinIcon as PlatformIcon;
|
|||
|
||||
use crate::event::DeviceId as RootDeviceId;
|
||||
use crate::icon::Icon;
|
||||
use crate::window::Theme;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct PlatformSpecificWindowBuilderAttributes {
|
||||
|
|
@ -20,6 +21,7 @@ pub struct PlatformSpecificWindowBuilderAttributes {
|
|||
pub taskbar_icon: Option<Icon>,
|
||||
pub no_redirection_bitmap: bool,
|
||||
pub drag_and_drop: bool,
|
||||
pub preferred_theme: Option<Theme>,
|
||||
}
|
||||
|
||||
impl Default for PlatformSpecificWindowBuilderAttributes {
|
||||
|
|
@ -29,6 +31,7 @@ impl Default for PlatformSpecificWindowBuilderAttributes {
|
|||
taskbar_icon: None,
|
||||
no_redirection_bitmap: false,
|
||||
drag_and_drop: true,
|
||||
preferred_theme: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ use crate::{
|
|||
icon::Icon,
|
||||
monitor::MonitorHandle as RootMonitorHandle,
|
||||
platform_impl::platform::{
|
||||
dark_mode::try_dark_mode,
|
||||
dark_mode::try_theme,
|
||||
dpi::{dpi_to_scale_factor, hwnd_dpi},
|
||||
drop_handler::FileDropHandler,
|
||||
event_loop::{self, EventLoopWindowTarget, DESTROY_MSG_ID},
|
||||
|
|
@ -43,7 +43,7 @@ use crate::{
|
|||
window_state::{CursorFlags, SavedWindow, WindowFlags, WindowState},
|
||||
PlatformSpecificWindowBuilderAttributes, WindowId,
|
||||
},
|
||||
window::{CursorIcon, Fullscreen, UserAttentionType, WindowAttributes},
|
||||
window::{CursorIcon, Fullscreen, Theme, UserAttentionType, WindowAttributes},
|
||||
};
|
||||
|
||||
/// The Win32 implementation of the main `Window` object.
|
||||
|
|
@ -653,8 +653,8 @@ impl Window {
|
|||
}
|
||||
|
||||
#[inline]
|
||||
pub fn is_dark_mode(&self) -> bool {
|
||||
self.window_state.lock().is_dark_mode
|
||||
pub fn theme(&self) -> Theme {
|
||||
self.window_state.lock().current_theme
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -764,14 +764,15 @@ unsafe fn init<T: 'static>(
|
|||
// If the system theme is dark, we need to set the window theme now
|
||||
// before we update the window flags (and possibly show the
|
||||
// window for the first time).
|
||||
let dark_mode = try_dark_mode(real_window.0);
|
||||
let current_theme = try_theme(real_window.0, pl_attribs.preferred_theme);
|
||||
|
||||
let window_state = {
|
||||
let window_state = WindowState::new(
|
||||
&attributes,
|
||||
pl_attribs.taskbar_icon,
|
||||
scale_factor,
|
||||
dark_mode,
|
||||
current_theme,
|
||||
pl_attribs.preferred_theme,
|
||||
);
|
||||
let window_state = Arc::new(Mutex::new(window_state));
|
||||
WindowState::set_window_flags(window_state.lock(), real_window.0, |f| *f = window_flags);
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ use crate::{
|
|||
event::ModifiersState,
|
||||
icon::Icon,
|
||||
platform_impl::platform::{event_loop, util},
|
||||
window::{CursorIcon, Fullscreen, WindowAttributes},
|
||||
window::{CursorIcon, Fullscreen, Theme, WindowAttributes},
|
||||
};
|
||||
use parking_lot::MutexGuard;
|
||||
use std::{io, ptr};
|
||||
|
|
@ -31,7 +31,8 @@ pub struct WindowState {
|
|||
|
||||
pub modifiers_state: ModifiersState,
|
||||
pub fullscreen: Option<Fullscreen>,
|
||||
pub is_dark_mode: bool,
|
||||
pub current_theme: Theme,
|
||||
pub preferred_theme: Option<Theme>,
|
||||
pub high_surrogate: Option<u16>,
|
||||
window_flags: WindowFlags,
|
||||
}
|
||||
|
|
@ -101,7 +102,8 @@ impl WindowState {
|
|||
attributes: &WindowAttributes,
|
||||
taskbar_icon: Option<Icon>,
|
||||
scale_factor: f64,
|
||||
is_dark_mode: bool,
|
||||
current_theme: Theme,
|
||||
preferred_theme: Option<Theme>,
|
||||
) -> WindowState {
|
||||
WindowState {
|
||||
mouse: MouseProperties {
|
||||
|
|
@ -122,7 +124,8 @@ impl WindowState {
|
|||
|
||||
modifiers_state: ModifiersState::default(),
|
||||
fullscreen: None,
|
||||
is_dark_mode,
|
||||
current_theme,
|
||||
preferred_theme,
|
||||
high_surrogate: None,
|
||||
window_flags: WindowFlags::empty(),
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue