diff options
Diffstat (limited to 'widget')
-rw-r--r-- | widget/src/helpers.rs | 180 | ||||
-rw-r--r-- | widget/src/image.rs | 8 | ||||
-rw-r--r-- | widget/src/lib.rs | 3 | ||||
-rw-r--r-- | widget/src/pop.rs | 232 | ||||
-rw-r--r-- | widget/src/progress_bar.rs | 94 | ||||
-rw-r--r-- | widget/src/vertical_slider.rs | 39 |
6 files changed, 511 insertions, 45 deletions
diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index 5a0f8107..17cf94cc 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -24,7 +24,7 @@ use crate::text_input::{self, TextInput}; use crate::toggler::{self, Toggler}; use crate::tooltip::{self, Tooltip}; use crate::vertical_slider::{self, VerticalSlider}; -use crate::{Column, MouseArea, Pin, Row, Space, Stack, Themer}; +use crate::{Column, MouseArea, Pin, Pop, Row, Space, Stack, Themer}; use std::borrow::Borrow; use std::ops::RangeInclusive; @@ -232,10 +232,10 @@ where /// /// This is equivalent to: /// ```rust,no_run -/// # use iced_widget::core::Length; +/// # use iced_widget::core::Length::Fill; /// # use iced_widget::Container; /// # fn container<A>(x: A) -> Container<'static, ()> { unreachable!() } -/// let centered = container("Centered!").center(Length::Fill); +/// let center = container("Center!").center(Fill); /// ``` /// /// [`Container`]: crate::Container @@ -249,6 +249,166 @@ where container(content).center(Length::Fill) } +/// Creates a new [`Container`] that fills all the available space +/// horizontally and centers its contents inside. +/// +/// This is equivalent to: +/// ```rust,no_run +/// # use iced_widget::core::Length::Fill; +/// # use iced_widget::Container; +/// # fn container<A>(x: A) -> Container<'static, ()> { unreachable!() } +/// let center_x = container("Horizontal Center!").center_x(Fill); +/// ``` +/// +/// [`Container`]: crate::Container +pub fn center_x<'a, Message, Theme, Renderer>( + content: impl Into<Element<'a, Message, Theme, Renderer>>, +) -> Container<'a, Message, Theme, Renderer> +where + Theme: container::Catalog + 'a, + Renderer: core::Renderer, +{ + container(content).center_x(Length::Fill) +} + +/// Creates a new [`Container`] that fills all the available space +/// vertically and centers its contents inside. +/// +/// This is equivalent to: +/// ```rust,no_run +/// # use iced_widget::core::Length::Fill; +/// # use iced_widget::Container; +/// # fn container<A>(x: A) -> Container<'static, ()> { unreachable!() } +/// let center_y = container("Vertical Center!").center_y(Fill); +/// ``` +/// +/// [`Container`]: crate::Container +pub fn center_y<'a, Message, Theme, Renderer>( + content: impl Into<Element<'a, Message, Theme, Renderer>>, +) -> Container<'a, Message, Theme, Renderer> +where + Theme: container::Catalog + 'a, + Renderer: core::Renderer, +{ + container(content).center_y(Length::Fill) +} + +/// Creates a new [`Container`] that fills all the available space +/// horizontally and right-aligns its contents inside. +/// +/// This is equivalent to: +/// ```rust,no_run +/// # use iced_widget::core::Length::Fill; +/// # use iced_widget::Container; +/// # fn container<A>(x: A) -> Container<'static, ()> { unreachable!() } +/// let right = container("Right!").align_right(Fill); +/// ``` +/// +/// [`Container`]: crate::Container +pub fn right<'a, Message, Theme, Renderer>( + content: impl Into<Element<'a, Message, Theme, Renderer>>, +) -> Container<'a, Message, Theme, Renderer> +where + Theme: container::Catalog + 'a, + Renderer: core::Renderer, +{ + container(content).align_right(Length::Fill) +} + +/// Creates a new [`Container`] that fills all the available space +/// and aligns its contents inside to the right center. +/// +/// This is equivalent to: +/// ```rust,no_run +/// # use iced_widget::core::Length::Fill; +/// # use iced_widget::Container; +/// # fn container<A>(x: A) -> Container<'static, ()> { unreachable!() } +/// let right_center = container("Bottom Center!").align_right(Fill).center_y(Fill); +/// ``` +/// +/// [`Container`]: crate::Container +pub fn right_center<'a, Message, Theme, Renderer>( + content: impl Into<Element<'a, Message, Theme, Renderer>>, +) -> Container<'a, Message, Theme, Renderer> +where + Theme: container::Catalog + 'a, + Renderer: core::Renderer, +{ + container(content) + .align_right(Length::Fill) + .center_y(Length::Fill) +} + +/// Creates a new [`Container`] that fills all the available space +/// vertically and bottom-aligns its contents inside. +/// +/// This is equivalent to: +/// ```rust,no_run +/// # use iced_widget::core::Length::Fill; +/// # use iced_widget::Container; +/// # fn container<A>(x: A) -> Container<'static, ()> { unreachable!() } +/// let bottom = container("Bottom!").align_bottom(Fill); +/// ``` +/// +/// [`Container`]: crate::Container +pub fn bottom<'a, Message, Theme, Renderer>( + content: impl Into<Element<'a, Message, Theme, Renderer>>, +) -> Container<'a, Message, Theme, Renderer> +where + Theme: container::Catalog + 'a, + Renderer: core::Renderer, +{ + container(content).align_bottom(Length::Fill) +} + +/// Creates a new [`Container`] that fills all the available space +/// and aligns its contents inside to the bottom center. +/// +/// This is equivalent to: +/// ```rust,no_run +/// # use iced_widget::core::Length::Fill; +/// # use iced_widget::Container; +/// # fn container<A>(x: A) -> Container<'static, ()> { unreachable!() } +/// let bottom_center = container("Bottom Center!").center_x(Fill).align_bottom(Fill); +/// ``` +/// +/// [`Container`]: crate::Container +pub fn bottom_center<'a, Message, Theme, Renderer>( + content: impl Into<Element<'a, Message, Theme, Renderer>>, +) -> Container<'a, Message, Theme, Renderer> +where + Theme: container::Catalog + 'a, + Renderer: core::Renderer, +{ + container(content) + .center_x(Length::Fill) + .align_bottom(Length::Fill) +} + +/// Creates a new [`Container`] that fills all the available space +/// and aligns its contents inside to the bottom right corner. +/// +/// This is equivalent to: +/// ```rust,no_run +/// # use iced_widget::core::Length::Fill; +/// # use iced_widget::Container; +/// # fn container<A>(x: A) -> Container<'static, ()> { unreachable!() } +/// let bottom_right = container("Bottom!").align_right(Fill).align_bottom(Fill); +/// ``` +/// +/// [`Container`]: crate::Container +pub fn bottom_right<'a, Message, Theme, Renderer>( + content: impl Into<Element<'a, Message, Theme, Renderer>>, +) -> Container<'a, Message, Theme, Renderer> +where + Theme: container::Catalog + 'a, + Renderer: core::Renderer, +{ + container(content) + .align_right(Length::Fill) + .align_bottom(Length::Fill) +} + /// Creates a new [`Pin`] widget with the given content. /// /// A [`Pin`] widget positions its contents at some fixed coordinates inside of its boundaries. @@ -810,6 +970,20 @@ where }) } +/// Creates a new [`Pop`] widget. +/// +/// A [`Pop`] widget can generate messages when it pops in and out of view. +/// It can even notify you with anticipation at a given distance! +pub fn pop<'a, Message, Theme, Renderer>( + content: impl Into<Element<'a, Message, Theme, Renderer>>, +) -> Pop<'a, Message, Theme, Renderer> +where + Renderer: core::Renderer, + Message: Clone, +{ + Pop::new(content) +} + /// Creates a new [`Scrollable`] with the provided content. /// /// Scrollables let users navigate an endless amount of content with a scrollbar. diff --git a/widget/src/image.rs b/widget/src/image.rs index c8f2a620..f5a9c7f3 100644 --- a/widget/src/image.rs +++ b/widget/src/image.rs @@ -167,6 +167,7 @@ where pub fn draw<Renderer, Handle>( renderer: &mut Renderer, layout: Layout<'_>, + viewport: &Rectangle, handle: &Handle, content_fit: ContentFit, filter_method: FilterMethod, @@ -218,7 +219,9 @@ pub fn draw<Renderer, Handle>( if adjusted_fit.width > bounds.width || adjusted_fit.height > bounds.height { - renderer.with_layer(bounds, render); + if let Some(bounds) = bounds.intersection(viewport) { + renderer.with_layer(bounds, render); + } } else { render(renderer); } @@ -262,11 +265,12 @@ where _style: &renderer::Style, layout: Layout<'_>, _cursor: mouse::Cursor, - _viewport: &Rectangle, + viewport: &Rectangle, ) { draw( renderer, layout, + viewport, &self.handle, self.content_fit, self.filter_method, diff --git a/widget/src/lib.rs b/widget/src/lib.rs index 38c9929a..b8cfa98f 100644 --- a/widget/src/lib.rs +++ b/widget/src/lib.rs @@ -25,6 +25,7 @@ pub mod keyed; pub mod overlay; pub mod pane_grid; pub mod pick_list; +pub mod pop; pub mod progress_bar; pub mod radio; pub mod rule; @@ -66,6 +67,8 @@ pub use pick_list::PickList; #[doc(no_inline)] pub use pin::Pin; #[doc(no_inline)] +pub use pop::Pop; +#[doc(no_inline)] pub use progress_bar::ProgressBar; #[doc(no_inline)] pub use radio::Radio; diff --git a/widget/src/pop.rs b/widget/src/pop.rs new file mode 100644 index 00000000..051fa76a --- /dev/null +++ b/widget/src/pop.rs @@ -0,0 +1,232 @@ +//! Generate messages when content pops in and out of view. +use crate::core::layout; +use crate::core::mouse; +use crate::core::overlay; +use crate::core::renderer; +use crate::core::widget; +use crate::core::widget::tree::{self, Tree}; +use crate::core::window; +use crate::core::{ + self, Clipboard, Element, Event, Layout, Length, Pixels, Rectangle, Shell, + Size, Vector, Widget, +}; + +/// A widget that can generate messages when its content pops in and out of view. +/// +/// It can even notify you with anticipation at a given distance! +#[allow(missing_debug_implementations)] +pub struct Pop<'a, Message, Theme = crate::Theme, Renderer = crate::Renderer> { + content: Element<'a, Message, Theme, Renderer>, + on_show: Option<Message>, + on_hide: Option<Message>, + anticipate: Pixels, +} + +impl<'a, Message, Theme, Renderer> Pop<'a, Message, Theme, Renderer> +where + Renderer: core::Renderer, + Message: Clone, +{ + /// Creates a new [`Pop`] widget with the given content. + pub fn new( + content: impl Into<Element<'a, Message, Theme, Renderer>>, + ) -> Self { + Self { + content: content.into(), + on_show: None, + on_hide: None, + anticipate: Pixels::ZERO, + } + } + + /// Sets the message to be produced when the content pops into view. + pub fn on_show(mut self, on_show: Message) -> Self { + self.on_show = Some(on_show); + self + } + + /// Sets the message to be produced when the content pops out of view. + pub fn on_hide(mut self, on_hide: Message) -> Self { + self.on_hide = Some(on_hide); + self + } + + /// Sets the distance in [`Pixels`] to use in anticipation of the + /// content popping into view. + /// + /// This can be quite useful to lazily load items in a long scrollable + /// behind the scenes before the user can notice it! + pub fn anticipate(mut self, distance: impl Into<Pixels>) -> Self { + self.anticipate = distance.into(); + self + } +} + +#[derive(Debug, Clone, Copy, Default)] +struct State { + has_popped_in: bool, +} + +impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer> + for Pop<'_, Message, Theme, Renderer> +where + Message: Clone, + Renderer: core::Renderer, +{ + fn tag(&self) -> tree::Tag { + tree::Tag::of::<State>() + } + + fn state(&self) -> tree::State { + tree::State::new(State::default()) + } + + fn children(&self) -> Vec<Tree> { + vec![Tree::new(&self.content)] + } + + fn diff(&self, tree: &mut Tree) { + tree.diff_children(&[&self.content]); + } + + fn update( + &mut self, + tree: &mut Tree, + event: Event, + layout: Layout<'_>, + cursor: mouse::Cursor, + renderer: &Renderer, + clipboard: &mut dyn Clipboard, + shell: &mut Shell<'_, Message>, + viewport: &Rectangle, + ) { + if let Event::Window(window::Event::RedrawRequested(_)) = &event { + let state = tree.state.downcast_mut::<State>(); + let bounds = layout.bounds(); + + let top_left_distance = viewport.distance(bounds.position()); + + let bottom_right_distance = viewport + .distance(bounds.position() + Vector::from(bounds.size())); + + let distance = top_left_distance.min(bottom_right_distance); + + if state.has_popped_in { + if let Some(on_hide) = &self.on_hide { + if distance > self.anticipate.0 { + state.has_popped_in = false; + shell.publish(on_hide.clone()); + } + } + } else if let Some(on_show) = &self.on_show { + if distance <= self.anticipate.0 { + state.has_popped_in = true; + shell.publish(on_show.clone()); + } + } + } + + self.content.as_widget_mut().update( + tree, event, layout, cursor, renderer, clipboard, shell, viewport, + ); + } + + fn size(&self) -> Size<Length> { + self.content.as_widget().size() + } + + fn size_hint(&self) -> Size<Length> { + self.content.as_widget().size_hint() + } + + fn layout( + &self, + tree: &mut Tree, + renderer: &Renderer, + limits: &layout::Limits, + ) -> layout::Node { + self.content + .as_widget() + .layout(&mut tree.children[0], renderer, limits) + } + + fn draw( + &self, + tree: &Tree, + renderer: &mut Renderer, + theme: &Theme, + style: &renderer::Style, + layout: layout::Layout<'_>, + cursor: mouse::Cursor, + viewport: &Rectangle, + ) { + self.content.as_widget().draw( + &tree.children[0], + renderer, + theme, + style, + layout, + cursor, + viewport, + ); + } + + fn operate( + &self, + tree: &mut Tree, + layout: core::Layout<'_>, + renderer: &Renderer, + operation: &mut dyn widget::Operation, + ) { + self.content.as_widget().operate( + &mut tree.children[0], + layout, + renderer, + operation, + ); + } + + fn mouse_interaction( + &self, + tree: &Tree, + layout: core::Layout<'_>, + cursor: mouse::Cursor, + viewport: &Rectangle, + renderer: &Renderer, + ) -> mouse::Interaction { + self.content.as_widget().mouse_interaction( + &tree.children[0], + layout, + cursor, + viewport, + renderer, + ) + } + + fn overlay<'b>( + &'b mut self, + tree: &'b mut Tree, + layout: core::Layout<'_>, + renderer: &Renderer, + translation: core::Vector, + ) -> Option<overlay::Element<'b, Message, Theme, Renderer>> { + self.content.as_widget_mut().overlay( + &mut tree.children[0], + layout, + renderer, + translation, + ) + } +} + +impl<'a, Message, Theme, Renderer> From<Pop<'a, Message, Theme, Renderer>> + for Element<'a, Message, Theme, Renderer> +where + Renderer: core::Renderer + 'a, + Theme: 'a, + Message: Clone + 'a, +{ + fn from(pop: Pop<'a, Message, Theme, Renderer>) -> Self { + Element::new(pop) + } +} diff --git a/widget/src/progress_bar.rs b/widget/src/progress_bar.rs index 8f85dcf3..554e8a44 100644 --- a/widget/src/progress_bar.rs +++ b/widget/src/progress_bar.rs @@ -59,8 +59,9 @@ where { range: RangeInclusive<f32>, value: f32, - width: Length, - height: Option<Length>, + length: Length, + girth: Length, + is_vertical: bool, class: Theme::Class<'a>, } @@ -68,8 +69,8 @@ impl<'a, Theme> ProgressBar<'a, Theme> where Theme: Catalog, { - /// The default height of a [`ProgressBar`]. - pub const DEFAULT_HEIGHT: f32 = 30.0; + /// The default girth of a [`ProgressBar`]. + pub const DEFAULT_GIRTH: f32 = 30.0; /// Creates a new [`ProgressBar`]. /// @@ -80,21 +81,30 @@ where ProgressBar { value: value.clamp(*range.start(), *range.end()), range, - width: Length::Fill, - height: None, + length: Length::Fill, + girth: Length::from(Self::DEFAULT_GIRTH), + is_vertical: false, class: Theme::default(), } } /// Sets the width of the [`ProgressBar`]. - pub fn width(mut self, width: impl Into<Length>) -> Self { - self.width = width.into(); + pub fn length(mut self, length: impl Into<Length>) -> Self { + self.length = length.into(); self } /// Sets the height of the [`ProgressBar`]. - pub fn height(mut self, height: impl Into<Length>) -> Self { - self.height = Some(height.into()); + pub fn girth(mut self, girth: impl Into<Length>) -> Self { + self.girth = girth.into(); + self + } + + /// Turns the [`ProgressBar`] into a vertical [`ProgressBar`]. + /// + /// By default, a [`ProgressBar`] is horizontal. + pub fn vertical(mut self) -> Self { + self.is_vertical = true; self } @@ -115,6 +125,22 @@ where self.class = class.into(); self } + + fn width(&self) -> Length { + if self.is_vertical { + self.girth + } else { + self.length + } + } + + fn height(&self) -> Length { + if self.is_vertical { + self.length + } else { + self.girth + } + } } impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer> @@ -125,8 +151,8 @@ where { fn size(&self) -> Size<Length> { Size { - width: self.width, - height: self.height.unwrap_or(Length::Fixed(Self::DEFAULT_HEIGHT)), + width: self.width(), + height: self.height(), } } @@ -136,11 +162,7 @@ where _renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - layout::atomic( - limits, - self.width, - self.height.unwrap_or(Length::Fixed(Self::DEFAULT_HEIGHT)), - ) + layout::atomic(limits, self.width(), self.height()) } fn draw( @@ -156,11 +178,16 @@ where let bounds = layout.bounds(); let (range_start, range_end) = self.range.clone().into_inner(); - let active_progress_width = if range_start >= range_end { + let length = if self.is_vertical { + bounds.height + } else { + bounds.width + }; + + let active_progress_length = if range_start >= range_end { 0.0 } else { - bounds.width * (self.value - range_start) - / (range_end - range_start) + length * (self.value - range_start) / (range_end - range_start) }; let style = theme.style(&self.class); @@ -174,13 +201,23 @@ where style.background, ); - if active_progress_width > 0.0 { + if active_progress_length > 0.0 { + let bounds = if self.is_vertical { + Rectangle { + y: bounds.y + bounds.height - active_progress_length, + height: active_progress_length, + ..bounds + } + } else { + Rectangle { + width: active_progress_length, + ..bounds + } + }; + renderer.fill_quad( renderer::Quad { - bounds: Rectangle { - width: active_progress_width, - ..bounds - }, + bounds, border: Border { color: Color::TRANSPARENT, ..style.border @@ -274,6 +311,13 @@ pub fn success(theme: &Theme) -> Style { styled(palette.background.strong.color, palette.success.base.color) } +/// The warning style of a [`ProgressBar`]. +pub fn warning(theme: &Theme) -> Style { + let palette = theme.extended_palette(); + + styled(palette.background.strong.color, palette.warning.base.color) +} + /// The danger style of a [`ProgressBar`]. pub fn danger(theme: &Theme) -> Style { let palette = theme.extended_palette(); diff --git a/widget/src/vertical_slider.rs b/widget/src/vertical_slider.rs index c1af142b..2ed9419a 100644 --- a/widget/src/vertical_slider.rs +++ b/widget/src/vertical_slider.rs @@ -42,6 +42,7 @@ use crate::core::mouse; use crate::core::renderer; use crate::core::touch; use crate::core::widget::tree::{self, Tree}; +use crate::core::window; use crate::core::{ self, Clipboard, Element, Event, Length, Pixels, Point, Rectangle, Shell, Size, Widget, @@ -98,6 +99,7 @@ where width: f32, height: Length, class: Theme::Class<'a>, + status: Option<Status>, } impl<'a, T, Message, Theme> VerticalSlider<'a, T, Message, Theme> @@ -144,6 +146,7 @@ where width: Self::DEFAULT_WIDTH, height: Length::Fill, class: Theme::default(), + status: None, } } @@ -390,7 +393,9 @@ where shell.capture_event(); } } - Event::Keyboard(keyboard::Event::KeyPressed { key, .. }) => { + Event::Keyboard(keyboard::Event::KeyPressed { + ref key, .. + }) => { if cursor.is_over(layout.bounds()) { match key { Key::Named(key::Named::ArrowUp) => { @@ -410,32 +415,36 @@ where } _ => {} } + + let current_status = if state.is_dragging { + Status::Dragged + } else if cursor.is_over(layout.bounds()) { + Status::Hovered + } else { + Status::Active + }; + + if let Event::Window(window::Event::RedrawRequested(_now)) = event { + self.status = Some(current_status); + } else if self.status.is_some_and(|status| status != current_status) { + shell.request_redraw(); + } } fn draw( &self, - tree: &Tree, + _tree: &Tree, renderer: &mut Renderer, theme: &Theme, _style: &renderer::Style, layout: Layout<'_>, - cursor: mouse::Cursor, + _cursor: mouse::Cursor, _viewport: &Rectangle, ) { - let state = tree.state.downcast_ref::<State>(); let bounds = layout.bounds(); - let is_mouse_over = cursor.is_over(bounds); - let style = theme.style( - &self.class, - if state.is_dragging { - Status::Dragged - } else if is_mouse_over { - Status::Hovered - } else { - Status::Active - }, - ); + let style = + theme.style(&self.class, self.status.unwrap_or(Status::Active)); let (handle_width, handle_height, handle_border_radius) = match style.handle.shape { |