From ccd02c525bd5751d828bb6ab7b656d88526b2b55 Mon Sep 17 00:00:00 2001 From: Jedsek Date: Fri, 20 Jan 2023 13:46:09 +0800 Subject: Update main.rs The app should not render qr_code when data is empty --- examples/qr_code/src/main.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/examples/qr_code/src/main.rs b/examples/qr_code/src/main.rs index 6f487e4c..27b41fdc 100644 --- a/examples/qr_code/src/main.rs +++ b/examples/qr_code/src/main.rs @@ -21,10 +21,7 @@ impl Sandbox for QRGenerator { type Message = Message; fn new() -> Self { - QRGenerator { - qr_code: qr_code::State::new("").ok(), - ..Self::default() - } + QRGenerator::default() } fn title(&self) -> String { @@ -36,7 +33,12 @@ impl Sandbox for QRGenerator { Message::DataChanged(mut data) => { data.truncate(100); - self.qr_code = qr_code::State::new(&data).ok(); + self.qr_code = if data.is_empty() { + None + } else { + qr_code::State::new(&data).ok() + } + self.data = data; } } -- cgit From 06fb7e0b51e0750122e6775ab5cec59d6e3431d4 Mon Sep 17 00:00:00 2001 From: Jedsek Date: Fri, 20 Jan 2023 13:56:02 +0800 Subject: Update main.rs --- examples/qr_code/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/qr_code/src/main.rs b/examples/qr_code/src/main.rs index 27b41fdc..42b58a21 100644 --- a/examples/qr_code/src/main.rs +++ b/examples/qr_code/src/main.rs @@ -37,7 +37,7 @@ impl Sandbox for QRGenerator { None } else { qr_code::State::new(&data).ok() - } + }; self.data = data; } -- cgit From c7d8467c46e519b28f8f630061e7d55fb3cd3f8b Mon Sep 17 00:00:00 2001 From: sushigiri <117967760+sushigiri@users.noreply.github.com> Date: Mon, 6 Feb 2023 09:05:07 -0700 Subject: Accept FnOnce instead of Fn in canvas cache draw Use FnOnce in `draw` function signature instead of `Fn`, permitting the use of iterators and other one-time functions. --- graphics/src/widget/canvas/cache.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/graphics/src/widget/canvas/cache.rs b/graphics/src/widget/canvas/cache.rs index 49873ac9..52217bbb 100644 --- a/graphics/src/widget/canvas/cache.rs +++ b/graphics/src/widget/canvas/cache.rs @@ -49,7 +49,11 @@ impl Cache { /// Otherwise, the previously stored [`Geometry`] will be returned. The /// [`Cache`] is not cleared in this case. In other words, it will keep /// returning the stored [`Geometry`] if needed. - pub fn draw(&self, bounds: Size, draw_fn: impl Fn(&mut Frame)) -> Geometry { + pub fn draw( + &self, + bounds: Size, + draw_fn: impl FnOnce(&mut Frame), + ) -> Geometry { use std::ops::Deref; if let State::Filled { -- cgit From 92ba26b8a168b1d58b8330d84753c5ab7e116f17 Mon Sep 17 00:00:00 2001 From: Yoo Dongryul Date: Wed, 8 Feb 2023 05:20:46 +0900 Subject: Resize images on README.md (#1659) * Resize images on README.md * hotfix: for mobile layout * fix: set height of both images equally --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b3790478..8ebd0e9c 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,10 @@ A cross-platform GUI library for Rust focused on simplicity and type-safety. Inspired by [Elm]. - + - + -- cgit From 9506fb1181e60e78d92b6f0712d385371966e07f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 11 Feb 2023 03:06:42 +0100 Subject: Hide window until `Renderer` has been initialized --- winit/src/application.rs | 20 +++++++++++++++----- winit/src/settings.rs | 3 +-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/winit/src/application.rs b/winit/src/application.rs index c1836ed9..769fe9dd 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -147,11 +147,15 @@ where #[cfg(target_arch = "wasm32")] let target = settings.window.platform_specific.target.clone(); - let builder = settings.window.into_builder( - &application.title(), - event_loop.primary_monitor(), - settings.id, - ); + let should_be_visible = settings.window.visible; + let builder = settings + .window + .into_builder( + &application.title(), + event_loop.primary_monitor(), + settings.id, + ) + .with_visible(false); log::info!("Window builder: {:#?}", builder); @@ -202,6 +206,7 @@ where control_sender, init_command, window, + should_be_visible, settings.exit_on_close_request, ); @@ -268,6 +273,7 @@ async fn run_instance( mut control_sender: mpsc::UnboundedSender, init_command: Command, window: winit::window::Window, + should_be_visible: bool, exit_on_close_request: bool, ) where A: Application + 'static, @@ -295,6 +301,10 @@ async fn run_instance( physical_size.height, ); + if should_be_visible { + window.set_visible(true); + } + run_command( &application, &mut cache, diff --git a/winit/src/settings.rs b/winit/src/settings.rs index 9bbdef5c..45f38833 100644 --- a/winit/src/settings.rs +++ b/winit/src/settings.rs @@ -114,8 +114,7 @@ impl Window { .with_decorations(self.decorations) .with_transparent(self.transparent) .with_window_icon(self.icon) - .with_always_on_top(self.always_on_top) - .with_visible(self.visible); + .with_always_on_top(self.always_on_top); if let Some(position) = conversion::position( primary_monitor.as_ref(), -- cgit From 8fe851057df0df113aaac6b8dc067b3a75d18d65 Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Fri, 10 Feb 2023 12:08:22 -0800 Subject: fix: lazy widgets overlay is_over --- lazy/src/component.rs | 7 +++++++ lazy/src/lazy.rs | 7 +++++++ lazy/src/responsive.rs | 7 +++++++ 3 files changed, 21 insertions(+) diff --git a/lazy/src/component.rs b/lazy/src/component.rs index 94263274..4c03e2a3 100644 --- a/lazy/src/component.rs +++ b/lazy/src/component.rs @@ -563,4 +563,11 @@ where event_status } + + fn is_over(&self, layout: Layout<'_>, cursor_position: Point) -> bool { + self.with_overlay_maybe(|overlay| { + overlay.is_over(layout, cursor_position) + }) + .unwrap_or_default() + } } diff --git a/lazy/src/lazy.rs b/lazy/src/lazy.rs index 9795afa4..5e909a49 100644 --- a/lazy/src/lazy.rs +++ b/lazy/src/lazy.rs @@ -372,6 +372,13 @@ where }) .unwrap_or(iced_native::event::Status::Ignored) } + + fn is_over(&self, layout: Layout<'_>, cursor_position: Point) -> bool { + self.with_overlay_maybe(|overlay| { + overlay.is_over(layout, cursor_position) + }) + .unwrap_or_default() + } } impl<'a, Message, Renderer, Dependency, View> diff --git a/lazy/src/responsive.rs b/lazy/src/responsive.rs index e399e7b0..93069493 100644 --- a/lazy/src/responsive.rs +++ b/lazy/src/responsive.rs @@ -415,4 +415,11 @@ where }) .unwrap_or(iced_native::event::Status::Ignored) } + + fn is_over(&self, layout: Layout<'_>, cursor_position: Point) -> bool { + self.with_overlay_maybe(|overlay| { + overlay.is_over(layout, cursor_position) + }) + .unwrap_or_default() + } } -- cgit From 41822d0dc55a68d3b515e76a6bc6d2accdc611f4 Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Sat, 11 Feb 2023 09:11:00 -0800 Subject: fix: panic when overlay event processing removes overlay --- native/src/user_interface.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/native/src/user_interface.rs b/native/src/user_interface.rs index 80dece21..2358bff1 100644 --- a/native/src/user_interface.rs +++ b/native/src/user_interface.rs @@ -263,16 +263,16 @@ where } } - let base_cursor = if manual_overlay + let base_cursor = manual_overlay .as_ref() - .unwrap() - .is_over(Layout::new(&layout), cursor_position) - { - // TODO: Type-safe cursor availability - Point::new(-1.0, -1.0) - } else { - cursor_position - }; + .filter(|overlay| { + overlay.is_over(Layout::new(&layout), cursor_position) + }) + .map(|_| { + // TODO: Type-safe cursor availability + Point::new(-1.0, -1.0) + }) + .unwrap_or(cursor_position); self.overlay = Some(layout); -- cgit From 2201f33c65e8bf85265ee1e996b36cbc5b43257e Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Sun, 12 Feb 2023 11:07:08 -0800 Subject: fix: diff widget sub-tree after rebuilding component with operation --- lazy/src/component.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lazy/src/component.rs b/lazy/src/component.rs index 94263274..e29ccca8 100644 --- a/lazy/src/component.rs +++ b/lazy/src/component.rs @@ -311,6 +311,8 @@ where } self.with_element(|element| { + tree.diff_children(std::slice::from_ref(&element)); + element.as_widget().operate( &mut tree.children[0], layout, -- cgit From f318e3fdbf88eee08a48b2d398518f8e456edbfb Mon Sep 17 00:00:00 2001 From: Joakim Frostegård Date: Mon, 13 Feb 2023 22:34:13 +0100 Subject: glow: enable GL_ARB_explicit_attrib_location to fix crashes --- glow/src/program.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/glow/src/program.rs b/glow/src/program.rs index e2155222..95437fcd 100644 --- a/glow/src/program.rs +++ b/glow/src/program.rs @@ -17,17 +17,17 @@ impl Version { ) { // OpenGL 3.0+ (3, 0 | 1 | 2, false) => ( - format!("#version 1{}0", version.minor + 3), + format!("#version 1{}0\n#extension GL_ARB_explicit_attrib_location : enable", version.minor + 3), format!( - "#version 1{}0\n#define HIGHER_THAN_300 1", + "#version 1{}0\n#extension GL_ARB_explicit_attrib_location : enable\n#define HIGHER_THAN_300 1", version.minor + 3 ), ), // OpenGL 3.3+ (3 | 4, _, false) => ( - format!("#version {}{}0", version.major, version.minor), + format!("#version {}{}0\n#extension GL_ARB_explicit_attrib_location : enable", version.major, version.minor), format!( - "#version {}{}0\n#define HIGHER_THAN_300 1", + "#version {}{}0\n#extension GL_ARB_explicit_attrib_location : enable\n#define HIGHER_THAN_300 1", version.major, version.minor ), ), -- cgit From efaa80fb4429e7f9bd2f8a1161be3fa66a3e9e32 Mon Sep 17 00:00:00 2001 From: Casper Storm Date: Thu, 26 Jan 2023 12:20:43 +0100 Subject: Extend pick_list::Handle --- native/src/widget/pick_list.rs | 82 ++++++++++++++++++++++++++++++++++-------- src/widget.rs | 4 ++- 2 files changed, 71 insertions(+), 15 deletions(-) diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs index c2853314..862c4157 100644 --- a/native/src/widget/pick_list.rs +++ b/native/src/widget/pick_list.rs @@ -20,6 +20,43 @@ use std::borrow::Cow; pub use iced_style::pick_list::{Appearance, StyleSheet}; +/// The content of the [`Handle`]. +#[derive(Clone)] +pub struct HandleContent +where + Renderer: text::Renderer, +{ + /// Font that will be used to display the `text`, + pub font: Renderer::Font, + /// Text that will be shown. + pub text: String, + /// Font size of the content. + pub size: Option, +} + +impl std::fmt::Debug for HandleContent +where + Renderer: text::Renderer, +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("HandleIcon") + .field("text", &self.text) + .field("size", &self.size) + .finish() + } +} + +impl PartialEq for HandleContent +where + Renderer: text::Renderer, +{ + fn eq(&self, other: &Self) -> bool { + self.text.eq(&other.text) && self.size.eq(&other.size) + } +} + +impl Eq for HandleContent where Renderer: text::Renderer {} + /// The handle to the right side of the [`PickList`]. #[derive(Debug, Clone, PartialEq, Eq)] pub enum Handle @@ -33,14 +70,14 @@ where /// Font size of the content. size: Option, }, - /// A custom handle. - Custom { - /// Font that will be used to display the `text`, - font: Renderer::Font, - /// Text that will be shown. - text: String, - /// Font size of the content. - size: Option, + /// A custom static handle. + Static(HandleContent), + /// A custom dynamic handle. + Dynamic { + /// The [`HandleContent`] used when [`PickList`] is closed. + closed: HandleContent, + /// The [`HandleContent`] used when [`PickList`] is open. + open: HandleContent, }, /// No handle will be shown. None, @@ -59,16 +96,30 @@ impl Handle where Renderer: text::Renderer, { - fn content(&self) -> Option<(Renderer::Font, String, Option)> { + fn content( + &self, + is_open: bool, + ) -> Option<(Renderer::Font, String, Option)> { match self { Self::Arrow { size } => Some(( Renderer::ICON_FONT, Renderer::ARROW_DOWN_ICON.to_string(), *size, )), - Self::Custom { font, text, size } => { + Self::Static(HandleContent { font, text, size }) => { Some((font.clone(), text.clone(), *size)) } + Self::Dynamic { open, closed } => { + if is_open { + Some((open.font.clone(), open.text.clone(), open.size)) + } else { + Some(( + closed.font.clone(), + closed.text.clone(), + closed.size, + )) + } + } Self::None => None, } } @@ -258,7 +309,7 @@ where fn draw( &self, - _tree: &Tree, + tree: &Tree, renderer: &mut Renderer, theme: &Renderer::Theme, _style: &renderer::Style, @@ -278,6 +329,7 @@ where self.selected.as_ref(), &self.handle, &self.style, + || tree.state.downcast_ref::>(), ) } @@ -568,7 +620,7 @@ where } /// Draws a [`PickList`]. -pub fn draw( +pub fn draw<'a, T, Renderer>( renderer: &mut Renderer, theme: &Renderer::Theme, layout: Layout<'_>, @@ -580,11 +632,13 @@ pub fn draw( selected: Option<&T>, handle: &Handle, style: &::Style, + state: impl FnOnce() -> &'a State, ) where Renderer: text::Renderer, Renderer::Theme: StyleSheet, - T: ToString, + T: ToString + 'a, { + let state = state(); let bounds = layout.bounds(); let is_mouse_over = bounds.contains(cursor_position); let is_selected = selected.is_some(); @@ -605,7 +659,7 @@ pub fn draw( style.background, ); - if let Some((font, text, size)) = handle.content() { + if let Some((font, text, size)) = handle.content(state.is_open) { let size = f32::from(size.unwrap_or_else(|| renderer.default_size())); renderer.fill_text(Text { diff --git a/src/widget.rs b/src/widget.rs index f0058f57..00edd763 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -80,7 +80,9 @@ pub mod pane_grid { pub mod pick_list { //! Display a dropdown list of selectable values. - pub use iced_native::widget::pick_list::{Appearance, Handle, StyleSheet}; + pub use iced_native::widget::pick_list::{ + Appearance, Handle, HandleContent, StyleSheet, + }; /// A widget allowing the selection of a single value from a list of options. pub type PickList<'a, T, Message, Renderer = crate::Renderer> = -- cgit From 5569e12149f9c29345fe404552ce156ef0bebf0e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Feb 2023 06:56:59 +0100 Subject: Rename `HandleContent` to `Icon` and simplify generics --- native/src/widget/pick_list.rs | 74 ++++++++++++------------------------------ src/widget.rs | 2 +- 2 files changed, 21 insertions(+), 55 deletions(-) diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs index 862c4157..17fb00d5 100644 --- a/native/src/widget/pick_list.rs +++ b/native/src/widget/pick_list.rs @@ -20,49 +20,20 @@ use std::borrow::Cow; pub use iced_style::pick_list::{Appearance, StyleSheet}; -/// The content of the [`Handle`]. -#[derive(Clone)] -pub struct HandleContent -where - Renderer: text::Renderer, -{ +/// The icon of a [`Handle`]. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Icon { /// Font that will be used to display the `text`, - pub font: Renderer::Font, + pub font: Font, /// Text that will be shown. pub text: String, /// Font size of the content. pub size: Option, } -impl std::fmt::Debug for HandleContent -where - Renderer: text::Renderer, -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("HandleIcon") - .field("text", &self.text) - .field("size", &self.size) - .finish() - } -} - -impl PartialEq for HandleContent -where - Renderer: text::Renderer, -{ - fn eq(&self, other: &Self) -> bool { - self.text.eq(&other.text) && self.size.eq(&other.size) - } -} - -impl Eq for HandleContent where Renderer: text::Renderer {} - /// The handle to the right side of the [`PickList`]. #[derive(Debug, Clone, PartialEq, Eq)] -pub enum Handle -where - Renderer: text::Renderer, -{ +pub enum Handle { /// Displays an arrow icon (▼). /// /// This is the default. @@ -71,42 +42,36 @@ where size: Option, }, /// A custom static handle. - Static(HandleContent), + Static(Icon), /// A custom dynamic handle. Dynamic { - /// The [`HandleContent`] used when [`PickList`] is closed. - closed: HandleContent, - /// The [`HandleContent`] used when [`PickList`] is open. - open: HandleContent, + /// The [`Icon`] used when [`PickList`] is closed. + closed: Icon, + /// The [`Icon`] used when [`PickList`] is open. + open: Icon, }, /// No handle will be shown. None, } -impl Default for Handle -where - Renderer: text::Renderer, -{ +impl Default for Handle { fn default() -> Self { Self::Arrow { size: None } } } -impl Handle -where - Renderer: text::Renderer, -{ - fn content( +impl Handle { + fn content>( &self, is_open: bool, - ) -> Option<(Renderer::Font, String, Option)> { + ) -> Option<(Font, String, Option)> { match self { Self::Arrow { size } => Some(( Renderer::ICON_FONT, Renderer::ARROW_DOWN_ICON.to_string(), *size, )), - Self::Static(HandleContent { font, text, size }) => { + Self::Static(Icon { font, text, size }) => { Some((font.clone(), text.clone(), *size)) } Self::Dynamic { open, closed } => { @@ -141,7 +106,7 @@ where padding: Padding, text_size: Option, font: Renderer::Font, - handle: Handle, + handle: Handle, style: ::Style, } @@ -212,7 +177,7 @@ where } /// Sets the [`Handle`] of the [`PickList`]. - pub fn handle(mut self, handle: Handle) -> Self { + pub fn handle(mut self, handle: Handle) -> Self { self.handle = handle; self } @@ -630,7 +595,7 @@ pub fn draw<'a, T, Renderer>( font: &Renderer::Font, placeholder: Option<&str>, selected: Option<&T>, - handle: &Handle, + handle: &Handle, style: &::Style, state: impl FnOnce() -> &'a State, ) where @@ -659,7 +624,8 @@ pub fn draw<'a, T, Renderer>( style.background, ); - if let Some((font, text, size)) = handle.content(state.is_open) { + if let Some((font, text, size)) = handle.content::(state.is_open) + { let size = f32::from(size.unwrap_or_else(|| renderer.default_size())); renderer.fill_text(Text { diff --git a/src/widget.rs b/src/widget.rs index 00edd763..5bf7b6b4 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -81,7 +81,7 @@ pub mod pane_grid { pub mod pick_list { //! Display a dropdown list of selectable values. pub use iced_native::widget::pick_list::{ - Appearance, Handle, HandleContent, StyleSheet, + Appearance, Handle, Icon, StyleSheet, }; /// A widget allowing the selection of a single value from a list of options. -- cgit From 0272cac89e2bb3e037d0d9d5caa8b7c584386417 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Feb 2023 06:59:37 +0100 Subject: Move `Handle` and `Icon` definitions in `pick_list` --- native/src/widget/pick_list.rs | 140 ++++++++++++++++++++--------------------- 1 file changed, 70 insertions(+), 70 deletions(-) diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs index 17fb00d5..b9f6f088 100644 --- a/native/src/widget/pick_list.rs +++ b/native/src/widget/pick_list.rs @@ -20,76 +20,6 @@ use std::borrow::Cow; pub use iced_style::pick_list::{Appearance, StyleSheet}; -/// The icon of a [`Handle`]. -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct Icon { - /// Font that will be used to display the `text`, - pub font: Font, - /// Text that will be shown. - pub text: String, - /// Font size of the content. - pub size: Option, -} - -/// The handle to the right side of the [`PickList`]. -#[derive(Debug, Clone, PartialEq, Eq)] -pub enum Handle { - /// Displays an arrow icon (▼). - /// - /// This is the default. - Arrow { - /// Font size of the content. - size: Option, - }, - /// A custom static handle. - Static(Icon), - /// A custom dynamic handle. - Dynamic { - /// The [`Icon`] used when [`PickList`] is closed. - closed: Icon, - /// The [`Icon`] used when [`PickList`] is open. - open: Icon, - }, - /// No handle will be shown. - None, -} - -impl Default for Handle { - fn default() -> Self { - Self::Arrow { size: None } - } -} - -impl Handle { - fn content>( - &self, - is_open: bool, - ) -> Option<(Font, String, Option)> { - match self { - Self::Arrow { size } => Some(( - Renderer::ICON_FONT, - Renderer::ARROW_DOWN_ICON.to_string(), - *size, - )), - Self::Static(Icon { font, text, size }) => { - Some((font.clone(), text.clone(), *size)) - } - Self::Dynamic { open, closed } => { - if is_open { - Some((open.font.clone(), open.text.clone(), open.size)) - } else { - Some(( - closed.font.clone(), - closed.text.clone(), - closed.size, - )) - } - } - Self::None => None, - } - } -} - /// A widget for selecting a single value from a list of options. #[allow(missing_debug_implementations)] pub struct PickList<'a, T, Message, Renderer> @@ -366,6 +296,76 @@ impl Default for State { } } +/// The handle to the right side of the [`PickList`]. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Handle { + /// Displays an arrow icon (▼). + /// + /// This is the default. + Arrow { + /// Font size of the content. + size: Option, + }, + /// A custom static handle. + Static(Icon), + /// A custom dynamic handle. + Dynamic { + /// The [`Icon`] used when [`PickList`] is closed. + closed: Icon, + /// The [`Icon`] used when [`PickList`] is open. + open: Icon, + }, + /// No handle will be shown. + None, +} + +impl Default for Handle { + fn default() -> Self { + Self::Arrow { size: None } + } +} + +impl Handle { + fn content>( + &self, + is_open: bool, + ) -> Option<(Font, String, Option)> { + match self { + Self::Arrow { size } => Some(( + Renderer::ICON_FONT, + Renderer::ARROW_DOWN_ICON.to_string(), + *size, + )), + Self::Static(Icon { font, text, size }) => { + Some((font.clone(), text.clone(), *size)) + } + Self::Dynamic { open, closed } => { + if is_open { + Some((open.font.clone(), open.text.clone(), open.size)) + } else { + Some(( + closed.font.clone(), + closed.text.clone(), + closed.size, + )) + } + } + Self::None => None, + } + } +} + +/// The icon of a [`Handle`]. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Icon { + /// Font that will be used to display the `text`, + pub font: Font, + /// Text that will be shown. + pub text: String, + /// Font size of the content. + pub size: Option, +} + /// Computes the layout of a [`PickList`]. pub fn layout( renderer: &Renderer, -- cgit From bbff06b4621ae586b951564c8cc4bf95608bbb81 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Feb 2023 07:02:33 +0100 Subject: Use `char` instead of `String` for `pick_list::Icon` --- native/src/widget/pick_list.rs | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs index b9f6f088..b96ffac2 100644 --- a/native/src/widget/pick_list.rs +++ b/native/src/widget/pick_list.rs @@ -329,25 +329,21 @@ impl Handle { fn content>( &self, is_open: bool, - ) -> Option<(Font, String, Option)> { + ) -> Option<(Font, char, Option)> { match self { - Self::Arrow { size } => Some(( - Renderer::ICON_FONT, - Renderer::ARROW_DOWN_ICON.to_string(), - *size, - )), - Self::Static(Icon { font, text, size }) => { - Some((font.clone(), text.clone(), *size)) + Self::Arrow { size } => { + Some((Renderer::ICON_FONT, Renderer::ARROW_DOWN_ICON, *size)) } + Self::Static(Icon { + font, + code_point, + size, + }) => Some((font.clone(), *code_point, *size)), Self::Dynamic { open, closed } => { if is_open { - Some((open.font.clone(), open.text.clone(), open.size)) + Some((open.font.clone(), open.code_point, open.size)) } else { - Some(( - closed.font.clone(), - closed.text.clone(), - closed.size, - )) + Some((closed.font.clone(), closed.code_point, closed.size)) } } Self::None => None, @@ -358,10 +354,10 @@ impl Handle { /// The icon of a [`Handle`]. #[derive(Debug, Clone, PartialEq, Eq)] pub struct Icon { - /// Font that will be used to display the `text`, + /// Font that will be used to display the `code_point`, pub font: Font, - /// Text that will be shown. - pub text: String, + /// The unicode code point that will be used as the icon. + pub code_point: char, /// Font size of the content. pub size: Option, } @@ -624,12 +620,13 @@ pub fn draw<'a, T, Renderer>( style.background, ); - if let Some((font, text, size)) = handle.content::(state.is_open) + if let Some((font, code_point, size)) = + handle.content::(state.is_open) { let size = f32::from(size.unwrap_or_else(|| renderer.default_size())); renderer.fill_text(Text { - content: &text, + content: &code_point.to_string(), size, font, color: style.handle_color, -- cgit From fee1ab69e2bab01e5d736540be8b253ff62c3e5e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Feb 2023 07:05:18 +0100 Subject: Provide `State` reference instead of closure to `pick_list::draw` --- native/src/widget/pick_list.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs index b96ffac2..8189dd61 100644 --- a/native/src/widget/pick_list.rs +++ b/native/src/widget/pick_list.rs @@ -224,7 +224,7 @@ where self.selected.as_ref(), &self.handle, &self.style, - || tree.state.downcast_ref::>(), + tree.state.downcast_ref::>(), ) } @@ -593,13 +593,12 @@ pub fn draw<'a, T, Renderer>( selected: Option<&T>, handle: &Handle, style: &::Style, - state: impl FnOnce() -> &'a State, + state: &State, ) where Renderer: text::Renderer, Renderer::Theme: StyleSheet, T: ToString + 'a, { - let state = state(); let bounds = layout.bounds(); let is_mouse_over = bounds.contains(cursor_position); let is_selected = selected.is_some(); -- cgit From 7f1d58aa4591eba40dd6f3e6bc2869dcdc3e9adb Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Feb 2023 07:09:24 +0100 Subject: Inline `Handle::content` for simplicity and efficiency We can avoid downcasting `state` :^) --- native/src/widget/pick_list.rs | 53 ++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 31 deletions(-) diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs index 8189dd61..b1cdfad4 100644 --- a/native/src/widget/pick_list.rs +++ b/native/src/widget/pick_list.rs @@ -224,7 +224,7 @@ where self.selected.as_ref(), &self.handle, &self.style, - tree.state.downcast_ref::>(), + || tree.state.downcast_ref::>(), ) } @@ -325,32 +325,6 @@ impl Default for Handle { } } -impl Handle { - fn content>( - &self, - is_open: bool, - ) -> Option<(Font, char, Option)> { - match self { - Self::Arrow { size } => { - Some((Renderer::ICON_FONT, Renderer::ARROW_DOWN_ICON, *size)) - } - Self::Static(Icon { - font, - code_point, - size, - }) => Some((font.clone(), *code_point, *size)), - Self::Dynamic { open, closed } => { - if is_open { - Some((open.font.clone(), open.code_point, open.size)) - } else { - Some((closed.font.clone(), closed.code_point, closed.size)) - } - } - Self::None => None, - } - } -} - /// The icon of a [`Handle`]. #[derive(Debug, Clone, PartialEq, Eq)] pub struct Icon { @@ -593,7 +567,7 @@ pub fn draw<'a, T, Renderer>( selected: Option<&T>, handle: &Handle, style: &::Style, - state: &State, + state: impl FnOnce() -> &'a State, ) where Renderer: text::Renderer, Renderer::Theme: StyleSheet, @@ -619,9 +593,26 @@ pub fn draw<'a, T, Renderer>( style.background, ); - if let Some((font, code_point, size)) = - handle.content::(state.is_open) - { + let handle = match handle { + Handle::Arrow { size } => { + Some((Renderer::ICON_FONT, Renderer::ARROW_DOWN_ICON, *size)) + } + Handle::Static(Icon { + font, + code_point, + size, + }) => Some((font.clone(), *code_point, *size)), + Handle::Dynamic { open, closed } => { + if state().is_open { + Some((open.font.clone(), open.code_point, open.size)) + } else { + Some((closed.font.clone(), closed.code_point, closed.size)) + } + } + Handle::None => None, + }; + + if let Some((font, code_point, size)) = handle { let size = f32::from(size.unwrap_or_else(|| renderer.default_size())); renderer.fill_text(Text { -- cgit From a9992d131b8cbbc2c73854ecf073ca28c35397e6 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Tue, 14 Feb 2023 11:40:29 -0800 Subject: Pad after setting width Otherwise `width` will set limits back to a fixed width if `Length::Units` is used, overwriting padding. --- native/src/widget/text_input.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index 8755b85d..5bfc918c 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -389,8 +389,8 @@ where let padding = padding.fit(Size::ZERO, limits.max()); let limits = limits - .pad(padding) .width(width) + .pad(padding) .height(Length::Units(text_size)); let mut text = layout::Node::new(limits.resolve(Size::ZERO)); -- cgit From 4fb0be179359097db4aaf6d88dff1367201727dd Mon Sep 17 00:00:00 2001 From: Casper Storm Date: Thu, 16 Feb 2023 14:13:04 +0100 Subject: Added the ability to change checkbox icon --- examples/checkbox/Cargo.toml | 9 ++++++ examples/checkbox/README.md | 12 ++++++++ examples/checkbox/fonts/icons.ttf | Bin 0 -> 1272 bytes examples/checkbox/src/main.rs | 63 ++++++++++++++++++++++++++++++++++++++ native/src/widget/checkbox.rs | 38 ++++++++++++++++++++--- src/widget.rs | 2 +- style/src/checkbox.rs | 4 +-- style/src/theme.rs | 4 +-- 8 files changed, 123 insertions(+), 9 deletions(-) create mode 100644 examples/checkbox/Cargo.toml create mode 100644 examples/checkbox/README.md create mode 100644 examples/checkbox/fonts/icons.ttf create mode 100644 examples/checkbox/src/main.rs diff --git a/examples/checkbox/Cargo.toml b/examples/checkbox/Cargo.toml new file mode 100644 index 00000000..dde8f910 --- /dev/null +++ b/examples/checkbox/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "checkbox" +version = "0.1.0" +authors = ["Casper Rogild Storm"] +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../.." } diff --git a/examples/checkbox/README.md b/examples/checkbox/README.md new file mode 100644 index 00000000..b7f85684 --- /dev/null +++ b/examples/checkbox/README.md @@ -0,0 +1,12 @@ +## Checkbox + +A box that can be checked. + +The __[`main`]__ file contains all the code of the example. + +You can run it with `cargo run`: +``` +cargo run --package pick_list +``` + +[`main`]: src/main.rs diff --git a/examples/checkbox/fonts/icons.ttf b/examples/checkbox/fonts/icons.ttf new file mode 100644 index 00000000..a2046844 Binary files /dev/null and b/examples/checkbox/fonts/icons.ttf differ diff --git a/examples/checkbox/src/main.rs b/examples/checkbox/src/main.rs new file mode 100644 index 00000000..09950bb8 --- /dev/null +++ b/examples/checkbox/src/main.rs @@ -0,0 +1,63 @@ +use iced::widget::{checkbox, column, container}; +use iced::{Element, Font, Length, Sandbox, Settings}; + +const ICON_FONT: Font = Font::External { + name: "Icons", + bytes: include_bytes!("../fonts/icons.ttf"), +}; + +pub fn main() -> iced::Result { + Example::run(Settings::default()) +} + +#[derive(Default)] +struct Example { + default_checkbox: bool, + custom_checkbox: bool, +} + +#[derive(Debug, Clone, Copy)] +enum Message { + DefaultChecked(bool), + CustomChecked(bool), +} + +impl Sandbox for Example { + type Message = Message; + + fn new() -> Self { + Default::default() + } + + fn title(&self) -> String { + String::from("Checkbox - Iced") + } + + fn update(&mut self, message: Message) { + match message { + Message::DefaultChecked(value) => self.default_checkbox = value, + Message::CustomChecked(value) => self.custom_checkbox = value, + } + } + + fn view(&self) -> Element { + let default_checkbox = + checkbox("Default", self.default_checkbox, Message::DefaultChecked); + let custom_checkbox = + checkbox("Custom", self.custom_checkbox, Message::CustomChecked) + .icon(checkbox::Icon { + font: ICON_FONT, + code_point: '\u{e901}', + size: None, + }); + + let content = column![default_checkbox, custom_checkbox].spacing(22); + + container(content) + .width(Length::Fill) + .height(Length::Fill) + .center_x() + .center_y() + .into() + } +} diff --git a/native/src/widget/checkbox.rs b/native/src/widget/checkbox.rs index b46433c2..f6298a8c 100644 --- a/native/src/widget/checkbox.rs +++ b/native/src/widget/checkbox.rs @@ -14,6 +14,17 @@ use crate::{ pub use iced_style::checkbox::{Appearance, StyleSheet}; +/// The icon in a [`Checkbox`]. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Icon { + /// Font that will be used to display the `code_point`, + pub font: Font, + /// The unicode code point that will be used as the icon. + pub code_point: char, + /// Font size of the content. + pub size: Option, +} + /// A box that can be checked. /// /// # Example @@ -45,6 +56,7 @@ where spacing: u16, text_size: Option, font: Renderer::Font, + icon: Icon, style: ::Style, } @@ -80,6 +92,11 @@ where spacing: Self::DEFAULT_SPACING, text_size: None, font: Renderer::Font::default(), + icon: Icon { + font: Renderer::ICON_FONT, + code_point: Renderer::CHECKMARK_ICON, + size: None, + }, style: Default::default(), } } @@ -116,6 +133,12 @@ where self } + /// Sets the [`Icon`] of the [`Checkbox`]. + pub fn icon(mut self, icon: Icon) -> Self { + self.icon = icon; + self + } + /// Sets the style of the [`Checkbox`]. pub fn style( mut self, @@ -243,17 +266,24 @@ where custom_style.background, ); + let Icon { + font, + code_point, + size, + } = &self.icon; + let size = size.map(f32::from).unwrap_or(bounds.height * 0.7); + if self.is_checked { renderer.fill_text(text::Text { - content: &Renderer::CHECKMARK_ICON.to_string(), - font: Renderer::ICON_FONT, - size: bounds.height * 0.7, + content: &code_point.to_string(), + font: font.clone(), + size, bounds: Rectangle { x: bounds.center_x(), y: bounds.center_y(), ..bounds }, - color: custom_style.checkmark_color, + color: custom_style.icon_color, horizontal_alignment: alignment::Horizontal::Center, vertical_alignment: alignment::Vertical::Center, }); diff --git a/src/widget.rs b/src/widget.rs index 5bf7b6b4..7da5b82b 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -31,7 +31,7 @@ pub mod button { pub mod checkbox { //! Show toggle controls using checkboxes. - pub use iced_native::widget::checkbox::{Appearance, StyleSheet}; + pub use iced_native::widget::checkbox::{Appearance, Icon, StyleSheet}; /// A box that can be checked. pub type Checkbox<'a, Message, Renderer = crate::Renderer> = diff --git a/style/src/checkbox.rs b/style/src/checkbox.rs index 827b3225..52b90ec9 100644 --- a/style/src/checkbox.rs +++ b/style/src/checkbox.rs @@ -6,8 +6,8 @@ use iced_core::{Background, Color}; pub struct Appearance { /// The [`Background`] of the checkbox. pub background: Background, - /// The checkmark [`Color`] of the checkbox. - pub checkmark_color: Color, + /// The icon [`Color`] of the checkbox. + pub icon_color: Color, /// The border radius of the checkbox. pub border_radius: f32, /// The border width of the checkbox. diff --git a/style/src/theme.rs b/style/src/theme.rs index 55bfa4ca..4ba4facf 100644 --- a/style/src/theme.rs +++ b/style/src/theme.rs @@ -320,7 +320,7 @@ impl checkbox::StyleSheet for Theme { } fn checkbox_appearance( - checkmark_color: Color, + icon_color: Color, base: palette::Pair, accent: palette::Pair, is_checked: bool, @@ -331,7 +331,7 @@ fn checkbox_appearance( } else { base.color }), - checkmark_color, + icon_color, border_radius: 2.0, border_width: 1.0, border_color: accent.color, -- cgit From 6a683b603d555c9d7f589f99738d4151192b91ef Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Thu, 9 Feb 2023 20:57:26 -0800 Subject: scrollable: provide ID to operation.container --- native/src/widget/scrollable.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/native/src/widget/scrollable.rs b/native/src/widget/scrollable.rs index 82286036..2de722e4 100644 --- a/native/src/widget/scrollable.rs +++ b/native/src/widget/scrollable.rs @@ -208,14 +208,17 @@ where operation.scrollable(state, self.id.as_ref().map(|id| &id.0)); - operation.container(None, &mut |operation| { - self.content.as_widget().operate( - &mut tree.children[0], - layout.children().next().unwrap(), - renderer, - operation, - ); - }); + operation.container( + self.id.as_ref().map(|id| &id.0), + &mut |operation| { + self.content.as_widget().operate( + &mut tree.children[0], + layout.children().next().unwrap(), + renderer, + operation, + ); + }, + ); } fn on_event( -- cgit From 84a6038961a5ebd1366ae8c6ba9e44be07d37f16 Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Thu, 9 Feb 2023 21:16:12 -0800 Subject: provide ID to operation.container in applicable widgets --- native/src/widget/button.rs | 53 ++++++++++++++++++++++++++++++------- native/src/widget/column.rs | 59 +++++++++++++++++++++++++++++++++--------- native/src/widget/container.rs | 53 ++++++++++++++++++++++++++++++------- native/src/widget/pane_grid.rs | 53 ++++++++++++++++++++++++++++++------- native/src/widget/row.rs | 59 +++++++++++++++++++++++++++++++++--------- 5 files changed, 226 insertions(+), 51 deletions(-) diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index b4276317..d1537b10 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -8,7 +8,7 @@ use crate::overlay; use crate::renderer; use crate::touch; use crate::widget::tree::{self, Tree}; -use crate::widget::Operation; +use crate::widget::{self, Operation}; use crate::{ Background, Clipboard, Color, Element, Layout, Length, Padding, Point, Rectangle, Shell, Vector, Widget, @@ -56,6 +56,7 @@ where Renderer: crate::Renderer, Renderer::Theme: StyleSheet, { + id: Option, content: Element<'a, Message, Renderer>, on_press: Option, width: Length, @@ -72,6 +73,7 @@ where /// Creates a new [`Button`] with the given content. pub fn new(content: impl Into>) -> Self { Button { + id: None, content: content.into(), on_press: None, width: Length::Shrink, @@ -81,6 +83,12 @@ where } } + /// Sets the [`Id`] of the [`Button`]. + pub fn id(mut self, id: Id) -> Self { + self.id = Some(id); + self + } + /// Sets the width of the [`Button`]. pub fn width(mut self, width: Length) -> Self { self.width = width; @@ -172,14 +180,17 @@ where renderer: &Renderer, operation: &mut dyn Operation, ) { - operation.container(None, &mut |operation| { - self.content.as_widget().operate( - &mut tree.children[0], - layout.children().next().unwrap(), - renderer, - operation, - ); - }); + operation.container( + self.id.as_ref().map(|id| &id.0), + &mut |operation| { + self.content.as_widget().operate( + &mut tree.children[0], + layout.children().next().unwrap(), + renderer, + operation, + ); + }, + ); } fn on_event( @@ -453,3 +464,27 @@ pub fn mouse_interaction( mouse::Interaction::default() } } + +/// The identifier of a [`Button`]. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct Id(widget::Id); + +impl Id { + /// Creates a custom [`Id`]. + pub fn new(id: impl Into>) -> Self { + Self(widget::Id::new(id)) + } + + /// Creates a unique [`Id`]. + /// + /// This function produces a different [`Id`] every time it is called. + pub fn unique() -> Self { + Self(widget::Id::unique()) + } +} + +impl From for widget::Id { + fn from(id: Id) -> Self { + id.0 + } +} diff --git a/native/src/widget/column.rs b/native/src/widget/column.rs index 5ad4d858..5cecd2b8 100644 --- a/native/src/widget/column.rs +++ b/native/src/widget/column.rs @@ -4,7 +4,7 @@ use crate::layout; use crate::mouse; use crate::overlay; use crate::renderer; -use crate::widget::{Operation, Tree}; +use crate::widget::{self, Operation, Tree}; use crate::{ Alignment, Clipboard, Element, Layout, Length, Padding, Point, Rectangle, Shell, Widget, @@ -13,6 +13,7 @@ use crate::{ /// A container that distributes its contents vertically. #[allow(missing_debug_implementations)] pub struct Column<'a, Message, Renderer> { + id: Option, spacing: u16, padding: Padding, width: Length, @@ -33,6 +34,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { children: Vec>, ) -> Self { Column { + id: None, spacing: 0, padding: Padding::ZERO, width: Length::Shrink, @@ -43,6 +45,12 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { } } + /// Sets the [`Id`] of the [`Column`]. + pub fn id(mut self, id: Id) -> Self { + self.id = Some(id); + self + } + /// Sets the vertical spacing _between_ elements. /// /// Custom margins per element do not exist in iced. You should use this @@ -148,17 +156,20 @@ where renderer: &Renderer, operation: &mut dyn Operation, ) { - operation.container(None, &mut |operation| { - self.children - .iter() - .zip(&mut tree.children) - .zip(layout.children()) - .for_each(|((child, state), layout)| { - child - .as_widget() - .operate(state, layout, renderer, operation); - }) - }); + operation.container( + self.id.as_ref().map(|id| &id.0), + &mut |operation| { + self.children + .iter() + .zip(&mut tree.children) + .zip(layout.children()) + .for_each(|((child, state), layout)| { + child + .as_widget() + .operate(state, layout, renderer, operation); + }) + }, + ); } fn on_event( @@ -262,3 +273,27 @@ where Self::new(column) } } + +/// The identifier of a [`Column`]. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct Id(widget::Id); + +impl Id { + /// Creates a custom [`Id`]. + pub fn new(id: impl Into>) -> Self { + Self(widget::Id::new(id)) + } + + /// Creates a unique [`Id`]. + /// + /// This function produces a different [`Id`] every time it is called. + pub fn unique() -> Self { + Self(widget::Id::unique()) + } +} + +impl From for widget::Id { + fn from(id: Id) -> Self { + id.0 + } +} diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index cdf1c859..c82b8be2 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -5,7 +5,7 @@ use crate::layout; use crate::mouse; use crate::overlay; use crate::renderer; -use crate::widget::{Operation, Tree}; +use crate::widget::{self, Operation, Tree}; use crate::{ Background, Clipboard, Color, Element, Layout, Length, Padding, Point, Rectangle, Shell, Widget, @@ -24,6 +24,7 @@ where Renderer: crate::Renderer, Renderer::Theme: StyleSheet, { + id: Option, padding: Padding, width: Length, height: Length, @@ -46,6 +47,7 @@ where T: Into>, { Container { + id: None, padding: Padding::ZERO, width: Length::Shrink, height: Length::Shrink, @@ -58,6 +60,12 @@ where } } + /// Sets the [`Id`] of the [`Container`]. + pub fn id(mut self, id: Id) -> Self { + self.id = Some(id); + self + } + /// Sets the [`Padding`] of the [`Container`]. pub fn padding>(mut self, padding: P) -> Self { self.padding = padding.into(); @@ -172,14 +180,17 @@ where renderer: &Renderer, operation: &mut dyn Operation, ) { - operation.container(None, &mut |operation| { - self.content.as_widget().operate( - &mut tree.children[0], - layout.children().next().unwrap(), - renderer, - operation, - ); - }); + operation.container( + self.id.as_ref().map(|id| &id.0), + &mut |operation| { + self.content.as_widget().operate( + &mut tree.children[0], + layout.children().next().unwrap(), + renderer, + operation, + ); + }, + ); } fn on_event( @@ -333,3 +344,27 @@ pub fn draw_background( ); } } + +/// The identifier of a [`Container`]. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct Id(widget::Id); + +impl Id { + /// Creates a custom [`Id`]. + pub fn new(id: impl Into>) -> Self { + Self(widget::Id::new(id)) + } + + /// Creates a unique [`Id`]. + /// + /// This function produces a different [`Id`] every time it is called. + pub fn unique() -> Self { + Self(widget::Id::unique()) + } +} + +impl From for widget::Id { + fn from(id: Id) -> Self { + id.0 + } +} diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index eb04c0ba..6a65754e 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -101,6 +101,7 @@ where Renderer: crate::Renderer, Renderer::Theme: StyleSheet + container::StyleSheet, { + id: Option, contents: Contents<'a, Content<'a, Message, Renderer>>, width: Length, height: Length, @@ -147,6 +148,7 @@ where }; Self { + id: None, contents, width: Length::Fill, height: Length::Fill, @@ -158,6 +160,12 @@ where } } + /// Sets the [`Id`] of the [`PaneGrid`]. + pub fn id(mut self, id: Id) -> Self { + self.id = Some(id); + self + } + /// Sets the width of the [`PaneGrid`]. pub fn width(mut self, width: Length) -> Self { self.width = width; @@ -297,15 +305,18 @@ where renderer: &Renderer, operation: &mut dyn widget::Operation, ) { - operation.container(None, &mut |operation| { - self.contents - .iter() - .zip(&mut tree.children) - .zip(layout.children()) - .for_each(|(((_pane, content), state), layout)| { - content.operate(state, layout, renderer, operation); - }) - }); + operation.container( + self.id.as_ref().map(|id| &id.0), + &mut |operation| { + self.contents + .iter() + .zip(&mut tree.children) + .zip(layout.children()) + .for_each(|(((_pane, content), state), layout)| { + content.operate(state, layout, renderer, operation); + }) + }, + ); } fn on_event( @@ -996,3 +1007,27 @@ impl<'a, T> Contents<'a, T> { matches!(self, Self::Maximized(..)) } } + +/// The identifier of a [`PaneGrid`]. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct Id(widget::Id); + +impl Id { + /// Creates a custom [`Id`]. + pub fn new(id: impl Into>) -> Self { + Self(widget::Id::new(id)) + } + + /// Creates a unique [`Id`]. + /// + /// This function produces a different [`Id`] every time it is called. + pub fn unique() -> Self { + Self(widget::Id::unique()) + } +} + +impl From for widget::Id { + fn from(id: Id) -> Self { + id.0 + } +} diff --git a/native/src/widget/row.rs b/native/src/widget/row.rs index 108e98e4..1ea670b0 100644 --- a/native/src/widget/row.rs +++ b/native/src/widget/row.rs @@ -4,7 +4,7 @@ use crate::layout::{self, Layout}; use crate::mouse; use crate::overlay; use crate::renderer; -use crate::widget::{Operation, Tree}; +use crate::widget::{self, Operation, Tree}; use crate::{ Alignment, Clipboard, Element, Length, Padding, Point, Rectangle, Shell, Widget, @@ -13,6 +13,7 @@ use crate::{ /// A container that distributes its contents horizontally. #[allow(missing_debug_implementations)] pub struct Row<'a, Message, Renderer> { + id: Option, spacing: u16, padding: Padding, width: Length, @@ -32,6 +33,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { children: Vec>, ) -> Self { Row { + id: None, spacing: 0, padding: Padding::ZERO, width: Length::Shrink, @@ -41,6 +43,12 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { } } + /// Sets the [`Id`] of the [`Row`]. + pub fn id(mut self, id: Id) -> Self { + self.id = Some(id); + self + } + /// Sets the horizontal spacing _between_ elements. /// /// Custom margins per element do not exist in iced. You should use this @@ -137,17 +145,20 @@ where renderer: &Renderer, operation: &mut dyn Operation, ) { - operation.container(None, &mut |operation| { - self.children - .iter() - .zip(&mut tree.children) - .zip(layout.children()) - .for_each(|((child, state), layout)| { - child - .as_widget() - .operate(state, layout, renderer, operation); - }) - }); + operation.container( + self.id.as_ref().map(|id| &id.0), + &mut |operation| { + self.children + .iter() + .zip(&mut tree.children) + .zip(layout.children()) + .for_each(|((child, state), layout)| { + child + .as_widget() + .operate(state, layout, renderer, operation); + }) + }, + ); } fn on_event( @@ -251,3 +262,27 @@ where Self::new(row) } } + +/// The identifier of a [`Row`]. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct Id(widget::Id); + +impl Id { + /// Creates a custom [`Id`]. + pub fn new(id: impl Into>) -> Self { + Self(widget::Id::new(id)) + } + + /// Creates a unique [`Id`]. + /// + /// This function produces a different [`Id`] every time it is called. + pub fn unique() -> Self { + Self(widget::Id::unique()) + } +} + +impl From for widget::Id { + fn from(id: Id) -> Self { + id.0 + } +} -- cgit From d05ac38159dc2828b6e5b0d416802d8ef4be80d2 Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Fri, 10 Feb 2023 14:46:03 -0800 Subject: Revert "provide ID to operation.container in applicable widgets" This reverts commit 8f9550bcf7c1cebbf90e80683761375406ca6139. --- native/src/widget/button.rs | 53 +++++++------------------------------ native/src/widget/column.rs | 59 +++++++++--------------------------------- native/src/widget/container.rs | 53 +++++++------------------------------ native/src/widget/pane_grid.rs | 53 +++++++------------------------------ native/src/widget/row.rs | 59 +++++++++--------------------------------- 5 files changed, 51 insertions(+), 226 deletions(-) diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index d1537b10..b4276317 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -8,7 +8,7 @@ use crate::overlay; use crate::renderer; use crate::touch; use crate::widget::tree::{self, Tree}; -use crate::widget::{self, Operation}; +use crate::widget::Operation; use crate::{ Background, Clipboard, Color, Element, Layout, Length, Padding, Point, Rectangle, Shell, Vector, Widget, @@ -56,7 +56,6 @@ where Renderer: crate::Renderer, Renderer::Theme: StyleSheet, { - id: Option, content: Element<'a, Message, Renderer>, on_press: Option, width: Length, @@ -73,7 +72,6 @@ where /// Creates a new [`Button`] with the given content. pub fn new(content: impl Into>) -> Self { Button { - id: None, content: content.into(), on_press: None, width: Length::Shrink, @@ -83,12 +81,6 @@ where } } - /// Sets the [`Id`] of the [`Button`]. - pub fn id(mut self, id: Id) -> Self { - self.id = Some(id); - self - } - /// Sets the width of the [`Button`]. pub fn width(mut self, width: Length) -> Self { self.width = width; @@ -180,17 +172,14 @@ where renderer: &Renderer, operation: &mut dyn Operation, ) { - operation.container( - self.id.as_ref().map(|id| &id.0), - &mut |operation| { - self.content.as_widget().operate( - &mut tree.children[0], - layout.children().next().unwrap(), - renderer, - operation, - ); - }, - ); + operation.container(None, &mut |operation| { + self.content.as_widget().operate( + &mut tree.children[0], + layout.children().next().unwrap(), + renderer, + operation, + ); + }); } fn on_event( @@ -464,27 +453,3 @@ pub fn mouse_interaction( mouse::Interaction::default() } } - -/// The identifier of a [`Button`]. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct Id(widget::Id); - -impl Id { - /// Creates a custom [`Id`]. - pub fn new(id: impl Into>) -> Self { - Self(widget::Id::new(id)) - } - - /// Creates a unique [`Id`]. - /// - /// This function produces a different [`Id`] every time it is called. - pub fn unique() -> Self { - Self(widget::Id::unique()) - } -} - -impl From for widget::Id { - fn from(id: Id) -> Self { - id.0 - } -} diff --git a/native/src/widget/column.rs b/native/src/widget/column.rs index 5cecd2b8..5ad4d858 100644 --- a/native/src/widget/column.rs +++ b/native/src/widget/column.rs @@ -4,7 +4,7 @@ use crate::layout; use crate::mouse; use crate::overlay; use crate::renderer; -use crate::widget::{self, Operation, Tree}; +use crate::widget::{Operation, Tree}; use crate::{ Alignment, Clipboard, Element, Layout, Length, Padding, Point, Rectangle, Shell, Widget, @@ -13,7 +13,6 @@ use crate::{ /// A container that distributes its contents vertically. #[allow(missing_debug_implementations)] pub struct Column<'a, Message, Renderer> { - id: Option, spacing: u16, padding: Padding, width: Length, @@ -34,7 +33,6 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { children: Vec>, ) -> Self { Column { - id: None, spacing: 0, padding: Padding::ZERO, width: Length::Shrink, @@ -45,12 +43,6 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { } } - /// Sets the [`Id`] of the [`Column`]. - pub fn id(mut self, id: Id) -> Self { - self.id = Some(id); - self - } - /// Sets the vertical spacing _between_ elements. /// /// Custom margins per element do not exist in iced. You should use this @@ -156,20 +148,17 @@ where renderer: &Renderer, operation: &mut dyn Operation, ) { - operation.container( - self.id.as_ref().map(|id| &id.0), - &mut |operation| { - self.children - .iter() - .zip(&mut tree.children) - .zip(layout.children()) - .for_each(|((child, state), layout)| { - child - .as_widget() - .operate(state, layout, renderer, operation); - }) - }, - ); + operation.container(None, &mut |operation| { + self.children + .iter() + .zip(&mut tree.children) + .zip(layout.children()) + .for_each(|((child, state), layout)| { + child + .as_widget() + .operate(state, layout, renderer, operation); + }) + }); } fn on_event( @@ -273,27 +262,3 @@ where Self::new(column) } } - -/// The identifier of a [`Column`]. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct Id(widget::Id); - -impl Id { - /// Creates a custom [`Id`]. - pub fn new(id: impl Into>) -> Self { - Self(widget::Id::new(id)) - } - - /// Creates a unique [`Id`]. - /// - /// This function produces a different [`Id`] every time it is called. - pub fn unique() -> Self { - Self(widget::Id::unique()) - } -} - -impl From for widget::Id { - fn from(id: Id) -> Self { - id.0 - } -} diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index c82b8be2..cdf1c859 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -5,7 +5,7 @@ use crate::layout; use crate::mouse; use crate::overlay; use crate::renderer; -use crate::widget::{self, Operation, Tree}; +use crate::widget::{Operation, Tree}; use crate::{ Background, Clipboard, Color, Element, Layout, Length, Padding, Point, Rectangle, Shell, Widget, @@ -24,7 +24,6 @@ where Renderer: crate::Renderer, Renderer::Theme: StyleSheet, { - id: Option, padding: Padding, width: Length, height: Length, @@ -47,7 +46,6 @@ where T: Into>, { Container { - id: None, padding: Padding::ZERO, width: Length::Shrink, height: Length::Shrink, @@ -60,12 +58,6 @@ where } } - /// Sets the [`Id`] of the [`Container`]. - pub fn id(mut self, id: Id) -> Self { - self.id = Some(id); - self - } - /// Sets the [`Padding`] of the [`Container`]. pub fn padding>(mut self, padding: P) -> Self { self.padding = padding.into(); @@ -180,17 +172,14 @@ where renderer: &Renderer, operation: &mut dyn Operation, ) { - operation.container( - self.id.as_ref().map(|id| &id.0), - &mut |operation| { - self.content.as_widget().operate( - &mut tree.children[0], - layout.children().next().unwrap(), - renderer, - operation, - ); - }, - ); + operation.container(None, &mut |operation| { + self.content.as_widget().operate( + &mut tree.children[0], + layout.children().next().unwrap(), + renderer, + operation, + ); + }); } fn on_event( @@ -344,27 +333,3 @@ pub fn draw_background( ); } } - -/// The identifier of a [`Container`]. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct Id(widget::Id); - -impl Id { - /// Creates a custom [`Id`]. - pub fn new(id: impl Into>) -> Self { - Self(widget::Id::new(id)) - } - - /// Creates a unique [`Id`]. - /// - /// This function produces a different [`Id`] every time it is called. - pub fn unique() -> Self { - Self(widget::Id::unique()) - } -} - -impl From for widget::Id { - fn from(id: Id) -> Self { - id.0 - } -} diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index 6a65754e..eb04c0ba 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -101,7 +101,6 @@ where Renderer: crate::Renderer, Renderer::Theme: StyleSheet + container::StyleSheet, { - id: Option, contents: Contents<'a, Content<'a, Message, Renderer>>, width: Length, height: Length, @@ -148,7 +147,6 @@ where }; Self { - id: None, contents, width: Length::Fill, height: Length::Fill, @@ -160,12 +158,6 @@ where } } - /// Sets the [`Id`] of the [`PaneGrid`]. - pub fn id(mut self, id: Id) -> Self { - self.id = Some(id); - self - } - /// Sets the width of the [`PaneGrid`]. pub fn width(mut self, width: Length) -> Self { self.width = width; @@ -305,18 +297,15 @@ where renderer: &Renderer, operation: &mut dyn widget::Operation, ) { - operation.container( - self.id.as_ref().map(|id| &id.0), - &mut |operation| { - self.contents - .iter() - .zip(&mut tree.children) - .zip(layout.children()) - .for_each(|(((_pane, content), state), layout)| { - content.operate(state, layout, renderer, operation); - }) - }, - ); + operation.container(None, &mut |operation| { + self.contents + .iter() + .zip(&mut tree.children) + .zip(layout.children()) + .for_each(|(((_pane, content), state), layout)| { + content.operate(state, layout, renderer, operation); + }) + }); } fn on_event( @@ -1007,27 +996,3 @@ impl<'a, T> Contents<'a, T> { matches!(self, Self::Maximized(..)) } } - -/// The identifier of a [`PaneGrid`]. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct Id(widget::Id); - -impl Id { - /// Creates a custom [`Id`]. - pub fn new(id: impl Into>) -> Self { - Self(widget::Id::new(id)) - } - - /// Creates a unique [`Id`]. - /// - /// This function produces a different [`Id`] every time it is called. - pub fn unique() -> Self { - Self(widget::Id::unique()) - } -} - -impl From for widget::Id { - fn from(id: Id) -> Self { - id.0 - } -} diff --git a/native/src/widget/row.rs b/native/src/widget/row.rs index 1ea670b0..108e98e4 100644 --- a/native/src/widget/row.rs +++ b/native/src/widget/row.rs @@ -4,7 +4,7 @@ use crate::layout::{self, Layout}; use crate::mouse; use crate::overlay; use crate::renderer; -use crate::widget::{self, Operation, Tree}; +use crate::widget::{Operation, Tree}; use crate::{ Alignment, Clipboard, Element, Length, Padding, Point, Rectangle, Shell, Widget, @@ -13,7 +13,6 @@ use crate::{ /// A container that distributes its contents horizontally. #[allow(missing_debug_implementations)] pub struct Row<'a, Message, Renderer> { - id: Option, spacing: u16, padding: Padding, width: Length, @@ -33,7 +32,6 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { children: Vec>, ) -> Self { Row { - id: None, spacing: 0, padding: Padding::ZERO, width: Length::Shrink, @@ -43,12 +41,6 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { } } - /// Sets the [`Id`] of the [`Row`]. - pub fn id(mut self, id: Id) -> Self { - self.id = Some(id); - self - } - /// Sets the horizontal spacing _between_ elements. /// /// Custom margins per element do not exist in iced. You should use this @@ -145,20 +137,17 @@ where renderer: &Renderer, operation: &mut dyn Operation, ) { - operation.container( - self.id.as_ref().map(|id| &id.0), - &mut |operation| { - self.children - .iter() - .zip(&mut tree.children) - .zip(layout.children()) - .for_each(|((child, state), layout)| { - child - .as_widget() - .operate(state, layout, renderer, operation); - }) - }, - ); + operation.container(None, &mut |operation| { + self.children + .iter() + .zip(&mut tree.children) + .zip(layout.children()) + .for_each(|((child, state), layout)| { + child + .as_widget() + .operate(state, layout, renderer, operation); + }) + }); } fn on_event( @@ -262,27 +251,3 @@ where Self::new(row) } } - -/// The identifier of a [`Row`]. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct Id(widget::Id); - -impl Id { - /// Creates a custom [`Id`]. - pub fn new(id: impl Into>) -> Self { - Self(widget::Id::new(id)) - } - - /// Creates a unique [`Id`]. - /// - /// This function produces a different [`Id`] every time it is called. - pub fn unique() -> Self { - Self(widget::Id::unique()) - } -} - -impl From for widget::Id { - fn from(id: Id) -> Self { - id.0 - } -} -- cgit From 273c9be00f80ba97b0f1330d035a2f9e073464a2 Mon Sep 17 00:00:00 2001 From: Nick Senger Date: Fri, 10 Feb 2023 14:51:59 -0800 Subject: container: allow specification of ID and provide to `Operation::container` --- native/src/widget/container.rs | 53 +++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index cdf1c859..c82b8be2 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -5,7 +5,7 @@ use crate::layout; use crate::mouse; use crate::overlay; use crate::renderer; -use crate::widget::{Operation, Tree}; +use crate::widget::{self, Operation, Tree}; use crate::{ Background, Clipboard, Color, Element, Layout, Length, Padding, Point, Rectangle, Shell, Widget, @@ -24,6 +24,7 @@ where Renderer: crate::Renderer, Renderer::Theme: StyleSheet, { + id: Option, padding: Padding, width: Length, height: Length, @@ -46,6 +47,7 @@ where T: Into>, { Container { + id: None, padding: Padding::ZERO, width: Length::Shrink, height: Length::Shrink, @@ -58,6 +60,12 @@ where } } + /// Sets the [`Id`] of the [`Container`]. + pub fn id(mut self, id: Id) -> Self { + self.id = Some(id); + self + } + /// Sets the [`Padding`] of the [`Container`]. pub fn padding>(mut self, padding: P) -> Self { self.padding = padding.into(); @@ -172,14 +180,17 @@ where renderer: &Renderer, operation: &mut dyn Operation, ) { - operation.container(None, &mut |operation| { - self.content.as_widget().operate( - &mut tree.children[0], - layout.children().next().unwrap(), - renderer, - operation, - ); - }); + operation.container( + self.id.as_ref().map(|id| &id.0), + &mut |operation| { + self.content.as_widget().operate( + &mut tree.children[0], + layout.children().next().unwrap(), + renderer, + operation, + ); + }, + ); } fn on_event( @@ -333,3 +344,27 @@ pub fn draw_background( ); } } + +/// The identifier of a [`Container`]. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct Id(widget::Id); + +impl Id { + /// Creates a custom [`Id`]. + pub fn new(id: impl Into>) -> Self { + Self(widget::Id::new(id)) + } + + /// Creates a unique [`Id`]. + /// + /// This function produces a different [`Id`] every time it is called. + pub fn unique() -> Self { + Self(widget::Id::unique()) + } +} + +impl From for widget::Id { + fn from(id: Id) -> Self { + id.0 + } +} -- cgit From d1d13f6f160af73e549e2c2dd8a9d7991f10f1f6 Mon Sep 17 00:00:00 2001 From: Night_Hunter Date: Sat, 10 Dec 2022 01:42:51 +1300 Subject: add always on top action --- native/src/window/action.rs | 10 ++++++++++ winit/src/application.rs | 3 +++ 2 files changed, 13 insertions(+) diff --git a/native/src/window/action.rs b/native/src/window/action.rs index 168974bc..b16b7243 100644 --- a/native/src/window/action.rs +++ b/native/src/window/action.rs @@ -70,6 +70,12 @@ pub enum Action { /// /// - **Web / Wayland:** Unsupported. GainFocus, + /// Change whether or not the window will always be on top of other windows. + /// + /// ## Platform-specific + /// + /// - **iOS / Android / Web / Wayland:** Unsupported. + AlwaysOnTop(bool), } impl Action { @@ -96,6 +102,7 @@ impl Action { Action::RequestUserAttention(attention_type) } Self::GainFocus => Action::GainFocus, + Self::AlwaysOnTop(bool) => Action::AlwaysOnTop(bool), } } } @@ -122,6 +129,9 @@ impl fmt::Debug for Action { write!(f, "Action::RequestUserAttention") } Self::GainFocus => write!(f, "Action::GainFocus"), + Self::AlwaysOnTop(value) => { + write!(f, "Action::AlwaysOnTop({})", value) + } } } } diff --git a/winit/src/application.rs b/winit/src/application.rs index 769fe9dd..fda559d0 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -788,6 +788,9 @@ pub fn run_command( user_attention.map(conversion::user_attention), ), window::Action::GainFocus => window.focus_window(), + window::Action::AlwaysOnTop(value) => { + window.set_always_on_top(value) + } }, command::Action::System(action) => match action { system::Action::QueryInformation(_tag) => { -- cgit From 095ecf016b53ad25337663fb9f11a84f373150e0 Mon Sep 17 00:00:00 2001 From: Night_Hunter Date: Thu, 29 Dec 2022 13:30:14 +1300 Subject: update docs and change to SetAlwaysOnTop --- native/src/window/action.rs | 8 ++++---- winit/src/application.rs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/native/src/window/action.rs b/native/src/window/action.rs index b16b7243..0c711090 100644 --- a/native/src/window/action.rs +++ b/native/src/window/action.rs @@ -74,8 +74,8 @@ pub enum Action { /// /// ## Platform-specific /// - /// - **iOS / Android / Web / Wayland:** Unsupported. - AlwaysOnTop(bool), + /// - **Web / Wayland:** Unsupported. + SetAlwaysOnTop(bool), } impl Action { @@ -102,7 +102,7 @@ impl Action { Action::RequestUserAttention(attention_type) } Self::GainFocus => Action::GainFocus, - Self::AlwaysOnTop(bool) => Action::AlwaysOnTop(bool), + Self::SetAlwaysOnTop(bool) => Action::SetAlwaysOnTop(bool), } } } @@ -129,7 +129,7 @@ impl fmt::Debug for Action { write!(f, "Action::RequestUserAttention") } Self::GainFocus => write!(f, "Action::GainFocus"), - Self::AlwaysOnTop(value) => { + Self::SetAlwaysOnTop(value) => { write!(f, "Action::AlwaysOnTop({})", value) } } diff --git a/winit/src/application.rs b/winit/src/application.rs index fda559d0..d2e40de0 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -788,7 +788,7 @@ pub fn run_command( user_attention.map(conversion::user_attention), ), window::Action::GainFocus => window.focus_window(), - window::Action::AlwaysOnTop(value) => { + window::Action::SetAlwaysOnTop(value) => { window.set_always_on_top(value) } }, -- cgit From df861d9ece5e8695d05d08f104d37f55222fb363 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 17 Feb 2023 13:22:45 +0100 Subject: Rename `SetAlwaysOnTop` to `ChangeAlwaysOnTop` --- native/src/window/action.rs | 22 ++++++++++++++-------- winit/src/application.rs | 25 ++++++++++++++----------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/native/src/window/action.rs b/native/src/window/action.rs index 0c711090..c6361449 100644 --- a/native/src/window/action.rs +++ b/native/src/window/action.rs @@ -75,7 +75,7 @@ pub enum Action { /// ## Platform-specific /// /// - **Web / Wayland:** Unsupported. - SetAlwaysOnTop(bool), + ChangeAlwaysOnTop(bool), } impl Action { @@ -91,8 +91,8 @@ impl Action { Self::Close => Action::Close, Self::Drag => Action::Drag, Self::Resize { width, height } => Action::Resize { width, height }, - Self::Maximize(bool) => Action::Maximize(bool), - Self::Minimize(bool) => Action::Minimize(bool), + Self::Maximize(maximized) => Action::Maximize(maximized), + Self::Minimize(minimized) => Action::Minimize(minimized), Self::Move { x, y } => Action::Move { x, y }, Self::ChangeMode(mode) => Action::ChangeMode(mode), Self::FetchMode(o) => Action::FetchMode(Box::new(move |s| f(o(s)))), @@ -102,7 +102,9 @@ impl Action { Action::RequestUserAttention(attention_type) } Self::GainFocus => Action::GainFocus, - Self::SetAlwaysOnTop(bool) => Action::SetAlwaysOnTop(bool), + Self::ChangeAlwaysOnTop(on_top) => { + Action::ChangeAlwaysOnTop(on_top) + } } } } @@ -116,8 +118,12 @@ impl fmt::Debug for Action { f, "Action::Resize {{ widget: {width}, height: {height} }}" ), - Self::Maximize(value) => write!(f, "Action::Maximize({value})"), - Self::Minimize(value) => write!(f, "Action::Minimize({value}"), + Self::Maximize(maximized) => { + write!(f, "Action::Maximize({maximized})") + } + Self::Minimize(minimized) => { + write!(f, "Action::Minimize({minimized}") + } Self::Move { x, y } => { write!(f, "Action::Move {{ x: {x}, y: {y} }}") } @@ -129,8 +135,8 @@ impl fmt::Debug for Action { write!(f, "Action::RequestUserAttention") } Self::GainFocus => write!(f, "Action::GainFocus"), - Self::SetAlwaysOnTop(value) => { - write!(f, "Action::AlwaysOnTop({})", value) + Self::ChangeAlwaysOnTop(on_top) => { + write!(f, "Action::AlwaysOnTop({on_top})") } } } diff --git a/winit/src/application.rs b/winit/src/application.rs index d2e40de0..1f37ffef 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -747,11 +747,11 @@ pub fn run_command( height, }); } - window::Action::Maximize(value) => { - window.set_maximized(value); + window::Action::Maximize(maximized) => { + window.set_maximized(maximized); } - window::Action::Minimize(value) => { - window.set_minimized(value); + window::Action::Minimize(minimized) => { + window.set_minimized(minimized); } window::Action::Move { x, y } => { window.set_outer_position(winit::dpi::LogicalPosition { @@ -781,15 +781,18 @@ pub fn run_command( window.set_maximized(!window.is_maximized()) } window::Action::ToggleDecorations => { - window.set_decorations(!window.is_decorated()) + window.set_decorations(!window.is_decorated()); } - window::Action::RequestUserAttention(user_attention) => window - .request_user_attention( + window::Action::RequestUserAttention(user_attention) => { + window.request_user_attention( user_attention.map(conversion::user_attention), - ), - window::Action::GainFocus => window.focus_window(), - window::Action::SetAlwaysOnTop(value) => { - window.set_always_on_top(value) + ); + } + window::Action::GainFocus => { + window.focus_window(); + } + window::Action::ChangeAlwaysOnTop(on_top) => { + window.set_always_on_top(on_top); } }, command::Action::System(action) => match action { -- cgit From db65c6904d38fe37ad0c31a11242d55f5f773c94 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 17 Feb 2023 13:24:46 +0100 Subject: Expose `change_always_on_top` helper in `window` module --- winit/src/window.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/winit/src/window.rs b/winit/src/window.rs index 6e3a383a..3d5a765b 100644 --- a/winit/src/window.rs +++ b/winit/src/window.rs @@ -23,13 +23,17 @@ pub fn resize(width: u32, height: u32) -> Command { } /// Maximizes the window. -pub fn maximize(value: bool) -> Command { - Command::single(command::Action::Window(window::Action::Maximize(value))) +pub fn maximize(maximized: bool) -> Command { + Command::single(command::Action::Window(window::Action::Maximize( + maximized, + ))) } /// Minimes the window. -pub fn minimize(value: bool) -> Command { - Command::single(command::Action::Window(window::Action::Minimize(value))) +pub fn minimize(minimized: bool) -> Command { + Command::single(command::Action::Window(window::Action::Minimize( + minimized, + ))) } /// Moves a window to the given logical coordinates. @@ -84,3 +88,10 @@ pub fn request_user_attention( pub fn gain_focus() -> Command { Command::single(command::Action::Window(window::Action::GainFocus)) } + +/// Changes whether or not the window will always be on top of other windows. +pub fn change_always_on_top(on_top: bool) -> Command { + Command::single(command::Action::Window(window::Action::ChangeAlwaysOnTop( + on_top, + ))) +} -- cgit From 9f75f01ddb7a13a3ab1cffdfa59997cb5b131f72 Mon Sep 17 00:00:00 2001 From: Night_Hunter Date: Sat, 10 Dec 2022 01:54:57 +1300 Subject: add action to get window id --- native/src/window/action.rs | 4 ++++ winit/src/application.rs | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/native/src/window/action.rs b/native/src/window/action.rs index c6361449..ce36d129 100644 --- a/native/src/window/action.rs +++ b/native/src/window/action.rs @@ -76,6 +76,8 @@ pub enum Action { /// /// - **Web / Wayland:** Unsupported. ChangeAlwaysOnTop(bool), + /// Fetch an identifier unique to the window. + FetchId(Box T + 'static>), } impl Action { @@ -105,6 +107,7 @@ impl Action { Self::ChangeAlwaysOnTop(on_top) => { Action::ChangeAlwaysOnTop(on_top) } + Self::FetchId(o) => Action::FetchId(Box::new(move |s| f(o(s)))), } } } @@ -138,6 +141,7 @@ impl fmt::Debug for Action { Self::ChangeAlwaysOnTop(on_top) => { write!(f, "Action::AlwaysOnTop({on_top})") } + Self::FetchId(_) => write!(f, "Action::FetchId"), } } } diff --git a/winit/src/application.rs b/winit/src/application.rs index 1f37ffef..3fdec658 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -794,6 +794,11 @@ pub fn run_command( window::Action::ChangeAlwaysOnTop(on_top) => { window.set_always_on_top(on_top); } + window::Action::FetchId(tag) => { + proxy + .send_event(tag(window.id().into())) + .expect("Send message to event loop"); + } }, command::Action::System(action) => match action { system::Action::QueryInformation(_tag) => { -- cgit From 2c2421ae5dad9afce1058e67ae8bcabd787fa373 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 17 Feb 2023 13:47:46 +0100 Subject: Expose `fetch_id` helper in `window` module --- winit/src/window.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/winit/src/window.rs b/winit/src/window.rs index 3d5a765b..961562bd 100644 --- a/winit/src/window.rs +++ b/winit/src/window.rs @@ -95,3 +95,12 @@ pub fn change_always_on_top(on_top: bool) -> Command { on_top, ))) } + +/// Fetches an identifier unique to the window. +pub fn fetch_id( + f: impl FnOnce(u64) -> Message + 'static, +) -> Command { + Command::single(command::Action::Window(window::Action::FetchId(Box::new( + f, + )))) +} -- cgit From ac1945404efde5fbec7f3004f688dfd08f28cf55 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 17 Feb 2023 13:57:04 +0100 Subject: Run `cargo fmt` --- examples/qr_code/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/qr_code/src/main.rs b/examples/qr_code/src/main.rs index 42b58a21..c10c665b 100644 --- a/examples/qr_code/src/main.rs +++ b/examples/qr_code/src/main.rs @@ -38,7 +38,7 @@ impl Sandbox for QRGenerator { } else { qr_code::State::new(&data).ok() }; - + self.data = data; } } -- cgit From d2996f3ed86e14ece846f595bc7d3133ce106eb3 Mon Sep 17 00:00:00 2001 From: Sebastian Dröge Date: Sun, 20 Nov 2022 17:30:11 +0200 Subject: image: Allow any kind of data that implements `AsRef<[u8]>` for the image data It's not required anywhere for it to be a plain slice or a `Vec` and this makes it possible to use data allocated in a different way without copying. --- examples/pokedex/src/main.rs | 2 +- native/src/image.rs | 71 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 65 insertions(+), 8 deletions(-) diff --git a/examples/pokedex/src/main.rs b/examples/pokedex/src/main.rs index 748acae0..1873b674 100644 --- a/examples/pokedex/src/main.rs +++ b/examples/pokedex/src/main.rs @@ -193,7 +193,7 @@ impl Pokemon { { let bytes = reqwest::get(&url).await?.bytes().await?; - Ok(image::Handle::from_memory(bytes.as_ref().to_vec())) + Ok(image::Handle::from_memory(bytes)) } #[cfg(target_arch = "wasm32")] diff --git a/native/src/image.rs b/native/src/image.rs index 5d2843c9..6d5d3a08 100644 --- a/native/src/image.rs +++ b/native/src/image.rs @@ -1,7 +1,6 @@ //! Load and draw raster graphics. use crate::{Hasher, Rectangle, Size}; -use std::borrow::Cow; use std::hash::{Hash, Hasher as _}; use std::path::PathBuf; use std::sync::Arc; @@ -29,12 +28,12 @@ impl Handle { pub fn from_pixels( width: u32, height: u32, - pixels: impl Into>, + pixels: impl AsRef<[u8]> + Clone + Send + Sync + 'static, ) -> Handle { Self::from_data(Data::Rgba { width, height, - pixels: pixels.into(), + pixels: ImageBytes::new(pixels), }) } @@ -44,8 +43,10 @@ impl Handle { /// /// This is useful if you already have your image loaded in-memory, maybe /// because you downloaded or generated it procedurally. - pub fn from_memory(bytes: impl Into>) -> Handle { - Self::from_data(Data::Bytes(bytes.into())) + pub fn from_memory( + bytes: impl AsRef<[u8]> + Clone + Send + Sync + 'static, + ) -> Handle { + Self::from_data(Data::Bytes(ImageBytes::new(bytes))) } fn from_data(data: Data) -> Handle { @@ -84,6 +85,62 @@ impl Hash for Handle { } } +/// Wrapper around raw image data. +/// +/// Behaves like a `&[u8]`. +pub struct ImageBytes(Box); + +trait ImageBytesTrait: AsRef<[u8]> + Send + Sync + 'static { + fn clone_boxed(&self) -> Box; +} + +impl + Clone + Send + Sync + 'static> ImageBytesTrait for T { + fn clone_boxed(&self) -> Box { + Box::new(self.clone()) + } +} + +impl ImageBytes { + /// Creates a new `ImageBytes` around `data`. + pub fn new( + data: impl AsRef<[u8]> + Clone + Send + Sync + 'static, + ) -> ImageBytes { + Self(Box::new(data)) + } +} + +impl Clone for ImageBytes { + fn clone(&self) -> Self { + ImageBytes(self.0.clone_boxed()) + } +} + +impl std::fmt::Debug for ImageBytes { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.0.as_ref().as_ref().fmt(f) + } +} + +impl std::hash::Hash for ImageBytes { + fn hash(&self, state: &mut H) { + self.0.as_ref().as_ref().hash(state); + } +} + +impl AsRef<[u8]> for ImageBytes { + fn as_ref(&self) -> &[u8] { + self.0.as_ref().as_ref() + } +} + +impl std::ops::Deref for ImageBytes { + type Target = [u8]; + + fn deref(&self) -> &[u8] { + self.0.as_ref().as_ref() + } +} + /// The data of a raster image. #[derive(Clone, Hash)] pub enum Data { @@ -91,7 +148,7 @@ pub enum Data { Path(PathBuf), /// In-memory data - Bytes(Cow<'static, [u8]>), + Bytes(ImageBytes), /// Decoded image pixels in RGBA format. Rgba { @@ -100,7 +157,7 @@ pub enum Data { /// The height of the image. height: u32, /// The pixels. - pixels: Cow<'static, [u8]>, + pixels: ImageBytes, }, } -- cgit From d7c83080f20d252b8dfb18a85345912af5df84ca Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 17 Feb 2023 14:37:15 +0100 Subject: Wrap image `Bytes` with `Arc` instead of `Data` --- native/src/image.rs | 53 ++++++++++++++++++----------------------------------- 1 file changed, 18 insertions(+), 35 deletions(-) diff --git a/native/src/image.rs b/native/src/image.rs index 6d5d3a08..4c5e926b 100644 --- a/native/src/image.rs +++ b/native/src/image.rs @@ -9,7 +9,7 @@ use std::sync::Arc; #[derive(Debug, Clone)] pub struct Handle { id: u64, - data: Arc, + data: Data, } impl Handle { @@ -33,7 +33,7 @@ impl Handle { Self::from_data(Data::Rgba { width, height, - pixels: ImageBytes::new(pixels), + pixels: Bytes::new(pixels), }) } @@ -46,7 +46,7 @@ impl Handle { pub fn from_memory( bytes: impl AsRef<[u8]> + Clone + Send + Sync + 'static, ) -> Handle { - Self::from_data(Data::Bytes(ImageBytes::new(bytes))) + Self::from_data(Data::Bytes(Bytes::new(bytes))) } fn from_data(data: Data) -> Handle { @@ -55,7 +55,7 @@ impl Handle { Handle { id: hasher.finish(), - data: Arc::new(data), + data, } } @@ -85,55 +85,38 @@ impl Hash for Handle { } } -/// Wrapper around raw image data. +/// A wrapper around raw image data. /// -/// Behaves like a `&[u8]`. -pub struct ImageBytes(Box); +/// It behaves like a `&[u8]`. +#[derive(Clone)] +pub struct Bytes(Arc + Send + Sync + 'static>); -trait ImageBytesTrait: AsRef<[u8]> + Send + Sync + 'static { - fn clone_boxed(&self) -> Box; -} - -impl + Clone + Send + Sync + 'static> ImageBytesTrait for T { - fn clone_boxed(&self) -> Box { - Box::new(self.clone()) - } -} - -impl ImageBytes { - /// Creates a new `ImageBytes` around `data`. - pub fn new( - data: impl AsRef<[u8]> + Clone + Send + Sync + 'static, - ) -> ImageBytes { - Self(Box::new(data)) - } -} - -impl Clone for ImageBytes { - fn clone(&self) -> Self { - ImageBytes(self.0.clone_boxed()) +impl Bytes { + /// Creates new [`Bytes`] around `data`. + pub fn new(data: impl AsRef<[u8]> + Clone + Send + Sync + 'static) -> Self { + Self(Arc::new(data)) } } -impl std::fmt::Debug for ImageBytes { +impl std::fmt::Debug for Bytes { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.0.as_ref().as_ref().fmt(f) } } -impl std::hash::Hash for ImageBytes { +impl std::hash::Hash for Bytes { fn hash(&self, state: &mut H) { self.0.as_ref().as_ref().hash(state); } } -impl AsRef<[u8]> for ImageBytes { +impl AsRef<[u8]> for Bytes { fn as_ref(&self) -> &[u8] { self.0.as_ref().as_ref() } } -impl std::ops::Deref for ImageBytes { +impl std::ops::Deref for Bytes { type Target = [u8]; fn deref(&self) -> &[u8] { @@ -148,7 +131,7 @@ pub enum Data { Path(PathBuf), /// In-memory data - Bytes(ImageBytes), + Bytes(Bytes), /// Decoded image pixels in RGBA format. Rgba { @@ -157,7 +140,7 @@ pub enum Data { /// The height of the image. height: u32, /// The pixels. - pixels: ImageBytes, + pixels: Bytes, }, } -- cgit From 7b8b01f560569ae18d9337a31ba94f6c1c2ba0dd Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 4 Feb 2023 12:24:13 +0100 Subject: Use `f32` in `Length::Units` and rename it to `Fixed` --- core/src/length.rs | 14 +++-- core/src/lib.rs | 2 + core/src/pixels.rs | 22 ++++++++ examples/color_palette/src/main.rs | 4 +- examples/component/src/main.rs | 2 +- examples/events/src/main.rs | 2 +- examples/integration_opengl/src/controls.rs | 2 +- examples/integration_wgpu/src/controls.rs | 2 +- examples/modal/src/main.rs | 2 +- examples/pick_list/src/main.rs | 4 +- examples/qr_code/src/main.rs | 2 +- examples/scrollable/src/main.rs | 22 ++++---- examples/slider/src/main.rs | 4 +- examples/stopwatch/src/main.rs | 2 +- examples/styling/src/main.rs | 12 ++-- examples/todos/src/main.rs | 4 +- examples/tour/src/main.rs | 8 +-- graphics/src/widget/canvas.rs | 14 ++--- lazy/src/responsive.rs | 2 +- native/src/layout/limits.rs | 37 ++++++------ native/src/lib.rs | 2 +- native/src/overlay/menu.rs | 10 ++-- native/src/widget/button.rs | 8 +-- native/src/widget/checkbox.rs | 22 +++----- native/src/widget/column.rs | 20 +++---- native/src/widget/container.rs | 36 ++++++------ native/src/widget/helpers.rs | 14 +++-- native/src/widget/image.rs | 10 ++-- native/src/widget/image/viewer.rs | 10 ++-- native/src/widget/pane_grid.rs | 8 +-- native/src/widget/pick_list.rs | 6 +- native/src/widget/progress_bar.rs | 14 ++--- native/src/widget/radio.rs | 16 ++---- native/src/widget/row.rs | 8 +-- native/src/widget/rule.rs | 12 ++-- native/src/widget/scrollable.rs | 87 ++++++++++++++--------------- native/src/widget/slider.rs | 14 ++--- native/src/widget/space.rs | 15 +++-- native/src/widget/svg.rs | 8 +-- native/src/widget/text.rs | 8 +-- native/src/widget/text_input.rs | 10 +--- native/src/widget/toggler.rs | 16 ++---- native/src/widget/vertical_slider.rs | 14 ++--- 43 files changed, 269 insertions(+), 262 deletions(-) create mode 100644 core/src/pixels.rs diff --git a/core/src/length.rs b/core/src/length.rs index 95ea6e0e..bb925c4b 100644 --- a/core/src/length.rs +++ b/core/src/length.rs @@ -1,5 +1,5 @@ /// The strategy used to fill space in a specific dimension. -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, Copy, PartialEq)] pub enum Length { /// Fill all the remaining space Fill, @@ -17,7 +17,7 @@ pub enum Length { Shrink, /// Fill a fixed amount of space - Units(u16), + Fixed(f32), } impl Length { @@ -31,13 +31,19 @@ impl Length { Length::Fill => 1, Length::FillPortion(factor) => *factor, Length::Shrink => 0, - Length::Units(_) => 0, + Length::Fixed(_) => 0, } } } +impl From for Length { + fn from(amount: f32) -> Self { + Length::Fixed(amount) + } +} + impl From for Length { fn from(units: u16) -> Self { - Length::Units(units) + Length::Fixed(f32::from(units)) } } diff --git a/core/src/lib.rs b/core/src/lib.rs index 3aa5defe..31280a05 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -35,6 +35,7 @@ mod content_fit; mod font; mod length; mod padding; +mod pixels; mod point; mod rectangle; mod size; @@ -47,6 +48,7 @@ pub use content_fit::ContentFit; pub use font::Font; pub use length::Length; pub use padding::Padding; +pub use pixels::Pixels; pub use point::Point; pub use rectangle::Rectangle; pub use size::Size; diff --git a/core/src/pixels.rs b/core/src/pixels.rs new file mode 100644 index 00000000..e42cd9f9 --- /dev/null +++ b/core/src/pixels.rs @@ -0,0 +1,22 @@ +/// An amount of logical pixels. +/// +/// Normally used to represent an amount of space, or the size of something. +/// +/// This type is normally asked as an argument in a generic way +/// (e.g. `impl Into`) and, since `Pixels` implements `From` both for +/// `f32` and `u16`, you should be able to provide both integers and float +/// literals as needed. +#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)] +pub struct Pixels(pub f32); + +impl From for Pixels { + fn from(amount: f32) -> Self { + Self(amount) + } +} + +impl From for Pixels { + fn from(amount: u16) -> Self { + Self(f32::from(amount)) + } +} diff --git a/examples/color_palette/src/main.rs b/examples/color_palette/src/main.rs index 42149965..a2df36c2 100644 --- a/examples/color_palette/src/main.rs +++ b/examples/color_palette/src/main.rs @@ -301,11 +301,11 @@ impl ColorPicker { } row![ - text(C::LABEL).width(Length::Units(50)), + text(C::LABEL).width(50), slider(cr1, c1, move |v| C::new(v, c2, c3)), slider(cr2, c2, move |v| C::new(c1, v, c3)), slider(cr3, c3, move |v| C::new(c1, c2, v)), - text(color.to_string()).width(Length::Units(185)).size(14), + text(color.to_string()).width(185).size(14), ] .spacing(10) .align_items(Alignment::Center) diff --git a/examples/component/src/main.rs b/examples/component/src/main.rs index 06b1e53a..c407bb06 100644 --- a/examples/component/src/main.rs +++ b/examples/component/src/main.rs @@ -127,7 +127,7 @@ mod numeric_input { .horizontal_alignment(alignment::Horizontal::Center) .vertical_alignment(alignment::Vertical::Center), ) - .width(Length::Units(50)) + .width(50) .on_press(on_press) }; diff --git a/examples/events/src/main.rs b/examples/events/src/main.rs index 0e583479..1b97018e 100644 --- a/examples/events/src/main.rs +++ b/examples/events/src/main.rs @@ -93,7 +93,7 @@ impl Application for Events { .width(Length::Fill) .horizontal_alignment(alignment::Horizontal::Center), ) - .width(Length::Units(100)) + .width(100) .padding(10) .on_press(Message::Exit); diff --git a/examples/integration_opengl/src/controls.rs b/examples/integration_opengl/src/controls.rs index 22c41066..c3648f44 100644 --- a/examples/integration_opengl/src/controls.rs +++ b/examples/integration_opengl/src/controls.rs @@ -42,7 +42,7 @@ impl Program for Controls { let background_color = self.background_color; let sliders = Row::new() - .width(Length::Units(500)) + .width(500) .spacing(20) .push( Slider::new(0.0..=1.0, background_color.r, move |r| { diff --git a/examples/integration_wgpu/src/controls.rs b/examples/integration_wgpu/src/controls.rs index 92300a45..533cb6e2 100644 --- a/examples/integration_wgpu/src/controls.rs +++ b/examples/integration_wgpu/src/controls.rs @@ -48,7 +48,7 @@ impl Program for Controls { let text = &self.text; let sliders = Row::new() - .width(Length::Units(500)) + .width(500) .spacing(20) .push( slider(0.0..=1.0, background_color.r, move |r| { diff --git a/examples/modal/src/main.rs b/examples/modal/src/main.rs index 5afafd0d..54555684 100644 --- a/examples/modal/src/main.rs +++ b/examples/modal/src/main.rs @@ -156,7 +156,7 @@ impl Application for App { ] .spacing(20), ) - .width(Length::Units(300)) + .width(300) .padding(10) .style(theme::Container::Box); diff --git a/examples/pick_list/src/main.rs b/examples/pick_list/src/main.rs index 9df1f5c7..62a4ef88 100644 --- a/examples/pick_list/src/main.rs +++ b/examples/pick_list/src/main.rs @@ -43,10 +43,10 @@ impl Sandbox for Example { .placeholder("Choose a language..."); let content = column![ - vertical_space(Length::Units(600)), + vertical_space(600), "Which is your favorite language?", pick_list, - vertical_space(Length::Units(600)), + vertical_space(600), ] .width(Length::Fill) .align_items(Alignment::Center) diff --git a/examples/qr_code/src/main.rs b/examples/qr_code/src/main.rs index c10c665b..d8041745 100644 --- a/examples/qr_code/src/main.rs +++ b/examples/qr_code/src/main.rs @@ -58,7 +58,7 @@ impl Sandbox for QRGenerator { .padding(15); let mut content = column![title, input] - .width(Length::Units(700)) + .width(700) .spacing(20) .align_items(Alignment::Center); diff --git a/examples/scrollable/src/main.rs b/examples/scrollable/src/main.rs index 128d98b2..7c858961 100644 --- a/examples/scrollable/src/main.rs +++ b/examples/scrollable/src/main.rs @@ -187,9 +187,9 @@ impl Application for ScrollableDemo { column![ scroll_to_end_button(), text("Beginning!"), - vertical_space(Length::Units(1200)), + vertical_space(1200), text("Middle!"), - vertical_space(Length::Units(1200)), + vertical_space(1200), text("End!"), scroll_to_beginning_button(), ] @@ -211,13 +211,13 @@ impl Application for ScrollableDemo { row![ scroll_to_end_button(), text("Beginning!"), - horizontal_space(Length::Units(1200)), + horizontal_space(1200), text("Middle!"), - horizontal_space(Length::Units(1200)), + horizontal_space(1200), text("End!"), scroll_to_beginning_button(), ] - .height(Length::Units(450)) + .height(450) .align_items(Alignment::Center) .padding([0, 40, 0, 40]) .spacing(40), @@ -237,26 +237,26 @@ impl Application for ScrollableDemo { row![ column![ text("Let's do some scrolling!"), - vertical_space(Length::Units(2400)) + vertical_space(2400) ], scroll_to_end_button(), text("Horizontal - Beginning!"), - horizontal_space(Length::Units(1200)), + horizontal_space(1200), //vertical content column![ text("Horizontal - Middle!"), scroll_to_end_button(), text("Vertical - Beginning!"), - vertical_space(Length::Units(1200)), + vertical_space(1200), text("Vertical - Middle!"), - vertical_space(Length::Units(1200)), + vertical_space(1200), text("Vertical - End!"), scroll_to_beginning_button(), - vertical_space(Length::Units(40)), + vertical_space(40), ] .align_items(Alignment::Fill) .spacing(40), - horizontal_space(Length::Units(1200)), + horizontal_space(1200), text("Horizontal - End!"), scroll_to_beginning_button(), ] diff --git a/examples/slider/src/main.rs b/examples/slider/src/main.rs index 6286d625..e83804c2 100644 --- a/examples/slider/src/main.rs +++ b/examples/slider/src/main.rs @@ -38,11 +38,11 @@ impl Sandbox for Slider { let h_slider = container(slider(0..=100, value, Message::SliderChanged)) - .width(Length::Units(250)); + .width(250); let v_slider = container(vertical_slider(0..=100, value, Message::SliderChanged)) - .height(Length::Units(200)); + .height(200); let text = text(format!("{value}")); diff --git a/examples/stopwatch/src/main.rs b/examples/stopwatch/src/main.rs index b8cee807..9581a3ce 100644 --- a/examples/stopwatch/src/main.rs +++ b/examples/stopwatch/src/main.rs @@ -105,7 +105,7 @@ impl Application for Stopwatch { text(label).horizontal_alignment(alignment::Horizontal::Center), ) .padding(10) - .width(Length::Units(80)) + .width(80) }; let toggle_button = { diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index 49bedce7..448c9792 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -108,14 +108,10 @@ impl Sandbox for Styling { let progress_bar = progress_bar(0.0..=100.0, self.slider_value); let scrollable = scrollable( - column![ - "Scroll me!", - vertical_space(Length::Units(800)), - "You did it!" - ] - .width(Length::Fill), + column!["Scroll me!", vertical_space(800), "You did it!"] + .width(Length::Fill), ) - .height(Length::Units(100)); + .height(100); let checkbox = checkbox( "Check me!", @@ -143,7 +139,7 @@ impl Sandbox for Styling { column![checkbox, toggler].spacing(20) ] .spacing(10) - .height(Length::Units(100)) + .height(100) .align_items(Alignment::Center), ] .spacing(20) diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 04411ed7..6408f09c 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -460,7 +460,7 @@ fn empty_message(message: &str) -> Element<'_, Message> { .style(Color::from([0.7, 0.7, 0.7])), ) .width(Length::Fill) - .height(Length::Units(200)) + .height(200) .center_y() .into() } @@ -474,7 +474,7 @@ const ICONS: Font = Font::External { fn icon(unicode: char) -> Text<'static> { text(unicode.to_string()) .font(ICONS) - .width(Length::Units(20)) + .width(20) .horizontal_alignment(alignment::Horizontal::Center) .size(20) } diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs index 5ee65562..de063d00 100644 --- a/examples/tour/src/main.rs +++ b/examples/tour/src/main.rs @@ -513,14 +513,14 @@ impl<'a> Step { text("Tip: You can use the scrollbar to scroll down faster!") .size(16), ) - .push(vertical_space(Length::Units(4096))) + .push(vertical_space(4096)) .push( text("You are halfway there!") .width(Length::Fill) .size(30) .horizontal_alignment(alignment::Horizontal::Center), ) - .push(vertical_space(Length::Units(4096))) + .push(vertical_space(4096)) .push(ferris(300)) .push( text("You made it!") @@ -605,7 +605,7 @@ fn ferris<'a>(width: u16) -> Container<'a, StepMessage> { } else { image(format!("{}/images/ferris.png", env!("CARGO_MANIFEST_DIR"))) } - .width(Length::Units(width)), + .width(width), ) .width(Length::Fill) .center_x() @@ -616,7 +616,7 @@ fn button<'a, Message: Clone>(label: &str) -> Button<'a, Message> { text(label).horizontal_alignment(alignment::Horizontal::Center), ) .padding(12) - .width(Length::Units(100)) + .width(100) } fn color_slider<'a>( diff --git a/graphics/src/widget/canvas.rs b/graphics/src/widget/canvas.rs index b070d0a6..a8d050f5 100644 --- a/graphics/src/widget/canvas.rs +++ b/graphics/src/widget/canvas.rs @@ -100,13 +100,13 @@ impl Canvas where P: Program, { - const DEFAULT_SIZE: u16 = 100; + const DEFAULT_SIZE: f32 = 100.0; /// Creates a new [`Canvas`]. pub fn new(program: P) -> Self { Canvas { - width: Length::Units(Self::DEFAULT_SIZE), - height: Length::Units(Self::DEFAULT_SIZE), + width: Length::Fixed(Self::DEFAULT_SIZE), + height: Length::Fixed(Self::DEFAULT_SIZE), program, message_: PhantomData, theme_: PhantomData, @@ -114,14 +114,14 @@ where } /// Sets the width of the [`Canvas`]. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into) -> Self { + self.width = width.into(); self } /// Sets the height of the [`Canvas`]. - pub fn height(mut self, height: Length) -> Self { - self.height = height; + pub fn height(mut self, height: impl Into) -> Self { + self.height = height.into(); self } } diff --git a/lazy/src/responsive.rs b/lazy/src/responsive.rs index 93069493..57c07de1 100644 --- a/lazy/src/responsive.rs +++ b/lazy/src/responsive.rs @@ -42,7 +42,7 @@ where content: RefCell::new(Content { size: Size::ZERO, layout: layout::Node::new(Size::ZERO), - element: Element::new(horizontal_space(Length::Units(0))), + element: Element::new(horizontal_space(0)), }), } } diff --git a/native/src/layout/limits.rs b/native/src/layout/limits.rs index 4cbb970d..137a054c 100644 --- a/native/src/layout/limits.rs +++ b/native/src/layout/limits.rs @@ -42,17 +42,16 @@ impl Limits { } /// Applies a width constraint to the current [`Limits`]. - pub fn width(mut self, width: Length) -> Limits { - match width { + pub fn width(mut self, width: impl Into) -> Limits { + match width.into() { Length::Shrink => { self.fill.width = self.min.width; } Length::Fill | Length::FillPortion(_) => { self.fill.width = self.fill.width.min(self.max.width); } - Length::Units(units) => { - let new_width = - (units as f32).min(self.max.width).max(self.min.width); + Length::Fixed(amount) => { + let new_width = amount.min(self.max.width).max(self.min.width); self.min.width = new_width; self.max.width = new_width; @@ -64,17 +63,17 @@ impl Limits { } /// Applies a height constraint to the current [`Limits`]. - pub fn height(mut self, height: Length) -> Limits { - match height { + pub fn height(mut self, height: impl Into) -> Limits { + match height.into() { Length::Shrink => { self.fill.height = self.min.height; } Length::Fill | Length::FillPortion(_) => { self.fill.height = self.fill.height.min(self.max.height); } - Length::Units(units) => { + Length::Fixed(amount) => { let new_height = - (units as f32).min(self.max.height).max(self.min.height); + amount.min(self.max.height).max(self.min.height); self.min.height = new_height; self.max.height = new_height; @@ -86,33 +85,29 @@ impl Limits { } /// Applies a minimum width constraint to the current [`Limits`]. - pub fn min_width(mut self, min_width: u32) -> Limits { - self.min.width = - self.min.width.max(min_width as f32).min(self.max.width); + pub fn min_width(mut self, min_width: f32) -> Limits { + self.min.width = self.min.width.max(min_width).min(self.max.width); self } /// Applies a maximum width constraint to the current [`Limits`]. - pub fn max_width(mut self, max_width: u32) -> Limits { - self.max.width = - self.max.width.min(max_width as f32).max(self.min.width); + pub fn max_width(mut self, max_width: f32) -> Limits { + self.max.width = self.max.width.min(max_width).max(self.min.width); self } /// Applies a minimum height constraint to the current [`Limits`]. - pub fn min_height(mut self, min_height: u32) -> Limits { - self.min.height = - self.min.height.max(min_height as f32).min(self.max.height); + pub fn min_height(mut self, min_height: f32) -> Limits { + self.min.height = self.min.height.max(min_height).min(self.max.height); self } /// Applies a maximum height constraint to the current [`Limits`]. - pub fn max_height(mut self, max_height: u32) -> Limits { - self.max.height = - self.max.height.min(max_height as f32).max(self.min.height); + pub fn max_height(mut self, max_height: f32) -> Limits { + self.max.height = self.max.height.min(max_height).max(self.min.height); self } diff --git a/native/src/lib.rs b/native/src/lib.rs index 124423a6..7c406ae5 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -81,7 +81,7 @@ pub use iced_core::alignment; pub use iced_core::time; pub use iced_core::{ color, Alignment, Background, Color, ContentFit, Font, Length, Padding, - Point, Rectangle, Size, Vector, + Pixels, Point, Rectangle, Size, Vector, }; pub use iced_futures::{executor, futures}; pub use iced_style::application; diff --git a/native/src/overlay/menu.rs b/native/src/overlay/menu.rs index 9e37380f..bd1f309e 100644 --- a/native/src/overlay/menu.rs +++ b/native/src/overlay/menu.rs @@ -28,7 +28,7 @@ where options: &'a [T], hovered_option: &'a mut Option, last_selection: &'a mut Option, - width: u16, + width: f32, padding: Padding, text_size: Option, font: Renderer::Font, @@ -55,7 +55,7 @@ where options, hovered_option, last_selection, - width: 0, + width: 0.0, padding: Padding::ZERO, text_size: None, font: Default::default(), @@ -64,7 +64,7 @@ where } /// Sets the width of the [`Menu`]. - pub fn width(mut self, width: u16) -> Self { + pub fn width(mut self, width: f32) -> Self { self.width = width; self } @@ -142,7 +142,7 @@ where { state: &'a mut Tree, container: Container<'a, Message, Renderer>, - width: u16, + width: f32, target_height: f32, style: ::Style, } @@ -219,7 +219,7 @@ where }, ), ) - .width(Length::Units(self.width)); + .width(self.width); let mut node = self.container.layout(renderer, &limits); diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index b4276317..3d96dfe1 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -82,14 +82,14 @@ where } /// Sets the width of the [`Button`]. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into) -> Self { + self.width = width.into(); self } /// Sets the height of the [`Button`]. - pub fn height(mut self, height: Length) -> Self { - self.height = height; + pub fn height(mut self, height: impl Into) -> Self { + self.height = height.into(); self } diff --git a/native/src/widget/checkbox.rs b/native/src/widget/checkbox.rs index f6298a8c..d2b5157a 100644 --- a/native/src/widget/checkbox.rs +++ b/native/src/widget/checkbox.rs @@ -8,8 +8,8 @@ use crate::text; use crate::touch; use crate::widget::{self, Row, Text, Tree}; use crate::{ - Alignment, Clipboard, Element, Layout, Length, Point, Rectangle, Shell, - Widget, + Alignment, Clipboard, Element, Layout, Length, Pixels, Point, Rectangle, + Shell, Widget, }; pub use iced_style::checkbox::{Appearance, StyleSheet}; @@ -52,7 +52,7 @@ where on_toggle: Box Message + 'a>, label: String, width: Length, - size: u16, + size: f32, spacing: u16, text_size: Option, font: Renderer::Font, @@ -66,7 +66,7 @@ where Renderer::Theme: StyleSheet + widget::text::StyleSheet, { /// The default size of a [`Checkbox`]. - const DEFAULT_SIZE: u16 = 20; + const DEFAULT_SIZE: f32 = 20.0; /// The default spacing of a [`Checkbox`]. const DEFAULT_SPACING: u16 = 15; @@ -102,14 +102,14 @@ where } /// Sets the size of the [`Checkbox`]. - pub fn size(mut self, size: u16) -> Self { - self.size = size; + pub fn size(mut self, size: impl Into) -> Self { + self.size = size.into().0; self } /// Sets the width of the [`Checkbox`]. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into) -> Self { + self.width = width.into(); self } @@ -172,11 +172,7 @@ where .width(self.width) .spacing(self.spacing) .align_items(Alignment::Center) - .push( - Row::new() - .width(Length::Units(self.size)) - .height(Length::Units(self.size)), - ) + .push(Row::new().width(self.size).height(self.size)) .push( Text::new(&self.label) .font(self.font.clone()) diff --git a/native/src/widget/column.rs b/native/src/widget/column.rs index 5ad4d858..65ca76a1 100644 --- a/native/src/widget/column.rs +++ b/native/src/widget/column.rs @@ -6,8 +6,8 @@ use crate::overlay; use crate::renderer; use crate::widget::{Operation, Tree}; use crate::{ - Alignment, Clipboard, Element, Layout, Length, Padding, Point, Rectangle, - Shell, Widget, + Alignment, Clipboard, Element, Layout, Length, Padding, Pixels, Point, + Rectangle, Shell, Widget, }; /// A container that distributes its contents vertically. @@ -17,7 +17,7 @@ pub struct Column<'a, Message, Renderer> { padding: Padding, width: Length, height: Length, - max_width: u32, + max_width: f32, align_items: Alignment, children: Vec>, } @@ -37,7 +37,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { padding: Padding::ZERO, width: Length::Shrink, height: Length::Shrink, - max_width: u32::MAX, + max_width: f32::INFINITY, align_items: Alignment::Start, children, } @@ -60,20 +60,20 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { } /// Sets the width of the [`Column`]. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into) -> Self { + self.width = width.into(); self } /// Sets the height of the [`Column`]. - pub fn height(mut self, height: Length) -> Self { - self.height = height; + pub fn height(mut self, height: impl Into) -> Self { + self.height = height.into(); self } /// Sets the maximum width of the [`Column`]. - pub fn max_width(mut self, max_width: u32) -> Self { - self.max_width = max_width; + pub fn max_width(mut self, max_width: impl Into) -> Self { + self.max_width = max_width.into().0; self } diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index c82b8be2..1621cf6e 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -7,12 +7,10 @@ use crate::overlay; use crate::renderer; use crate::widget::{self, Operation, Tree}; use crate::{ - Background, Clipboard, Color, Element, Layout, Length, Padding, Point, - Rectangle, Shell, Widget, + Background, Clipboard, Color, Element, Layout, Length, Padding, Pixels, + Point, Rectangle, Shell, Widget, }; -use std::u32; - pub use iced_style::container::{Appearance, StyleSheet}; /// An element decorating some content. @@ -28,8 +26,8 @@ where padding: Padding, width: Length, height: Length, - max_width: u32, - max_height: u32, + max_width: f32, + max_height: f32, horizontal_alignment: alignment::Horizontal, vertical_alignment: alignment::Vertical, style: ::Style, @@ -51,8 +49,8 @@ where padding: Padding::ZERO, width: Length::Shrink, height: Length::Shrink, - max_width: u32::MAX, - max_height: u32::MAX, + max_width: f32::INFINITY, + max_height: f32::INFINITY, horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Top, style: Default::default(), @@ -73,26 +71,26 @@ where } /// Sets the width of the [`Container`]. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into) -> Self { + self.width = width.into(); self } /// Sets the height of the [`Container`]. - pub fn height(mut self, height: Length) -> Self { - self.height = height; + pub fn height(mut self, height: impl Into) -> Self { + self.height = height.into(); self } /// Sets the maximum width of the [`Container`]. - pub fn max_width(mut self, max_width: u32) -> Self { - self.max_width = max_width; + pub fn max_width(mut self, max_width: impl Into) -> Self { + self.max_width = max_width.into().0; self } - /// Sets the maximum height of the [`Container`] in pixels. - pub fn max_height(mut self, max_height: u32) -> Self { - self.max_height = max_height; + /// Sets the maximum height of the [`Container`]. + pub fn max_height(mut self, max_height: impl Into) -> Self { + self.max_height = max_height.into().0; self } @@ -294,8 +292,8 @@ pub fn layout( limits: &layout::Limits, width: Length, height: Length, - max_width: u32, - max_height: u32, + max_width: f32, + max_height: f32, padding: Padding, horizontal_alignment: alignment::Horizontal, vertical_alignment: alignment::Vertical, diff --git a/native/src/widget/helpers.rs b/native/src/widget/helpers.rs index dfd949f6..d13eca75 100644 --- a/native/src/widget/helpers.rs +++ b/native/src/widget/helpers.rs @@ -1,7 +1,7 @@ //! Helper functions to create pure widgets. use crate::overlay; use crate::widget; -use crate::{Element, Length}; +use crate::{Element, Length, Pixels}; use std::borrow::Cow; use std::ops::RangeInclusive; @@ -247,21 +247,23 @@ pub fn image(handle: impl Into) -> widget::Image { /// Creates a new horizontal [`Space`] with the given [`Length`]. /// /// [`Space`]: widget::Space -pub fn horizontal_space(width: Length) -> widget::Space { +pub fn horizontal_space(width: impl Into) -> widget::Space { widget::Space::with_width(width) } /// Creates a new vertical [`Space`] with the given [`Length`]. /// /// [`Space`]: widget::Space -pub fn vertical_space(height: Length) -> widget::Space { +pub fn vertical_space(height: impl Into) -> widget::Space { widget::Space::with_height(height) } /// Creates a horizontal [`Rule`] with the given height. /// /// [`Rule`]: widget::Rule -pub fn horizontal_rule(height: u16) -> widget::Rule +pub fn horizontal_rule( + height: impl Into, +) -> widget::Rule where Renderer: crate::Renderer, Renderer::Theme: widget::rule::StyleSheet, @@ -272,7 +274,9 @@ where /// Creates a vertical [`Rule`] with the given width. /// /// [`Rule`]: widget::Rule -pub fn vertical_rule(width: u16) -> widget::Rule +pub fn vertical_rule( + width: impl Into, +) -> widget::Rule where Renderer: crate::Renderer, Renderer::Theme: widget::rule::StyleSheet, diff --git a/native/src/widget/image.rs b/native/src/widget/image.rs index 3ff06a76..73257a74 100644 --- a/native/src/widget/image.rs +++ b/native/src/widget/image.rs @@ -29,7 +29,7 @@ pub fn viewer(handle: Handle) -> Viewer { /// ``` /// /// -#[derive(Debug, Hash)] +#[derive(Debug)] pub struct Image { handle: Handle, width: Length, @@ -49,14 +49,14 @@ impl Image { } /// Sets the width of the [`Image`] boundaries. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into) -> Self { + self.width = width.into(); self } /// Sets the height of the [`Image`] boundaries. - pub fn height(mut self, height: Length) -> Self { - self.height = height; + pub fn height(mut self, height: impl Into) -> Self { + self.height = height.into(); self } diff --git a/native/src/widget/image/viewer.rs b/native/src/widget/image/viewer.rs index fdbd3216..a9d3e5b4 100644 --- a/native/src/widget/image/viewer.rs +++ b/native/src/widget/image/viewer.rs @@ -45,14 +45,14 @@ impl Viewer { } /// Sets the width of the [`Viewer`]. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into) -> Self { + self.width = width.into(); self } /// Sets the height of the [`Viewer`]. - pub fn height(mut self, height: Length) -> Self { - self.height = height; + pub fn height(mut self, height: impl Into) -> Self { + self.height = height.into(); self } @@ -124,7 +124,7 @@ where // Only calculate viewport sizes if the images are constrained to a limited space. // If they are Fill|Portion let them expand within their alotted space. match expansion_size { - Length::Shrink | Length::Units(_) => { + Length::Shrink | Length::Fixed(_) => { let aspect_ratio = width as f32 / height as f32; let viewport_aspect_ratio = size.width / size.height; if viewport_aspect_ratio > aspect_ratio { diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index eb04c0ba..c6ff61ac 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -159,14 +159,14 @@ where } /// Sets the width of the [`PaneGrid`]. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into) -> Self { + self.width = width.into(); self } /// Sets the height of the [`PaneGrid`]. - pub fn height(mut self, height: Length) -> Self { - self.height = height; + pub fn height(mut self, height: impl Into) -> Self { + self.height = height.into(); self } diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs index b1cdfad4..1c0886f2 100644 --- a/native/src/widget/pick_list.rs +++ b/native/src/widget/pick_list.rs @@ -83,8 +83,8 @@ where } /// Sets the width of the [`PickList`]. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into) -> Self { + self.width = width.into(); self } @@ -539,7 +539,7 @@ where &mut state.hovered_option, &mut state.last_selection, ) - .width(bounds.width.round() as u16) + .width(bounds.width) .padding(padding) .font(font) .style(style); diff --git a/native/src/widget/progress_bar.rs b/native/src/widget/progress_bar.rs index 7d5d5be5..dd46fa76 100644 --- a/native/src/widget/progress_bar.rs +++ b/native/src/widget/progress_bar.rs @@ -38,7 +38,7 @@ where Renderer::Theme: StyleSheet, { /// The default height of a [`ProgressBar`]. - pub const DEFAULT_HEIGHT: u16 = 30; + pub const DEFAULT_HEIGHT: f32 = 30.0; /// Creates a new [`ProgressBar`]. /// @@ -56,14 +56,14 @@ where } /// Sets the width of the [`ProgressBar`]. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into) -> Self { + self.width = width.into(); self } /// Sets the height of the [`ProgressBar`]. - pub fn height(mut self, height: Length) -> Self { - self.height = Some(height); + pub fn height(mut self, height: impl Into) -> Self { + self.height = Some(height.into()); self } @@ -87,7 +87,7 @@ where } fn height(&self) -> Length { - self.height.unwrap_or(Length::Units(Self::DEFAULT_HEIGHT)) + self.height.unwrap_or(Length::Fixed(Self::DEFAULT_HEIGHT)) } fn layout( @@ -97,7 +97,7 @@ where ) -> layout::Node { let limits = limits .width(self.width) - .height(self.height.unwrap_or(Length::Units(Self::DEFAULT_HEIGHT))); + .height(self.height.unwrap_or(Length::Fixed(Self::DEFAULT_HEIGHT))); let size = limits.resolve(Size::ZERO); diff --git a/native/src/widget/radio.rs b/native/src/widget/radio.rs index b95ccc5b..22aeb4ce 100644 --- a/native/src/widget/radio.rs +++ b/native/src/widget/radio.rs @@ -50,7 +50,7 @@ where on_click: Message, label: String, width: Length, - size: u16, + size: f32, spacing: u16, text_size: Option, font: Renderer::Font, @@ -64,7 +64,7 @@ where Renderer::Theme: StyleSheet, { /// The default size of a [`Radio`] button. - pub const DEFAULT_SIZE: u16 = 28; + pub const DEFAULT_SIZE: f32 = 28.0; /// The default spacing of a [`Radio`] button. pub const DEFAULT_SPACING: u16 = 15; @@ -101,14 +101,14 @@ where } /// Sets the size of the [`Radio`] button. - pub fn size(mut self, size: u16) -> Self { + pub fn size(mut self, size: f32) -> Self { self.size = size; self } /// Sets the width of the [`Radio`] button. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into) -> Self { + self.width = width.into(); self } @@ -163,11 +163,7 @@ where .width(self.width) .spacing(self.spacing) .align_items(Alignment::Center) - .push( - Row::new() - .width(Length::Units(self.size)) - .height(Length::Units(self.size)), - ) + .push(Row::new().width(self.size).height(self.size)) .push(Text::new(&self.label).width(self.width).size( self.text_size.unwrap_or_else(|| renderer.default_size()), )) diff --git a/native/src/widget/row.rs b/native/src/widget/row.rs index 108e98e4..a340d84a 100644 --- a/native/src/widget/row.rs +++ b/native/src/widget/row.rs @@ -58,14 +58,14 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { } /// Sets the width of the [`Row`]. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into) -> Self { + self.width = width.into(); self } /// Sets the height of the [`Row`]. - pub fn height(mut self, height: Length) -> Self { - self.height = height; + pub fn height(mut self, height: impl Into) -> Self { + self.height = height.into(); self } diff --git a/native/src/widget/rule.rs b/native/src/widget/rule.rs index 2dc7b6f0..1ab6a0d3 100644 --- a/native/src/widget/rule.rs +++ b/native/src/widget/rule.rs @@ -2,7 +2,9 @@ use crate::layout; use crate::renderer; use crate::widget::Tree; -use crate::{Color, Element, Layout, Length, Point, Rectangle, Size, Widget}; +use crate::{ + Color, Element, Layout, Length, Pixels, Point, Rectangle, Size, Widget, +}; pub use iced_style::rule::{Appearance, FillMode, StyleSheet}; @@ -25,19 +27,19 @@ where Renderer::Theme: StyleSheet, { /// Creates a horizontal [`Rule`] with the given height. - pub fn horizontal(height: u16) -> Self { + pub fn horizontal(height: impl Into) -> Self { Rule { width: Length::Fill, - height: Length::Units(height), + height: Length::Fixed(height.into().0), is_horizontal: true, style: Default::default(), } } /// Creates a vertical [`Rule`] with the given width. - pub fn vertical(width: u16) -> Self { + pub fn vertical(width: impl Into) -> Self { Rule { - width: Length::Units(width), + width: Length::Fixed(width.into().0), height: Length::Fill, is_horizontal: false, style: Default::default(), diff --git a/native/src/widget/scrollable.rs b/native/src/widget/scrollable.rs index 2de722e4..e084d4b0 100644 --- a/native/src/widget/scrollable.rs +++ b/native/src/widget/scrollable.rs @@ -10,8 +10,8 @@ use crate::widget; use crate::widget::operation::{self, Operation}; use crate::widget::tree::{self, Tree}; use crate::{ - Background, Clipboard, Color, Command, Element, Layout, Length, Point, - Rectangle, Shell, Size, Vector, Widget, + Background, Clipboard, Color, Command, Element, Layout, Length, Pixels, + Point, Rectangle, Shell, Size, Vector, Widget, }; pub use iced_style::scrollable::StyleSheet; @@ -66,8 +66,8 @@ where } /// Sets the height of the [`Scrollable`]. - pub fn height(mut self, height: Length) -> Self { - self.height = height; + pub fn height(mut self, height: impl Into) -> Self { + self.height = height.into(); self } @@ -108,17 +108,17 @@ where /// Properties of a scrollbar within a [`Scrollable`]. #[derive(Debug)] pub struct Properties { - width: u16, - margin: u16, - scroller_width: u16, + width: f32, + margin: f32, + scroller_width: f32, } impl Default for Properties { fn default() -> Self { Self { - width: 10, - margin: 0, - scroller_width: 10, + width: 10.0, + margin: 0.0, + scroller_width: 10.0, } } } @@ -131,21 +131,21 @@ impl Properties { /// Sets the scrollbar width of the [`Scrollable`] . /// Silently enforces a minimum width of 1. - pub fn width(mut self, width: u16) -> Self { - self.width = width.max(1); + pub fn width(mut self, width: impl Into) -> Self { + self.width = width.into().0.max(1.0); self } /// Sets the scrollbar margin of the [`Scrollable`] . - pub fn margin(mut self, margin: u16) -> Self { - self.margin = margin; + pub fn margin(mut self, margin: impl Into) -> Self { + self.margin = margin.into().0; self } /// Sets the scroller width of the [`Scrollable`] . /// Silently enforces a minimum width of 1. - pub fn scroller_width(mut self, scroller_width: u16) -> Self { - self.scroller_width = scroller_width.max(1); + pub fn scroller_width(mut self, scroller_width: impl Into) -> Self { + self.scroller_width = scroller_width.into().0.max(1.0); self } } @@ -398,11 +398,11 @@ pub fn layout( layout_content: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node, ) -> layout::Node { let limits = limits - .max_height(u32::MAX) + .max_height(f32::INFINITY) .max_width(if horizontal_enabled { - u32::MAX + f32::INFINITY } else { - limits.max().width as u32 + limits.max().width }) .width(width) .height(height); @@ -1100,26 +1100,26 @@ impl Scrollbars { // Adjust the height of the vertical scrollbar if the horizontal scrollbar // is present - let x_scrollbar_height = show_scrollbar_x.map_or(0.0, |h| { - (h.width.max(h.scroller_width) + h.margin) as f32 - }); + let x_scrollbar_height = show_scrollbar_x + .map_or(0.0, |h| h.width.max(h.scroller_width) + h.margin); - let total_scrollbar_width = width.max(scroller_width) + 2 * margin; + let total_scrollbar_width = + width.max(scroller_width) + 2.0 * margin; // Total bounds of the scrollbar + margin + scroller width let total_scrollbar_bounds = Rectangle { - x: bounds.x + bounds.width - total_scrollbar_width as f32, + x: bounds.x + bounds.width - total_scrollbar_width, y: bounds.y, - width: total_scrollbar_width as f32, + width: total_scrollbar_width, height: (bounds.height - x_scrollbar_height).max(0.0), }; // Bounds of just the scrollbar let scrollbar_bounds = Rectangle { - x: bounds.x + bounds.width - - f32::from(total_scrollbar_width / 2 + width / 2), + x: bounds.x + bounds.width - total_scrollbar_width / 2.0 + + width / 2.0, y: bounds.y, - width: width as f32, + width, height: (bounds.height - x_scrollbar_height).max(0.0), }; @@ -1129,11 +1129,11 @@ impl Scrollbars { let scroller_offset = offset.y * ratio; let scroller_bounds = Rectangle { - x: bounds.x + bounds.width - - f32::from(total_scrollbar_width / 2 + scroller_width / 2), + x: bounds.x + bounds.width - total_scrollbar_width / 2.0 + + scroller_width / 2.0, y: (scrollbar_bounds.y + scroller_offset - x_scrollbar_height) .max(0.0), - width: scroller_width as f32, + width: scroller_width, height: scroller_height, }; @@ -1158,27 +1158,27 @@ impl Scrollbars { // Need to adjust the width of the horizontal scrollbar if the vertical scrollbar // is present let scrollbar_y_width = y_scrollbar.map_or(0.0, |_| { - (vertical.width.max(vertical.scroller_width) + vertical.margin) - as f32 + vertical.width.max(vertical.scroller_width) + vertical.margin }); - let total_scrollbar_height = width.max(scroller_width) + 2 * margin; + let total_scrollbar_height = + width.max(scroller_width) + 2.0 * margin; // Total bounds of the scrollbar + margin + scroller width let total_scrollbar_bounds = Rectangle { x: bounds.x, - y: bounds.y + bounds.height - total_scrollbar_height as f32, + y: bounds.y + bounds.height - total_scrollbar_height, width: (bounds.width - scrollbar_y_width).max(0.0), - height: total_scrollbar_height as f32, + height: total_scrollbar_height, }; // Bounds of just the scrollbar let scrollbar_bounds = Rectangle { x: bounds.x, - y: bounds.y + bounds.height - - f32::from(total_scrollbar_height / 2 + width / 2), + y: bounds.y + bounds.height - total_scrollbar_height / 2.0 + + width / 2.0, width: (bounds.width - scrollbar_y_width).max(0.0), - height: width as f32, + height: width, }; let ratio = bounds.width / content_bounds.width; @@ -1189,12 +1189,11 @@ impl Scrollbars { let scroller_bounds = Rectangle { x: (scrollbar_bounds.x + scroller_offset - scrollbar_y_width) .max(0.0), - y: bounds.y + bounds.height - - f32::from( - total_scrollbar_height / 2 + scroller_width / 2, - ), + y: bounds.y + bounds.height - total_scrollbar_height / 2.0 + + scroller_width / 2.0, + width: scroller_length, - height: scroller_width as f32, + height: scroller_width, }; Some(Scrollbar { diff --git a/native/src/widget/slider.rs b/native/src/widget/slider.rs index 87030a4d..854f71c3 100644 --- a/native/src/widget/slider.rs +++ b/native/src/widget/slider.rs @@ -54,7 +54,7 @@ where on_change: Box Message + 'a>, on_release: Option, width: Length, - height: u16, + height: f32, style: ::Style, } @@ -66,7 +66,7 @@ where Renderer::Theme: StyleSheet, { /// The default height of a [`Slider`]. - pub const DEFAULT_HEIGHT: u16 = 22; + pub const DEFAULT_HEIGHT: f32 = 22.0; /// Creates a new [`Slider`]. /// @@ -116,13 +116,13 @@ where } /// Sets the width of the [`Slider`]. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into) -> Self { + self.width = width.into(); self } /// Sets the height of the [`Slider`]. - pub fn height(mut self, height: u16) -> Self { + pub fn height(mut self, height: f32) -> Self { self.height = height; self } @@ -172,9 +172,7 @@ where _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let limits = - limits.width(self.width).height(Length::Units(self.height)); - + let limits = limits.width(self.width).height(self.height); let size = limits.resolve(Size::ZERO); layout::Node::new(size) diff --git a/native/src/widget/space.rs b/native/src/widget/space.rs index 9f835893..a6fc977e 100644 --- a/native/src/widget/space.rs +++ b/native/src/widget/space.rs @@ -15,23 +15,26 @@ pub struct Space { impl Space { /// Creates an amount of empty [`Space`] with the given width and height. - pub fn new(width: Length, height: Length) -> Self { - Space { width, height } + pub fn new(width: impl Into, height: impl Into) -> Self { + Space { + width: width.into(), + height: height.into(), + } } /// Creates an amount of horizontal [`Space`]. - pub fn with_width(width: Length) -> Self { + pub fn with_width(width: impl Into) -> Self { Space { - width, + width: width.into(), height: Length::Shrink, } } /// Creates an amount of vertical [`Space`]. - pub fn with_height(height: Length) -> Self { + pub fn with_height(height: impl Into) -> Self { Space { width: Length::Shrink, - height, + height: height.into(), } } } diff --git a/native/src/widget/svg.rs b/native/src/widget/svg.rs index f83f5acf..f5ed0a6c 100644 --- a/native/src/widget/svg.rs +++ b/native/src/widget/svg.rs @@ -56,15 +56,15 @@ where /// Sets the width of the [`Svg`]. #[must_use] - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into) -> Self { + self.width = width.into(); self } /// Sets the height of the [`Svg`]. #[must_use] - pub fn height(mut self, height: Length) -> Self { - self.height = height; + pub fn height(mut self, height: impl Into) -> Self { + self.height = height.into(); self } diff --git a/native/src/widget/text.rs b/native/src/widget/text.rs index be9e775e..bac1adcf 100644 --- a/native/src/widget/text.rs +++ b/native/src/widget/text.rs @@ -84,14 +84,14 @@ where } /// Sets the width of the [`Text`] boundaries. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into) -> Self { + self.width = width.into(); self } /// Sets the height of the [`Text`] boundaries. - pub fn height(mut self, height: Length) -> Self { - self.height = height; + pub fn height(mut self, height: impl Into) -> Self { + self.height = height.into(); self } diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index 5bfc918c..d4ab3d8d 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -133,8 +133,8 @@ where self } /// Sets the width of the [`TextInput`]. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into) -> Self { + self.width = width.into(); self } @@ -387,11 +387,7 @@ where let text_size = size.unwrap_or_else(|| renderer.default_size()); let padding = padding.fit(Size::ZERO, limits.max()); - - let limits = limits - .width(width) - .pad(padding) - .height(Length::Units(text_size)); + let limits = limits.width(width).pad(padding).height(text_size); let mut text = layout::Node::new(limits.resolve(Size::ZERO)); text.move_to(Point::new(padding.left.into(), padding.top.into())); diff --git a/native/src/widget/toggler.rs b/native/src/widget/toggler.rs index f0a944a3..13d0124d 100644 --- a/native/src/widget/toggler.rs +++ b/native/src/widget/toggler.rs @@ -38,7 +38,7 @@ where on_toggle: Box Message + 'a>, label: Option, width: Length, - size: u16, + size: f32, text_size: Option, text_alignment: alignment::Horizontal, spacing: u16, @@ -52,7 +52,7 @@ where Renderer::Theme: StyleSheet, { /// The default size of a [`Toggler`]. - pub const DEFAULT_SIZE: u16 = 20; + pub const DEFAULT_SIZE: f32 = 20.0; /// Creates a new [`Toggler`]. /// @@ -85,14 +85,14 @@ where } /// Sets the size of the [`Toggler`]. - pub fn size(mut self, size: u16) -> Self { + pub fn size(mut self, size: f32) -> Self { self.size = size; self } /// Sets the width of the [`Toggler`]. - pub fn width(mut self, width: Length) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into) -> Self { + self.width = width.into(); self } @@ -169,11 +169,7 @@ where ); } - row = row.push( - Row::new() - .width(Length::Units(2 * self.size)) - .height(Length::Units(self.size)), - ); + row = row.push(Row::new().width(2.0 * self.size).height(self.size)); row.layout(renderer, limits) } diff --git a/native/src/widget/vertical_slider.rs b/native/src/widget/vertical_slider.rs index 28e8405c..7363b55f 100644 --- a/native/src/widget/vertical_slider.rs +++ b/native/src/widget/vertical_slider.rs @@ -47,7 +47,7 @@ where value: T, on_change: Box Message + 'a>, on_release: Option, - width: u16, + width: f32, height: Length, style: ::Style, } @@ -60,7 +60,7 @@ where Renderer::Theme: StyleSheet, { /// The default width of a [`VerticalSlider`]. - pub const DEFAULT_WIDTH: u16 = 22; + pub const DEFAULT_WIDTH: f32 = 22.0; /// Creates a new [`VerticalSlider`]. /// @@ -110,14 +110,14 @@ where } /// Sets the width of the [`VerticalSlider`]. - pub fn width(mut self, width: u16) -> Self { + pub fn width(mut self, width: f32) -> Self { self.width = width; self } /// Sets the height of the [`VerticalSlider`]. - pub fn height(mut self, height: Length) -> Self { - self.height = height; + pub fn height(mut self, height: impl Into) -> Self { + self.height = height.into(); self } @@ -166,9 +166,7 @@ where _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let limits = - limits.width(Length::Units(self.width)).height(self.height); - + let limits = limits.width(self.width).height(self.height); let size = limits.resolve(Size::ZERO); layout::Node::new(size) -- cgit From 570600ce513e7e02b23c1da8322c68fbb876d1b0 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 4 Feb 2023 16:41:18 +0100 Subject: Use `Pixels` for `Text::size` --- glow/src/backend.rs | 4 ++-- glow/src/settings.rs | 8 ++++---- graphics/src/backend.rs | 2 +- graphics/src/renderer.rs | 4 ++-- native/src/overlay/menu.rs | 26 ++++++++++++++------------ native/src/renderer/null.rs | 6 +++--- native/src/text.rs | 6 +++--- native/src/widget/checkbox.rs | 6 +++--- native/src/widget/pick_list.rs | 25 ++++++++++++------------- native/src/widget/radio.rs | 10 +++++----- native/src/widget/text.rs | 12 ++++++------ native/src/widget/text_input.rs | 20 ++++++++++---------- native/src/widget/toggler.rs | 10 +++++----- src/settings.rs | 6 +++--- wgpu/src/backend.rs | 4 ++-- wgpu/src/settings.rs | 8 ++++---- 16 files changed, 79 insertions(+), 78 deletions(-) diff --git a/glow/src/backend.rs b/glow/src/backend.rs index 416c3b94..36a34eda 100644 --- a/glow/src/backend.rs +++ b/glow/src/backend.rs @@ -22,7 +22,7 @@ pub struct Backend { quad_pipeline: quad::Pipeline, text_pipeline: text::Pipeline, triangle_pipeline: triangle::Pipeline, - default_text_size: u16, + default_text_size: f32, } impl Backend { @@ -228,7 +228,7 @@ impl backend::Text for Backend { const CHECKMARK_ICON: char = font::CHECKMARK_ICON; const ARROW_DOWN_ICON: char = font::ARROW_DOWN_ICON; - fn default_size(&self) -> u16 { + fn default_size(&self) -> f32 { self.default_text_size } diff --git a/glow/src/settings.rs b/glow/src/settings.rs index 3691747b..8ccffbad 100644 --- a/glow/src/settings.rs +++ b/glow/src/settings.rs @@ -4,7 +4,7 @@ pub use iced_graphics::Antialiasing; /// The settings of a [`Backend`]. /// /// [`Backend`]: crate::Backend -#[derive(Clone, Copy, PartialEq, Eq)] +#[derive(Clone, Copy, PartialEq)] pub struct Settings { /// The bytes of the font that will be used by default. /// @@ -13,8 +13,8 @@ pub struct Settings { /// The default size of text. /// - /// By default, it will be set to 20. - pub default_text_size: u16, + /// By default, it will be set to `20.0`. + pub default_text_size: f32, /// If enabled, spread text workload in multiple threads when multiple cores /// are available. @@ -32,7 +32,7 @@ impl Default for Settings { fn default() -> Settings { Settings { default_font: None, - default_text_size: 20, + default_text_size: 20.0, text_multithreading: false, antialiasing: None, } diff --git a/graphics/src/backend.rs b/graphics/src/backend.rs index 2f8e9fc3..256b7ab5 100644 --- a/graphics/src/backend.rs +++ b/graphics/src/backend.rs @@ -32,7 +32,7 @@ pub trait Text { const ARROW_DOWN_ICON: char; /// Returns the default size of text. - fn default_size(&self) -> u16; + fn default_size(&self) -> f32; /// Measures the text contents with the given size and font, /// returning the size of a laid out paragraph that fits in the provided diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index 298cf4a1..e07e7206 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -130,14 +130,14 @@ where const CHECKMARK_ICON: char = B::CHECKMARK_ICON; const ARROW_DOWN_ICON: char = B::ARROW_DOWN_ICON; - fn default_size(&self) -> u16 { + fn default_size(&self) -> f32 { self.backend().default_size() } fn measure( &self, content: &str, - size: u16, + size: f32, font: Font, bounds: Size, ) -> (f32, f32) { diff --git a/native/src/overlay/menu.rs b/native/src/overlay/menu.rs index bd1f309e..fac3028e 100644 --- a/native/src/overlay/menu.rs +++ b/native/src/overlay/menu.rs @@ -11,8 +11,8 @@ use crate::widget::container::{self, Container}; use crate::widget::scrollable::{self, Scrollable}; use crate::widget::Tree; use crate::{ - Clipboard, Color, Element, Layout, Length, Padding, Point, Rectangle, - Shell, Size, Vector, Widget, + Clipboard, Color, Element, Layout, Length, Padding, Pixels, Point, + Rectangle, Shell, Size, Vector, Widget, }; pub use iced_style::menu::{Appearance, StyleSheet}; @@ -30,7 +30,7 @@ where last_selection: &'a mut Option, width: f32, padding: Padding, - text_size: Option, + text_size: Option, font: Renderer::Font, style: ::Style, } @@ -76,8 +76,8 @@ where } /// Sets the text size of the [`Menu`]. - pub fn text_size(mut self, text_size: u16) -> Self { - self.text_size = Some(text_size); + pub fn text_size(mut self, text_size: impl Into) -> Self { + self.text_size = Some(text_size.into().0); self } @@ -310,7 +310,7 @@ where hovered_option: &'a mut Option, last_selection: &'a mut Option, padding: Padding, - text_size: Option, + text_size: Option, font: Renderer::Font, style: ::Style, } @@ -344,8 +344,9 @@ where let size = { let intrinsic = Size::new( 0.0, - f32::from(text_size + self.padding.vertical()) - * self.options.len() as f32, + text_size + + f32::from(self.padding.vertical()) + * self.options.len() as f32, ); limits.resolve(intrinsic) @@ -386,7 +387,7 @@ where *self.hovered_option = Some( ((cursor_position.y - bounds.y) - / f32::from(text_size + self.padding.vertical())) + / (text_size + f32::from(self.padding.vertical()))) as usize, ); } @@ -401,7 +402,7 @@ where *self.hovered_option = Some( ((cursor_position.y - bounds.y) - / f32::from(text_size + self.padding.vertical())) + / (text_size + f32::from(self.padding.vertical()))) as usize, ); @@ -450,7 +451,8 @@ where let text_size = self.text_size.unwrap_or_else(|| renderer.default_size()); - let option_height = (text_size + self.padding.vertical()) as usize; + let option_height = + (text_size + f32::from(self.padding.vertical())) as usize; let offset = viewport.y - bounds.y; let start = (offset / option_height as f32) as usize; @@ -467,7 +469,7 @@ where x: bounds.x, y: bounds.y + (option_height * i) as f32, width: bounds.width, - height: f32::from(text_size + self.padding.vertical()), + height: text_size + f32::from(self.padding.vertical()), }; if is_selected { diff --git a/native/src/renderer/null.rs b/native/src/renderer/null.rs index b1743dbf..9376d540 100644 --- a/native/src/renderer/null.rs +++ b/native/src/renderer/null.rs @@ -44,14 +44,14 @@ impl text::Renderer for Null { const CHECKMARK_ICON: char = '0'; const ARROW_DOWN_ICON: char = '0'; - fn default_size(&self) -> u16 { - 20 + fn default_size(&self) -> f32 { + 20.0 } fn measure( &self, _content: &str, - _size: u16, + _size: f32, _font: Font, _bounds: Size, ) -> (f32, f32) { diff --git a/native/src/text.rs b/native/src/text.rs index 6e28681d..55c3cfd3 100644 --- a/native/src/text.rs +++ b/native/src/text.rs @@ -73,20 +73,20 @@ pub trait Renderer: crate::Renderer { const ARROW_DOWN_ICON: char; /// Returns the default size of [`Text`]. - fn default_size(&self) -> u16; + fn default_size(&self) -> f32; /// Measures the text in the given bounds and returns the minimum boundaries /// that can fit the contents. fn measure( &self, content: &str, - size: u16, + size: f32, font: Self::Font, bounds: Size, ) -> (f32, f32); /// Measures the width of the text as if it were laid out in a single line. - fn measure_width(&self, content: &str, size: u16, font: Self::Font) -> f32 { + fn measure_width(&self, content: &str, size: f32, font: Self::Font) -> f32 { let (width, _) = self.measure(content, size, font, Size::INFINITY); width diff --git a/native/src/widget/checkbox.rs b/native/src/widget/checkbox.rs index d2b5157a..835a0651 100644 --- a/native/src/widget/checkbox.rs +++ b/native/src/widget/checkbox.rs @@ -54,7 +54,7 @@ where width: Length, size: f32, spacing: u16, - text_size: Option, + text_size: Option, font: Renderer::Font, icon: Icon, style: ::Style, @@ -120,8 +120,8 @@ where } /// Sets the text size of the [`Checkbox`]. - pub fn text_size(mut self, text_size: u16) -> Self { - self.text_size = Some(text_size); + pub fn text_size(mut self, text_size: impl Into) -> Self { + self.text_size = Some(text_size.into().0); self } diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs index 1c0886f2..7a61d560 100644 --- a/native/src/widget/pick_list.rs +++ b/native/src/widget/pick_list.rs @@ -13,8 +13,8 @@ use crate::widget::container; use crate::widget::scrollable; use crate::widget::tree::{self, Tree}; use crate::{ - Clipboard, Element, Layout, Length, Padding, Point, Rectangle, Shell, Size, - Widget, + Clipboard, Element, Layout, Length, Padding, Pixels, Point, Rectangle, + Shell, Size, Widget, }; use std::borrow::Cow; @@ -34,7 +34,7 @@ where selected: Option, width: Length, padding: Padding, - text_size: Option, + text_size: Option, font: Renderer::Font, handle: Handle, style: ::Style, @@ -95,8 +95,8 @@ where } /// Sets the text size of the [`PickList`]. - pub fn text_size(mut self, size: u16) -> Self { - self.text_size = Some(size); + pub fn text_size(mut self, size: impl Into) -> Self { + self.text_size = Some(size.into().0); self } @@ -297,14 +297,14 @@ impl Default for State { } /// The handle to the right side of the [`PickList`]. -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq)] pub enum Handle { /// Displays an arrow icon (▼). /// /// This is the default. Arrow { /// Font size of the content. - size: Option, + size: Option, }, /// A custom static handle. Static(Icon), @@ -326,14 +326,14 @@ impl Default for Handle { } /// The icon of a [`Handle`]. -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq)] pub struct Icon { /// Font that will be used to display the `code_point`, pub font: Font, /// The unicode code point that will be used as the icon. pub code_point: char, /// Font size of the content. - pub size: Option, + pub size: Option, } /// Computes the layout of a [`PickList`]. @@ -342,7 +342,7 @@ pub fn layout( limits: &layout::Limits, width: Length, padding: Padding, - text_size: Option, + text_size: Option, font: &Renderer::Font, placeholder: Option<&str>, options: &[T], @@ -354,7 +354,6 @@ where use std::f32; let limits = limits.width(width).height(Length::Shrink).pad(padding); - let text_size = text_size.unwrap_or_else(|| renderer.default_size()); let max_width = match width { @@ -514,7 +513,7 @@ pub fn overlay<'a, T, Message, Renderer>( layout: Layout<'_>, state: &'a mut State, padding: Padding, - text_size: Option, + text_size: Option, font: Renderer::Font, options: &'a [T], style: ::Style, @@ -561,7 +560,7 @@ pub fn draw<'a, T, Renderer>( layout: Layout<'_>, cursor_position: Point, padding: Padding, - text_size: Option, + text_size: Option, font: &Renderer::Font, placeholder: Option<&str>, selected: Option<&T>, diff --git a/native/src/widget/radio.rs b/native/src/widget/radio.rs index 22aeb4ce..62bc2447 100644 --- a/native/src/widget/radio.rs +++ b/native/src/widget/radio.rs @@ -8,8 +8,8 @@ use crate::text; use crate::touch; use crate::widget::{self, Row, Text, Tree}; use crate::{ - Alignment, Clipboard, Color, Element, Layout, Length, Point, Rectangle, - Shell, Widget, + Alignment, Clipboard, Color, Element, Layout, Length, Pixels, Point, + Rectangle, Shell, Widget, }; pub use iced_style::radio::{Appearance, StyleSheet}; @@ -52,7 +52,7 @@ where width: Length, size: f32, spacing: u16, - text_size: Option, + text_size: Option, font: Renderer::Font, style: ::Style, } @@ -119,8 +119,8 @@ where } /// Sets the text size of the [`Radio`] button. - pub fn text_size(mut self, text_size: u16) -> Self { - self.text_size = Some(text_size); + pub fn text_size(mut self, text_size: impl Into) -> Self { + self.text_size = Some(text_size.into().0); self } diff --git a/native/src/widget/text.rs b/native/src/widget/text.rs index bac1adcf..3fee48f2 100644 --- a/native/src/widget/text.rs +++ b/native/src/widget/text.rs @@ -4,7 +4,7 @@ use crate::layout; use crate::renderer; use crate::text; use crate::widget::Tree; -use crate::{Element, Layout, Length, Point, Rectangle, Size, Widget}; +use crate::{Element, Layout, Length, Pixels, Point, Rectangle, Size, Widget}; use std::borrow::Cow; @@ -32,7 +32,7 @@ where Renderer::Theme: StyleSheet, { content: Cow<'a, str>, - size: Option, + size: Option, width: Length, height: Length, horizontal_alignment: alignment::Horizontal, @@ -61,8 +61,8 @@ where } /// Sets the size of the [`Text`]. - pub fn size(mut self, size: u16) -> Self { - self.size = Some(size); + pub fn size(mut self, size: impl Into) -> Self { + self.size = Some(size.into().0); self } @@ -185,7 +185,7 @@ pub fn draw( style: &renderer::Style, layout: Layout<'_>, content: &str, - size: Option, + size: Option, font: Renderer::Font, appearance: Appearance, horizontal_alignment: alignment::Horizontal, @@ -209,7 +209,7 @@ pub fn draw( renderer.fill_text(crate::text::Text { content, - size: f32::from(size.unwrap_or_else(|| renderer.default_size())), + size: size.unwrap_or_else(|| renderer.default_size()), bounds: Rectangle { x, y, ..bounds }, color: appearance.color.unwrap_or(style.text_color), font, diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index d4ab3d8d..02304cae 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -25,7 +25,7 @@ use crate::widget::operation::{self, Operation}; use crate::widget::tree::{self, Tree}; use crate::window; use crate::{ - Clipboard, Color, Command, Element, Layout, Length, Padding, Point, + Clipboard, Color, Command, Element, Layout, Length, Padding, Pixels, Point, Rectangle, Shell, Size, Vector, Widget, }; @@ -64,7 +64,7 @@ where font: Renderer::Font, width: Length, padding: Padding, - size: Option, + size: Option, on_change: Box Message + 'a>, on_paste: Option Message + 'a>>, on_submit: Option, @@ -145,8 +145,8 @@ where } /// Sets the text size of the [`TextInput`]. - pub fn size(mut self, size: u16) -> Self { - self.size = Some(size); + pub fn size(mut self, size: impl Into) -> Self { + self.size = Some(size.into().0); self } @@ -379,7 +379,7 @@ pub fn layout( limits: &layout::Limits, width: Length, padding: Padding, - size: Option, + size: Option, ) -> layout::Node where Renderer: text::Renderer, @@ -405,7 +405,7 @@ pub fn update<'a, Message, Renderer>( clipboard: &mut dyn Clipboard, shell: &mut Shell<'_, Message>, value: &mut Value, - size: Option, + size: Option, font: &Renderer::Font, is_secure: bool, on_change: &dyn Fn(String) -> Message, @@ -811,7 +811,7 @@ pub fn draw( state: &State, value: &Value, placeholder: &str, - size: Option, + size: Option, font: &Renderer::Font, is_secure: bool, style: &::Style, @@ -1124,7 +1124,7 @@ fn offset( renderer: &Renderer, text_bounds: Rectangle, font: Renderer::Font, - size: u16, + size: f32, value: &Value, state: &State, ) -> f32 @@ -1158,7 +1158,7 @@ fn measure_cursor_and_scroll_offset( renderer: &Renderer, text_bounds: Rectangle, value: &Value, - size: u16, + size: f32, cursor_index: usize, font: Renderer::Font, ) -> (f32, f32) @@ -1181,7 +1181,7 @@ fn find_cursor_position( renderer: &Renderer, text_bounds: Rectangle, font: Renderer::Font, - size: Option, + size: Option, value: &Value, state: &State, x: f32, diff --git a/native/src/widget/toggler.rs b/native/src/widget/toggler.rs index 13d0124d..eda8f85f 100644 --- a/native/src/widget/toggler.rs +++ b/native/src/widget/toggler.rs @@ -7,8 +7,8 @@ use crate::renderer; use crate::text; use crate::widget::{self, Row, Text, Tree}; use crate::{ - Alignment, Clipboard, Element, Event, Layout, Length, Point, Rectangle, - Shell, Widget, + Alignment, Clipboard, Element, Event, Layout, Length, Pixels, Point, + Rectangle, Shell, Widget, }; pub use iced_style::toggler::{Appearance, StyleSheet}; @@ -39,7 +39,7 @@ where label: Option, width: Length, size: f32, - text_size: Option, + text_size: Option, text_alignment: alignment::Horizontal, spacing: u16, font: Renderer::Font, @@ -97,8 +97,8 @@ where } /// Sets the text size o the [`Toggler`]. - pub fn text_size(mut self, text_size: u16) -> Self { - self.text_size = Some(text_size); + pub fn text_size(mut self, text_size: impl Into) -> Self { + self.text_size = Some(text_size.into().0); self } diff --git a/src/settings.rs b/src/settings.rs index d31448fb..0eb3e62d 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -28,8 +28,8 @@ pub struct Settings { /// The text size that will be used by default. /// - /// The default value is 20. - pub default_text_size: u16, + /// The default value is `20.0`. + pub default_text_size: f32, /// If enabled, spread text workload in multiple threads when multiple cores /// are available. @@ -97,7 +97,7 @@ where window: Default::default(), flags: Default::default(), default_font: Default::default(), - default_text_size: 20, + default_text_size: 20.0, text_multithreading: false, antialiasing: false, exit_on_close_request: true, diff --git a/wgpu/src/backend.rs b/wgpu/src/backend.rs index 9ab12ce0..6a299425 100644 --- a/wgpu/src/backend.rs +++ b/wgpu/src/backend.rs @@ -29,7 +29,7 @@ pub struct Backend { #[cfg(any(feature = "image", feature = "svg"))] image_pipeline: image::Pipeline, - default_text_size: u16, + default_text_size: f32, } impl Backend { @@ -265,7 +265,7 @@ impl backend::Text for Backend { const CHECKMARK_ICON: char = font::CHECKMARK_ICON; const ARROW_DOWN_ICON: char = font::ARROW_DOWN_ICON; - fn default_size(&self) -> u16 { + fn default_size(&self) -> f32 { self.default_text_size } diff --git a/wgpu/src/settings.rs b/wgpu/src/settings.rs index 21c2427d..fd3b990a 100644 --- a/wgpu/src/settings.rs +++ b/wgpu/src/settings.rs @@ -4,7 +4,7 @@ pub use crate::Antialiasing; /// The settings of a [`Backend`]. /// /// [`Backend`]: crate::Backend -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq)] pub struct Settings { /// The present mode of the [`Backend`]. /// @@ -21,8 +21,8 @@ pub struct Settings { /// The default size of text. /// - /// By default, it will be set to 20. - pub default_text_size: u16, + /// By default, it will be set to `16.0`. + pub default_text_size: f32, /// If enabled, spread text workload in multiple threads when multiple cores /// are available. @@ -66,7 +66,7 @@ impl Default for Settings { present_mode: wgpu::PresentMode::AutoVsync, internal_backend: wgpu::Backends::all(), default_font: None, - default_text_size: 20, + default_text_size: 20.0, text_multithreading: false, antialiasing: None, } -- cgit From 70483e7fdd44e168143bc1252a167c645a5ab9b9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 7 Feb 2023 19:27:03 +0100 Subject: Fix `Scrollbar` bound calculations in `Scrollable` --- native/src/widget/scrollable.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/native/src/widget/scrollable.rs b/native/src/widget/scrollable.rs index e084d4b0..c1df8c39 100644 --- a/native/src/widget/scrollable.rs +++ b/native/src/widget/scrollable.rs @@ -1116,8 +1116,9 @@ impl Scrollbars { // Bounds of just the scrollbar let scrollbar_bounds = Rectangle { - x: bounds.x + bounds.width - total_scrollbar_width / 2.0 - + width / 2.0, + x: bounds.x + bounds.width + - total_scrollbar_width / 2.0 + - width / 2.0, y: bounds.y, width, height: (bounds.height - x_scrollbar_height).max(0.0), @@ -1129,8 +1130,9 @@ impl Scrollbars { let scroller_offset = offset.y * ratio; let scroller_bounds = Rectangle { - x: bounds.x + bounds.width - total_scrollbar_width / 2.0 - + scroller_width / 2.0, + x: bounds.x + bounds.width + - total_scrollbar_width / 2.0 + - scroller_width / 2.0, y: (scrollbar_bounds.y + scroller_offset - x_scrollbar_height) .max(0.0), width: scroller_width, @@ -1175,8 +1177,9 @@ impl Scrollbars { // Bounds of just the scrollbar let scrollbar_bounds = Rectangle { x: bounds.x, - y: bounds.y + bounds.height - total_scrollbar_height / 2.0 - + width / 2.0, + y: bounds.y + bounds.height + - total_scrollbar_height / 2.0 + - width / 2.0, width: (bounds.width - scrollbar_y_width).max(0.0), height: width, }; @@ -1189,9 +1192,9 @@ impl Scrollbars { let scroller_bounds = Rectangle { x: (scrollbar_bounds.x + scroller_offset - scrollbar_y_width) .max(0.0), - y: bounds.y + bounds.height - total_scrollbar_height / 2.0 - + scroller_width / 2.0, - + y: bounds.y + bounds.height + - total_scrollbar_height / 2.0 + - scroller_width / 2.0, width: scroller_length, height: scroller_width, }; -- cgit From 67e3da56690c0681469276f7fa8155bbfc6e585a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 17 Feb 2023 15:55:12 +0100 Subject: Use `Pixels` for `Slider::height` and `VerticalSlider::width` --- native/src/widget/slider.rs | 8 ++++---- native/src/widget/vertical_slider.rs | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/native/src/widget/slider.rs b/native/src/widget/slider.rs index 854f71c3..d10797bb 100644 --- a/native/src/widget/slider.rs +++ b/native/src/widget/slider.rs @@ -8,8 +8,8 @@ use crate::renderer; use crate::touch; use crate::widget::tree::{self, Tree}; use crate::{ - Background, Clipboard, Color, Element, Layout, Length, Point, Rectangle, - Shell, Size, Widget, + Background, Clipboard, Color, Element, Layout, Length, Pixels, Point, + Rectangle, Shell, Size, Widget, }; use std::ops::RangeInclusive; @@ -122,8 +122,8 @@ where } /// Sets the height of the [`Slider`]. - pub fn height(mut self, height: f32) -> Self { - self.height = height; + pub fn height(mut self, height: impl Into) -> Self { + self.height = height.into().0; self } diff --git a/native/src/widget/vertical_slider.rs b/native/src/widget/vertical_slider.rs index 7363b55f..0e2f5fc9 100644 --- a/native/src/widget/vertical_slider.rs +++ b/native/src/widget/vertical_slider.rs @@ -9,7 +9,7 @@ use crate::event::{self, Event}; use crate::widget::tree::{self, Tree}; use crate::{ layout, mouse, renderer, touch, Background, Clipboard, Color, Element, - Layout, Length, Point, Rectangle, Shell, Size, Widget, + Layout, Length, Pixels, Point, Rectangle, Shell, Size, Widget, }; /// An vertical bar and a handle that selects a single value from a range of @@ -110,8 +110,8 @@ where } /// Sets the width of the [`VerticalSlider`]. - pub fn width(mut self, width: f32) -> Self { - self.width = width; + pub fn width(mut self, width: impl Into) -> Self { + self.width = width.into().0; self } -- cgit From 0872d078e2e3200e2aa2f5ee0005c34fff9effb7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 17 Feb 2023 15:56:19 +0100 Subject: Use `Pixels` for `size` methods --- native/src/widget/radio.rs | 4 ++-- native/src/widget/toggler.rs | 4 ++-- native/src/widget/tooltip.rs | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/native/src/widget/radio.rs b/native/src/widget/radio.rs index 62bc2447..4477b810 100644 --- a/native/src/widget/radio.rs +++ b/native/src/widget/radio.rs @@ -101,8 +101,8 @@ where } /// Sets the size of the [`Radio`] button. - pub fn size(mut self, size: f32) -> Self { - self.size = size; + pub fn size(mut self, size: impl Into) -> Self { + self.size = size.into().0; self } diff --git a/native/src/widget/toggler.rs b/native/src/widget/toggler.rs index eda8f85f..62bdce4a 100644 --- a/native/src/widget/toggler.rs +++ b/native/src/widget/toggler.rs @@ -85,8 +85,8 @@ where } /// Sets the size of the [`Toggler`]. - pub fn size(mut self, size: f32) -> Self { - self.size = size; + pub fn size(mut self, size: impl Into) -> Self { + self.size = size.into().0; self } diff --git a/native/src/widget/tooltip.rs b/native/src/widget/tooltip.rs index 084dc269..955586aa 100644 --- a/native/src/widget/tooltip.rs +++ b/native/src/widget/tooltip.rs @@ -9,8 +9,8 @@ use crate::widget::container; use crate::widget::overlay; use crate::widget::{Text, Tree}; use crate::{ - Clipboard, Element, Event, Layout, Length, Padding, Point, Rectangle, - Shell, Size, Vector, Widget, + Clipboard, Element, Event, Layout, Length, Padding, Pixels, Point, + Rectangle, Shell, Size, Vector, Widget, }; use std::borrow::Cow; @@ -59,7 +59,7 @@ where } /// Sets the size of the text of the [`Tooltip`]. - pub fn size(mut self, size: u16) -> Self { + pub fn size(mut self, size: impl Into) -> Self { self.tooltip = self.tooltip.size(size); self } -- cgit From 3320ac1126750ed1c462d4f1ff81a59c74d1e9fb Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 17 Feb 2023 16:09:49 +0100 Subject: Use `f32` for `Padding` --- core/src/padding.rs | 73 +++++++++++++++++++++++--------- core/src/size.rs | 4 +- graphics/src/renderer.rs | 3 +- native/src/layout/flex.rs | 2 +- native/src/layout/limits.rs | 5 +-- native/src/overlay/menu.rs | 17 +++----- native/src/widget/button.rs | 4 +- native/src/widget/container.rs | 2 +- native/src/widget/pane_grid/title_bar.rs | 5 +-- native/src/widget/pick_list.rs | 32 +++++++------- native/src/widget/text_input.rs | 8 ++-- native/src/widget/tooltip.rs | 22 +++++----- 12 files changed, 98 insertions(+), 79 deletions(-) diff --git a/core/src/padding.rs b/core/src/padding.rs index 140ad8ee..752b2b86 100644 --- a/core/src/padding.rs +++ b/core/src/padding.rs @@ -33,29 +33,29 @@ use crate::Size; /// let widget = Widget::new().padding([10, 20]); // top/bottom, left/right /// let widget = Widget::new().padding([5, 10, 15, 20]); // top, right, bottom, left /// ``` -#[derive(Debug, Hash, Copy, Clone)] +#[derive(Debug, Copy, Clone)] pub struct Padding { /// Top padding - pub top: u16, + pub top: f32, /// Right padding - pub right: u16, + pub right: f32, /// Bottom padding - pub bottom: u16, + pub bottom: f32, /// Left padding - pub left: u16, + pub left: f32, } impl Padding { /// Padding of zero pub const ZERO: Padding = Padding { - top: 0, - right: 0, - bottom: 0, - left: 0, + top: 0.0, + right: 0.0, + bottom: 0.0, + left: 0.0, }; /// Create a Padding that is equal on all sides - pub const fn new(padding: u16) -> Padding { + pub const fn new(padding: f32) -> Padding { Padding { top: padding, right: padding, @@ -65,12 +65,12 @@ impl Padding { } /// Returns the total amount of vertical [`Padding`]. - pub fn vertical(self) -> u16 { + pub fn vertical(self) -> f32 { self.top + self.bottom } /// Returns the total amount of horizontal [`Padding`]. - pub fn horizontal(self) -> u16 { + pub fn horizontal(self) -> f32 { self.left + self.right } @@ -79,16 +79,49 @@ impl Padding { let available = (outer - inner).max(Size::ZERO); Padding { - top: self.top.min((available.height as u16) / 2), - right: self.right.min((available.width as u16) / 2), - bottom: self.bottom.min((available.height as u16) / 2), - left: self.left.min((available.width as u16) / 2), + top: self.top.min(available.height / 2.0), + right: self.right.min(available.width / 2.0), + bottom: self.bottom.min(available.height / 2.0), + left: self.left.min(available.width / 2.0), } } } impl From for Padding { fn from(p: u16) -> Self { + Padding { + top: f32::from(p), + right: f32::from(p), + bottom: f32::from(p), + left: f32::from(p), + } + } +} + +impl From<[u16; 2]> for Padding { + fn from(p: [u16; 2]) -> Self { + Padding { + top: f32::from(p[0]), + right: f32::from(p[1]), + bottom: f32::from(p[0]), + left: f32::from(p[1]), + } + } +} + +impl From<[u16; 4]> for Padding { + fn from(p: [u16; 4]) -> Self { + Padding { + top: f32::from(p[0]), + right: f32::from(p[1]), + bottom: f32::from(p[2]), + left: f32::from(p[3]), + } + } +} + +impl From for Padding { + fn from(p: f32) -> Self { Padding { top: p, right: p, @@ -98,8 +131,8 @@ impl From for Padding { } } -impl From<[u16; 2]> for Padding { - fn from(p: [u16; 2]) -> Self { +impl From<[f32; 2]> for Padding { + fn from(p: [f32; 2]) -> Self { Padding { top: p[0], right: p[1], @@ -109,8 +142,8 @@ impl From<[u16; 2]> for Padding { } } -impl From<[u16; 4]> for Padding { - fn from(p: [u16; 4]) -> Self { +impl From<[f32; 4]> for Padding { + fn from(p: [f32; 4]) -> Self { Padding { top: p[0], right: p[1], diff --git a/core/src/size.rs b/core/src/size.rs index a2c72926..fbe940ef 100644 --- a/core/src/size.rs +++ b/core/src/size.rs @@ -29,8 +29,8 @@ impl Size { /// Increments the [`Size`] to account for the given padding. pub fn pad(&self, padding: Padding) -> Self { Size { - width: self.width + padding.horizontal() as f32, - height: self.height + padding.vertical() as f32, + width: self.width + padding.horizontal(), + height: self.height + padding.vertical(), } } diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index e07e7206..34b6eb1d 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -141,8 +141,7 @@ where font: Font, bounds: Size, ) -> (f32, f32) { - self.backend() - .measure(content, f32::from(size), font, bounds) + self.backend().measure(content, size, font, bounds) } fn hit_test( diff --git a/native/src/layout/flex.rs b/native/src/layout/flex.rs index 94121d76..5d70c2fc 100644 --- a/native/src/layout/flex.rs +++ b/native/src/layout/flex.rs @@ -191,7 +191,7 @@ where } } - let pad = axis.pack(padding.left as f32, padding.top as f32); + let pad = axis.pack(padding.left, padding.top); let mut main = pad.0; for (i, node) in nodes.iter_mut().enumerate() { diff --git a/native/src/layout/limits.rs b/native/src/layout/limits.rs index 137a054c..5d3c1556 100644 --- a/native/src/layout/limits.rs +++ b/native/src/layout/limits.rs @@ -114,10 +114,7 @@ impl Limits { /// Shrinks the current [`Limits`] to account for the given padding. pub fn pad(&self, padding: Padding) -> Limits { - self.shrink(Size::new( - padding.horizontal() as f32, - padding.vertical() as f32, - )) + self.shrink(Size::new(padding.horizontal(), padding.vertical())) } /// Shrinks the current [`Limits`] by the given [`Size`]. diff --git a/native/src/overlay/menu.rs b/native/src/overlay/menu.rs index fac3028e..efee14d4 100644 --- a/native/src/overlay/menu.rs +++ b/native/src/overlay/menu.rs @@ -344,9 +344,7 @@ where let size = { let intrinsic = Size::new( 0.0, - text_size - + f32::from(self.padding.vertical()) - * self.options.len() as f32, + text_size + self.padding.vertical() * self.options.len() as f32, ); limits.resolve(intrinsic) @@ -387,7 +385,7 @@ where *self.hovered_option = Some( ((cursor_position.y - bounds.y) - / (text_size + f32::from(self.padding.vertical()))) + / (text_size + self.padding.vertical())) as usize, ); } @@ -402,7 +400,7 @@ where *self.hovered_option = Some( ((cursor_position.y - bounds.y) - / (text_size + f32::from(self.padding.vertical()))) + / (text_size + self.padding.vertical())) as usize, ); @@ -451,8 +449,7 @@ where let text_size = self.text_size.unwrap_or_else(|| renderer.default_size()); - let option_height = - (text_size + f32::from(self.padding.vertical())) as usize; + let option_height = (text_size + self.padding.vertical()) as usize; let offset = viewport.y - bounds.y; let start = (offset / option_height as f32) as usize; @@ -469,7 +466,7 @@ where x: bounds.x, y: bounds.y + (option_height * i) as f32, width: bounds.width, - height: text_size + f32::from(self.padding.vertical()), + height: text_size + self.padding.vertical(), }; if is_selected { @@ -487,12 +484,12 @@ where renderer.fill_text(Text { content: &option.to_string(), bounds: Rectangle { - x: bounds.x + self.padding.left as f32, + x: bounds.x + self.padding.left, y: bounds.center_y(), width: f32::INFINITY, ..bounds }, - size: f32::from(text_size), + size: text_size, font: self.font.clone(), color: if is_selected { appearance.selected_text_color diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index 3d96dfe1..39387173 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -76,7 +76,7 @@ where on_press: None, width: Length::Shrink, height: Length::Shrink, - padding: Padding::new(5), + padding: Padding::new(5.0), style: ::Style::default(), } } @@ -434,7 +434,7 @@ pub fn layout( let padding = padding.fit(content.size(), limits.max()); let size = limits.pad(padding).resolve(content.size()).pad(padding); - content.move_to(Point::new(padding.left.into(), padding.top.into())); + content.move_to(Point::new(padding.left, padding.top)); layout::Node::with_children(size, vec![content]) } diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index 1621cf6e..b77bf50d 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -310,7 +310,7 @@ pub fn layout( let padding = padding.fit(content.size(), limits.max()); let size = limits.pad(padding).resolve(content.size()); - content.move_to(Point::new(padding.left.into(), padding.top.into())); + content.move_to(Point::new(padding.left, padding.top)); content.align( Alignment::from(horizontal_alignment), Alignment::from(vertical_alignment), diff --git a/native/src/widget/pane_grid/title_bar.rs b/native/src/widget/pane_grid/title_bar.rs index ea0969aa..107078ef 100644 --- a/native/src/widget/pane_grid/title_bar.rs +++ b/native/src/widget/pane_grid/title_bar.rs @@ -249,10 +249,7 @@ where ) }; - node.move_to(Point::new( - self.padding.left.into(), - self.padding.top.into(), - )); + node.move_to(Point::new(self.padding.left, self.padding.top)); layout::Node::with_children(node.size().pad(self.padding), vec![node]) } diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs index 7a61d560..17528db4 100644 --- a/native/src/widget/pick_list.rs +++ b/native/src/widget/pick_list.rs @@ -53,7 +53,7 @@ where From<::Style>, { /// The default padding of a [`PickList`]. - pub const DEFAULT_PADDING: Padding = Padding::new(5); + pub const DEFAULT_PADDING: Padding = Padding::new(5.0); /// Creates a new [`PickList`] with the given list of options, the current /// selected value, and the message to produce when an option is selected. @@ -358,7 +358,7 @@ where let max_width = match width { Length::Shrink => { - let measure = |label: &str| -> u32 { + let measure = |label: &str| -> f32 { let (width, _) = renderer.measure( label, text_size, @@ -366,26 +366,25 @@ where Size::new(f32::INFINITY, f32::INFINITY), ); - width.round() as u32 + width.round() }; let labels = options.iter().map(ToString::to_string); - let labels_width = - labels.map(|label| measure(&label)).max().unwrap_or(100); + let labels_width = labels + .map(|label| measure(&label)) + .fold(100.0, |candidate, current| current.max(candidate)); - let placeholder_width = placeholder.map(measure).unwrap_or(100); + let placeholder_width = placeholder.map(measure).unwrap_or(100.0); labels_width.max(placeholder_width) } - _ => 0, + _ => 0.0, }; let size = { - let intrinsic = Size::new( - max_width as f32 + f32::from(text_size) + f32::from(padding.left), - f32::from(text_size), - ); + let intrinsic = + Size::new(max_width + text_size + padding.left, text_size); limits.resolve(intrinsic).pad(padding) }; @@ -612,7 +611,7 @@ pub fn draw<'a, T, Renderer>( }; if let Some((font, code_point, size)) = handle { - let size = f32::from(size.unwrap_or_else(|| renderer.default_size())); + let size = size.unwrap_or_else(|| renderer.default_size()); renderer.fill_text(Text { content: &code_point.to_string(), @@ -620,7 +619,7 @@ pub fn draw<'a, T, Renderer>( font, color: style.handle_color, bounds: Rectangle { - x: bounds.x + bounds.width - f32::from(padding.horizontal()), + x: bounds.x + bounds.width - padding.horizontal(), y: bounds.center_y() - size / 2.0, height: size, ..bounds @@ -633,8 +632,7 @@ pub fn draw<'a, T, Renderer>( let label = selected.map(ToString::to_string); if let Some(label) = label.as_deref().or(placeholder) { - let text_size = - f32::from(text_size.unwrap_or_else(|| renderer.default_size())); + let text_size = text_size.unwrap_or_else(|| renderer.default_size()); renderer.fill_text(Text { content: label, @@ -646,9 +644,9 @@ pub fn draw<'a, T, Renderer>( style.placeholder_color }, bounds: Rectangle { - x: bounds.x + f32::from(padding.left), + x: bounds.x + padding.left, y: bounds.center_y() - text_size / 2.0, - width: bounds.width - f32::from(padding.horizontal()), + width: bounds.width - padding.horizontal(), height: text_size, }, horizontal_alignment: alignment::Horizontal::Left, diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index 02304cae..ee0473ea 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -94,7 +94,7 @@ where is_secure: false, font: Default::default(), width: Length::Fill, - padding: Padding::new(5), + padding: Padding::new(5.0), size: None, on_change: Box::new(on_change), on_paste: None, @@ -390,7 +390,7 @@ where let limits = limits.width(width).pad(padding).height(text_size); let mut text = layout::Node::new(limits.resolve(Size::ZERO)); - text.move_to(Point::new(padding.left.into(), padding.top.into())); + text.move_to(Point::new(padding.left, padding.top)); layout::Node::with_children(text.size().pad(padding), vec![text]) } @@ -965,7 +965,7 @@ pub fn draw( width: f32::INFINITY, ..text_bounds }, - size: f32::from(size), + size, horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Center, }); @@ -1197,7 +1197,7 @@ where renderer .hit_test( &value.to_string(), - size.into(), + size, font, Size::INFINITY, Point::new(x + offset, text_bounds.height / 2.0), diff --git a/native/src/widget/tooltip.rs b/native/src/widget/tooltip.rs index 955586aa..2a24c055 100644 --- a/native/src/widget/tooltip.rs +++ b/native/src/widget/tooltip.rs @@ -25,8 +25,8 @@ where content: Element<'a, Message, Renderer>, tooltip: Text<'a, Renderer>, position: Position, - gap: u16, - padding: u16, + gap: f32, + padding: f32, snap_within_viewport: bool, style: ::Style, } @@ -37,7 +37,7 @@ where Renderer::Theme: container::StyleSheet + widget::text::StyleSheet, { /// The default padding of a [`Tooltip`] drawn by this renderer. - const DEFAULT_PADDING: u16 = 5; + const DEFAULT_PADDING: f32 = 5.0; /// Creates a new [`Tooltip`]. /// @@ -51,7 +51,7 @@ where content: content.into(), tooltip: Text::new(tooltip), position, - gap: 0, + gap: 0.0, padding: Self::DEFAULT_PADDING, snap_within_viewport: true, style: Default::default(), @@ -73,14 +73,14 @@ where } /// Sets the gap between the content and its [`Tooltip`]. - pub fn gap(mut self, gap: u16) -> Self { - self.gap = gap; + pub fn gap(mut self, gap: impl Into) -> Self { + self.gap = gap.into().0; self } /// Sets the padding of the [`Tooltip`]. - pub fn padding(mut self, padding: u16) -> Self { - self.padding = padding; + pub fn padding(mut self, padding: impl Into) -> Self { + self.padding = padding.into().0; self } @@ -272,8 +272,8 @@ pub fn draw( cursor_position: Point, viewport: &Rectangle, position: Position, - gap: u16, - padding: u16, + gap: f32, + padding: f32, snap_within_viewport: bool, style: &::Style, layout_text: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node, @@ -293,7 +293,6 @@ pub fn draw( let bounds = layout.bounds(); if bounds.contains(cursor_position) { - let gap = f32::from(gap); let style = theme.appearance(style); let defaults = renderer::Style { @@ -311,7 +310,6 @@ pub fn draw( .pad(Padding::new(padding)), ); - let padding = f32::from(padding); let text_bounds = text_layout.bounds(); let x_center = bounds.x + (bounds.width - text_bounds.width) / 2.0; let y_center = bounds.y + (bounds.height - text_bounds.height) / 2.0; -- cgit From fd3a141024a11e17bba0c568f0df7ff34b79315b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 17 Feb 2023 16:18:27 +0100 Subject: Use `f32` for `Icon::size` in `checkbox` --- native/src/widget/checkbox.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/native/src/widget/checkbox.rs b/native/src/widget/checkbox.rs index 835a0651..dc00052d 100644 --- a/native/src/widget/checkbox.rs +++ b/native/src/widget/checkbox.rs @@ -15,14 +15,14 @@ use crate::{ pub use iced_style::checkbox::{Appearance, StyleSheet}; /// The icon in a [`Checkbox`]. -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq)] pub struct Icon { /// Font that will be used to display the `code_point`, pub font: Font, /// The unicode code point that will be used as the icon. pub code_point: char, /// Font size of the content. - pub size: Option, + pub size: Option, } /// A box that can be checked. -- cgit From a467a037c3aee6cf659ec11a8deb744ca0408c59 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 17 Feb 2023 16:23:29 +0100 Subject: Use `Pixels` for `spacing` --- native/src/widget/checkbox.rs | 8 +++--- native/src/widget/column.rs | 10 +++---- native/src/widget/pane_grid.rs | 61 +++++++++++++++++++----------------------- native/src/widget/radio.rs | 8 +++--- native/src/widget/row.rs | 14 +++++----- native/src/widget/toggler.rs | 8 +++--- 6 files changed, 51 insertions(+), 58 deletions(-) diff --git a/native/src/widget/checkbox.rs b/native/src/widget/checkbox.rs index dc00052d..9b69e574 100644 --- a/native/src/widget/checkbox.rs +++ b/native/src/widget/checkbox.rs @@ -53,7 +53,7 @@ where label: String, width: Length, size: f32, - spacing: u16, + spacing: f32, text_size: Option, font: Renderer::Font, icon: Icon, @@ -69,7 +69,7 @@ where const DEFAULT_SIZE: f32 = 20.0; /// The default spacing of a [`Checkbox`]. - const DEFAULT_SPACING: u16 = 15; + const DEFAULT_SPACING: f32 = 15.0; /// Creates a new [`Checkbox`]. /// @@ -114,8 +114,8 @@ where } /// Sets the spacing between the [`Checkbox`] and the text. - pub fn spacing(mut self, spacing: u16) -> Self { - self.spacing = spacing; + pub fn spacing(mut self, spacing: impl Into) -> Self { + self.spacing = spacing.into().0; self } diff --git a/native/src/widget/column.rs b/native/src/widget/column.rs index 65ca76a1..ebe579d5 100644 --- a/native/src/widget/column.rs +++ b/native/src/widget/column.rs @@ -13,7 +13,7 @@ use crate::{ /// A container that distributes its contents vertically. #[allow(missing_debug_implementations)] pub struct Column<'a, Message, Renderer> { - spacing: u16, + spacing: f32, padding: Padding, width: Length, height: Length, @@ -33,7 +33,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { children: Vec>, ) -> Self { Column { - spacing: 0, + spacing: 0.0, padding: Padding::ZERO, width: Length::Shrink, height: Length::Shrink, @@ -48,8 +48,8 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { /// Custom margins per element do not exist in iced. You should use this /// method instead! While less flexible, it helps you keep spacing between /// elements consistent. - pub fn spacing(mut self, units: u16) -> Self { - self.spacing = units; + pub fn spacing(mut self, amount: impl Into) -> Self { + self.spacing = amount.into().0; self } @@ -135,7 +135,7 @@ where renderer, &limits, self.padding, - self.spacing as f32, + self.spacing, self.align_items, &self.children, ) diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index c6ff61ac..83564c3f 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -42,8 +42,8 @@ use crate::widget; use crate::widget::container; use crate::widget::tree::{self, Tree}; use crate::{ - Clipboard, Color, Element, Layout, Length, Point, Rectangle, Shell, Size, - Vector, Widget, + Clipboard, Color, Element, Layout, Length, Pixels, Point, Rectangle, Shell, + Size, Vector, Widget, }; /// A collection of panes distributed using either vertical or horizontal splits @@ -104,10 +104,10 @@ where contents: Contents<'a, Content<'a, Message, Renderer>>, width: Length, height: Length, - spacing: u16, + spacing: f32, on_click: Option Message + 'a>>, on_drag: Option Message + 'a>>, - on_resize: Option<(u16, Box Message + 'a>)>, + on_resize: Option<(f32, Box Message + 'a>)>, style: ::Style, } @@ -150,7 +150,7 @@ where contents, width: Length::Fill, height: Length::Fill, - spacing: 0, + spacing: 0.0, on_click: None, on_drag: None, on_resize: None, @@ -171,8 +171,8 @@ where } /// Sets the spacing _between_ the panes of the [`PaneGrid`]. - pub fn spacing(mut self, units: u16) -> Self { - self.spacing = units; + pub fn spacing(mut self, amount: impl Into) -> Self { + self.spacing = amount.into().0; self } @@ -205,11 +205,11 @@ where /// The grabbable area of a split will have a length of `spacing + leeway`, /// properly centered. In other words, a length of /// `(spacing + leeway) / 2.0` on either side of the split line. - pub fn on_resize(mut self, leeway: u16, f: F) -> Self + pub fn on_resize(mut self, leeway: impl Into, f: F) -> Self where F: 'a + Fn(ResizeEvent) -> Message, { - self.on_resize = Some((leeway, Box::new(f))); + self.on_resize = Some((leeway.into().0, Box::new(f))); self } @@ -485,14 +485,14 @@ pub fn layout( node: &Node, width: Length, height: Length, - spacing: u16, + spacing: f32, contents: impl Iterator, layout_content: impl Fn(T, &Renderer, &layout::Limits) -> layout::Node, ) -> layout::Node { let limits = limits.width(width).height(height); let size = limits.resolve(Size::ZERO); - let regions = node.pane_regions(f32::from(spacing), size); + let regions = node.pane_regions(spacing, size); let children = contents .filter_map(|(pane, content)| { let region = regions.get(&pane)?; @@ -522,11 +522,11 @@ pub fn update<'a, Message, T: Draggable>( layout: Layout<'_>, cursor_position: Point, shell: &mut Shell<'_, Message>, - spacing: u16, + spacing: f32, contents: impl Iterator, on_click: &Option Message + 'a>>, on_drag: &Option Message + 'a>>, - on_resize: &Option<(u16, Box Message + 'a>)>, + on_resize: &Option<(f32, Box Message + 'a>)>, ) -> event::Status { let mut event_status = event::Status::Ignored; @@ -546,13 +546,13 @@ pub fn update<'a, Message, T: Draggable>( ); let splits = node.split_regions( - f32::from(spacing), + spacing, Size::new(bounds.width, bounds.height), ); let clicked_split = hovered_split( splits.iter(), - f32::from(spacing + leeway), + spacing + leeway, relative_cursor, ); @@ -624,7 +624,7 @@ pub fn update<'a, Message, T: Draggable>( let bounds = layout.bounds(); let splits = node.split_regions( - f32::from(spacing), + spacing, Size::new(bounds.width, bounds.height), ); @@ -698,8 +698,8 @@ pub fn mouse_interaction( node: &Node, layout: Layout<'_>, cursor_position: Point, - spacing: u16, - resize_leeway: Option, + spacing: f32, + resize_leeway: Option, ) -> Option { if action.picked_pane().is_some() { return Some(mouse::Interaction::Grabbing); @@ -710,20 +710,15 @@ pub fn mouse_interaction( resize_leeway.and_then(|leeway| { let bounds = layout.bounds(); - let splits = - node.split_regions(f32::from(spacing), bounds.size()); + let splits = node.split_regions(spacing, bounds.size()); let relative_cursor = Point::new( cursor_position.x - bounds.x, cursor_position.y - bounds.y, ); - hovered_split( - splits.iter(), - f32::from(spacing + leeway), - relative_cursor, - ) - .map(|(_, axis, _)| axis) + hovered_split(splits.iter(), spacing + leeway, relative_cursor) + .map(|(_, axis, _)| axis) }) }); @@ -747,8 +742,8 @@ pub fn draw( theme: &Renderer::Theme, default_style: &renderer::Style, viewport: &Rectangle, - spacing: u16, - resize_leeway: Option, + spacing: f32, + resize_leeway: Option, style: &::Style, contents: impl Iterator, draw_pane: impl Fn( @@ -770,12 +765,11 @@ pub fn draw( .and_then(|(split, axis)| { let bounds = layout.bounds(); - let splits = node.split_regions(f32::from(spacing), bounds.size()); + let splits = node.split_regions(spacing, bounds.size()); let (_axis, region, ratio) = splits.get(&split)?; - let region = - axis.split_line_bounds(*region, *ratio, f32::from(spacing)); + let region = axis.split_line_bounds(*region, *ratio, spacing); Some((axis, region + Vector::new(bounds.x, bounds.y), true)) }) @@ -788,12 +782,11 @@ pub fn draw( cursor_position.y - bounds.y, ); - let splits = - node.split_regions(f32::from(spacing), bounds.size()); + let splits = node.split_regions(spacing, bounds.size()); let (_split, axis, region) = hovered_split( splits.iter(), - f32::from(spacing + leeway), + spacing + leeway, relative_cursor, )?; diff --git a/native/src/widget/radio.rs b/native/src/widget/radio.rs index 4477b810..9daddfbc 100644 --- a/native/src/widget/radio.rs +++ b/native/src/widget/radio.rs @@ -51,7 +51,7 @@ where label: String, width: Length, size: f32, - spacing: u16, + spacing: f32, text_size: Option, font: Renderer::Font, style: ::Style, @@ -67,7 +67,7 @@ where pub const DEFAULT_SIZE: f32 = 28.0; /// The default spacing of a [`Radio`] button. - pub const DEFAULT_SPACING: u16 = 15; + pub const DEFAULT_SPACING: f32 = 15.0; /// Creates a new [`Radio`] button. /// @@ -113,8 +113,8 @@ where } /// Sets the spacing between the [`Radio`] button and the text. - pub fn spacing(mut self, spacing: u16) -> Self { - self.spacing = spacing; + pub fn spacing(mut self, spacing: impl Into) -> Self { + self.spacing = spacing.into().0; self } diff --git a/native/src/widget/row.rs b/native/src/widget/row.rs index a340d84a..286c1c2d 100644 --- a/native/src/widget/row.rs +++ b/native/src/widget/row.rs @@ -6,14 +6,14 @@ use crate::overlay; use crate::renderer; use crate::widget::{Operation, Tree}; use crate::{ - Alignment, Clipboard, Element, Length, Padding, Point, Rectangle, Shell, - Widget, + Alignment, Clipboard, Element, Length, Padding, Pixels, Point, Rectangle, + Shell, Widget, }; /// A container that distributes its contents horizontally. #[allow(missing_debug_implementations)] pub struct Row<'a, Message, Renderer> { - spacing: u16, + spacing: f32, padding: Padding, width: Length, height: Length, @@ -32,7 +32,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { children: Vec>, ) -> Self { Row { - spacing: 0, + spacing: 0.0, padding: Padding::ZERO, width: Length::Shrink, height: Length::Shrink, @@ -46,8 +46,8 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { /// Custom margins per element do not exist in iced. You should use this /// method instead! While less flexible, it helps you keep spacing between /// elements consistent. - pub fn spacing(mut self, units: u16) -> Self { - self.spacing = units; + pub fn spacing(mut self, amount: impl Into) -> Self { + self.spacing = amount.into().0; self } @@ -124,7 +124,7 @@ where renderer, &limits, self.padding, - self.spacing as f32, + self.spacing, self.align_items, &self.children, ) diff --git a/native/src/widget/toggler.rs b/native/src/widget/toggler.rs index 62bdce4a..a434af65 100644 --- a/native/src/widget/toggler.rs +++ b/native/src/widget/toggler.rs @@ -41,7 +41,7 @@ where size: f32, text_size: Option, text_alignment: alignment::Horizontal, - spacing: u16, + spacing: f32, font: Renderer::Font, style: ::Style, } @@ -78,7 +78,7 @@ where size: Self::DEFAULT_SIZE, text_size: None, text_alignment: alignment::Horizontal::Left, - spacing: 0, + spacing: 0.0, font: Renderer::Font::default(), style: Default::default(), } @@ -109,8 +109,8 @@ where } /// Sets the spacing between the [`Toggler`] and the text. - pub fn spacing(mut self, spacing: u16) -> Self { - self.spacing = spacing; + pub fn spacing(mut self, spacing: impl Into) -> Self { + self.spacing = spacing.into().0; self } -- cgit From fd1408693322f5bfdaee7f27bd098808658d7310 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 17 Feb 2023 16:24:37 +0100 Subject: Use `Pixels` for `padding` in `image::Viewer` --- native/src/widget/image/viewer.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/native/src/widget/image/viewer.rs b/native/src/widget/image/viewer.rs index a9d3e5b4..1f8d5d7a 100644 --- a/native/src/widget/image/viewer.rs +++ b/native/src/widget/image/viewer.rs @@ -6,8 +6,8 @@ use crate::mouse; use crate::renderer; use crate::widget::tree::{self, Tree}; use crate::{ - Clipboard, Element, Layout, Length, Point, Rectangle, Shell, Size, Vector, - Widget, + Clipboard, Element, Layout, Length, Pixels, Point, Rectangle, Shell, Size, + Vector, Widget, }; use std::hash::Hash; @@ -15,7 +15,7 @@ use std::hash::Hash; /// A frame that displays an image with the ability to zoom in/out and pan. #[allow(missing_debug_implementations)] pub struct Viewer { - padding: u16, + padding: f32, width: Length, height: Length, min_scale: f32, @@ -28,7 +28,7 @@ impl Viewer { /// Creates a new [`Viewer`] with the given [`State`]. pub fn new(handle: Handle) -> Self { Viewer { - padding: 0, + padding: 0.0, width: Length::Shrink, height: Length::Shrink, min_scale: 0.25, @@ -39,8 +39,8 @@ impl Viewer { } /// Sets the padding of the [`Viewer`]. - pub fn padding(mut self, units: u16) -> Self { - self.padding = units; + pub fn padding(mut self, padding: impl Into) -> Self { + self.padding = padding.into().0; self } -- cgit From ffcd4f1becbfb02435664fb4e494c1c3de97e0be Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 18 Feb 2023 08:45:54 +0100 Subject: Fix height of `overlay::Menu` --- native/src/overlay/menu.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/native/src/overlay/menu.rs b/native/src/overlay/menu.rs index efee14d4..50f741ef 100644 --- a/native/src/overlay/menu.rs +++ b/native/src/overlay/menu.rs @@ -344,7 +344,8 @@ where let size = { let intrinsic = Size::new( 0.0, - text_size + self.padding.vertical() * self.options.len() as f32, + (text_size + self.padding.vertical()) + * self.options.len() as f32, ); limits.resolve(intrinsic) -- cgit From 0d1656937b459237670cdc0b1f45e09d78c47494 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 18 Feb 2023 12:04:40 +0100 Subject: Bump versions :tada: --- CHANGELOG.md | 53 +++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 20 ++++++++-------- README.md | 4 ++-- core/Cargo.toml | 2 +- core/README.md | 2 +- core/src/lib.rs | 2 +- futures/Cargo.toml | 2 +- futures/src/subscription.rs | 6 ++--- glow/Cargo.toml | 6 ++--- glow/README.md | 2 +- glow/src/lib.rs | 2 +- glutin/Cargo.toml | 8 +++---- glutin/README.md | 2 +- graphics/Cargo.toml | 6 ++--- lazy/Cargo.toml | 4 ++-- native/Cargo.toml | 8 +++---- native/README.md | 2 +- native/src/lib.rs | 4 ++-- native/src/subscription.rs | 2 +- native/src/user_interface.rs | 4 ++-- native/src/widget.rs | 10 ++++---- native/src/widget/pane_grid.rs | 2 +- src/application.rs | 18 +++++++------- src/lib.rs | 8 +++---- src/sandbox.rs | 22 +++++++++--------- src/widget.rs | 2 +- style/Cargo.toml | 4 ++-- wgpu/Cargo.toml | 6 ++--- wgpu/README.md | 2 +- wgpu/src/lib.rs | 2 +- winit/Cargo.toml | 8 +++---- winit/README.md | 2 +- winit/src/conversion.rs | 12 +++++----- winit/src/lib.rs | 2 +- 34 files changed, 146 insertions(+), 95 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3e7641b..1d9d1fb3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,56 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.8.0] - 2023-02-18 +### Added +- Generic pixel units. [#1711](https://github.com/iced-rs/iced/pull/1711) +- `custom` method to `widget::Operation` trait. [#1649](https://github.com/iced-rs/iced/pull/1649) +- `Group` overlay. [#1655](https://github.com/iced-rs/iced/pull/1655) +- Standalone `draw` helper for `image`. [#1682](https://github.com/iced-rs/iced/pull/1682) +- Dynamic `pick_list::Handle`. [#1675](https://github.com/iced-rs/iced/pull/1675) +- `Id` support for `Container`. [#1695](https://github.com/iced-rs/iced/pull/1695) +- Custom `Checkbox` icon support. [#1707](https://github.com/iced-rs/iced/pull/1707) +- `window` action to change always on top setting. [#1587](https://github.com/iced-rs/iced/pull/1587) +- `window` action to fetch its unique identifier. [#1589](https://github.com/iced-rs/iced/pull/1589) + +### Changed +- Annotated `Command` and `Subscription` with `#[must_use]`. [#1676](https://github.com/iced-rs/iced/pull/1676) +- Replaced `Fn` with `FnOnce` in `canvas::Cache::draw`. [#1694](https://github.com/iced-rs/iced/pull/1694) +- Used `[default]` on enum in `game_of_life` example. [#1660](https://github.com/iced-rs/iced/pull/1660) +- Made `QRCode` hide when data is empty in `qr_code` example. [#1665](https://github.com/iced-rs/iced/pull/1665) +- Replaced `Cow` with `Bytes` in `image` to accept any kind of data that implements `AsRef<[u8]>`. [#1551](https://github.com/iced-rs/iced/pull/1551) + +### Fixed +- Blank window on application startup. [#1698](https://github.com/iced-rs/iced/pull/1698) +- Off-by-one pixel error on `pick_list` width. [#1679](https://github.com/iced-rs/iced/pull/1679) +- Missing `text_input` implementation in `operation::Map`. [#1678](https://github.com/iced-rs/iced/pull/1678) +- Widget-driven animations for `Component`. [#1685](https://github.com/iced-rs/iced/pull/1685) +- Layout translation in `overlay::Group`. [#1686](https://github.com/iced-rs/iced/pull/1686) +- Missing `is_over` implementation for overlays of `iced_lazy` widgets. [#1699](https://github.com/iced-rs/iced/pull/1699) +- Panic when overlay event processing removes overlay. [#1700](https://github.com/iced-rs/iced/pull/1700) +- Panic when using operations with components in certain cases. [#1701](https://github.com/iced-rs/iced/pull/1701) +- `TextInput` width when using padding. [#1706](https://github.com/iced-rs/iced/pull/1706) +- `iced_glow` crash on some hardware. [#1703](https://github.com/iced-rs/iced/pull/1703) +- Height of `overlay::Menu`. [#1714](https://github.com/iced-rs/iced/pull/1714) +- Size of images in `README`. [#1659](https://github.com/iced-rs/iced/pull/1659) +- New `clippy` lints. [#1681](https://github.com/iced-rs/iced/pull/1681) + +Many thanks to... + +- @13r0ck +- @bungoboingo +- @casperstorm +- @frey +- @greatest-ape +- @ids1024 +- @Jedsek +- @nicksenger +- @Night-Hunter-NF +- @sdroege +- @Sn-Kinos +- @sushigiri +- @tarkah + ## [0.7.0] - 2023-01-14 ### Added - Widget-driven animations. [#1647](https://github.com/iced-rs/iced/pull/1647) @@ -364,7 +414,8 @@ Many thanks to... ### Added - First release! :tada: -[Unreleased]: https://github.com/iced-rs/iced/compare/0.7.0...HEAD +[Unreleased]: https://github.com/iced-rs/iced/compare/0.8.0...HEAD +[0.8.0]: https://github.com/iced-rs/iced/compare/0.7.0...0.8.0 [0.7.0]: https://github.com/iced-rs/iced/compare/0.6.0...0.7.0 [0.6.0]: https://github.com/iced-rs/iced/compare/0.5.0...0.6.0 [0.5.0]: https://github.com/iced-rs/iced/compare/0.4.2...0.5.0 diff --git a/Cargo.toml b/Cargo.toml index 185cd39d..d26ec2b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "iced" -version = "0.7.0" +version = "0.8.0" authors = ["Héctor Ramón Jiménez "] edition = "2021" description = "A cross-platform GUI library inspired by Elm" @@ -66,13 +66,13 @@ members = [ ] [dependencies] -iced_core = { version = "0.7", path = "core" } -iced_futures = { version = "0.5", path = "futures" } -iced_native = { version = "0.8", path = "native" } -iced_graphics = { version = "0.6", path = "graphics" } -iced_winit = { version = "0.7", path = "winit", features = ["application"] } -iced_glutin = { version = "0.6", path = "glutin", optional = true } -iced_glow = { version = "0.6", path = "glow", optional = true } +iced_core = { version = "0.8", path = "core" } +iced_futures = { version = "0.6", path = "futures" } +iced_native = { version = "0.9", path = "native" } +iced_graphics = { version = "0.7", path = "graphics" } +iced_winit = { version = "0.8", path = "winit", features = ["application"] } +iced_glutin = { version = "0.7", path = "glutin", optional = true } +iced_glow = { version = "0.7", path = "glow", optional = true } thiserror = "1.0" [dependencies.image_rs] @@ -81,10 +81,10 @@ package = "image" optional = true [target.'cfg(not(target_arch = "wasm32"))'.dependencies] -iced_wgpu = { version = "0.8", path = "wgpu", optional = true } +iced_wgpu = { version = "0.9", path = "wgpu", optional = true } [target.'cfg(target_arch = "wasm32")'.dependencies] -iced_wgpu = { version = "0.8", path = "wgpu", features = ["webgl"], optional = true } +iced_wgpu = { version = "0.9", path = "wgpu", features = ["webgl"], optional = true } [package.metadata.docs.rs] rustdoc-args = ["--cfg", "docsrs"] diff --git a/README.md b/README.md index 8ebd0e9c..a5ebf230 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ __Iced is currently experimental software.__ [Take a look at the roadmap], Add `iced` as a dependency in your `Cargo.toml`: ```toml -iced = "0.7" +iced = "0.8" ``` If your project is using a Rust edition older than 2021, then you will need to @@ -215,7 +215,7 @@ cargo run --features iced/glow --package game_of_life and then use it in your project with ```toml -iced = { version = "0.7", default-features = false, features = ["glow"] } +iced = { version = "0.8", default-features = false, features = ["glow"] } ``` __NOTE:__ Chances are you have hardware that supports at least OpenGL 2.1 or OpenGL ES 2.0, diff --git a/core/Cargo.toml b/core/Cargo.toml index eebd2fe3..43865e4d 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "iced_core" -version = "0.7.0" +version = "0.8.0" authors = ["Héctor Ramón Jiménez "] edition = "2021" description = "The essential concepts of Iced" diff --git a/core/README.md b/core/README.md index cecbc1e4..64d92e78 100644 --- a/core/README.md +++ b/core/README.md @@ -18,7 +18,7 @@ This crate is meant to be a starting point for an Iced runtime. Add `iced_core` as a dependency in your `Cargo.toml`: ```toml -iced_core = "0.7" +iced_core = "0.8" ``` __Iced moves fast and the `master` branch can contain breaking changes!__ If diff --git a/core/src/lib.rs b/core/src/lib.rs index 31280a05..d3596b4d 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -7,7 +7,7 @@ //! ![The foundations of the Iced ecosystem](https://github.com/iced-rs/iced/blob/0525d76ff94e828b7b21634fa94a747022001c83/docs/graphs/foundations.png?raw=true) //! //! [Iced]: https://github.com/iced-rs/iced -//! [`iced_native`]: https://github.com/iced-rs/iced/tree/0.7/native +//! [`iced_native`]: https://github.com/iced-rs/iced/tree/0.8/native //! [`iced_web`]: https://github.com/iced-rs/iced_web #![doc( html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg" diff --git a/futures/Cargo.toml b/futures/Cargo.toml index b69cb59b..e4d355ee 100644 --- a/futures/Cargo.toml +++ b/futures/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "iced_futures" -version = "0.5.1" +version = "0.6.0" authors = ["Héctor Ramón Jiménez "] edition = "2021" description = "Commands, subscriptions, and runtimes for Iced" diff --git a/futures/src/subscription.rs b/futures/src/subscription.rs index d18ec4f7..fe53fd7e 100644 --- a/futures/src/subscription.rs +++ b/futures/src/subscription.rs @@ -126,9 +126,9 @@ impl std::fmt::Debug for Subscription { /// - [`stopwatch`], a watch with start/stop and reset buttons showcasing how /// to listen to time. /// -/// [examples]: https://github.com/iced-rs/iced/tree/0.7/examples -/// [`download_progress`]: https://github.com/iced-rs/iced/tree/0.7/examples/download_progress -/// [`stopwatch`]: https://github.com/iced-rs/iced/tree/0.7/examples/stopwatch +/// [examples]: https://github.com/iced-rs/iced/tree/0.8/examples +/// [`download_progress`]: https://github.com/iced-rs/iced/tree/0.8/examples/download_progress +/// [`stopwatch`]: https://github.com/iced-rs/iced/tree/0.8/examples/stopwatch pub trait Recipe { /// The events that will be produced by a [`Subscription`] with this /// [`Recipe`]. diff --git a/glow/Cargo.toml b/glow/Cargo.toml index cf5dfb8a..1a848ab7 100644 --- a/glow/Cargo.toml +++ b/glow/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "iced_glow" -version = "0.6.0" +version = "0.7.0" authors = ["Héctor Ramón Jiménez "] edition = "2021" description = "A glow renderer for iced" @@ -34,11 +34,11 @@ bytemuck = "1.4" log = "0.4" [dependencies.iced_native] -version = "0.8" +version = "0.9" path = "../native" [dependencies.iced_graphics] -version = "0.6" +version = "0.7" path = "../graphics" features = ["font-fallback", "font-icons", "opengl"] diff --git a/glow/README.md b/glow/README.md index 38449c64..95c9d62a 100644 --- a/glow/README.md +++ b/glow/README.md @@ -28,7 +28,7 @@ Currently, `iced_glow` supports the following primitives: Add `iced_glow` as a dependency in your `Cargo.toml`: ```toml -iced_glow = "0.6" +iced_glow = "0.7" ``` __Iced moves fast and the `master` branch can contain breaking changes!__ If diff --git a/glow/src/lib.rs b/glow/src/lib.rs index a12c45b8..9e7de0d9 100644 --- a/glow/src/lib.rs +++ b/glow/src/lib.rs @@ -3,7 +3,7 @@ //! ![The native path of the Iced ecosystem](https://github.com/iced-rs/iced/blob/0525d76ff94e828b7b21634fa94a747022001c83/docs/graphs/native.png?raw=true) //! //! [`glow`]: https://github.com/grovesNL/glow -//! [`iced_native`]: https://github.com/iced-rs/iced/tree/0.7/native +//! [`iced_native`]: https://github.com/iced-rs/iced/tree/0.8/native #![doc( html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg" )] diff --git a/glutin/Cargo.toml b/glutin/Cargo.toml index 27ed29e8..10d3778b 100644 --- a/glutin/Cargo.toml +++ b/glutin/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "iced_glutin" -version = "0.6.0" +version = "0.7.0" authors = ["Héctor Ramón Jiménez "] edition = "2021" description = "A glutin runtime for Iced" @@ -24,16 +24,16 @@ git = "https://github.com/iced-rs/glutin" rev = "da8d291486b4c9bec12487a46c119c4b1d386abf" [dependencies.iced_native] -version = "0.8" +version = "0.9" path = "../native" [dependencies.iced_winit] -version = "0.7" +version = "0.8" path = "../winit" features = ["application"] [dependencies.iced_graphics] -version = "0.6" +version = "0.7" path = "../graphics" features = ["opengl"] diff --git a/glutin/README.md b/glutin/README.md index 1d873874..45e8ee6b 100644 --- a/glutin/README.md +++ b/glutin/README.md @@ -20,7 +20,7 @@ It exposes a renderer-agnostic `Application` trait that can be implemented and t Add `iced_glutin` as a dependency in your `Cargo.toml`: ```toml -iced_glutin = "0.6" +iced_glutin = "0.7" ``` __Iced moves fast and the `master` branch can contain breaking changes!__ If diff --git a/graphics/Cargo.toml b/graphics/Cargo.toml index 664bb19f..13ab61d8 100644 --- a/graphics/Cargo.toml +++ b/graphics/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "iced_graphics" -version = "0.6.0" +version = "0.7.0" authors = ["Héctor Ramón Jiménez "] edition = "2021" description = "A bunch of backend-agnostic types that can be leveraged to build a renderer for Iced" @@ -44,11 +44,11 @@ version = "1.4" features = ["derive"] [dependencies.iced_native] -version = "0.8" +version = "0.9" path = "../native" [dependencies.iced_style] -version = "0.6" +version = "0.7" path = "../style" [dependencies.lyon] diff --git a/lazy/Cargo.toml b/lazy/Cargo.toml index 657da5ca..c739b312 100644 --- a/lazy/Cargo.toml +++ b/lazy/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "iced_lazy" -version = "0.4.0" +version = "0.5.0" authors = ["Héctor Ramón Jiménez "] edition = "2021" description = "Lazy widgets for Iced" @@ -14,5 +14,5 @@ categories = ["gui"] ouroboros = "0.13" [dependencies.iced_native] -version = "0.8" +version = "0.9" path = "../native" diff --git a/native/Cargo.toml b/native/Cargo.toml index 79e4dac4..25281ea7 100644 --- a/native/Cargo.toml +++ b/native/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "iced_native" -version = "0.8.0" +version = "0.9.0" authors = ["Héctor Ramón Jiménez "] edition = "2021" description = "A renderer-agnostic library for native GUIs" @@ -16,14 +16,14 @@ unicode-segmentation = "1.6" num-traits = "0.2" [dependencies.iced_core] -version = "0.7" +version = "0.8" path = "../core" [dependencies.iced_futures] -version = "0.5" +version = "0.6" path = "../futures" features = ["thread-pool"] [dependencies.iced_style] -version = "0.6.0" +version = "0.7" path = "../style" diff --git a/native/README.md b/native/README.md index 9e1f65fb..996daa76 100644 --- a/native/README.md +++ b/native/README.md @@ -28,7 +28,7 @@ To achieve this, it introduces a bunch of reusable interfaces: Add `iced_native` as a dependency in your `Cargo.toml`: ```toml -iced_native = "0.8" +iced_native = "0.9" ``` __Iced moves fast and the `master` branch can contain breaking changes!__ If diff --git a/native/src/lib.rs b/native/src/lib.rs index 7c406ae5..ebdc8490 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -23,8 +23,8 @@ //! - Build a new renderer, see the [renderer] module. //! - Build a custom widget, start at the [`Widget`] trait. //! -//! [`iced_core`]: https://github.com/iced-rs/iced/tree/0.7/core -//! [`iced_winit`]: https://github.com/iced-rs/iced/tree/0.7/winit +//! [`iced_core`]: https://github.com/iced-rs/iced/tree/0.8/core +//! [`iced_winit`]: https://github.com/iced-rs/iced/tree/0.8/winit //! [`druid`]: https://github.com/xi-editor/druid //! [`raw-window-handle`]: https://github.com/rust-windowing/raw-window-handle //! [renderer]: crate::renderer diff --git a/native/src/subscription.rs b/native/src/subscription.rs index 8c92efad..b505f3cc 100644 --- a/native/src/subscription.rs +++ b/native/src/subscription.rs @@ -184,7 +184,7 @@ where /// Check out the [`websocket`] example, which showcases this pattern to maintain a WebSocket /// connection open. /// -/// [`websocket`]: https://github.com/iced-rs/iced/tree/0.7/examples/websocket +/// [`websocket`]: https://github.com/iced-rs/iced/tree/0.8/examples/websocket pub fn unfold( id: I, initial: T, diff --git a/native/src/user_interface.rs b/native/src/user_interface.rs index 2358bff1..f5202609 100644 --- a/native/src/user_interface.rs +++ b/native/src/user_interface.rs @@ -21,8 +21,8 @@ use crate::{ /// The [`integration_opengl`] & [`integration_wgpu`] examples use a /// [`UserInterface`] to integrate Iced in an existing graphical application. /// -/// [`integration_opengl`]: https://github.com/iced-rs/iced/tree/0.7/examples/integration_opengl -/// [`integration_wgpu`]: https://github.com/iced-rs/iced/tree/0.7/examples/integration_wgpu +/// [`integration_opengl`]: https://github.com/iced-rs/iced/tree/0.8/examples/integration_opengl +/// [`integration_wgpu`]: https://github.com/iced-rs/iced/tree/0.8/examples/integration_wgpu #[allow(missing_debug_implementations)] pub struct UserInterface<'a, Message, Renderer> { root: Element<'a, Message, Renderer>, diff --git a/native/src/widget.rs b/native/src/widget.rs index fb759ec8..2b3ca7be 100644 --- a/native/src/widget.rs +++ b/native/src/widget.rs @@ -110,12 +110,12 @@ use crate::{Clipboard, Layout, Length, Point, Rectangle, Shell}; /// - [`geometry`], a custom widget showcasing how to draw geometry with the /// `Mesh2D` primitive in [`iced_wgpu`]. /// -/// [examples]: https://github.com/iced-rs/iced/tree/0.7/examples -/// [`bezier_tool`]: https://github.com/iced-rs/iced/tree/0.7/examples/bezier_tool -/// [`custom_widget`]: https://github.com/iced-rs/iced/tree/0.7/examples/custom_widget -/// [`geometry`]: https://github.com/iced-rs/iced/tree/0.7/examples/geometry +/// [examples]: https://github.com/iced-rs/iced/tree/0.8/examples +/// [`bezier_tool`]: https://github.com/iced-rs/iced/tree/0.8/examples/bezier_tool +/// [`custom_widget`]: https://github.com/iced-rs/iced/tree/0.8/examples/custom_widget +/// [`geometry`]: https://github.com/iced-rs/iced/tree/0.8/examples/geometry /// [`lyon`]: https://github.com/nical/lyon -/// [`iced_wgpu`]: https://github.com/iced-rs/iced/tree/0.7/wgpu +/// [`iced_wgpu`]: https://github.com/iced-rs/iced/tree/0.8/wgpu pub trait Widget where Renderer: crate::Renderer, diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index 83564c3f..bcb17ebd 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -6,7 +6,7 @@ //! The [`pane_grid` example] showcases how to use a [`PaneGrid`] with resizing, //! drag and drop, and hotkey support. //! -//! [`pane_grid` example]: https://github.com/iced-rs/iced/tree/0.7/examples/pane_grid +//! [`pane_grid` example]: https://github.com/iced-rs/iced/tree/0.8/examples/pane_grid mod axis; mod configuration; mod content; diff --git a/src/application.rs b/src/application.rs index 96f4e9a6..1db5c93f 100644 --- a/src/application.rs +++ b/src/application.rs @@ -39,15 +39,15 @@ pub use iced_native::application::{Appearance, StyleSheet}; /// to listen to time. /// - [`todos`], a todos tracker inspired by [TodoMVC]. /// -/// [The repository has a bunch of examples]: https://github.com/iced-rs/iced/tree/0.7/examples -/// [`clock`]: https://github.com/iced-rs/iced/tree/0.7/examples/clock -/// [`download_progress`]: https://github.com/iced-rs/iced/tree/0.7/examples/download_progress -/// [`events`]: https://github.com/iced-rs/iced/tree/0.7/examples/events -/// [`game_of_life`]: https://github.com/iced-rs/iced/tree/0.7/examples/game_of_life -/// [`pokedex`]: https://github.com/iced-rs/iced/tree/0.7/examples/pokedex -/// [`solar_system`]: https://github.com/iced-rs/iced/tree/0.7/examples/solar_system -/// [`stopwatch`]: https://github.com/iced-rs/iced/tree/0.7/examples/stopwatch -/// [`todos`]: https://github.com/iced-rs/iced/tree/0.7/examples/todos +/// [The repository has a bunch of examples]: https://github.com/iced-rs/iced/tree/0.8/examples +/// [`clock`]: https://github.com/iced-rs/iced/tree/0.8/examples/clock +/// [`download_progress`]: https://github.com/iced-rs/iced/tree/0.8/examples/download_progress +/// [`events`]: https://github.com/iced-rs/iced/tree/0.8/examples/events +/// [`game_of_life`]: https://github.com/iced-rs/iced/tree/0.8/examples/game_of_life +/// [`pokedex`]: https://github.com/iced-rs/iced/tree/0.8/examples/pokedex +/// [`solar_system`]: https://github.com/iced-rs/iced/tree/0.8/examples/solar_system +/// [`stopwatch`]: https://github.com/iced-rs/iced/tree/0.8/examples/stopwatch +/// [`todos`]: https://github.com/iced-rs/iced/tree/0.8/examples/todos /// [`Sandbox`]: crate::Sandbox /// [`Canvas`]: crate::widget::Canvas /// [PokéAPI]: https://pokeapi.co/ diff --git a/src/lib.rs b/src/lib.rs index 60281d50..318852f9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,13 +24,13 @@ //! [scrollables]: https://gfycat.com/perkybaggybaboon-rust-gui //! [Debug overlay with performance metrics]: https://gfycat.com/incredibledarlingbee //! [Modular ecosystem]: https://github.com/iced-rs/iced/blob/master/ECOSYSTEM.md -//! [renderer-agnostic native runtime]: https://github.com/iced-rs/iced/tree/0.7/native +//! [renderer-agnostic native runtime]: https://github.com/iced-rs/iced/tree/0.8/native //! [`wgpu`]: https://github.com/gfx-rs/wgpu-rs -//! [built-in renderer]: https://github.com/iced-rs/iced/tree/0.7/wgpu -//! [windowing shell]: https://github.com/iced-rs/iced/tree/0.7/winit +//! [built-in renderer]: https://github.com/iced-rs/iced/tree/0.8/wgpu +//! [windowing shell]: https://github.com/iced-rs/iced/tree/0.8/winit //! [`dodrio`]: https://github.com/fitzgen/dodrio //! [web runtime]: https://github.com/iced-rs/iced_web -//! [examples]: https://github.com/iced-rs/iced/tree/0.7/examples +//! [examples]: https://github.com/iced-rs/iced/tree/0.8/examples //! [repository]: https://github.com/iced-rs/iced //! //! # Overview diff --git a/src/sandbox.rs b/src/sandbox.rs index 31e861ed..e8ed0f81 100644 --- a/src/sandbox.rs +++ b/src/sandbox.rs @@ -34,19 +34,19 @@ use crate::{Application, Command, Element, Error, Settings, Subscription}; /// - [`tour`], a simple UI tour that can run both on native platforms and the /// web! /// -/// [The repository has a bunch of examples]: https://github.com/iced-rs/iced/tree/0.7/examples -/// [`bezier_tool`]: https://github.com/iced-rs/iced/tree/0.7/examples/bezier_tool -/// [`counter`]: https://github.com/iced-rs/iced/tree/0.7/examples/counter -/// [`custom_widget`]: https://github.com/iced-rs/iced/tree/0.7/examples/custom_widget -/// [`geometry`]: https://github.com/iced-rs/iced/tree/0.7/examples/geometry -/// [`pane_grid`]: https://github.com/iced-rs/iced/tree/0.7/examples/pane_grid -/// [`progress_bar`]: https://github.com/iced-rs/iced/tree/0.7/examples/progress_bar -/// [`styling`]: https://github.com/iced-rs/iced/tree/0.7/examples/styling -/// [`svg`]: https://github.com/iced-rs/iced/tree/0.7/examples/svg -/// [`tour`]: https://github.com/iced-rs/iced/tree/0.7/examples/tour +/// [The repository has a bunch of examples]: https://github.com/iced-rs/iced/tree/0.8/examples +/// [`bezier_tool`]: https://github.com/iced-rs/iced/tree/0.8/examples/bezier_tool +/// [`counter`]: https://github.com/iced-rs/iced/tree/0.8/examples/counter +/// [`custom_widget`]: https://github.com/iced-rs/iced/tree/0.8/examples/custom_widget +/// [`geometry`]: https://github.com/iced-rs/iced/tree/0.8/examples/geometry +/// [`pane_grid`]: https://github.com/iced-rs/iced/tree/0.8/examples/pane_grid +/// [`progress_bar`]: https://github.com/iced-rs/iced/tree/0.8/examples/progress_bar +/// [`styling`]: https://github.com/iced-rs/iced/tree/0.8/examples/styling +/// [`svg`]: https://github.com/iced-rs/iced/tree/0.8/examples/svg +/// [`tour`]: https://github.com/iced-rs/iced/tree/0.8/examples/tour /// [`Canvas widget`]: crate::widget::Canvas /// [the overview]: index.html#overview -/// [`iced_wgpu`]: https://github.com/iced-rs/iced/tree/0.7/wgpu +/// [`iced_wgpu`]: https://github.com/iced-rs/iced/tree/0.8/wgpu /// [`Svg` widget]: crate::widget::Svg /// [Ghostscript Tiger]: https://commons.wikimedia.org/wiki/File:Ghostscript_Tiger.svg /// diff --git a/src/widget.rs b/src/widget.rs index 7da5b82b..e2b0537e 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -56,7 +56,7 @@ pub mod pane_grid { //! The [`pane_grid` example] showcases how to use a [`PaneGrid`] with resizing, //! drag and drop, and hotkey support. //! - //! [`pane_grid` example]: https://github.com/iced-rs/iced/tree/0.7/examples/pane_grid + //! [`pane_grid` example]: https://github.com/iced-rs/iced/tree/0.8/examples/pane_grid pub use iced_native::widget::pane_grid::{ Axis, Configuration, Direction, DragEvent, Line, Node, Pane, ResizeEvent, Split, State, StyleSheet, diff --git a/style/Cargo.toml b/style/Cargo.toml index 2be3e78d..3b54f1ec 100644 --- a/style/Cargo.toml +++ b/style/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "iced_style" -version = "0.6.0" +version = "0.7.0" authors = ["Héctor Ramón Jiménez "] edition = "2021" description = "The default set of styles of Iced" @@ -11,7 +11,7 @@ keywords = ["gui", "ui", "graphics", "interface", "widgets"] categories = ["gui"] [dependencies.iced_core] -version = "0.7" +version = "0.8" path = "../core" features = ["palette"] diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index 352802b8..f1e22cf6 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "iced_wgpu" -version = "0.8.0" +version = "0.9.0" authors = ["Héctor Ramón Jiménez "] edition = "2021" description = "A wgpu renderer for Iced" @@ -42,11 +42,11 @@ version = "1.9" features = ["derive"] [dependencies.iced_native] -version = "0.8" +version = "0.9" path = "../native" [dependencies.iced_graphics] -version = "0.6" +version = "0.7" path = "../graphics" features = ["font-fallback", "font-icons"] diff --git a/wgpu/README.md b/wgpu/README.md index 8ef68c62..3e6af103 100644 --- a/wgpu/README.md +++ b/wgpu/README.md @@ -30,7 +30,7 @@ Currently, `iced_wgpu` supports the following primitives: Add `iced_wgpu` as a dependency in your `Cargo.toml`: ```toml -iced_wgpu = "0.8" +iced_wgpu = "0.9" ``` __Iced moves fast and the `master` branch can contain breaking changes!__ If diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 5198276d..1a293681 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -16,7 +16,7 @@ //! - Meshes of triangles, useful to draw geometry freely. //! //! [Iced]: https://github.com/iced-rs/iced -//! [`iced_native`]: https://github.com/iced-rs/iced/tree/0.7/native +//! [`iced_native`]: https://github.com/iced-rs/iced/tree/0.8/native //! [`wgpu`]: https://github.com/gfx-rs/wgpu-rs //! [WebGPU API]: https://gpuweb.github.io/gpuweb/ //! [`wgpu_glyph`]: https://github.com/hecrj/wgpu_glyph diff --git a/winit/Cargo.toml b/winit/Cargo.toml index dd975cbe..60e464c6 100644 --- a/winit/Cargo.toml +++ b/winit/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "iced_winit" -version = "0.7.0" +version = "0.8.0" authors = ["Héctor Ramón Jiménez "] edition = "2021" description = "A winit runtime for Iced" @@ -28,15 +28,15 @@ git = "https://github.com/iced-rs/winit.git" rev = "940457522e9fb9f5dac228b0ecfafe0138b4048c" [dependencies.iced_native] -version = "0.8" +version = "0.9" path = "../native" [dependencies.iced_graphics] -version = "0.6" +version = "0.7" path = "../graphics" [dependencies.iced_futures] -version = "0.5" +version = "0.6" path = "../futures" [dependencies.tracing] diff --git a/winit/README.md b/winit/README.md index 44286c2c..83810473 100644 --- a/winit/README.md +++ b/winit/README.md @@ -20,7 +20,7 @@ It exposes a renderer-agnostic `Application` trait that can be implemented and t Add `iced_winit` as a dependency in your `Cargo.toml`: ```toml -iced_winit = "0.7" +iced_winit = "0.8" ``` __Iced moves fast and the `master` branch can contain breaking changes!__ If diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index e83e55ec..1b2ead36 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -1,7 +1,7 @@ //! Convert [`winit`] types into [`iced_native`] types, and viceversa. //! //! [`winit`]: https://github.com/rust-windowing/winit -//! [`iced_native`]: https://github.com/iced-rs/iced/tree/0.7/native +//! [`iced_native`]: https://github.com/iced-rs/iced/tree/0.8/native use crate::keyboard; use crate::mouse; use crate::touch; @@ -218,7 +218,7 @@ pub fn mode(mode: Option) -> window::Mode { /// Converts a `MouseCursor` from [`iced_native`] to a [`winit`] cursor icon. /// /// [`winit`]: https://github.com/rust-windowing/winit -/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.7/native +/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.8/native pub fn mouse_interaction( interaction: mouse::Interaction, ) -> winit::window::CursorIcon { @@ -242,7 +242,7 @@ pub fn mouse_interaction( /// Converts a `MouseButton` from [`winit`] to an [`iced_native`] mouse button. /// /// [`winit`]: https://github.com/rust-windowing/winit -/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.7/native +/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.8/native pub fn mouse_button(mouse_button: winit::event::MouseButton) -> mouse::Button { match mouse_button { winit::event::MouseButton::Left => mouse::Button::Left, @@ -258,7 +258,7 @@ pub fn mouse_button(mouse_button: winit::event::MouseButton) -> mouse::Button { /// modifiers state. /// /// [`winit`]: https://github.com/rust-windowing/winit -/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.7/native +/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.8/native pub fn modifiers( modifiers: winit::event::ModifiersState, ) -> keyboard::Modifiers { @@ -285,7 +285,7 @@ pub fn cursor_position( /// Converts a `Touch` from [`winit`] to an [`iced_native`] touch event. /// /// [`winit`]: https://github.com/rust-windowing/winit -/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.7/native +/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.8/native pub fn touch_event( touch: winit::event::Touch, scale_factor: f64, @@ -316,7 +316,7 @@ pub fn touch_event( /// Converts a `VirtualKeyCode` from [`winit`] to an [`iced_native`] key code. /// /// [`winit`]: https://github.com/rust-windowing/winit -/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.7/native +/// [`iced_native`]: https://github.com/iced-rs/iced/tree/0.8/native pub fn key_code( virtual_keycode: winit::event::VirtualKeyCode, ) -> keyboard::KeyCode { diff --git a/winit/src/lib.rs b/winit/src/lib.rs index c3172319..3a33e174 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -11,7 +11,7 @@ //! Additionally, a [`conversion`] module is available for users that decide to //! implement a custom event loop. //! -//! [`iced_native`]: https://github.com/iced-rs/iced/tree/0.7/native +//! [`iced_native`]: https://github.com/iced-rs/iced/tree/0.8/native //! [`winit`]: https://github.com/rust-windowing/winit //! [`conversion`]: crate::conversion #![doc( -- cgit From 3d8f1ad238cea6faaa168a3be516097e5817b9c2 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 18 Feb 2023 21:52:08 +0100 Subject: Fix base cursor position during `UserInterface::draw` when overlay is present --- native/src/user_interface.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/native/src/user_interface.rs b/native/src/user_interface.rs index f5202609..68ccda55 100644 --- a/native/src/user_interface.rs +++ b/native/src/user_interface.rs @@ -440,12 +440,13 @@ where overlay.layout(renderer, self.bounds, Vector::ZERO) }); - let new_cursor_position = - if overlay_layout.bounds().contains(cursor_position) { - Point::new(-1.0, -1.0) - } else { - cursor_position - }; + let new_cursor_position = if overlay + .is_over(Layout::new(&overlay_layout), cursor_position) + { + Point::new(-1.0, -1.0) + } else { + cursor_position + }; self.overlay = Some(overlay_layout); -- cgit From bf061a0d628e97997f9af744a66239cb03ae6d78 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 18 Feb 2023 21:55:49 +0100 Subject: Remove `Clone` bound for `Bytes::new` in `image` --- native/src/image.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/native/src/image.rs b/native/src/image.rs index 4c5e926b..70fbade0 100644 --- a/native/src/image.rs +++ b/native/src/image.rs @@ -28,7 +28,7 @@ impl Handle { pub fn from_pixels( width: u32, height: u32, - pixels: impl AsRef<[u8]> + Clone + Send + Sync + 'static, + pixels: impl AsRef<[u8]> + Send + Sync + 'static, ) -> Handle { Self::from_data(Data::Rgba { width, @@ -44,7 +44,7 @@ impl Handle { /// This is useful if you already have your image loaded in-memory, maybe /// because you downloaded or generated it procedurally. pub fn from_memory( - bytes: impl AsRef<[u8]> + Clone + Send + Sync + 'static, + bytes: impl AsRef<[u8]> + Send + Sync + 'static, ) -> Handle { Self::from_data(Data::Bytes(Bytes::new(bytes))) } @@ -93,7 +93,7 @@ pub struct Bytes(Arc + Send + Sync + 'static>); impl Bytes { /// Creates new [`Bytes`] around `data`. - pub fn new(data: impl AsRef<[u8]> + Clone + Send + Sync + 'static) -> Self { + pub fn new(data: impl AsRef<[u8]> + Send + Sync + 'static) -> Self { Self(Arc::new(data)) } } -- cgit From 2b8742937fb41fbe2ce60a494bfce2fd8b6ab916 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 18 Feb 2023 22:16:44 +0100 Subject: Bump version of `iced_native` :tada: --- native/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/native/Cargo.toml b/native/Cargo.toml index 25281ea7..3f92783e 100644 --- a/native/Cargo.toml +++ b/native/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "iced_native" -version = "0.9.0" +version = "0.9.1" authors = ["Héctor Ramón Jiménez "] edition = "2021" description = "A renderer-agnostic library for native GUIs" -- cgit From 96c0bd65df5b85682a2d674b231a5ed420f106fb Mon Sep 17 00:00:00 2001 From: Casper Storm Date: Mon, 20 Feb 2023 12:24:31 +0100 Subject: Sliders no longer bleed over rail --- native/src/widget/slider.rs | 4 ++-- native/src/widget/vertical_slider.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/native/src/widget/slider.rs b/native/src/widget/slider.rs index 87030a4d..f5251dfa 100644 --- a/native/src/widget/slider.rs +++ b/native/src/widget/slider.rs @@ -423,8 +423,8 @@ pub fn draw( let handle_offset = if range_start >= range_end { 0.0 } else { - bounds.width * (value - range_start) / (range_end - range_start) - - handle_width / 2.0 + (bounds.width - handle_width) * (value - range_start) + / (range_end - range_start) }; renderer.fill_quad( diff --git a/native/src/widget/vertical_slider.rs b/native/src/widget/vertical_slider.rs index 28e8405c..0302f3cf 100644 --- a/native/src/widget/vertical_slider.rs +++ b/native/src/widget/vertical_slider.rs @@ -418,8 +418,8 @@ pub fn draw( let handle_offset = if range_start >= range_end { 0.0 } else { - bounds.height * (value - range_end) / (range_start - range_end) - - handle_width / 2.0 + (bounds.height - handle_width) * (value - range_end) + / (range_start - range_end) }; renderer.fill_quad( -- cgit From 1fb413fd8014ad5911a95292bcfbeadb5a58d72f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 21 Feb 2023 20:56:10 +0100 Subject: Change `subscription::run` to take a function pointer ... and rename the old `run` to `run_with_id`. --- native/src/subscription.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/native/src/subscription.rs b/native/src/subscription.rs index b505f3cc..16e78e82 100644 --- a/native/src/subscription.rs +++ b/native/src/subscription.rs @@ -100,11 +100,24 @@ where }) } +/// Returns a [`Subscription`] that will call the given function to create and +/// asynchronously run the given [`Stream`]. +pub fn run(builder: fn() -> S) -> Subscription +where + S: Stream + MaybeSend + 'static, + Message: 'static, +{ + Subscription::from_recipe(Runner { + id: builder, + spawn: move |_| builder(), + }) +} + /// Returns a [`Subscription`] that will create and asynchronously run the /// given [`Stream`]. /// /// The `id` will be used to uniquely identify the [`Subscription`]. -pub fn run(id: I, stream: S) -> Subscription +pub fn run_with_id(id: I, stream: S) -> Subscription where I: Hash + 'static, S: Stream + MaybeSend + 'static, @@ -199,7 +212,7 @@ where use futures::future::{self, FutureExt}; use futures::stream::StreamExt; - run( + run_with_id( id, futures::stream::unfold(initial, move |state| f(state).map(Some)) .filter_map(future::ready), -- cgit