From faa53647cc83577e1ecb81a450c948b3fa4203e0 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 30 Mar 2024 15:57:12 +0100 Subject: Replace `xxhash-rust` with `rustc-hash` --- core/Cargo.toml | 2 +- core/src/hasher.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'core') diff --git a/core/Cargo.toml b/core/Cargo.toml index 32d233ee..d3529d98 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -21,10 +21,10 @@ log.workspace = true num-traits.workspace = true once_cell.workspace = true palette.workspace = true +rustc-hash.workspace = true smol_str.workspace = true thiserror.workspace = true web-time.workspace = true -xxhash-rust.workspace = true dark-light.workspace = true dark-light.optional = true diff --git a/core/src/hasher.rs b/core/src/hasher.rs index a13d78af..13180e41 100644 --- a/core/src/hasher.rs +++ b/core/src/hasher.rs @@ -1,7 +1,7 @@ /// The hasher used to compare layouts. #[allow(missing_debug_implementations)] // Doesn't really make sense to have debug on the hasher state anyways. #[derive(Default)] -pub struct Hasher(xxhash_rust::xxh3::Xxh3); +pub struct Hasher(rustc_hash::FxHasher); impl core::hash::Hasher for Hasher { fn write(&mut self, bytes: &[u8]) { -- cgit From 6216c513d5e5853bf1d43342094e91a74981f4f2 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 1 Apr 2024 11:30:01 +0200 Subject: Use generic `Content` in `Text` to avoid reallocation in `fill_text` --- core/src/renderer/null.rs | 6 +++--- core/src/text.rs | 6 +++--- core/src/text/paragraph.rs | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) (limited to 'core') diff --git a/core/src/renderer/null.rs b/core/src/renderer/null.rs index c26ce1a5..1caf71b3 100644 --- a/core/src/renderer/null.rs +++ b/core/src/renderer/null.rs @@ -67,7 +67,7 @@ impl text::Renderer for () { fn fill_text( &mut self, - _paragraph: Text<'_, Self::Font>, + _paragraph: Text, _position: Point, _color: Color, _clip_bounds: Rectangle, @@ -78,11 +78,11 @@ impl text::Renderer for () { impl text::Paragraph for () { type Font = Font; - fn with_text(_text: Text<'_, Self::Font>) -> Self {} + fn with_text(_text: Text<&str>) -> Self {} fn resize(&mut self, _new_bounds: Size) {} - fn compare(&self, _text: Text<'_, Self::Font>) -> text::Difference { + fn compare(&self, _text: Text<&str>) -> text::Difference { text::Difference::None } diff --git a/core/src/text.rs b/core/src/text.rs index edef79c2..3f1d2c77 100644 --- a/core/src/text.rs +++ b/core/src/text.rs @@ -16,9 +16,9 @@ use std::hash::{Hash, Hasher}; /// A paragraph. #[derive(Debug, Clone, Copy)] -pub struct Text<'a, Font> { +pub struct Text { /// The content of the paragraph. - pub content: &'a str, + pub content: Content, /// The bounds of the paragraph. pub bounds: Size, @@ -219,7 +219,7 @@ pub trait Renderer: crate::Renderer { /// [`Color`]. fn fill_text( &mut self, - text: Text<'_, Self::Font>, + text: Text, position: Point, color: Color, clip_bounds: Rectangle, diff --git a/core/src/text/paragraph.rs b/core/src/text/paragraph.rs index de1fb74d..8ff04015 100644 --- a/core/src/text/paragraph.rs +++ b/core/src/text/paragraph.rs @@ -8,14 +8,14 @@ pub trait Paragraph: Sized + Default { type Font: Copy + PartialEq; /// Creates a new [`Paragraph`] laid out with the given [`Text`]. - fn with_text(text: Text<'_, Self::Font>) -> Self; + fn with_text(text: Text<&str, Self::Font>) -> Self; /// Lays out the [`Paragraph`] with some new boundaries. fn resize(&mut self, new_bounds: Size); /// Compares the [`Paragraph`] with some desired [`Text`] and returns the /// [`Difference`]. - fn compare(&self, text: Text<'_, Self::Font>) -> Difference; + fn compare(&self, text: Text<&str, Self::Font>) -> Difference; /// Returns the horizontal alignment of the [`Paragraph`]. fn horizontal_alignment(&self) -> alignment::Horizontal; @@ -35,7 +35,7 @@ pub trait Paragraph: Sized + Default { fn grapheme_position(&self, line: usize, index: usize) -> Option; /// Updates the [`Paragraph`] to match the given [`Text`], if needed. - fn update(&mut self, text: Text<'_, Self::Font>) { + fn update(&mut self, text: Text<&str, Self::Font>) { match self.compare(text) { Difference::None => {} Difference::Bounds => { -- cgit From f5bcfec8211c04c4b05f63d01d52d3e5d2cc123e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 1 Apr 2024 11:59:46 +0200 Subject: Use `rustc-hash` for most of our `HashMap` and `HashSet` instances --- core/src/image.rs | 5 +++-- core/src/lib.rs | 2 -- core/src/svg.rs | 5 +++-- 3 files changed, 6 insertions(+), 6 deletions(-) (limited to 'core') diff --git a/core/src/image.rs b/core/src/image.rs index 32b95f03..dc74e5c1 100644 --- a/core/src/image.rs +++ b/core/src/image.rs @@ -1,6 +1,7 @@ //! Load and draw raster graphics. -use crate::{Hasher, Rectangle, Size}; +use crate::{Rectangle, Size}; +use rustc_hash::FxHasher; use std::hash::{Hash, Hasher as _}; use std::path::PathBuf; use std::sync::Arc; @@ -50,7 +51,7 @@ impl Handle { } fn from_data(data: Data) -> Handle { - let mut hasher = Hasher::default(); + let mut hasher = FxHasher::default(); data.hash(&mut hasher); Handle { diff --git a/core/src/lib.rs b/core/src/lib.rs index d076413e..832b2d2d 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -41,7 +41,6 @@ mod background; mod color; mod content_fit; mod element; -mod hasher; mod length; mod padding; mod pixels; @@ -64,7 +63,6 @@ pub use element::Element; pub use event::Event; pub use font::Font; pub use gradient::Gradient; -pub use hasher::Hasher; pub use layout::Layout; pub use length::Length; pub use overlay::Overlay; diff --git a/core/src/svg.rs b/core/src/svg.rs index ab207cca..0106e0c2 100644 --- a/core/src/svg.rs +++ b/core/src/svg.rs @@ -1,6 +1,7 @@ //! Load and draw vector graphics. -use crate::{Color, Hasher, Rectangle, Size}; +use crate::{Color, Rectangle, Size}; +use rustc_hash::FxHasher; use std::borrow::Cow; use std::hash::{Hash, Hasher as _}; use std::path::PathBuf; @@ -30,7 +31,7 @@ impl Handle { } fn from_data(data: Data) -> Handle { - let mut hasher = Hasher::default(); + let mut hasher = FxHasher::default(); data.hash(&mut hasher); Handle { -- cgit From 1d83e59e8ab51d403baee0daa878644c6a37be3f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 1 Apr 2024 21:36:08 +0200 Subject: Specialize `widget::text` helper with custom `IntoContent` trait --- core/src/widget/text.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 3 deletions(-) (limited to 'core') diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs index 12f6956a..e05eb0d9 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -21,7 +21,7 @@ where Theme: Catalog, Renderer: text::Renderer, { - content: Cow<'a, str>, + content: Content<'a>, size: Option, line_height: LineHeight, width: Length, @@ -39,9 +39,9 @@ where Renderer: text::Renderer, { /// Create a new fragment of [`Text`] with the given contents. - pub fn new(content: impl Into>) -> Self { + pub fn new(content: impl IntoContent<'a>) -> Self { Text { - content: content.into(), + content: content.into_content(), size: None, line_height: LineHeight::default(), font: None, @@ -366,3 +366,67 @@ impl Catalog for Theme { class(self) } } + +/// The content of a [`Text`] widget. +/// +/// This is just an alias to a string that may be either +/// borrowed or owned. +pub type Content<'a> = Cow<'a, str>; + +/// A trait for converting a value to some text [`Content`]. +pub trait IntoContent<'a> { + /// Converts the value to some text [`Content`]. + fn into_content(self) -> Content<'a>; +} + +impl<'a> IntoContent<'a> for &'a str { + fn into_content(self) -> Content<'a> { + Content::Borrowed(self) + } +} + +impl<'a> IntoContent<'a> for &'a String { + fn into_content(self) -> Content<'a> { + Content::Borrowed(self.as_str()) + } +} + +impl<'a> IntoContent<'a> for String { + fn into_content(self) -> Content<'a> { + Content::Owned(self) + } +} + +macro_rules! into_content { + ($type:ty) => { + impl<'a> IntoContent<'a> for $type { + fn into_content(self) -> Content<'a> { + Content::Owned(self.to_string()) + } + } + + impl<'a> IntoContent<'a> for &$type { + fn into_content(self) -> Content<'a> { + Content::Owned(self.to_string()) + } + } + }; +} + +into_content!(char); +into_content!(bool); + +into_content!(u8); +into_content!(u16); +into_content!(u32); +into_content!(u64); +into_content!(u128); + +into_content!(i8); +into_content!(i16); +into_content!(i32); +into_content!(i64); +into_content!(i128); + +into_content!(f32); +into_content!(f64); -- cgit From 34f799aa3dbb58c49708c1f1d9ee436922db636d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 1 Apr 2024 21:47:55 +0200 Subject: Rename `text::IntoContent` to `IntoFragment` --- core/src/widget/text.rs | 80 ++++++++++++++++++++++++------------------------- 1 file changed, 40 insertions(+), 40 deletions(-) (limited to 'core') diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs index e05eb0d9..123c6a69 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -21,7 +21,7 @@ where Theme: Catalog, Renderer: text::Renderer, { - content: Content<'a>, + fragment: Fragment<'a>, size: Option, line_height: LineHeight, width: Length, @@ -39,9 +39,9 @@ where Renderer: text::Renderer, { /// Create a new fragment of [`Text`] with the given contents. - pub fn new(content: impl IntoContent<'a>) -> Self { + pub fn new(fragment: impl IntoFragment<'a>) -> Self { Text { - content: content.into_content(), + fragment: fragment.into_fragment(), size: None, line_height: LineHeight::default(), font: None, @@ -184,7 +184,7 @@ where limits, self.width, self.height, - &self.content, + &self.fragment, self.line_height, self.size, self.font, @@ -367,66 +367,66 @@ impl Catalog for Theme { } } -/// The content of a [`Text`] widget. +/// A fragment of [`Text`]. /// /// This is just an alias to a string that may be either /// borrowed or owned. -pub type Content<'a> = Cow<'a, str>; +pub type Fragment<'a> = Cow<'a, str>; -/// A trait for converting a value to some text [`Content`]. -pub trait IntoContent<'a> { - /// Converts the value to some text [`Content`]. - fn into_content(self) -> Content<'a>; +/// A trait for converting a value to some text [`Fragment`]. +pub trait IntoFragment<'a> { + /// Converts the value to some text [`Fragment`]. + fn into_fragment(self) -> Fragment<'a>; } -impl<'a> IntoContent<'a> for &'a str { - fn into_content(self) -> Content<'a> { - Content::Borrowed(self) +impl<'a> IntoFragment<'a> for &'a str { + fn into_fragment(self) -> Fragment<'a> { + Fragment::Borrowed(self) } } -impl<'a> IntoContent<'a> for &'a String { - fn into_content(self) -> Content<'a> { - Content::Borrowed(self.as_str()) +impl<'a> IntoFragment<'a> for &'a String { + fn into_fragment(self) -> Fragment<'a> { + Fragment::Borrowed(self.as_str()) } } -impl<'a> IntoContent<'a> for String { - fn into_content(self) -> Content<'a> { - Content::Owned(self) +impl<'a> IntoFragment<'a> for String { + fn into_fragment(self) -> Fragment<'a> { + Fragment::Owned(self) } } -macro_rules! into_content { +macro_rules! into_fragment { ($type:ty) => { - impl<'a> IntoContent<'a> for $type { - fn into_content(self) -> Content<'a> { - Content::Owned(self.to_string()) + impl<'a> IntoFragment<'a> for $type { + fn into_fragment(self) -> Fragment<'a> { + Fragment::Owned(self.to_string()) } } - impl<'a> IntoContent<'a> for &$type { - fn into_content(self) -> Content<'a> { - Content::Owned(self.to_string()) + impl<'a> IntoFragment<'a> for &$type { + fn into_fragment(self) -> Fragment<'a> { + Fragment::Owned(self.to_string()) } } }; } -into_content!(char); -into_content!(bool); +into_fragment!(char); +into_fragment!(bool); -into_content!(u8); -into_content!(u16); -into_content!(u32); -into_content!(u64); -into_content!(u128); +into_fragment!(u8); +into_fragment!(u16); +into_fragment!(u32); +into_fragment!(u64); +into_fragment!(u128); -into_content!(i8); -into_content!(i16); -into_content!(i32); -into_content!(i64); -into_content!(i128); +into_fragment!(i8); +into_fragment!(i16); +into_fragment!(i32); +into_fragment!(i64); +into_fragment!(i128); -into_content!(f32); -into_content!(f64); +into_fragment!(f32); +into_fragment!(f64); -- cgit From dee43d5f66c97dddffebbc02c4a87674fb2c13b9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 2 Apr 2024 09:24:22 +0200 Subject: Implement `IntoFragment` for `usize` and `isize` --- core/src/widget/text.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'core') diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs index 123c6a69..53591e41 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -421,12 +421,14 @@ into_fragment!(u16); into_fragment!(u32); into_fragment!(u64); into_fragment!(u128); +into_fragment!(usize); into_fragment!(i8); into_fragment!(i16); into_fragment!(i32); into_fragment!(i64); into_fragment!(i128); +into_fragment!(isize); into_fragment!(f32); into_fragment!(f64); -- cgit From 99a904112ca111f2ab0e60e30b6c369741b1653b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 2 Apr 2024 10:08:10 +0200 Subject: Implement `IntoFragment` for `Fragment` --- core/src/widget/text.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'core') diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs index 53591e41..f1f0b345 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -379,6 +379,18 @@ pub trait IntoFragment<'a> { fn into_fragment(self) -> Fragment<'a>; } +impl<'a> IntoFragment<'a> for Fragment<'a> { + fn into_fragment(self) -> Fragment<'a> { + self + } +} + +impl<'a, 'b> IntoFragment<'a> for &'a Fragment<'b> { + fn into_fragment(self) -> Fragment<'a> { + Fragment::Borrowed(self) + } +} + impl<'a> IntoFragment<'a> for &'a str { fn into_fragment(self) -> Fragment<'a> { Fragment::Borrowed(self) -- cgit From b05e61f5c8ae61c9f3c7cc08cded53901ebbccfd Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 3 Apr 2024 21:07:54 +0200 Subject: Redesign `iced_wgpu` layering architecture --- core/src/rectangle.rs | 27 ++++++++++++++++----------- core/src/renderer.rs | 8 ++++---- core/src/renderer/null.rs | 4 ++-- core/src/size.rs | 14 ++++++++++++++ core/src/transformation.rs | 6 ++++++ 5 files changed, 42 insertions(+), 17 deletions(-) (limited to 'core') diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs index c1c2eeac..45acd5ac 100644 --- a/core/src/rectangle.rs +++ b/core/src/rectangle.rs @@ -16,24 +16,29 @@ pub struct Rectangle { pub height: T, } -impl Rectangle { - /// Creates a new [`Rectangle`] with its top-left corner in the given - /// [`Point`] and with the provided [`Size`]. - pub fn new(top_left: Point, size: Size) -> Self { +impl Rectangle +where + T: Default, +{ + /// Creates a new [`Rectangle`] with its top-left corner at the origin + /// and with the provided [`Size`]. + pub fn with_size(size: Size) -> Self { Self { - x: top_left.x, - y: top_left.y, + x: T::default(), + y: T::default(), width: size.width, height: size.height, } } +} - /// Creates a new [`Rectangle`] with its top-left corner at the origin - /// and with the provided [`Size`]. - pub fn with_size(size: Size) -> Self { +impl Rectangle { + /// Creates a new [`Rectangle`] with its top-left corner in the given + /// [`Point`] and with the provided [`Size`]. + pub fn new(top_left: Point, size: Size) -> Self { Self { - x: 0.0, - y: 0.0, + x: top_left.x, + y: top_left.y, width: size.width, height: size.height, } diff --git a/core/src/renderer.rs b/core/src/renderer.rs index 6712314e..f5ef8f68 100644 --- a/core/src/renderer.rs +++ b/core/src/renderer.rs @@ -9,7 +9,7 @@ use crate::{ /// A component that can be used by widgets to draw themselves on a screen. pub trait Renderer { /// Starts recording a new layer. - fn start_layer(&mut self); + fn start_layer(&mut self, bounds: Rectangle); /// Ends recording a new layer. /// @@ -20,13 +20,13 @@ pub trait Renderer { /// /// The layer will clip its contents to the provided `bounds`. fn with_layer(&mut self, bounds: Rectangle, f: impl FnOnce(&mut Self)) { - self.start_layer(); + self.start_layer(bounds); f(self); self.end_layer(bounds); } /// Starts recording with a new [`Transformation`]. - fn start_transformation(&mut self); + fn start_transformation(&mut self, transformation: Transformation); /// Ends recording a new layer. /// @@ -39,7 +39,7 @@ pub trait Renderer { transformation: Transformation, f: impl FnOnce(&mut Self), ) { - self.start_transformation(); + self.start_transformation(transformation); f(self); self.end_transformation(transformation); } diff --git a/core/src/renderer/null.rs b/core/src/renderer/null.rs index 1caf71b3..f36d19aa 100644 --- a/core/src/renderer/null.rs +++ b/core/src/renderer/null.rs @@ -10,11 +10,11 @@ use crate::{ use std::borrow::Cow; impl Renderer for () { - fn start_layer(&mut self) {} + fn start_layer(&mut self, _bounds: Rectangle) {} fn end_layer(&mut self, _bounds: Rectangle) {} - fn start_transformation(&mut self) {} + fn start_transformation(&mut self, _transformation: Transformation) {} fn end_transformation(&mut self, _transformation: Transformation) {} diff --git a/core/src/size.rs b/core/src/size.rs index 55db759d..c2b5671a 100644 --- a/core/src/size.rs +++ b/core/src/size.rs @@ -99,3 +99,17 @@ where } } } + +impl std::ops::Mul for Size +where + T: std::ops::Mul + Copy, +{ + type Output = Size; + + fn mul(self, rhs: T) -> Self::Output { + Size { + width: self.width * rhs, + height: self.height * rhs, + } + } +} diff --git a/core/src/transformation.rs b/core/src/transformation.rs index b2c488b0..74183147 100644 --- a/core/src/transformation.rs +++ b/core/src/transformation.rs @@ -42,6 +42,12 @@ impl Transformation { } } +impl Default for Transformation { + fn default() -> Self { + Transformation::IDENTITY + } +} + impl Mul for Transformation { type Output = Self; -- cgit From 6d3e1d835e1688fbc58622a03a784ed25ed3f0e1 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 5 Apr 2024 23:59:21 +0200 Subject: Decouple caching from layering and simplify everything --- core/src/rectangle.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'core') diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs index 45acd5ac..446d3769 100644 --- a/core/src/rectangle.rs +++ b/core/src/rectangle.rs @@ -33,9 +33,12 @@ where } impl Rectangle { + /// A rectangle starting at [`Point::ORIGIN`] with infinite width and height. + pub const INFINITE: Self = Self::new(Point::ORIGIN, Size::INFINITY); + /// Creates a new [`Rectangle`] with its top-left corner in the given /// [`Point`] and with the provided [`Size`]. - pub fn new(top_left: Point, size: Size) -> Self { + pub const fn new(top_left: Point, size: Size) -> Self { Self { x: top_left.x, y: top_left.y, -- cgit From c45c79b5d6e25f73dc76a816c08a7041ad971c66 Mon Sep 17 00:00:00 2001 From: David Huculak Date: Sun, 7 Apr 2024 02:20:44 -0400 Subject: add stronger guarantee of readability/contrast for palette background/text color pairs --- core/src/theme/palette.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'core') diff --git a/core/src/theme/palette.rs b/core/src/theme/palette.rs index ca91c248..91543567 100644 --- a/core/src/theme/palette.rs +++ b/core/src/theme/palette.rs @@ -612,11 +612,19 @@ fn mix(a: Color, b: Color, factor: f32) -> Color { fn readable(background: Color, text: Color) -> Color { if is_readable(background, text) { - text - } else if is_dark(background) { + return text; + } + + let fallback = if is_dark(background) { Color::WHITE } else { Color::BLACK + }; + + if is_readable(background, fallback) { + fallback + } else { + fallback.inverse() } } -- cgit From 5cd98f069dea8720bca7748d6c12fa410cbe79b5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 7 Apr 2024 12:42:12 +0200 Subject: Use built-in `[lints]` table in `Cargo.toml` --- core/Cargo.toml | 3 +++ core/src/lib.rs | 7 ------- 2 files changed, 3 insertions(+), 7 deletions(-) (limited to 'core') diff --git a/core/Cargo.toml b/core/Cargo.toml index d3529d98..7bd37021 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -10,6 +10,9 @@ homepage.workspace = true categories.workspace = true keywords.workspace = true +[lints] +workspace = true + [features] auto-detect-theme = ["dep:dark-light"] advanced = [] diff --git a/core/src/lib.rs b/core/src/lib.rs index 832b2d2d..feda4fb4 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -9,13 +9,6 @@ #![doc( html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg" )] -#![forbid(unsafe_code, rust_2018_idioms)] -#![deny( - missing_debug_implementations, - missing_docs, - unused_results, - rustdoc::broken_intra_doc_links -)] pub mod alignment; pub mod border; pub mod clipboard; -- cgit From efa75607baa073aba333ab0bfdee1e490a016097 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 7 Apr 2024 18:52:07 +0200 Subject: Revert "Merge pull request #2376 from Davidster/fix_palette_readable_color_contrast" This reverts commit 63042354fc51884098f88e240f73e689295df31c, reversing changes made to 31d1d5fecbef50fa319cabd5d4194f1e4aaefa21. --- core/src/theme/palette.rs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) (limited to 'core') diff --git a/core/src/theme/palette.rs b/core/src/theme/palette.rs index 91543567..ca91c248 100644 --- a/core/src/theme/palette.rs +++ b/core/src/theme/palette.rs @@ -612,19 +612,11 @@ fn mix(a: Color, b: Color, factor: f32) -> Color { fn readable(background: Color, text: Color) -> Color { if is_readable(background, text) { - return text; - } - - let fallback = if is_dark(background) { + text + } else if is_dark(background) { Color::WHITE } else { Color::BLACK - }; - - if is_readable(background, fallback) { - fallback - } else { - fallback.inverse() } } -- cgit From ee86aea7f298c0bdc72733b47c40270ff38c2ba6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 7 Apr 2024 19:32:49 +0200 Subject: Use `Lch` to choose white text when not readable in `theme::palette` --- core/src/theme/palette.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'core') diff --git a/core/src/theme/palette.rs b/core/src/theme/palette.rs index ca91c248..aca72eb0 100644 --- a/core/src/theme/palette.rs +++ b/core/src/theme/palette.rs @@ -4,7 +4,7 @@ use crate::{color, Color}; use once_cell::sync::Lazy; use palette::color_difference::Wcag21RelativeContrast; use palette::rgb::Rgb; -use palette::{FromColor, Hsl, Mix}; +use palette::{FromColor, Hsl, Lch, Mix}; /// A color palette. #[derive(Debug, Clone, Copy, PartialEq)] @@ -613,7 +613,7 @@ fn mix(a: Color, b: Color, factor: f32) -> Color { fn readable(background: Color, text: Color) -> Color { if is_readable(background, text) { text - } else if is_dark(background) { + } else if to_lch(background).l < 70.0 { Color::WHITE } else { Color::BLACK @@ -635,6 +635,10 @@ fn to_hsl(color: Color) -> Hsl { Hsl::from_color(Rgb::from(color)) } +fn to_lch(color: Color) -> Lch { + Lch::from_color(Rgb::from(color)) +} + fn from_hsl(hsl: Hsl) -> Color { Rgb::from_color(hsl).into() } -- cgit From 72b975ec82660b39f27b6cb015b763caf20e6483 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 7 Apr 2024 19:37:35 +0200 Subject: Pick best contrast between black/white in `theme::palette` --- core/src/theme/palette.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'core') diff --git a/core/src/theme/palette.rs b/core/src/theme/palette.rs index aca72eb0..e0ff397a 100644 --- a/core/src/theme/palette.rs +++ b/core/src/theme/palette.rs @@ -4,7 +4,7 @@ use crate::{color, Color}; use once_cell::sync::Lazy; use palette::color_difference::Wcag21RelativeContrast; use palette::rgb::Rgb; -use palette::{FromColor, Hsl, Lch, Mix}; +use palette::{FromColor, Hsl, Mix}; /// A color palette. #[derive(Debug, Clone, Copy, PartialEq)] @@ -613,10 +613,15 @@ fn mix(a: Color, b: Color, factor: f32) -> Color { fn readable(background: Color, text: Color) -> Color { if is_readable(background, text) { text - } else if to_lch(background).l < 70.0 { - Color::WHITE } else { - Color::BLACK + let white_contrast = relative_contrast(background, Color::WHITE); + let black_contrast = relative_contrast(background, Color::BLACK); + + if white_contrast >= black_contrast { + Color::WHITE + } else { + Color::BLACK + } } } @@ -631,12 +636,15 @@ fn is_readable(a: Color, b: Color) -> bool { a_srgb.has_enhanced_contrast_text(b_srgb) } -fn to_hsl(color: Color) -> Hsl { - Hsl::from_color(Rgb::from(color)) +fn relative_contrast(a: Color, b: Color) -> f32 { + let a_srgb = Rgb::from(a); + let b_srgb = Rgb::from(b); + + a_srgb.relative_contrast(b_srgb) } -fn to_lch(color: Color) -> Lch { - Lch::from_color(Rgb::from(color)) +fn to_hsl(color: Color) -> Hsl { + Hsl::from_color(Rgb::from(color)) } fn from_hsl(hsl: Hsl) -> Color { -- cgit From d922b478156488a7bc03c6e791e05c040d702634 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 8 Apr 2024 15:04:35 +0200 Subject: Reintroduce support for custom primitives in `iced_wgpu` --- core/src/rectangle.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'core') diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs index 446d3769..2ab50137 100644 --- a/core/src/rectangle.rs +++ b/core/src/rectangle.rs @@ -147,13 +147,20 @@ impl Rectangle { } /// Snaps the [`Rectangle`] to __unsigned__ integer coordinates. - pub fn snap(self) -> Rectangle { - Rectangle { + pub fn snap(self) -> Option> { + let width = self.width as u32; + let height = self.height as u32; + + if width < 1 || height < 1 { + return None; + } + + Some(Rectangle { x: self.x as u32, y: self.y as u32, - width: self.width as u32, - height: self.height as u32, - } + width, + height, + }) } /// Expands the [`Rectangle`] a given amount. -- cgit From 6ad5bb3597f640ac329801adf735d633bf0a512f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 9 Apr 2024 22:25:16 +0200 Subject: Port `iced_tiny_skia` to new layering architecture --- core/src/renderer.rs | 8 ++++---- core/src/renderer/null.rs | 8 ++------ core/src/text.rs | 4 ---- 3 files changed, 6 insertions(+), 14 deletions(-) (limited to 'core') diff --git a/core/src/renderer.rs b/core/src/renderer.rs index f5ef8f68..a2785ae8 100644 --- a/core/src/renderer.rs +++ b/core/src/renderer.rs @@ -14,7 +14,7 @@ pub trait Renderer { /// Ends recording a new layer. /// /// The new layer will clip its contents to the provided `bounds`. - fn end_layer(&mut self, bounds: Rectangle); + fn end_layer(&mut self); /// Draws the primitives recorded in the given closure in a new layer. /// @@ -22,7 +22,7 @@ pub trait Renderer { fn with_layer(&mut self, bounds: Rectangle, f: impl FnOnce(&mut Self)) { self.start_layer(bounds); f(self); - self.end_layer(bounds); + self.end_layer(); } /// Starts recording with a new [`Transformation`]. @@ -31,7 +31,7 @@ pub trait Renderer { /// Ends recording a new layer. /// /// The new layer will clip its contents to the provided `bounds`. - fn end_transformation(&mut self, transformation: Transformation); + fn end_transformation(&mut self); /// Applies a [`Transformation`] to the primitives recorded in the given closure. fn with_transformation( @@ -41,7 +41,7 @@ pub trait Renderer { ) { self.start_transformation(transformation); f(self); - self.end_transformation(transformation); + self.end_transformation(); } /// Applies a translation to the primitives recorded in the given closure. diff --git a/core/src/renderer/null.rs b/core/src/renderer/null.rs index f36d19aa..fe38ec87 100644 --- a/core/src/renderer/null.rs +++ b/core/src/renderer/null.rs @@ -7,16 +7,14 @@ use crate::{ Background, Color, Font, Pixels, Point, Rectangle, Size, Transformation, }; -use std::borrow::Cow; - impl Renderer for () { fn start_layer(&mut self, _bounds: Rectangle) {} - fn end_layer(&mut self, _bounds: Rectangle) {} + fn end_layer(&mut self) {} fn start_transformation(&mut self, _transformation: Transformation) {} - fn end_transformation(&mut self, _transformation: Transformation) {} + fn end_transformation(&mut self) {} fn clear(&mut self) {} @@ -45,8 +43,6 @@ impl text::Renderer for () { Pixels(16.0) } - fn load_font(&mut self, _font: Cow<'static, [u8]>) {} - fn fill_paragraph( &mut self, _paragraph: &Self::Paragraph, diff --git a/core/src/text.rs b/core/src/text.rs index 3f1d2c77..b30feae0 100644 --- a/core/src/text.rs +++ b/core/src/text.rs @@ -11,7 +11,6 @@ pub use paragraph::Paragraph; use crate::alignment; use crate::{Color, Pixels, Point, Rectangle, Size}; -use std::borrow::Cow; use std::hash::{Hash, Hasher}; /// A paragraph. @@ -192,9 +191,6 @@ pub trait Renderer: crate::Renderer { /// Returns the default size of [`Text`]. fn default_size(&self) -> Pixels; - /// Loads a [`Self::Font`] from its bytes. - fn load_font(&mut self, font: Cow<'static, [u8]>); - /// Draws the given [`Paragraph`] at the given position and with the given /// [`Color`]. fn fill_paragraph( -- cgit