From 98e9d75022b75cdf8df1d1b95d2dc8c097b426a6 Mon Sep 17 00:00:00 2001 From: Nico Burns Date: Thu, 28 Nov 2024 19:44:32 +1300 Subject: [PATCH] Implement theme override for readme app --- apps/readme/src/readme_application.rs | 30 ++++++++++++++++------ packages/blitz-shell/src/stylo_to_winit.rs | 7 +++++ packages/blitz-shell/src/window.rs | 22 ++++++++++++++-- 3 files changed, 49 insertions(+), 10 deletions(-) diff --git a/apps/readme/src/readme_application.rs b/apps/readme/src/readme_application.rs index 436cb5ce..282e3784 100644 --- a/apps/readme/src/readme_application.rs +++ b/apps/readme/src/readme_application.rs @@ -2,13 +2,13 @@ use std::sync::Arc; use blitz_dom::net::Resource; use blitz_html::HtmlDocument; -use blitz_shell::{BlitzApplication, BlitzEvent, WindowConfig}; +use blitz_shell::{BlitzApplication, BlitzEvent, View, WindowConfig}; use blitz_traits::net::NetProvider; use winit::application::ApplicationHandler; use winit::event::{StartCause, WindowEvent}; use winit::event_loop::{ActiveEventLoop, EventLoopProxy}; use winit::keyboard::{KeyCode, PhysicalKey}; -use winit::window::WindowId; +use winit::window::{Theme, WindowId}; use crate::fetch; use crate::markdown::{markdown_to_html, BLITZ_MD_STYLES, GITHUB_MD_STYLES}; @@ -42,6 +42,10 @@ impl ReadmeApplication { self.inner.add_window(window_config); } + fn window_mut(&mut self) -> &mut View { + self.inner.windows.values_mut().next().unwrap() + } + fn reload_document(&mut self) { let (base_url, contents, is_md, _) = self.handle.block_on(fetch(&self.raw_url)); @@ -60,8 +64,16 @@ impl ReadmeApplication { self.net_provider.clone(), None, ); - let window = self.inner.windows.values_mut().next().unwrap(); - window.replace_document(doc); + self.window_mut().replace_document(doc); + } + + fn toggle_theme(&mut self) { + let window = self.window_mut(); + let new_theme = match window.current_theme() { + Theme::Light => Theme::Dark, + Theme::Dark => Theme::Light, + }; + window.set_theme_override(Some(new_theme)); } } @@ -85,10 +97,12 @@ impl ApplicationHandler for ReadmeApplication { event: WindowEvent, ) { if let WindowEvent::KeyboardInput { event, .. } = &event { - if event.state.is_pressed() - && matches!(event.physical_key, PhysicalKey::Code(KeyCode::KeyR)) - { - self.reload_document(); + if !event.state.is_pressed() { + match event.physical_key { + PhysicalKey::Code(KeyCode::KeyR) => self.reload_document(), + PhysicalKey::Code(KeyCode::KeyT) => self.toggle_theme(), + _ => {} + } } } diff --git a/packages/blitz-shell/src/stylo_to_winit.rs b/packages/blitz-shell/src/stylo_to_winit.rs index 049e265d..9e3db664 100644 --- a/packages/blitz-shell/src/stylo_to_winit.rs +++ b/packages/blitz-shell/src/stylo_to_winit.rs @@ -54,3 +54,10 @@ pub(crate) fn theme_to_color_scheme(theme: Theme) -> ColorScheme { Theme::Dark => ColorScheme::Dark, } } + +pub(crate) fn color_scheme_to_theme(scheme: ColorScheme) -> Theme { + match scheme { + ColorScheme::Light => Theme::Light, + ColorScheme::Dark => Theme::Dark, + } +} diff --git a/packages/blitz-shell/src/window.rs b/packages/blitz-shell/src/window.rs index 7ff8da05..696fd33b 100644 --- a/packages/blitz-shell/src/window.rs +++ b/packages/blitz-shell/src/window.rs @@ -1,6 +1,6 @@ use crate::accessibility::AccessibilityState; use crate::event::{create_waker, BlitzEvent}; -use crate::stylo_to_winit::{self, theme_to_color_scheme}; +use crate::stylo_to_winit::{self, color_scheme_to_theme, theme_to_color_scheme}; use blitz_dom::events::{EventData, RendererEvent}; use blitz_dom::{DocumentLike, Viewport}; use blitz_renderer_vello::Renderer; @@ -52,6 +52,7 @@ pub struct View { /// The state of the keyboard modifiers (ctrl, shift, etc). Winit/Tao don't track these for us so we /// need to store them in order to have access to them when processing keypress events pub devtools: Devtools, + theme_override: Option, keyboard_modifiers: Modifiers, mouse_pos: (f32, f32), dom_mouse_pos: (f32, f32), @@ -94,6 +95,7 @@ impl View { doc: config.doc, viewport, devtools: Default::default(), + theme_override: None, mouse_pos: Default::default(), dom_mouse_pos: Default::default(), @@ -113,6 +115,22 @@ impl View { self.poll(); self.request_redraw(); } + + pub fn theme_override(&self) -> Option { + self.theme_override + } + + pub fn current_theme(&self) -> Theme { + color_scheme_to_theme(self.viewport.color_scheme) + } + + pub fn set_theme_override(&mut self, theme: Option) { + self.theme_override = theme; + let theme = theme.or(self.window.theme()).unwrap_or(Theme::Light); + self.viewport.color_scheme = theme_to_color_scheme(theme); + self.kick_viewport(); + self.request_redraw(); + } } impl View { @@ -285,7 +303,7 @@ impl View { // Theme events WindowEvent::ThemeChanged(theme) => { - self.viewport.color_scheme = theme_to_color_scheme(theme); + self.viewport.color_scheme = theme_to_color_scheme(self.theme_override.unwrap_or(theme)); self.kick_viewport(); }