Add ThemeChanged variant to Event in iced_sentinel

This commit is contained in:
Héctor Ramón Jiménez 2024-02-27 15:19:26 +01:00
parent 7f7c5ea337
commit c856d2b513
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
13 changed files with 69 additions and 4 deletions

View file

@ -23,6 +23,10 @@ xxhash-rust.workspace = true
palette.workspace = true palette.workspace = true
palette.optional = true palette.optional = true
serde.workspace = true
serde.optional = true
serde.features = ["derive"]
[target.'cfg(windows)'.dependencies] [target.'cfg(windows)'.dependencies]
raw-window-handle.workspace = true raw-window-handle.workspace = true

View file

@ -3,6 +3,7 @@ use palette::rgb::{Srgb, Srgba};
/// A color in the `sRGB` color space. /// A color in the `sRGB` color space.
#[derive(Debug, Clone, Copy, PartialEq, Default)] #[derive(Debug, Clone, Copy, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Color { pub struct Color {
/// Red component, 0.0 - 1.0 /// Red component, 0.0 - 1.0
pub r: f32, pub r: f32,

View file

@ -9,11 +9,12 @@
#![doc( #![doc(
html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg" html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg"
)] )]
#![forbid(unsafe_code, rust_2018_idioms)] #![forbid(unsafe_code)]
#![deny( #![deny(
missing_debug_implementations, missing_debug_implementations,
missing_docs, missing_docs,
unused_results, unused_results,
rust_2018_idioms,
rustdoc::broken_intra_doc_links rustdoc::broken_intra_doc_links
)] )]
pub mod alignment; pub mod alignment;

View file

@ -11,10 +11,11 @@ categories.workspace = true
keywords.workspace = true keywords.workspace = true
[features] [features]
enable = ["iced_sentinel", "once_cell"] enable = ["dep:iced_sentinel", "dep:once_cell"]
[dependencies] [dependencies]
iced_core.workspace = true iced_core.workspace = true
iced_style.workspace = true
iced_sentinel.workspace = true iced_sentinel.workspace = true
iced_sentinel.optional = true iced_sentinel.optional = true

View file

