Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(windows): always allow dark theme for app and window #928

Merged
merged 2 commits into from
May 16, 2024
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
5 changes: 5 additions & 0 deletions .changes/windows-theme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": "patch"
---

On Windows, always allow dark theme for app and window through `AllowDarkModeForApp` and `AllowDarkModeForWindow`, which fixes an issue when calling `IsDarkModeAllowedForWindow()`
20 changes: 4 additions & 16 deletions src/platform_impl/windows/dark_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,7 @@ static DARK_MODE_SUPPORTED: Lazy<bool> = Lazy::new(|| {
}
});

/// Attempts to set dark mode for the app
pub fn try_app_theme(preferred_theme: Option<Theme>) {
if *DARK_MODE_SUPPORTED {
let is_dark_mode = match preferred_theme {
Some(theme) => theme == Theme::Dark,
None => should_use_dark_mode(),
};

allow_dark_mode_for_app(is_dark_mode);
refresh_immersive_color_policy_state();
}
}

fn allow_dark_mode_for_app(is_dark_mode: bool) {
pub fn allow_dark_mode_for_app(is_dark_mode: bool) {
const UXTHEME_ALLOWDARKMODEFORAPP_ORDINAL: u16 = 135;
type AllowDarkModeForApp = unsafe extern "system" fn(bool) -> bool;
static ALLOW_DARK_MODE_FOR_APP: Lazy<Option<AllowDarkModeForApp>> = Lazy::new(|| unsafe {
Expand Down Expand Up @@ -104,6 +91,8 @@ fn allow_dark_mode_for_app(is_dark_mode: bool) {
unsafe { _set_preferred_app_mode(mode) };
}
}

refresh_immersive_color_policy_state();
}

fn refresh_immersive_color_policy_state() {
Expand Down Expand Up @@ -141,7 +130,6 @@ pub fn try_window_theme(hwnd: HWND, preferred_theme: Option<Theme>) -> Theme {
false => Theme::Light,
};

allow_dark_mode_for_window(hwnd, is_dark_mode);
refresh_titlebar_theme_color(hwnd, is_dark_mode);

theme
Expand All @@ -150,7 +138,7 @@ pub fn try_window_theme(hwnd: HWND, preferred_theme: Option<Theme>) -> Theme {
}
}

fn allow_dark_mode_for_window(hwnd: HWND, is_dark_mode: bool) {
pub fn allow_dark_mode_for_window(hwnd: HWND, is_dark_mode: bool) {
const UXTHEME_ALLOWDARKMODEFORWINDOW_ORDINAL: u16 = 133;
type AllowDarkModeForWindow = unsafe extern "system" fn(HWND, bool) -> bool;
static ALLOW_DARK_MODE_FOR_WINDOW: Lazy<Option<AllowDarkModeForWindow>> = Lazy::new(|| unsafe {
Expand Down
4 changes: 2 additions & 2 deletions src/platform_impl/windows/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use crate::{
keyboard::{KeyCode, ModifiersState},
monitor::MonitorHandle as RootMonitorHandle,
platform_impl::platform::{
dark_mode::{try_app_theme, try_window_theme},
dark_mode::try_window_theme,
dpi::{become_dpi_aware, dpi_to_scale_factor, enable_non_client_dpi_scaling},
keyboard::is_msg_keyboard_related,
keyboard_layout::LAYOUT_CACHE,
Expand Down Expand Up @@ -184,7 +184,7 @@ impl<T: 'static> EventLoop<T> {

let thread_msg_target = create_event_target_window();

try_app_theme(attributes.preferred_theme);
super::dark_mode::allow_dark_mode_for_app(true);

let send_thread_msg_target = thread_msg_target;
thread::spawn(move || wait_thread(thread_id, send_thread_msg_target));
Expand Down
2 changes: 2 additions & 0 deletions src/platform_impl/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,8 @@ unsafe fn init<T: 'static>(
return Err(os_error!(OsError::IoError(io::Error::last_os_error())));
}

super::dark_mode::allow_dark_mode_for_window(handle, true);

WindowWrapper(handle)
};

Expand Down
Loading