Skip to content

Commit

Permalink
Implement theme override for readme app
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Nov 28, 2024
1 parent 3475c8c commit 98e9d75
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
30 changes: 22 additions & 8 deletions apps/readme/src/readme_application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -42,6 +42,10 @@ impl ReadmeApplication {
self.inner.add_window(window_config);
}

fn window_mut(&mut self) -> &mut View<HtmlDocument> {
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));

Expand All @@ -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));
}
}

Expand All @@ -85,10 +97,12 @@ impl ApplicationHandler<BlitzEvent> 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(),
_ => {}
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions packages/blitz-shell/src/stylo_to_winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
22 changes: 20 additions & 2 deletions packages/blitz-shell/src/window.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -52,6 +52,7 @@ pub struct View<Doc: DocumentLike> {
/// 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<Theme>,
keyboard_modifiers: Modifiers,
mouse_pos: (f32, f32),
dom_mouse_pos: (f32, f32),
Expand Down Expand Up @@ -94,6 +95,7 @@ impl<Doc: DocumentLike> View<Doc> {
doc: config.doc,
viewport,
devtools: Default::default(),
theme_override: None,
mouse_pos: Default::default(),
dom_mouse_pos: Default::default(),

Expand All @@ -113,6 +115,22 @@ impl<Doc: DocumentLike> View<Doc> {
self.poll();
self.request_redraw();
}

pub fn theme_override(&self) -> Option<Theme> {
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<Theme>) {
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<Doc: DocumentLike> View<Doc> {
Expand Down Expand Up @@ -285,7 +303,7 @@ impl<Doc: DocumentLike> View<Doc> {

// 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();
}

Expand Down

0 comments on commit 98e9d75

Please sign in to comment.