From f54590d7adac611db84b88cbcbf4f56c7542039c Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Tue, 8 Dec 2020 18:47:01 -0600 Subject: Replace TitleBar string title with generic Content --- graphics/src/widget/pane_grid.rs | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/pane_grid.rs b/graphics/src/widget/pane_grid.rs index f09984fc..b4380232 100644 --- a/graphics/src/widget/pane_grid.rs +++ b/graphics/src/widget/pane_grid.rs @@ -12,11 +12,7 @@ use crate::defaults; use crate::{Primitive, Renderer}; use iced_native::mouse; use iced_native::pane_grid; -use iced_native::text; -use iced_native::{ - Element, HorizontalAlignment, Layout, Point, Rectangle, Vector, - VerticalAlignment, -}; +use iced_native::{Element, Layout, Point, Rectangle, Vector}; pub use iced_native::pane_grid::{ Axis, Configuration, Content, Direction, DragEvent, Pane, ResizeEvent, @@ -188,14 +184,12 @@ where defaults: &Self::Defaults, bounds: Rectangle, style_sheet: &Self::Style, - title: &str, - title_size: u16, - title_font: Self::Font, - title_bounds: Rectangle, + content: (&Element<'_, Message, Self>, Layout<'_>), controls: Option<(&Element<'_, Message, Self>, Layout<'_>)>, cursor_position: Point, ) -> Self::Output { let style = style_sheet.style(); + let (title_content, title_layout) = content; let defaults = Self::Defaults { text: defaults::Text { @@ -205,16 +199,12 @@ where let background = crate::widget::container::background(bounds, &style); - let (title_primitive, _) = text::Renderer::draw( + let (title_primitive, title_interaction) = title_content.draw( self, &defaults, - title_bounds, - title, - title_size, - title_font, - None, - HorizontalAlignment::Left, - VerticalAlignment::Top, + title_layout, + cursor_position, + &bounds, ); if let Some((controls, controls_layout)) = controls { @@ -234,7 +224,7 @@ where controls_primitive, ], }, - controls_interaction, + controls_interaction.max(title_interaction), ) } else { ( @@ -245,7 +235,7 @@ where } else { title_primitive }, - mouse::Interaction::default(), + title_interaction, ) } } -- cgit From 74ee7cca811d6f462ca253bced4e3f07e52d9300 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 18 Dec 2020 10:18:39 +0100 Subject: Format use declarations in `image::viewer` --- graphics/src/widget/image/viewer.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/image/viewer.rs b/graphics/src/widget/image/viewer.rs index b6217ff7..0bcd09fc 100644 --- a/graphics/src/widget/image/viewer.rs +++ b/graphics/src/widget/image/viewer.rs @@ -2,10 +2,10 @@ use crate::backend::{self, Backend}; use crate::{Primitive, Renderer}; -use iced_native::{ - image::{self, viewer}, - mouse, Rectangle, Vector, -}; +use iced_native::image; +use iced_native::image::viewer; +use iced_native::mouse; +use iced_native::{Rectangle, Vector}; impl viewer::Renderer for Renderer where -- cgit From 21b10dc103638ead2a567b3426c937f39e9addbd Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 18 Dec 2020 10:44:24 +0100 Subject: Fix `layout` of `image::Viewer` --- graphics/src/widget/image/viewer.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/image/viewer.rs b/graphics/src/widget/image/viewer.rs index 0bcd09fc..804f2be6 100644 --- a/graphics/src/widget/image/viewer.rs +++ b/graphics/src/widget/image/viewer.rs @@ -5,7 +5,7 @@ use crate::{Primitive, Renderer}; use iced_native::image; use iced_native::image::viewer; use iced_native::mouse; -use iced_native::{Rectangle, Vector}; +use iced_native::{Rectangle, Size, Vector}; impl viewer::Renderer for Renderer where @@ -15,7 +15,7 @@ where &mut self, state: &viewer::State, bounds: Rectangle, - image_bounds: Rectangle, + image_size: Size, translation: Vector, handle: image::Handle, is_mouse_over: bool, @@ -28,7 +28,11 @@ where translation, content: Box::new(Primitive::Image { handle, - bounds: image_bounds, + bounds: Rectangle { + x: bounds.x, + y: bounds.y, + ..Rectangle::with_size(image_size) + }, }), }), offset: Vector::new(0, 0), @@ -38,8 +42,8 @@ where if state.is_cursor_clicked() { mouse::Interaction::Grabbing } else if is_mouse_over - && (image_bounds.width > bounds.width - || image_bounds.height > bounds.height) + && (image_size.width > bounds.width + || image_size.height > bounds.height) { mouse::Interaction::Grab } else { -- cgit From 43ef85ae5c5b8901642e0832456ed907635600ff Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 18 Dec 2020 11:04:07 +0100 Subject: Rename `starting_cursor_pos` to `cursor_grabbed_at` in `image::Viewer` --- graphics/src/widget/image/viewer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'graphics') diff --git a/graphics/src/widget/image/viewer.rs b/graphics/src/widget/image/viewer.rs index 804f2be6..28dffc4f 100644 --- a/graphics/src/widget/image/viewer.rs +++ b/graphics/src/widget/image/viewer.rs @@ -39,7 +39,7 @@ where } }, { - if state.is_cursor_clicked() { + if state.is_cursor_grabbed() { mouse::Interaction::Grabbing } else if is_mouse_over && (image_size.width > bounds.width -- cgit From 4bbfdef14b6983c05d91137f930c15a52da37d86 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 19 Dec 2020 01:24:46 +0100 Subject: Update `font-kit` to `0.10` --- graphics/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'graphics') diff --git a/graphics/Cargo.toml b/graphics/Cargo.toml index 73dc47bf..aac9ccf2 100644 --- a/graphics/Cargo.toml +++ b/graphics/Cargo.toml @@ -44,7 +44,7 @@ version = "0.12" optional = true [dependencies.font-kit] -version = "0.8" +version = "0.10" optional = true [package.metadata.docs.rs] -- cgit From f2c2f3fc7588054417a0c44d3890defa976c5f61 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 22 Dec 2020 14:44:44 +0100 Subject: Remove unnecessary `text::Renderer` bound for `PaneGrid` This is no longer necessary, as we do not render text directly anymore. --- graphics/src/widget/pane_grid.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/pane_grid.rs b/graphics/src/widget/pane_grid.rs index b4380232..29478447 100644 --- a/graphics/src/widget/pane_grid.rs +++ b/graphics/src/widget/pane_grid.rs @@ -7,9 +7,8 @@ //! drag and drop, and hotkey support. //! //! [`pane_grid` example]: https://github.com/hecrj/iced/tree/0.2/examples/pane_grid -use crate::backend::{self, Backend}; use crate::defaults; -use crate::{Primitive, Renderer}; +use crate::{Backend, Primitive, Renderer}; use iced_native::mouse; use iced_native::pane_grid; use iced_native::{Element, Layout, Point, Rectangle, Vector}; @@ -30,7 +29,7 @@ pub type PaneGrid<'a, Message, Backend> = impl pane_grid::Renderer for Renderer where - B: Backend + backend::Text, + B: Backend, { fn draw( &mut self, -- cgit From a7bb7bb2eaaae5a016721788fcaf03c4c7413acd Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 1 Jan 2021 15:28:38 +0100 Subject: Implement split highlight on hover for `PaneGrid` --- graphics/src/widget/pane_grid.rs | 78 +++++++++++++++++++++++++++++++++------- 1 file changed, 66 insertions(+), 12 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/pane_grid.rs b/graphics/src/widget/pane_grid.rs index 29478447..3cd4fd34 100644 --- a/graphics/src/widget/pane_grid.rs +++ b/graphics/src/widget/pane_grid.rs @@ -8,16 +8,19 @@ //! //! [`pane_grid` example]: https://github.com/hecrj/iced/tree/0.2/examples/pane_grid use crate::defaults; -use crate::{Backend, Primitive, Renderer}; +use crate::{Backend, Color, Primitive, Renderer}; +use iced_native::container; use iced_native::mouse; use iced_native::pane_grid; use iced_native::{Element, Layout, Point, Rectangle, Vector}; pub use iced_native::pane_grid::{ - Axis, Configuration, Content, Direction, DragEvent, Pane, ResizeEvent, - Split, State, TitleBar, + Axis, Configuration, Content, Direction, DragEvent, Node, Pane, + ResizeEvent, Split, State, TitleBar, }; +pub use iced_style::pane_grid::{Line, StyleSheet}; + /// A collection of panes distributed using either vertical or horizontal splits /// to completely fill the space available. /// @@ -31,13 +34,16 @@ impl pane_grid::Renderer for Renderer where B: Backend, { + type Style = Box; + fn draw( &mut self, defaults: &Self::Defaults, content: &[(Pane, Content<'_, Message, Self>)], dragging: Option<(Pane, Point)>, - resizing: Option, + resizing: Option<(Axis, Rectangle, bool)>, layout: Layout<'_>, + style_sheet: &::Style, cursor_position: Point, ) -> Self::Output { let pane_cursor_position = if dragging.is_some() { @@ -73,7 +79,8 @@ where }) .collect(); - let primitives = if let Some((index, layout, origin)) = dragged_pane { + let mut primitives = if let Some((index, layout, origin)) = dragged_pane + { let pane = panes.remove(index); let bounds = layout.bounds(); @@ -103,15 +110,62 @@ where panes }; + let (primitives, mouse_interaction) = + if let Some((axis, split_region, is_picked)) = resizing { + let highlight = if is_picked { + style_sheet.picked_split() + } else { + style_sheet.hovered_split() + }; + + if let Some(highlight) = highlight { + primitives.push(Primitive::Quad { + bounds: match axis { + Axis::Horizontal => Rectangle { + x: split_region.x, + y: (split_region.y + + (split_region.height - highlight.width) + / 2.0) + .round(), + width: split_region.width, + height: highlight.width, + }, + Axis::Vertical => Rectangle { + x: (split_region.x + + (split_region.width - highlight.width) + / 2.0) + .round(), + y: split_region.y, + width: highlight.width, + height: split_region.height, + }, + }, + background: highlight.color.into(), + border_radius: 0.0, + border_width: 0.0, + border_color: Color::TRANSPARENT, + }); + } + + ( + primitives, + match axis { + Axis::Horizontal => { + mouse::Interaction::ResizingVertically + } + Axis::Vertical => { + mouse::Interaction::ResizingHorizontally + } + }, + ) + } else { + (primitives, mouse_interaction) + }; + ( Primitive::Group { primitives }, if dragging.is_some() { mouse::Interaction::Grabbing - } else if let Some(axis) = resizing { - match axis { - Axis::Horizontal => mouse::Interaction::ResizingVertically, - Axis::Vertical => mouse::Interaction::ResizingHorizontally, - } } else { mouse_interaction }, @@ -122,7 +176,7 @@ where &mut self, defaults: &Self::Defaults, bounds: Rectangle, - style_sheet: &Self::Style, + style_sheet: &::Style, title_bar: Option<(&TitleBar<'_, Message, Self>, Layout<'_>)>, body: (&Element<'_, Message, Self>, Layout<'_>), cursor_position: Point, @@ -182,7 +236,7 @@ where &mut self, defaults: &Self::Defaults, bounds: Rectangle, - style_sheet: &Self::Style, + style_sheet: &::Style, content: (&Element<'_, Message, Self>, Layout<'_>), controls: Option<(&Element<'_, Message, Self>, Layout<'_>)>, cursor_position: Point, -- cgit From 766bb7f5ccf4ac9294fab92a5126c60c0fa8d818 Mon Sep 17 00:00:00 2001 From: Tanner Rogalsky Date: Thu, 14 Jan 2021 10:38:12 -0500 Subject: Fix `physical_width` getter incorrectly returning the height --- graphics/src/viewport.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'graphics') diff --git a/graphics/src/viewport.rs b/graphics/src/viewport.rs index 78d539af..2c0b541a 100644 --- a/graphics/src/viewport.rs +++ b/graphics/src/viewport.rs @@ -31,7 +31,7 @@ impl Viewport { /// Returns the physical width of the [`Viewport`]. pub fn physical_width(&self) -> u32 { - self.physical_size.height + self.physical_size.width } /// Returns the physical height of the [`Viewport`]. -- cgit From d1c4239ac7ffdf299e4f9fae36406361cfef9267 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 21 Jan 2021 05:07:41 +0100 Subject: Disable default features of `qrcode` for `iced_graphics` --- graphics/Cargo.toml | 1 + 1 file changed, 1 insertion(+) (limited to 'graphics') diff --git a/graphics/Cargo.toml b/graphics/Cargo.toml index aac9ccf2..ab41ac44 100644 --- a/graphics/Cargo.toml +++ b/graphics/Cargo.toml @@ -42,6 +42,7 @@ optional = true [dependencies.qrcode] version = "0.12" optional = true +default-features = false [dependencies.font-kit] version = "0.10" -- cgit From a19f89d3a6af2804f2ac4e30f6d639b56a9bebfd Mon Sep 17 00:00:00 2001 From: Yusuf Bera Ertan Date: Tue, 28 Jul 2020 18:07:46 +0300 Subject: feat(native): add Tooltip widget --- graphics/src/widget.rs | 3 +++ graphics/src/widget/tooltip.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 graphics/src/widget/tooltip.rs (limited to 'graphics') diff --git a/graphics/src/widget.rs b/graphics/src/widget.rs index 159ca91b..190ea9c0 100644 --- a/graphics/src/widget.rs +++ b/graphics/src/widget.rs @@ -20,6 +20,7 @@ pub mod scrollable; pub mod slider; pub mod svg; pub mod text_input; +pub mod tooltip; mod column; mod row; @@ -48,6 +49,8 @@ pub use scrollable::Scrollable; pub use slider::Slider; #[doc(no_inline)] pub use text_input::TextInput; +#[doc(no_inline)] +pub use tooltip::Tooltip; pub use column::Column; pub use image::Image; diff --git a/graphics/src/widget/tooltip.rs b/graphics/src/widget/tooltip.rs new file mode 100644 index 00000000..b5b0c558 --- /dev/null +++ b/graphics/src/widget/tooltip.rs @@ -0,0 +1,37 @@ +//! Decorate content and apply alignment. +use crate::defaults::Defaults; +use crate::{Backend, Renderer}; +use iced_native::{Element, Layout, Point, Rectangle}; + +/// An element decorating some content. +/// +/// This is an alias of an `iced_native` tooltip with a default +/// `Renderer`. +pub type Tooltip<'a, Message, Backend> = + iced_native::Tooltip<'a, Message, Renderer>; + +impl iced_native::tooltip::Renderer for Renderer +where + B: Backend, +{ + type Style = (); + + fn draw( + &mut self, + defaults: &Defaults, + cursor_position: Point, + content: &Element<'_, Message, Self>, + content_layout: Layout<'_>, + viewport: &Rectangle, + ) -> Self::Output { + let (content, mouse_interaction) = content.draw( + self, + &defaults, + content_layout, + cursor_position, + viewport, + ); + + (content, mouse_interaction) + } +} -- cgit From 81c75c15249b608dd8a6d47e25f96feb10ca68da Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 23 Feb 2021 03:09:16 +0100 Subject: Change `Tooltip` to support `Text` only for now --- graphics/src/widget/tooltip.rs | 76 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 71 insertions(+), 5 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/tooltip.rs b/graphics/src/widget/tooltip.rs index b5b0c558..0b4d1d2f 100644 --- a/graphics/src/widget/tooltip.rs +++ b/graphics/src/widget/tooltip.rs @@ -1,7 +1,10 @@ //! Decorate content and apply alignment. +use crate::backend::{self, Backend}; use crate::defaults::Defaults; -use crate::{Backend, Renderer}; -use iced_native::{Element, Layout, Point, Rectangle}; +use crate::{Primitive, Renderer, Vector}; + +use iced_native::layout::{self, Layout}; +use iced_native::{Element, Point, Rectangle, Size, Text}; /// An element decorating some content. /// @@ -10,9 +13,11 @@ use iced_native::{Element, Layout, Point, Rectangle}; pub type Tooltip<'a, Message, Backend> = iced_native::Tooltip<'a, Message, Renderer>; +pub use iced_native::tooltip::Position; + impl iced_native::tooltip::Renderer for Renderer where - B: Backend, + B: Backend + backend::Text, { type Style = (); @@ -20,10 +25,14 @@ where &mut self, defaults: &Defaults, cursor_position: Point, - content: &Element<'_, Message, Self>, content_layout: Layout<'_>, viewport: &Rectangle, + content: &Element<'_, Message, Self>, + tooltip: &Text, + position: Position, ) -> Self::Output { + let bounds = content_layout.bounds(); + let (content, mouse_interaction) = content.draw( self, &defaults, @@ -32,6 +41,63 @@ where viewport, ); - (content, mouse_interaction) + if bounds.contains(cursor_position) { + use iced_native::Widget; + + let tooltip_layout = Widget::<(), Self>::layout( + tooltip, + self, + &layout::Limits::new(Size::ZERO, viewport.size()), + ); + + let tooltip_bounds = tooltip_layout.bounds(); + + let x_center = + bounds.x + (bounds.width - tooltip_bounds.width) / 2.0; + + let y_center = + bounds.y + (bounds.height - tooltip_bounds.height) / 2.0; + + let offset = match position { + Position::Top => { + Vector::new(x_center, bounds.y - tooltip_bounds.height) + } + Position::Bottom => { + Vector::new(x_center, bounds.y + bounds.height) + } + Position::Left => { + Vector::new(bounds.x - tooltip_bounds.width, y_center) + } + Position::Right => { + Vector::new(bounds.x + bounds.width, y_center) + } + Position::FollowCursor => Vector::new( + cursor_position.x, + cursor_position.y - tooltip_bounds.height, + ), + }; + + let (tooltip, _) = Widget::<(), Self>::draw( + tooltip, + self, + defaults, + Layout::with_offset(offset, &tooltip_layout), + cursor_position, + viewport, + ); + + ( + Primitive::Clip { + bounds: *viewport, + offset: Vector::new(0, 0), + content: Box::new(Primitive::Group { + primitives: vec![content, tooltip], + }), + }, + mouse_interaction, + ) + } else { + (content, mouse_interaction) + } } } -- cgit From 2f766b73413fe60cd881e139fa0e84a0f0134d91 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 23 Feb 2021 03:16:37 +0100 Subject: Introduce `Tooltip::gap` to control spacing --- graphics/src/widget/tooltip.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/tooltip.rs b/graphics/src/widget/tooltip.rs index 0b4d1d2f..26b18507 100644 --- a/graphics/src/widget/tooltip.rs +++ b/graphics/src/widget/tooltip.rs @@ -30,6 +30,7 @@ where content: &Element<'_, Message, Self>, tooltip: &Text, position: Position, + gap: u16, ) -> Self::Output { let bounds = content_layout.bounds(); @@ -58,18 +59,21 @@ where let y_center = bounds.y + (bounds.height - tooltip_bounds.height) / 2.0; + let gap = f32::from(gap); + let offset = match position { - Position::Top => { - Vector::new(x_center, bounds.y - tooltip_bounds.height) - } + Position::Top => Vector::new( + x_center, + bounds.y - tooltip_bounds.height - gap, + ), Position::Bottom => { - Vector::new(x_center, bounds.y + bounds.height) + Vector::new(x_center, bounds.y + bounds.height + gap) } Position::Left => { - Vector::new(bounds.x - tooltip_bounds.width, y_center) + Vector::new(bounds.x - tooltip_bounds.width - gap, y_center) } Position::Right => { - Vector::new(bounds.x + bounds.width, y_center) + Vector::new(bounds.x + bounds.width + gap, y_center) } Position::FollowCursor => Vector::new( cursor_position.x, -- cgit From 4e923290ccb38dc9cee05592554f98f1f0f12966 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 23 Feb 2021 04:00:35 +0100 Subject: Add `style` and `padding` to `Tooltip` --- graphics/src/widget/tooltip.rs | 86 ++++++++++++++++++++++++++++++------------ 1 file changed, 62 insertions(+), 24 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/tooltip.rs b/graphics/src/widget/tooltip.rs index 26b18507..51b465a5 100644 --- a/graphics/src/widget/tooltip.rs +++ b/graphics/src/widget/tooltip.rs @@ -1,8 +1,9 @@ //! Decorate content and apply alignment. use crate::backend::{self, Backend}; -use crate::defaults::Defaults; +use crate::defaults::{self, Defaults}; use crate::{Primitive, Renderer, Vector}; +use iced_native::container; use iced_native::layout::{self, Layout}; use iced_native::{Element, Point, Rectangle, Size, Text}; @@ -19,7 +20,7 @@ impl iced_native::tooltip::Renderer for Renderer where B: Backend + backend::Text, { - type Style = (); + const DEFAULT_PADDING: u16 = 5; fn draw( &mut self, @@ -30,10 +31,10 @@ where content: &Element<'_, Message, Self>, tooltip: &Text, position: Position, + style_sheet: &::Style, gap: u16, + padding: u16, ) -> Self::Output { - let bounds = content_layout.bounds(); - let (content, mouse_interaction) = content.draw( self, &defaults, @@ -42,13 +43,26 @@ where viewport, ); + let bounds = content_layout.bounds(); + if bounds.contains(cursor_position) { use iced_native::Widget; + let gap = f32::from(gap); + let padding = f32::from(padding); + let style = style_sheet.style(); + + let defaults = Defaults { + text: defaults::Text { + color: style.text_color.unwrap_or(defaults.text.color), + }, + }; + let tooltip_layout = Widget::<(), Self>::layout( tooltip, self, - &layout::Limits::new(Size::ZERO, viewport.size()), + &layout::Limits::new(Size::ZERO, viewport.size()) + .pad(f32::from(padding)), ); let tooltip_bounds = tooltip_layout.bounds(); @@ -59,22 +73,23 @@ where let y_center = bounds.y + (bounds.height - tooltip_bounds.height) / 2.0; - let gap = f32::from(gap); - let offset = match position { Position::Top => Vector::new( x_center, - bounds.y - tooltip_bounds.height - gap, + bounds.y - tooltip_bounds.height - gap - padding, + ), + Position::Bottom => Vector::new( + x_center, + bounds.y + bounds.height + gap + padding, + ), + Position::Left => Vector::new( + bounds.x - tooltip_bounds.width - gap - padding, + y_center, + ), + Position::Right => Vector::new( + bounds.x + bounds.width + gap + padding, + y_center, ), - Position::Bottom => { - Vector::new(x_center, bounds.y + bounds.height + gap) - } - Position::Left => { - Vector::new(bounds.x - tooltip_bounds.width - gap, y_center) - } - Position::Right => { - Vector::new(bounds.x + bounds.width + gap, y_center) - } Position::FollowCursor => Vector::new( cursor_position.x, cursor_position.y - tooltip_bounds.height, @@ -84,19 +99,42 @@ where let (tooltip, _) = Widget::<(), Self>::draw( tooltip, self, - defaults, + &defaults, Layout::with_offset(offset, &tooltip_layout), cursor_position, viewport, ); + let tooltip_bounds = Rectangle { + x: offset.x - padding, + y: offset.y - padding, + width: tooltip_bounds.width + padding * 2.0, + height: tooltip_bounds.height + padding * 2.0, + }; + ( - Primitive::Clip { - bounds: *viewport, - offset: Vector::new(0, 0), - content: Box::new(Primitive::Group { - primitives: vec![content, tooltip], - }), + Primitive::Group { + primitives: vec![ + content, + Primitive::Clip { + bounds: *viewport, + offset: Vector::new(0, 0), + content: Box::new( + if let Some(background) = + crate::container::background( + tooltip_bounds, + &style, + ) + { + Primitive::Group { + primitives: vec![background, tooltip], + } + } else { + tooltip + }, + ), + }, + ], }, mouse_interaction, ) -- cgit From 5e2743361bf0a2e10c0e14638a9b2818c9e5e364 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 23 Feb 2021 04:02:55 +0100 Subject: Generate new layers only for clip primitives in `Layer::generate` --- graphics/src/layer.rs | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) (limited to 'graphics') diff --git a/graphics/src/layer.rs b/graphics/src/layer.rs index ab40b114..7dce1d4c 100644 --- a/graphics/src/layer.rs +++ b/graphics/src/layer.rs @@ -82,7 +82,12 @@ impl<'a> Layer<'a> { let mut layers = vec![first_layer]; - Self::process_primitive(&mut layers, Vector::new(0.0, 0.0), primitive); + Self::process_primitive( + &mut layers, + Vector::new(0.0, 0.0), + primitive, + 0, + ); layers } @@ -91,13 +96,19 @@ impl<'a> Layer<'a> { layers: &mut Vec, translation: Vector, primitive: &'a Primitive, + current_layer: usize, ) { match primitive { Primitive::None => {} Primitive::Group { primitives } => { // TODO: Inspect a bit and regroup (?) for primitive in primitives { - Self::process_primitive(layers, translation, primitive) + Self::process_primitive( + layers, + translation, + primitive, + current_layer, + ) } } Primitive::Text { @@ -109,7 +120,7 @@ impl<'a> Layer<'a> { horizontal_alignment, vertical_alignment, } => { - let layer = layers.last_mut().unwrap(); + let layer = &mut layers[current_layer]; layer.text.push(Text { content, @@ -128,7 +139,7 @@ impl<'a> Layer<'a> { border_width, border_color, } => { - let layer = layers.last_mut().unwrap(); + let layer = &mut layers[current_layer]; // TODO: Move some of these computations to the GPU (?) layer.quads.push(Quad { @@ -146,7 +157,7 @@ impl<'a> Layer<'a> { }); } Primitive::Mesh2D { buffers, size } => { - let layer = layers.last_mut().unwrap(); + let layer = &mut layers[current_layer]; let bounds = Rectangle::new( Point::new(translation.x, translation.y), @@ -167,7 +178,7 @@ impl<'a> Layer<'a> { offset, content, } => { - let layer = layers.last_mut().unwrap(); + let layer = &mut layers[current_layer]; let translated_bounds = *bounds + translation; // Only draw visible content @@ -175,16 +186,15 @@ impl<'a> Layer<'a> { layer.bounds.intersection(&translated_bounds) { let clip_layer = Layer::new(clip_bounds); - let new_layer = Layer::new(layer.bounds); - layers.push(clip_layer); + Self::process_primitive( layers, translation - Vector::new(offset.x as f32, offset.y as f32), content, + layers.len() - 1, ); - layers.push(new_layer); } } Primitive::Translate { @@ -195,13 +205,19 @@ impl<'a> Layer<'a> { layers, translation + *new_translation, &content, + current_layer, ); } Primitive::Cached { cache } => { - Self::process_primitive(layers, translation, &cache); + Self::process_primitive( + layers, + translation, + &cache, + current_layer, + ); } Primitive::Image { handle, bounds } => { - let layer = layers.last_mut().unwrap(); + let layer = &mut layers[current_layer]; layer.images.push(Image::Raster { handle: handle.clone(), @@ -209,7 +225,7 @@ impl<'a> Layer<'a> { }); } Primitive::Svg { handle, bounds } => { - let layer = layers.last_mut().unwrap(); + let layer = &mut layers[current_layer]; layer.images.push(Image::Vector { handle: handle.clone(), -- cgit From f52f8c1337f42cf9483abb40784129f4effbe48e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 27 Feb 2021 03:36:46 +0100 Subject: Fix `viewport` argument in `PaneGrid` draw calls --- graphics/src/widget/pane_grid.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/pane_grid.rs b/graphics/src/widget/pane_grid.rs index 3cd4fd34..d06f8c6c 100644 --- a/graphics/src/widget/pane_grid.rs +++ b/graphics/src/widget/pane_grid.rs @@ -45,6 +45,7 @@ where layout: Layout<'_>, style_sheet: &::Style, cursor_position: Point, + viewport: &Rectangle, ) -> Self::Output { let pane_cursor_position = if dragging.is_some() { // TODO: Remove once cursor availability is encoded in the type @@ -62,8 +63,13 @@ where .zip(layout.children()) .enumerate() .map(|(i, ((id, pane), layout))| { - let (primitive, new_mouse_interaction) = - pane.draw(self, defaults, layout, pane_cursor_position); + let (primitive, new_mouse_interaction) = pane.draw( + self, + defaults, + layout, + pane_cursor_position, + viewport, + ); if new_mouse_interaction > mouse_interaction { mouse_interaction = new_mouse_interaction; @@ -180,12 +186,13 @@ where title_bar: Option<(&TitleBar<'_, Message, Self>, Layout<'_>)>, body: (&Element<'_, Message, Self>, Layout<'_>), cursor_position: Point, + viewport: &Rectangle, ) -> Self::Output { let style = style_sheet.style(); let (body, body_layout) = body; let (body_primitive, body_interaction) = - body.draw(self, defaults, body_layout, cursor_position, &bounds); + body.draw(self, defaults, body_layout, cursor_position, viewport); let background = crate::widget::container::background(bounds, &style); @@ -199,6 +206,7 @@ where defaults, title_bar_layout, cursor_position, + viewport, show_controls, ); @@ -240,6 +248,7 @@ where content: (&Element<'_, Message, Self>, Layout<'_>), controls: Option<(&Element<'_, Message, Self>, Layout<'_>)>, cursor_position: Point, + viewport: &Rectangle, ) -> Self::Output { let style = style_sheet.style(); let (title_content, title_layout) = content; @@ -257,7 +266,7 @@ where &defaults, title_layout, cursor_position, - &bounds, + viewport, ); if let Some((controls, controls_layout)) = controls { @@ -266,7 +275,7 @@ where &defaults, controls_layout, cursor_position, - &bounds, + viewport, ); ( -- cgit From c51b771519c5da5a4d5cd39eaadfe763c1e60978 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 27 Feb 2021 03:47:13 +0100 Subject: Reposition `Tooltip` inside `viewport` bounds ... only when out of bounds. --- graphics/src/widget/tooltip.rs | 97 ++++++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 37 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/tooltip.rs b/graphics/src/widget/tooltip.rs index 51b465a5..1a1b5352 100644 --- a/graphics/src/widget/tooltip.rs +++ b/graphics/src/widget/tooltip.rs @@ -58,60 +58,83 @@ where }, }; - let tooltip_layout = Widget::<(), Self>::layout( + let text_layout = Widget::<(), Self>::layout( tooltip, self, &layout::Limits::new(Size::ZERO, viewport.size()) .pad(f32::from(padding)), ); - let tooltip_bounds = tooltip_layout.bounds(); - - let x_center = - bounds.x + (bounds.width - tooltip_bounds.width) / 2.0; - + 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 - tooltip_bounds.height) / 2.0; - - let offset = match position { - Position::Top => Vector::new( - x_center, - bounds.y - tooltip_bounds.height - gap - padding, - ), - Position::Bottom => Vector::new( - x_center, - bounds.y + bounds.height + gap + padding, - ), - Position::Left => Vector::new( - bounds.x - tooltip_bounds.width - gap - padding, - y_center, - ), - Position::Right => Vector::new( - bounds.x + bounds.width + gap + padding, - y_center, - ), - Position::FollowCursor => Vector::new( - cursor_position.x, - cursor_position.y - tooltip_bounds.height, - ), + bounds.y + (bounds.height - text_bounds.height) / 2.0; + + let mut tooltip_bounds = { + let offset = match position { + Position::Top => Vector::new( + x_center, + bounds.y - text_bounds.height - gap - padding, + ), + Position::Bottom => Vector::new( + x_center, + bounds.y + bounds.height + gap + padding, + ), + Position::Left => Vector::new( + bounds.x - text_bounds.width - gap - padding, + y_center, + ), + Position::Right => Vector::new( + bounds.x + bounds.width + gap + padding, + y_center, + ), + Position::FollowCursor => Vector::new( + cursor_position.x, + cursor_position.y - text_bounds.height, + ), + }; + + Rectangle { + x: offset.x - padding, + y: offset.y - padding, + width: text_bounds.width + padding * 2.0, + height: text_bounds.height + padding * 2.0, + } }; + if tooltip_bounds.x < viewport.x { + tooltip_bounds.x = viewport.x; + } else if viewport.x + viewport.width + < tooltip_bounds.x + tooltip_bounds.width + { + tooltip_bounds.x = + viewport.x + viewport.width - tooltip_bounds.width; + } + + if tooltip_bounds.y < viewport.y { + tooltip_bounds.y = viewport.y; + } else if viewport.y + viewport.height + < tooltip_bounds.y + tooltip_bounds.height + { + tooltip_bounds.y = + viewport.y + viewport.height - tooltip_bounds.height; + } + let (tooltip, _) = Widget::<(), Self>::draw( tooltip, self, &defaults, - Layout::with_offset(offset, &tooltip_layout), + Layout::with_offset( + Vector::new( + tooltip_bounds.x + padding, + tooltip_bounds.y + padding, + ), + &text_layout, + ), cursor_position, viewport, ); - let tooltip_bounds = Rectangle { - x: offset.x - padding, - y: offset.y - padding, - width: tooltip_bounds.width + padding * 2.0, - height: tooltip_bounds.height + padding * 2.0, - }; - ( Primitive::Group { primitives: vec![ -- cgit From 21971e0037c2ddcb96fd48ea96332445de4137bb Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 10 Mar 2021 01:59:02 +0100 Subject: Make `Clipboard` argument in `Widget` trait mutable --- graphics/src/widget/canvas.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/canvas.rs b/graphics/src/widget/canvas.rs index 95ede50f..7897c8ec 100644 --- a/graphics/src/widget/canvas.rs +++ b/graphics/src/widget/canvas.rs @@ -154,9 +154,9 @@ where event: iced_native::Event, layout: Layout<'_>, cursor_position: Point, - messages: &mut Vec, _renderer: &Renderer, - _clipboard: Option<&dyn Clipboard>, + _clipboard: &mut dyn Clipboard, + messages: &mut Vec, ) -> event::Status { let bounds = layout.bounds(); -- cgit From 0864e63bde129b95261590b960efdc46c6d2d4d0 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 31 Mar 2021 20:06:03 +0200 Subject: Bump versions :tada: --- graphics/Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'graphics') diff --git a/graphics/Cargo.toml b/graphics/Cargo.toml index ab41ac44..ea9471c6 100644 --- a/graphics/Cargo.toml +++ b/graphics/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "iced_graphics" -version = "0.1.0" +version = "0.2.0" authors = ["Héctor Ramón Jiménez "] edition = "2018" description = "A bunch of backend-agnostic types that can be leveraged to build a renderer for Iced" @@ -28,11 +28,11 @@ version = "1.4" features = ["derive"] [dependencies.iced_native] -version = "0.3" +version = "0.4" path = "../native" [dependencies.iced_style] -version = "0.2" +version = "0.3" path = "../style" [dependencies.lyon] -- cgit From ca4257ff5c252e84e4bfe8a56b43801f26b56ef6 Mon Sep 17 00:00:00 2001 From: Downtime Date: Thu, 1 Apr 2021 21:58:25 +0800 Subject: Update frame.rs --- graphics/src/widget/canvas/frame.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'graphics') diff --git a/graphics/src/widget/canvas/frame.rs b/graphics/src/widget/canvas/frame.rs index b86f9e04..5af9d11f 100644 --- a/graphics/src/widget/canvas/frame.rs +++ b/graphics/src/widget/canvas/frame.rs @@ -54,7 +54,7 @@ impl Frame { self.size.width } - /// Returns the width of the [`Frame`]. + /// Returns the height of the [`Frame`]. #[inline] pub fn height(&self) -> f32 { self.size.height -- cgit From 59c2500c55430ac715b846017f79ad93a58fc3a5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Date: Mon, 3 May 2021 15:42:02 +0700 Subject: Fix `Scrollable` scrollbar being rendered behind contents ... by issuing a new clip layer just for the scrollbar itself. --- graphics/src/widget/scrollable.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'graphics') diff --git a/graphics/src/widget/scrollable.rs b/graphics/src/widget/scrollable.rs index 57065ba2..2220e4b8 100644 --- a/graphics/src/widget/scrollable.rs +++ b/graphics/src/widget/scrollable.rs @@ -134,8 +134,16 @@ where Primitive::None }; + let scroll = Primitive::Clip { + bounds, + offset: Vector::new(0, 0), + content: Box::new(Primitive::Group { + primitives: vec![scrollbar, scroller], + }), + }; + Primitive::Group { - primitives: vec![clip, scrollbar, scroller], + primitives: vec![clip, scroll], } } else { content -- cgit From 3840b75beaa3925f3438a7b40f01aaac221b8206 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Date: Wed, 5 May 2021 14:33:03 +0700 Subject: Provide `compatible_surface` in `iced_wgpu::Compositor` --- graphics/src/window/compositor.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'graphics') diff --git a/graphics/src/window/compositor.rs b/graphics/src/window/compositor.rs index 0bc8cbc8..7d5d789b 100644 --- a/graphics/src/window/compositor.rs +++ b/graphics/src/window/compositor.rs @@ -17,7 +17,10 @@ pub trait Compositor: Sized { type SwapChain; /// Creates a new [`Compositor`]. - fn new(settings: Self::Settings) -> Result<(Self, Self::Renderer), Error>; + fn new( + settings: Self::Settings, + compatible_window: Option<&W>, + ) -> Result<(Self, Self::Renderer), Error>; /// Crates a new [`Surface`] for the given window. /// -- cgit From d4c5f3ee950262c578c7b9b2a4aab60d3c5edaed Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Mon, 24 May 2021 16:37:47 -0500 Subject: Enable event handling within the title elements Shrink the pick area to avoid both the controls and the title elements. Handle events and merge title area event status with control events. --- graphics/src/widget/pane_grid.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/pane_grid.rs b/graphics/src/widget/pane_grid.rs index d06f8c6c..93b4b672 100644 --- a/graphics/src/widget/pane_grid.rs +++ b/graphics/src/widget/pane_grid.rs @@ -218,10 +218,10 @@ where body_primitive, ], }, - if is_over_pick_area { - mouse::Interaction::Grab - } else if title_bar_interaction > body_interaction { + if title_bar_interaction > body_interaction { title_bar_interaction + } else if is_over_pick_area { + mouse::Interaction::Grab } else { body_interaction }, -- cgit From fe0a27c56d9d75fb521e69352259f1d737402a20 Mon Sep 17 00:00:00 2001 From: Ben LeFevre Date: Mon, 23 Nov 2020 17:19:21 +0000 Subject: Add support for asymmetrical padding --- graphics/src/overlay/menu.rs | 12 ++++++------ graphics/src/widget/button.rs | 4 ++-- graphics/src/widget/pick_list.rs | 12 +++++++----- 3 files changed, 15 insertions(+), 13 deletions(-) (limited to 'graphics') diff --git a/graphics/src/overlay/menu.rs b/graphics/src/overlay/menu.rs index ffe998c5..443f1746 100644 --- a/graphics/src/overlay/menu.rs +++ b/graphics/src/overlay/menu.rs @@ -2,8 +2,8 @@ use crate::backend::{self, Backend}; use crate::{Primitive, Renderer}; use iced_native::{ - mouse, overlay, Color, Font, HorizontalAlignment, Point, Rectangle, - VerticalAlignment, + mouse, overlay, Color, Font, HorizontalAlignment, Padding, Point, + Rectangle, VerticalAlignment, }; pub use iced_style::menu::Style; @@ -45,7 +45,7 @@ where viewport: &Rectangle, options: &[T], hovered_option: Option, - padding: u16, + padding: Padding, text_size: u16, font: Font, style: &Style, @@ -53,7 +53,7 @@ where use std::f32; let is_mouse_over = bounds.contains(cursor_position); - let option_height = text_size as usize + padding as usize * 2; + let option_height = (text_size + padding.top + padding.bottom) as usize; let mut primitives = Vec::new(); @@ -72,7 +72,7 @@ where x: bounds.x, y: bounds.y + (option_height * i) as f32, width: bounds.width, - height: f32::from(text_size + padding * 2), + height: f32::from(text_size + padding.top + padding.bottom), }; if is_selected { @@ -88,7 +88,7 @@ where primitives.push(Primitive::Text { content: option.to_string(), bounds: Rectangle { - x: bounds.x + f32::from(padding), + x: bounds.x + padding.left as f32, y: bounds.center_y(), width: f32::INFINITY, ..bounds diff --git a/graphics/src/widget/button.rs b/graphics/src/widget/button.rs index 2e3f78ca..60400ed8 100644 --- a/graphics/src/widget/button.rs +++ b/graphics/src/widget/button.rs @@ -5,7 +5,7 @@ use crate::defaults::{self, Defaults}; use crate::{Backend, Primitive, Renderer}; use iced_native::mouse; use iced_native::{ - Background, Color, Element, Layout, Point, Rectangle, Vector, + Background, Color, Element, Layout, Padding, Point, Rectangle, Vector, }; pub use iced_native::button::State; @@ -21,7 +21,7 @@ impl iced_native::button::Renderer for Renderer where B: Backend, { - const DEFAULT_PADDING: u16 = 5; + const DEFAULT_PADDING: Padding = Padding::new(5); type Style = Box; diff --git a/graphics/src/widget/pick_list.rs b/graphics/src/widget/pick_list.rs index f42a8707..c6fbcf76 100644 --- a/graphics/src/widget/pick_list.rs +++ b/graphics/src/widget/pick_list.rs @@ -2,7 +2,8 @@ use crate::backend::{self, Backend}; use crate::{Primitive, Renderer}; use iced_native::{ - mouse, Font, HorizontalAlignment, Point, Rectangle, VerticalAlignment, + mouse, Font, HorizontalAlignment, Padding, Point, Rectangle, + VerticalAlignment, }; use iced_style::menu; @@ -19,7 +20,7 @@ where { type Style = Box; - const DEFAULT_PADDING: u16 = 5; + const DEFAULT_PADDING: Padding = Padding::new(5); fn menu_style(style: &Box) -> menu::Style { style.menu() @@ -30,7 +31,7 @@ where bounds: Rectangle, cursor_position: Point, selected: Option, - padding: u16, + padding: Padding, text_size: u16, font: Font, style: &Box, @@ -56,7 +57,8 @@ where font: B::ICON_FONT, size: bounds.height * style.icon_size, bounds: Rectangle { - x: bounds.x + bounds.width - f32::from(padding) * 2.0, + x: bounds.x + bounds.width + - f32::from(padding.left + padding.right), y: bounds.center_y(), ..bounds }, @@ -74,7 +76,7 @@ where font, color: style.text_color, bounds: Rectangle { - x: bounds.x + f32::from(padding), + x: bounds.x + f32::from(padding.left), y: bounds.center_y(), ..bounds }, -- cgit From 0a14492343e30bf0856088c91406c2da7c7f5860 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Date: Tue, 1 Jun 2021 19:13:52 +0700 Subject: Fix `Tooltip` widget --- graphics/src/widget/tooltip.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/tooltip.rs b/graphics/src/widget/tooltip.rs index 1a1b5352..493a6389 100644 --- a/graphics/src/widget/tooltip.rs +++ b/graphics/src/widget/tooltip.rs @@ -5,7 +5,7 @@ use crate::{Primitive, Renderer, Vector}; use iced_native::container; use iced_native::layout::{self, Layout}; -use iced_native::{Element, Point, Rectangle, Size, Text}; +use iced_native::{Element, Padding, Point, Rectangle, Size, Text}; /// An element decorating some content. /// @@ -49,7 +49,6 @@ where use iced_native::Widget; let gap = f32::from(gap); - let padding = f32::from(padding); let style = style_sheet.style(); let defaults = Defaults { @@ -62,9 +61,10 @@ where tooltip, self, &layout::Limits::new(Size::ZERO, viewport.size()) - .pad(f32::from(padding)), + .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 = -- cgit From b94cd7a2a83d81769d31f6379539089ce68cbdcd Mon Sep 17 00:00:00 2001 From: Héctor Ramón Date: Tue, 1 Jun 2021 19:21:43 +0700 Subject: Use `Padding::horizontal` and `Padding::vertical` helpers --- graphics/src/overlay/menu.rs | 4 ++-- graphics/src/widget/pick_list.rs | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) (limited to 'graphics') diff --git a/graphics/src/overlay/menu.rs b/graphics/src/overlay/menu.rs index 443f1746..9e91a0ef 100644 --- a/graphics/src/overlay/menu.rs +++ b/graphics/src/overlay/menu.rs @@ -53,7 +53,7 @@ where use std::f32; let is_mouse_over = bounds.contains(cursor_position); - let option_height = (text_size + padding.top + padding.bottom) as usize; + let option_height = (text_size + padding.vertical()) as usize; let mut primitives = Vec::new(); @@ -72,7 +72,7 @@ where x: bounds.x, y: bounds.y + (option_height * i) as f32, width: bounds.width, - height: f32::from(text_size + padding.top + padding.bottom), + height: f32::from(text_size + padding.vertical()), }; if is_selected { diff --git a/graphics/src/widget/pick_list.rs b/graphics/src/widget/pick_list.rs index c6fbcf76..32dfbdf9 100644 --- a/graphics/src/widget/pick_list.rs +++ b/graphics/src/widget/pick_list.rs @@ -57,8 +57,7 @@ where font: B::ICON_FONT, size: bounds.height * style.icon_size, bounds: Rectangle { - x: bounds.x + bounds.width - - f32::from(padding.left + padding.right), + x: bounds.x + bounds.width - f32::from(padding.horizontal()), y: bounds.center_y(), ..bounds }, -- cgit From 52a185fbab728b85cf414d4997567f52ebc66205 Mon Sep 17 00:00:00 2001 From: Kaiden42 Date: Sat, 19 Sep 2020 18:44:27 +0200 Subject: Implement `Toggler` widget for iced_native --- graphics/src/widget.rs | 3 ++ graphics/src/widget/toggler.rs | 94 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 graphics/src/widget/toggler.rs (limited to 'graphics') diff --git a/graphics/src/widget.rs b/graphics/src/widget.rs index 190ea9c0..e34d267f 100644 --- a/graphics/src/widget.rs +++ b/graphics/src/widget.rs @@ -20,6 +20,7 @@ pub mod scrollable; pub mod slider; pub mod svg; pub mod text_input; +pub mod toggler; pub mod tooltip; mod column; @@ -50,6 +51,8 @@ pub use slider::Slider; #[doc(no_inline)] pub use text_input::TextInput; #[doc(no_inline)] +pub use toggler::Toggler; +#[doc(no_inline)] pub use tooltip::Tooltip; pub use column::Column; diff --git a/graphics/src/widget/toggler.rs b/graphics/src/widget/toggler.rs new file mode 100644 index 00000000..a258443e --- /dev/null +++ b/graphics/src/widget/toggler.rs @@ -0,0 +1,94 @@ +//! Show toggle controls using togglers. +use crate::backend::{self, Backend}; +use crate::{Primitive, Renderer}; +use iced_native::mouse; +use iced_native::toggler; +use iced_native::Rectangle; + +pub use iced_style::toggler::{Style, StyleSheet}; + +/// Makes sure that the border radius of the toggler looks good at every size. +const BORDER_RADIUS_RATIO: f32 = 32.0 / 13.0; + +/// The space ratio between the background Quad and the Toggler bounds, and +/// between the background Quad and foreground Quad. +const SPACE_RATIO: f32 = 0.05; + +/// A toggler that can be toggled. +/// +/// This is an alias of an `iced_native` toggler with an `iced_wgpu::Renderer`. +pub type Toggler = + iced_native::Toggler>; + +impl toggler::Renderer for Renderer +where + B: Backend + backend::Text, +{ + type Style = Box; + + const DEFAULT_SIZE: u16 = 20; + + fn draw( + &mut self, + bounds: Rectangle, + is_active: bool, + is_mouse_over: bool, + (label, _): Self::Output, + style_sheet: &Self::Style, + ) -> Self::Output { + let style = if is_mouse_over { + style_sheet.hovered(is_active) + } else { + style_sheet.active(is_active) + }; + + let border_radius = bounds.height as f32 / BORDER_RADIUS_RATIO; + let space = SPACE_RATIO * bounds.height as f32; + + let toggler_background_bounds = Rectangle { + x: bounds.x + space, + y: bounds.y + space, + width: bounds.width - (2.0 * space), + height: bounds.height - (2.0 * space), + }; + + let toggler_background = Primitive::Quad { + bounds: toggler_background_bounds, + background: style.background.into(), + border_radius, + border_width: 1.0, + border_color: style.background_border.unwrap_or(style.background), + }; + + let toggler_foreground_bounds = Rectangle { + x: bounds.x + + if is_active { + bounds.width - 2.0 * space - (bounds.height - (4.0 * space)) + } else { + 2.0 * space + }, + y: bounds.y + (2.0 * space), + width: bounds.height - (4.0 * space), + height: bounds.height - (4.0 * space), + }; + + let toggler_foreground = Primitive::Quad { + bounds: toggler_foreground_bounds, + background: style.foreground.into(), + border_radius, + border_width: 1.0, + border_color: style.foreground_border.unwrap_or(style.foreground), + }; + + ( + Primitive::Group { + primitives: vec![label, toggler_background, toggler_foreground], + }, + if is_mouse_over { + mouse::Interaction::Pointer + } else { + mouse::Interaction::default() + }, + ) + } +} -- cgit From 7a626f3b7b6871052e5fade697e120cfb7d726d7 Mon Sep 17 00:00:00 2001 From: Kaiden42 Date: Thu, 24 Sep 2020 16:31:39 +0200 Subject: Change label of `Toggler` to optional --- graphics/src/widget/toggler.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/toggler.rs b/graphics/src/widget/toggler.rs index a258443e..852d18ee 100644 --- a/graphics/src/widget/toggler.rs +++ b/graphics/src/widget/toggler.rs @@ -33,7 +33,7 @@ where bounds: Rectangle, is_active: bool, is_mouse_over: bool, - (label, _): Self::Output, + label: Option, style_sheet: &Self::Style, ) -> Self::Output { let style = if is_mouse_over { @@ -82,7 +82,12 @@ where ( Primitive::Group { - primitives: vec![label, toggler_background, toggler_foreground], + primitives: match label { + Some((l, _)) => { + vec![l, toggler_background, toggler_foreground] + } + None => vec![toggler_background, toggler_foreground], + }, }, if is_mouse_over { mouse::Interaction::Pointer -- cgit From 83d19689c80266874e0a26085f17a94fd3507e1e Mon Sep 17 00:00:00 2001 From: Yusuf Bera Ertan Date: Mon, 14 Jun 2021 21:01:37 +0300 Subject: docs: update all 0.2 github links to 0.3 --- graphics/src/widget/pane_grid.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'graphics') diff --git a/graphics/src/widget/pane_grid.rs b/graphics/src/widget/pane_grid.rs index 93b4b672..92cdbb77 100644 --- a/graphics/src/widget/pane_grid.rs +++ b/graphics/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/hecrj/iced/tree/0.2/examples/pane_grid +//! [`pane_grid` example]: https://github.com/hecrj/iced/tree/0.3/examples/pane_grid use crate::defaults; use crate::{Backend, Color, Primitive, Renderer}; use iced_native::container; -- cgit From 80df17ab5561ffe2bc814a164958418ebc25d38b Mon Sep 17 00:00:00 2001 From: Diego Fujii Date: Thu, 24 Jun 2021 00:16:08 +0900 Subject: fix-typo --- graphics/src/widget/canvas/program.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'graphics') diff --git a/graphics/src/widget/canvas/program.rs b/graphics/src/widget/canvas/program.rs index d703caad..85a2f67b 100644 --- a/graphics/src/widget/canvas/program.rs +++ b/graphics/src/widget/canvas/program.rs @@ -34,7 +34,7 @@ pub trait Program { /// [`Geometry`] can be easily generated with a [`Frame`] or stored in a /// [`Cache`]. /// - /// [`Frame`]: crate::widget::canvas::Cache + /// [`Frame`]: crate::widget::canvas::Frame /// [`Cache`]: crate::widget::canvas::Cache fn draw(&self, bounds: Rectangle, cursor: Cursor) -> Vec; -- cgit From fa433743b352f9a27e0669d4da41f645db8b04cb Mon Sep 17 00:00:00 2001 From: Jon Pacheco Date: Sat, 22 May 2021 19:28:17 +0100 Subject: feat: add placeholders to pick_list see issue #726 --- graphics/src/widget/pick_list.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'graphics') diff --git a/graphics/src/widget/pick_list.rs b/graphics/src/widget/pick_list.rs index 32dfbdf9..a7fe3e93 100644 --- a/graphics/src/widget/pick_list.rs +++ b/graphics/src/widget/pick_list.rs @@ -31,6 +31,7 @@ where bounds: Rectangle, cursor_position: Point, selected: Option, + placeholder: Option, padding: Padding, text_size: u16, font: Font, @@ -68,7 +69,7 @@ where ( Primitive::Group { - primitives: if let Some(label) = selected { + primitives: if let Some(label) = selected.or(placeholder) { let label = Primitive::Text { content: label, size: f32::from(text_size), -- cgit From 1b3606884747374f1e5599e3c783f36a2f2cac6f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 22 Jul 2021 20:13:14 +0700 Subject: Introduce `placeholder_color` to `pick_list::Style` --- graphics/src/widget/pick_list.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'graphics') diff --git a/graphics/src/widget/pick_list.rs b/graphics/src/widget/pick_list.rs index a7fe3e93..01283bd5 100644 --- a/graphics/src/widget/pick_list.rs +++ b/graphics/src/widget/pick_list.rs @@ -38,6 +38,7 @@ where style: &Box, ) -> Self::Output { let is_mouse_over = bounds.contains(cursor_position); + let is_selected = selected.is_some(); let style = if is_mouse_over { style.hovered() @@ -74,7 +75,9 @@ where content: label, size: f32::from(text_size), font, - color: style.text_color, + color: is_selected + .then(|| style.text_color) + .unwrap_or(style.placeholder_color), bounds: Rectangle { x: bounds.x + f32::from(padding.left), y: bounds.center_y(), -- cgit From a866f8742e4ddf5714455519790fed0f961fad66 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 22 Jul 2021 20:16:53 +0700 Subject: Avoid cloning `placeholder` for `PickList` unnecessarily during `draw` --- graphics/src/widget/pick_list.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'graphics') diff --git a/graphics/src/widget/pick_list.rs b/graphics/src/widget/pick_list.rs index 01283bd5..88a590b5 100644 --- a/graphics/src/widget/pick_list.rs +++ b/graphics/src/widget/pick_list.rs @@ -31,7 +31,7 @@ where bounds: Rectangle, cursor_position: Point, selected: Option, - placeholder: Option, + placeholder: Option<&str>, padding: Padding, text_size: u16, font: Font, @@ -70,7 +70,9 @@ where ( Primitive::Group { - primitives: if let Some(label) = selected.or(placeholder) { + primitives: if let Some(label) = + selected.or_else(|| placeholder.map(str::to_string)) + { let label = Primitive::Text { content: label, size: f32::from(text_size), -- cgit