Leverage DefaultStyle traits instead of Default

This commit is contained in:
Héctor Ramón Jiménez 2024-03-07 20:11:32 +01:00
parent 44f002f64a
commit 833538ee7f
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
30 changed files with 393 additions and 437 deletions

View file

@ -38,7 +38,7 @@ use std::sync::Arc;
/// can be toggled by pressing `F12`.
pub trait Application: Program
where
Style<Self::Theme>: Default,
Self::Theme: DefaultStyle,
{
/// The data needed to initialize your [`Application`].
type Flags;
@ -64,7 +64,7 @@ where
/// Returns the `Style` variation of the `Theme`.
fn style(&self, theme: &Self::Theme) -> Appearance {
Style::default().resolve(theme)
theme.default_style()
}
/// Returns the event `Subscription` for the current state of the
@ -104,39 +104,19 @@ pub struct Appearance {
pub text_color: Color,
}
/// The style of an [`Application`].
#[derive(Debug, PartialEq, Eq)]
pub struct Style<Theme>(pub fn(&Theme) -> Appearance);
impl<Theme> Style<Theme> {
/// Resolves the [`Style`] with the given `Theme` to produce
/// an [`Appearance`].
pub fn resolve(self, theme: &Theme) -> Appearance {
(self.0)(theme)
}
}
impl<Theme> Clone for Style<Theme> {
fn clone(&self) -> Self {
*self
}
}
impl<Theme> Copy for Style<Theme> {}
impl<Theme> From<fn(&Theme) -> Appearance> for Style<Theme> {
fn from(f: fn(&Theme) -> Appearance) -> Self {
Style(f)
}
}
impl Default for Style<Theme> {
fn default() -> Self {
Style(default)
}
}
/// The default style of an [`Application`].
pub trait DefaultStyle {
/// Returns the default style of an [`Application`].
fn default_style(&self) -> Appearance;
}
impl DefaultStyle for Theme {
fn default_style(&self) -> Appearance {
default(self)
}
}
/// The default [`Appearance`] of an [`Application`] with the built-in [`Theme`].
pub fn default(theme: &Theme) -> Appearance {
let palette = theme.extended_palette();
@ -156,7 +136,7 @@ where
A: Application + 'static,
E: Executor + 'static,
C: Compositor<Renderer = A::Renderer> + 'static,
Style<A::Theme>: Default,
A::Theme: DefaultStyle,
{
use futures::task;
use futures::Future;
@ -340,7 +320,7 @@ async fn run_instance<A, E, C>(
A: Application + 'static,
E: Executor + 'static,
C: Compositor<Renderer = A::Renderer> + 'static,
Style<A::Theme>: Default,
A::Theme: DefaultStyle,
{
use futures::stream::StreamExt;
use winit::event;
@ -663,7 +643,7 @@ pub fn build_user_interface<'a, A: Application>(
debug: &mut Debug,
) -> UserInterface<'a, A::Message, A::Theme, A::Renderer>
where
Style<A::Theme>: Default,
A::Theme: DefaultStyle,
{
debug.view_started();
let view = application.view();
@ -694,7 +674,7 @@ pub fn update<A: Application, C, E: Executor>(
window: &winit::window::Window,
) where
C: Compositor<Renderer = A::Renderer> + 'static,
Style<A::Theme>: Default,
A::Theme: DefaultStyle,
{
for message in messages.drain(..) {
debug.log_message(&message);
@ -745,7 +725,7 @@ pub fn run_command<A, C, E>(
A: Application,
E: Executor,
C: Compositor<Renderer = A::Renderer> + 'static,
Style<A::Theme>: Default,
A::Theme: DefaultStyle,
{
use crate::runtime::command;
use crate::runtime::system;

View file

@ -14,7 +14,7 @@ use winit::window::Window;
#[allow(missing_debug_implementations)]
pub struct State<A: Application>
where
application::Style<A::Theme>: Default,
A::Theme: application::DefaultStyle,
{
title: String,
scale_factor: f64,
@ -29,7 +29,7 @@ where
impl<A: Application> State<A>
where
application::Style<A::Theme>: Default,
A::Theme: application::DefaultStyle,
{
/// Creates a new [`State`] for the provided [`Application`] and window.
pub fn new(application: &A, window: &Window) -> Self {

View file

@ -22,7 +22,7 @@ use crate::runtime::user_interface::{self, UserInterface};
use crate::runtime::Debug;
use crate::{Clipboard, Error, Proxy, Settings};
pub use crate::application::{default, Appearance, Style};
pub use crate::application::{default, Appearance, DefaultStyle};
use std::collections::HashMap;
use std::mem::ManuallyDrop;
@ -42,7 +42,7 @@ use std::time::Instant;
/// can be toggled by pressing `F12`.
pub trait Application: Program
where
Style<Self::Theme>: Default,
Self::Theme: DefaultStyle,
{
/// The data needed to initialize your [`Application`].
type Flags;
@ -68,7 +68,7 @@ where
/// Returns the `Style` variation of the `Theme`.
fn style(&self, theme: &Self::Theme) -> Appearance {
Style::default().resolve(theme)
theme.default_style()
}
/// Returns the event `Subscription` for the current state of the
@ -109,7 +109,7 @@ where
A: Application + 'static,
E: Executor + 'static,
C: Compositor<Renderer = A::Renderer> + 'static,
Style<A::Theme>: Default,
A::Theme: DefaultStyle,
{
use winit::event_loop::EventLoopBuilder;
@ -350,7 +350,7 @@ async fn run_instance<A, E, C>(
A: Application + 'static,
E: Executor + 'static,
C: Compositor<Renderer = A::Renderer> + 'static,
Style<A::Theme>: Default,
A::Theme: DefaultStyle,
{
use winit::event;
use winit::event_loop::ControlFlow;
@ -820,7 +820,7 @@ fn build_user_interface<'a, A: Application>(
id: window::Id,
) -> UserInterface<'a, A::Message, A::Theme, A::Renderer>
where
Style<A::Theme>: Default,
A::Theme: DefaultStyle,
{
debug.view_started();
let view = application.view(id);
@ -848,7 +848,7 @@ fn update<A: Application, C, E: Executor>(
ui_caches: &mut HashMap<window::Id, user_interface::Cache>,
) where
C: Compositor<Renderer = A::Renderer> + 'static,
Style<A::Theme>: Default,
A::Theme: DefaultStyle,
{
for message in messages.drain(..) {
debug.log_message(&message);
@ -891,7 +891,7 @@ fn run_command<A, C, E>(
A: Application,
E: Executor,
C: Compositor<Renderer = A::Renderer> + 'static,
Style<A::Theme>: Default,
A::Theme: DefaultStyle,
{
use crate::runtime::clipboard;
use crate::runtime::system;
@ -1218,7 +1218,7 @@ pub fn build_user_interfaces<'a, A: Application, C: Compositor>(
) -> HashMap<window::Id, UserInterface<'a, A::Message, A::Theme, A::Renderer>>
where
C: Compositor<Renderer = A::Renderer>,
Style<A::Theme>: Default,
A::Theme: DefaultStyle,
{
cached_user_interfaces
.drain()

View file

@ -11,7 +11,7 @@ use winit::window::Window;
/// The state of a multi-windowed [`Application`].
pub struct State<A: Application>
where
multi_window::Style<A::Theme>: Default,
A::Theme: multi_window::DefaultStyle,
{
title: String,
scale_factor: f64,
@ -25,7 +25,7 @@ where
impl<A: Application> Debug for State<A>
where
multi_window::Style<A::Theme>: Default,
A::Theme: multi_window::DefaultStyle,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("multi_window::State")
@ -41,7 +41,7 @@ where
impl<A: Application> State<A>
where
multi_window::Style<A::Theme>: Default,
A::Theme: multi_window::DefaultStyle,
{
/// Creates a new [`State`] for the provided [`Application`]'s `window`.
pub fn new(

View file

@ -2,7 +2,7 @@ use crate::core::mouse;
use crate::core::window::Id;
use crate::core::{Point, Size};
use crate::graphics::Compositor;
use crate::multi_window::{Application, State, Style};
use crate::multi_window::{Application, DefaultStyle, State};
use std::collections::BTreeMap;
use std::sync::Arc;
@ -12,7 +12,7 @@ use winit::monitor::MonitorHandle;
pub struct WindowManager<A: Application, C: Compositor>
where
C: Compositor<Renderer = A::Renderer>,
Style<A::Theme>: Default,
A::Theme: DefaultStyle,
{
aliases: BTreeMap<winit::window::WindowId, Id>,
entries: BTreeMap<Id, Window<A, C>>,
@ -22,7 +22,7 @@ impl<A, C> WindowManager<A, C>
where
A: Application,
C: Compositor<Renderer = A::Renderer>,
Style<A::Theme>: Default,
A::Theme: DefaultStyle,
{
pub fn new() -> Self {
Self {
@ -108,7 +108,7 @@ impl<A, C> Default for WindowManager<A, C>
where
A: Application,
C: Compositor<Renderer = A::Renderer>,
Style<A::Theme>: Default,
A::Theme: DefaultStyle,
{
fn default() -> Self {
Self::new()
@ -120,7 +120,7 @@ pub struct Window<A, C>
where
A: Application,
C: Compositor<Renderer = A::Renderer>,
Style<A::Theme>: Default,
A::Theme: DefaultStyle,
{
pub raw: Arc<winit::window::Window>,
pub state: State<A>,
@ -135,7 +135,7 @@ impl<A, C> Window<A, C>
where
A: Application,
C: Compositor<Renderer = A::Renderer>,
Style<A::Theme>: Default,
A::Theme: DefaultStyle,
{
pub fn position(&self) -> Option<Point> {
self.raw