Added per-window theme support.

This commit is contained in:
Bingus 2023-03-15 18:20:38 -07:00
parent ce4b2c93d9
commit 41836dd80d
No known key found for this signature in database
GPG key ID: 5F84D2AA40A9F170
4 changed files with 35 additions and 19 deletions

View file

@ -1,5 +1,4 @@
use iced::alignment::{self, Alignment}; use iced::alignment::{self, Alignment};
use iced::{executor, time};
use iced::keyboard; use iced::keyboard;
use iced::multi_window::Application; use iced::multi_window::Application;
use iced::theme::{self, Theme}; use iced::theme::{self, Theme};
@ -8,6 +7,7 @@ use iced::widget::{
button, column, container, pick_list, row, scrollable, text, text_input, button, column, container, pick_list, row, scrollable, text, text_input,
}; };
use iced::window; use iced::window;
use iced::{executor, time};
use iced::{Color, Command, Element, Length, Settings, Size, Subscription}; use iced::{Color, Command, Element, Length, Settings, Size, Subscription};
use iced_lazy::responsive; use iced_lazy::responsive;
use iced_native::{event, subscription, Event}; use iced_native::{event, subscription, Event};
@ -34,6 +34,7 @@ struct Example {
struct Window { struct Window {
title: String, title: String,
scale: f64, scale: f64,
theme: Theme,
panes: pane_grid::State<Pane>, panes: pane_grid::State<Pane>,
focus: Option<pane_grid::Pane>, focus: Option<pane_grid::Pane>,
} }
@ -77,6 +78,7 @@ impl Application for Example {
focus: None, focus: None,
title: String::from("Default window"), title: String::from("Default window"),
scale: 1.0, scale: 1.0,
theme: Theme::default(),
}; };
( (
@ -167,7 +169,10 @@ impl Application for Example {
let _ = self.windows.remove(&id); let _ = self.windows.remove(&id);
return window::close(id); return window::close(id);
} }
WindowMessage::Resized(pane_grid::ResizeEvent { split, ratio }) => { WindowMessage::Resized(pane_grid::ResizeEvent {
split,
ratio,
}) => {
let window = self.windows.get_mut(&id).unwrap(); let window = self.windows.get_mut(&id).unwrap();
window.panes.resize(&split, ratio); window.panes.resize(&split, ratio);
} }
@ -177,7 +182,8 @@ impl Application for Example {
pane.is_moving = false; pane.is_moving = false;
if let Some(window) = self.windows.get_mut(&selected.0) { if let Some(window) = self.windows.get_mut(&selected.0) {
let (&first_pane, _) = window.panes.iter().next().unwrap(); let (&first_pane, _) =
window.panes.iter().next().unwrap();
let result = let result =
window.panes.split(pane.axis, &first_pane, pane); window.panes.split(pane.axis, &first_pane, pane);
@ -205,8 +211,16 @@ impl Application for Example {
let window = Window { let window = Window {
panes, panes,
focus: None, focus: None,
title: format!("New window ({})", self.windows.len()), title: format!(
"New window ({})",
self.windows.len()
),
scale: 1.0 + (self.windows.len() as f64 / 10.0), scale: 1.0 + (self.windows.len() as f64 / 10.0),
theme: if self.windows.len() % 2 == 0 {
Theme::Light
} else {
Theme::Dark
},
}; };
let window_id = window::Id::new(self.windows.len()); let window_id = window::Id::new(self.windows.len());
@ -215,15 +229,12 @@ impl Application for Example {
} }
} }
WindowMessage::Dragged(pane_grid::DragEvent::Dropped { WindowMessage::Dragged(pane_grid::DragEvent::Dropped {
pane, pane,
target, target,
}) => { }) => {
let window = self.windows.get_mut(&id).unwrap(); let window = self.windows.get_mut(&id).unwrap();
window.panes.swap(&pane, &target); window.panes.swap(&pane, &target);
} }
// WindowMessage::Dragged(pane_grid::DragEvent::Picked { pane }) => {
// println!("Picked {pane:?}");
// }
WindowMessage::Dragged(_) => {} WindowMessage::Dragged(_) => {}
WindowMessage::TogglePin(pane) => { WindowMessage::TogglePin(pane) => {
let window = self.windows.get_mut(&id).unwrap(); let window = self.windows.get_mut(&id).unwrap();
@ -273,9 +284,9 @@ impl Application for Example {
match event { match event {
Event::Keyboard(keyboard::Event::KeyPressed { Event::Keyboard(keyboard::Event::KeyPressed {
modifiers, modifiers,
key_code, key_code,
}) if modifiers.command() => { }) if modifiers.command() => {
handle_hotkey(key_code).map(|message| { handle_hotkey(key_code).map(|message| {
Message::Window(window::Id::new(0usize), message) Message::Window(window::Id::new(0usize), message)
}) })
@ -391,6 +402,10 @@ impl Application for Example {
fn scale_factor(&self, window: Id) -> f64 { fn scale_factor(&self, window: Id) -> f64 {
self.windows.get(&window).map(|w| w.scale).unwrap_or(1.0) self.windows.get(&window).map(|w| w.scale).unwrap_or(1.0)
} }
fn theme(&self, window: Id) -> Theme {
self.windows.get(&window).expect("Window not found!").theme.clone()
}
} }
const PANE_ID_COLOR_UNFOCUSED: Color = Color::from_rgb( const PANE_ID_COLOR_UNFOCUSED: Color = Color::from_rgb(

View file

@ -107,7 +107,8 @@ pub trait Application: Sized {
/// Returns the current [`Theme`] of the [`Application`]. /// Returns the current [`Theme`] of the [`Application`].
/// ///
/// [`Theme`]: Self::Theme /// [`Theme`]: Self::Theme
fn theme(&self) -> Self::Theme { #[allow(unused_variables)]
fn theme(&self, window: window::Id) -> Self::Theme {
Self::Theme::default() Self::Theme::default()
} }
@ -229,8 +230,8 @@ where
self.0.title(window) self.0.title(window)
} }
fn theme(&self) -> A::Theme { fn theme(&self, window: window::Id) -> A::Theme {
self.0.theme() self.0.theme(window)
} }
fn style(&self) -> <A::Theme as StyleSheet>::Style { fn style(&self) -> <A::Theme as StyleSheet>::Style {

View file

@ -112,7 +112,7 @@ where
fn title(&self, window: window::Id) -> String; fn title(&self, window: window::Id) -> String;
/// Returns the current [`Theme`] of the [`Application`]. /// Returns the current [`Theme`] of the [`Application`].
fn theme(&self) -> <Self::Renderer as crate::Renderer>::Theme; fn theme(&self, window: window::Id) -> <Self::Renderer as crate::Renderer>::Theme;
/// Returns the [`Style`] variation of the [`Theme`]. /// Returns the [`Style`] variation of the [`Theme`].
fn style( fn style(

View file

@ -37,7 +37,7 @@ where
) -> Self { ) -> Self {
let title = application.title(window_id); let title = application.title(window_id);
let scale_factor = application.scale_factor(window_id); let scale_factor = application.scale_factor(window_id);
let theme = application.theme(); let theme = application.theme(window_id);
let appearance = theme.appearance(&application.style()); let appearance = theme.appearance(&application.style());
let viewport = { let viewport = {
@ -212,7 +212,7 @@ where
} }
// Update theme and appearance // Update theme and appearance
self.theme = application.theme(); self.theme = application.theme(window_id);
self.appearance = self.theme.appearance(&application.style()); self.appearance = self.theme.appearance(&application.style());
} }
} }