Skip to content

Commit

Permalink
enhance(windows): apply dark mode to menus and other controls (#751)
Browse files Browse the repository at this point in the history
* fix(windows): Apply dark mode to menus

* refactor dark mode logic, and add app-wide theme api

* fix docs build

---------

Co-authored-by: ahodesuka <[email protected]>
Co-authored-by: Amr Bashir <[email protected]>
  • Loading branch information
3 people authored Aug 14, 2023
1 parent 5c0bfb6 commit 6df56c2
Show file tree
Hide file tree
Showing 8 changed files with 285 additions and 130 deletions.
5 changes: 5 additions & 0 deletions .changes/apply-dark-mode-to-menus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": patch
---

On Windows, apply dark mode app-wide to some controls like context menus.
5 changes: 5 additions & 0 deletions .changes/windows-app-wide-theme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": "patch"
---

On Windows, add `EventLoopBuilderExtWindows::with_theme` to control the app-wide theme.
13 changes: 8 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ url = "2"
image = "0.24"
env_logger = "0.10"

[target."cfg(any(target_os = \"android\", target_os = \"windows\"))".dependencies]
once_cell = "1"

[target."cfg(target_os = \"android\")".dependencies]
jni = "0.21"
ndk = "0.7"
ndk-sys = "0.4"
ndk-context = "0.1"
once_cell = "1"
tao-macros = { version = "0.1.0", path = "./tao-macros" }

[target."cfg(any(target_os = \"ios\", target_os = \"macos\"))".dependencies]
Expand All @@ -76,9 +78,9 @@ unicode-segmentation = "1.10"
image = { version = "0.24", default-features = false }
windows-implement = "0.48.0"

[target."cfg(target_os = \"windows\")".dependencies.windows]
version = "0.48.0"
features = [
[target."cfg(target_os = \"windows\")".dependencies.windows]
version = "0.48.0"
features = [
"implement",
"Win32_Devices_HumanInterfaceDevice",
"Win32_Foundation",
Expand All @@ -95,6 +97,7 @@ windows-implement = "0.48.0"
"Win32_System_SystemServices",
"Win32_System_Threading",
"Win32_System_WindowsProgramming",
"Win32_System_SystemInformation",
"Win32_UI_Accessibility",
"Win32_UI_Controls",
"Win32_UI_HiDpi",
Expand All @@ -104,7 +107,7 @@ windows-implement = "0.48.0"
"Win32_UI_Input_Touch",
"Win32_UI_Shell",
"Win32_UI_TextServices",
"Win32_UI_WindowsAndMessaging"
"Win32_UI_WindowsAndMessaging",
]

[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
Expand Down
20 changes: 20 additions & 0 deletions src/platform/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ pub trait EventLoopBuilderExtWindows {
fn with_msg_hook<F>(&mut self, callback: F) -> &mut Self
where
F: FnMut(*const std::ffi::c_void) -> bool + 'static;

/// Forces a theme or uses the system settings if `None` was provided.
///
/// This will only affect some controls like context menus.
///
/// ## Note
///
/// Since this setting is app-wide, using [`WindowBuilder::with_theme`]
/// will not change the affected controls for that specific window,
/// so it is recommended to always use the same theme used for this app-wide setting
/// or use `None` so it automatically uses the theme of this method
/// or falls back to the system preference.
fn with_theme(&mut self, theme: Option<Theme>) -> &mut Self;
}

impl<T> EventLoopBuilderExtWindows for EventLoopBuilder<T> {
Expand All @@ -110,6 +123,13 @@ impl<T> EventLoopBuilderExtWindows for EventLoopBuilder<T> {
self.platform_specific.msg_hook = Some(Box::new(callback));
self
}

#[inline]

fn with_theme(&mut self, theme: Option<Theme>) -> &mut Self {
self.platform_specific.preferred_theme = theme;
self
}
}

/// Additional methods on `Window` that are specific to Windows.
Expand Down
Loading

0 comments on commit 6df56c2

Please sign in to comment.