@ -1,4 +1,7 @@
pub use iced_core as core; pub use iced_core as core;
pub use iced_style as style;
use crate::style::theme;
pub use internal::Timer; pub use internal::Timer;
@ -6,6 +9,10 @@ pub fn open_axe() {}
pub fn log_message(_message: &impl std::fmt::Debug) {} pub fn log_message(_message: &impl std::fmt::Debug) {}
pub fn theme_changed(palette: theme::Palette) {
internal::theme_changed(palette);
}
pub fn boot_time() -> Timer { pub fn boot_time() -> Timer {
internal::boot_time() internal::boot_time()
} }
@ -41,6 +48,7 @@ pub fn time(name: impl AsRef<str>) -> Timer {
#[cfg(feature = "enable")] #[cfg(feature = "enable")]
mod internal { mod internal {
use crate::core::time::Instant; use crate::core::time::Instant;
use crate::style::theme;
use iced_sentinel::client::{self, Client}; use iced_sentinel::client::{self, Client};
use iced_sentinel::timing::{self, Timing}; use iced_sentinel::timing::{self, Timing};
@ -48,6 +56,10 @@ mod internal {
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use std::sync::{Mutex, MutexGuard}; use std::sync::{Mutex, MutexGuard};
pub fn theme_changed(palette: theme::Palette) {
lock().sentinel.report_theme_change(palette);
}
pub fn boot_time() -> Timer { pub fn boot_time() -> Timer {
timer(timing::Stage::Boot) timer(timing::Stage::Boot)
} }
@ -120,6 +132,10 @@ mod internal {
#[cfg(not(feature = "enable"))] #[cfg(not(feature = "enable"))]
mod internal { mod internal {
use crate::style::theme;
pub fn theme_changed(_palette: theme::Palette) {}
pub fn boot_time() -> Timer { pub fn boot_time() -> Timer {
Timer Timer
} }

View file

@ -12,6 +12,10 @@ keywords.workspace = true
[dependencies] [dependencies]
iced_core.workspace = true iced_core.workspace = true
iced_style.workspace = true
iced_style.features = ["serde"]
serde_json.workspace = true serde_json.workspace = true
futures.workspace = true futures.workspace = true
log.workspace = true log.workspace = true

View file

@ -1,3 +1,4 @@
use crate::style::theme;
use crate::{Input, Timing, SOCKET_ADDRESS}; use crate::{Input, Timing, SOCKET_ADDRESS};
use tokio::io::{self, AsyncWriteExt}; use tokio::io::{self, AsyncWriteExt};
@ -11,6 +12,10 @@ pub struct Client {
} }
impl Client { impl Client {
pub fn report_theme_change(&mut self, palette: theme::Palette) {
let _ = self.sender.try_send(Input::ThemeChanged(palette));
}
pub fn report_timing(&mut self, timing: Timing) { pub fn report_timing(&mut self, timing: Timing) {
let _ = self.sender.try_send(Input::TimingMeasured(timing)); let _ = self.sender.try_send(Input::TimingMeasured(timing));
} }

View file

@ -1,8 +1,10 @@
pub use iced_core as core; pub use iced_core as core;
pub use iced_style as style;
pub mod client; pub mod client;
pub mod timing; pub mod timing;
use crate::style::theme;
use crate::timing::Timing; use crate::timing::Timing;
use futures::future; use futures::future;
@ -18,6 +20,7 @@ pub const SOCKET_ADDRESS: &str = "127.0.0.1:9167";
pub enum Input { pub enum Input {
Connected(Version), Connected(Version),
TimingMeasured(Timing), TimingMeasured(Timing),
ThemeChanged(theme::Palette),
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -25,6 +28,7 @@ pub enum Event {
Connected(Version), Connected(Version),
Disconnected, Disconnected,
TimingMeasured(Timing), TimingMeasured(Timing),
ThemeChanged(theme::Palette),
} }
pub fn run() -> impl Stream<Item = Event> { pub fn run() -> impl Stream<Item = Event> {
@ -87,6 +91,9 @@ async fn receive(
Input::TimingMeasured(timing) => { Input::TimingMeasured(timing) => {
Event::TimingMeasured(timing) Event::TimingMeasured(timing)
} }
Input::ThemeChanged(palette) => {
Event::ThemeChanged(palette)
}
}, },
)) ))
} }

View file

@ -10,9 +10,16 @@ homepage.workspace = true
categories.workspace = true categories.workspace = true
keywords.workspace = true keywords.workspace = true
[features]
serde = ["dep:serde", "iced_core/serde"]
[dependencies] [dependencies]
iced_core.workspace = true iced_core.workspace = true
iced_core.features = ["palette"] iced_core.features = ["palette"]
palette.workspace = true palette.workspace = true
once_cell.workspace = true once_cell.workspace = true
serde.workspace = true
serde.optional = true
serde.features = ["derive"]

View file

@ -1,5 +1,6 @@
//! Change the appearance of an application. //! Change the appearance of an application.
use iced_core::Color; use crate::core::Color;
use crate::theme;
/// A set of rules that dictate the style of an application. /// A set of rules that dictate the style of an application.
pub trait StyleSheet { pub trait StyleSheet {
@ -10,6 +11,17 @@ pub trait StyleSheet {
/// ///
/// [`Style`]: Self::Style /// [`Style`]: Self::Style
fn appearance(&self, style: &Self::Style) -> Appearance; fn appearance(&self, style: &Self::Style) -> Appearance;
/// Returns the [`theme::Palette`] of the application, if any.
///
/// This may be used by other parts of the `iced` runtime to
/// try to match the style of your application.
///
/// For instance, the Iced Axe uses this [`theme::Palette`] to
/// automatically style itself using your application's colors.
fn palette(&self) -> Option<theme::Palette> {
None
}
} }
/// The appearance of an application. /// The appearance of an application.

View file

@ -7,11 +7,12 @@
#![doc( #![doc(
html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg" html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg"
)] )]
#![forbid(unsafe_code, rust_2018_idioms)] #![forbid(unsafe_code)]
#![deny( #![deny(
unused_results, unused_results,
missing_docs, missing_docs,
unused_results, unused_results,
rust_2018_idioms,
rustdoc::broken_intra_doc_links rustdoc::broken_intra_doc_links
)] )]
pub use iced_core as core; pub use iced_core as core;

View file

@ -8,6 +8,7 @@ use palette::{FromColor, Hsl, Mix};
/// A color palette. /// A color palette.
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Palette { pub struct Palette {
/// The background [`Color`] of the [`Palette`]. /// The background [`Color`] of the [`Palette`].
pub background: Color, pub background: Color,

View file

@ -2,6 +2,7 @@ use crate::application::{self, StyleSheet as _};
use crate::conversion; use crate::conversion;
use crate::core::mouse; use crate::core::mouse;
use crate::core::{Color, Size}; use crate::core::{Color, Size};
use crate::debug;
use crate::graphics::Viewport; use crate::graphics::Viewport;
use crate::Application; use crate::Application;
@ -37,6 +38,8 @@ where
let theme = application.theme(); let theme = application.theme();
let appearance = theme.appearance(&application.style()); let appearance = theme.appearance(&application.style());
let _ = theme.palette().map(debug::theme_changed);
let viewport = { let viewport = {
let physical_size = window.inner_size(); let physical_size = window.inner_size();
@ -211,5 +214,7 @@ where
// Update theme and appearance // Update theme and appearance
self.theme = application.theme(); self.theme = application.theme();
self.appearance = self.theme.appearance(&application.style()); self.appearance = self.theme.appearance(&application.style());
let _ = self.theme.palette().map(debug::theme_changed);
} }
} }