Skip to content

Commit

Permalink
fix(windows): always allow dark theme for app and window (#928)
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir authored May 16, 2024
1 parent 7e8f75e commit ab792db
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 18 deletions.
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

0 comments on commit ab792db

Please sign in to comment.