Skip to content

Commit

Permalink
Add support for Window::theme on the web (#2687)
Browse files Browse the repository at this point in the history
  • Loading branch information
tronical authored Feb 20, 2023
1 parent 0f89aac commit a31f71e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ And please only add new entries to the top of this list, right below the `# Unre
- Implement `HasRawDisplayHandle` for `EventLoop`.
- On macOS, set resize increments only for live resizes.
- On Wayland, fix rare crash on DPI change
- Web: Added support for `Window::theme`.

# 0.28.1

Expand Down
15 changes: 14 additions & 1 deletion src/platform_impl/web/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,20 @@ impl Window {

#[inline]
pub fn theme(&self) -> Option<Theme> {
None
web_sys::window()
.and_then(|window| {
window
.match_media("(prefers-color-scheme: dark)")
.ok()
.flatten()
})
.map(|media_query_list| {
if media_query_list.matches() {
Theme::Dark
} else {
Theme::Light
}
})
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ impl Window {
/// ## Platform-specific
///
/// - **macOS:** This is an app-wide setting.
/// - **iOS / Android / Web / Wayland / x11 / Orbital:** Unsupported.
/// - **iOS / Android / Wayland / x11 / Orbital:** Unsupported.
#[inline]
pub fn theme(&self) -> Option<Theme> {
self.window.theme()
Expand Down

0 comments on commit a31f71e

Please sign in to comment.