summaryrefslogtreecommitdiffstats
path: root/native/src
diff options
context:
space:
mode:
Diffstat (limited to 'native/src')
-rw-r--r--native/src/clipboard.rs7
-rw-r--r--native/src/command.rs8
-rw-r--r--native/src/command/action.rs7
-rw-r--r--native/src/debug/basic.rs4
-rw-r--r--native/src/element.rs15
-rw-r--r--native/src/lib.rs5
-rw-r--r--native/src/mouse/click.rs11
-rw-r--r--native/src/overlay.rs15
-rw-r--r--native/src/overlay/element.rs11
-rw-r--r--native/src/overlay/menu.rs26
-rw-r--r--native/src/subscription.rs20
-rw-r--r--native/src/user_interface.rs244
-rw-r--r--native/src/widget.rs15
-rw-r--r--native/src/widget/button.rs14
-rw-r--r--native/src/widget/checkbox.rs13
-rw-r--r--native/src/widget/column.rs23
-rw-r--r--native/src/widget/container.rs21
-rw-r--r--native/src/widget/image.rs90
-rw-r--r--native/src/widget/image/viewer.rs15
-rw-r--r--native/src/widget/pane_grid.rs19
-rw-r--r--native/src/widget/pane_grid/content.rs12
-rw-r--r--native/src/widget/pane_grid/state.rs8
-rw-r--r--native/src/widget/pane_grid/title_bar.rs13
-rw-r--r--native/src/widget/pick_list.rs22
-rw-r--r--native/src/widget/progress_bar.rs14
-rw-r--r--native/src/widget/radio.rs13
-rw-r--r--native/src/widget/row.rs22
-rw-r--r--native/src/widget/rule.rs14
-rw-r--r--native/src/widget/scrollable.rs16
-rw-r--r--native/src/widget/slider.rs12
-rw-r--r--native/src/widget/space.rs11
-rw-r--r--native/src/widget/svg.rs90
-rw-r--r--native/src/widget/text.rs16
-rw-r--r--native/src/widget/text_input.rs15
-rw-r--r--native/src/widget/toggler.rs12
-rw-r--r--native/src/widget/tooltip.rs15
36 files changed, 279 insertions, 609 deletions
diff --git a/native/src/clipboard.rs b/native/src/clipboard.rs
index 60703c31..c9105bc0 100644
--- a/native/src/clipboard.rs
+++ b/native/src/clipboard.rs
@@ -1,4 +1,6 @@
//! Access the clipboard.
+use iced_futures::MaybeSend;
+
use std::fmt;
/// A buffer for short-term storage and transfer within and between
@@ -36,7 +38,10 @@ pub enum Action<T> {
impl<T> Action<T> {
/// Maps the output of a clipboard [`Action`] using the provided closure.
- pub fn map<A>(self, f: impl Fn(T) -> A + 'static + Send + Sync) -> Action<A>
+ pub fn map<A>(
+ self,
+ f: impl Fn(T) -> A + 'static + MaybeSend + Sync,
+ ) -> Action<A>
where
T: 'static,
{
diff --git a/native/src/command.rs b/native/src/command.rs
index 6fe518d7..89d0f045 100644
--- a/native/src/command.rs
+++ b/native/src/command.rs
@@ -3,6 +3,8 @@ mod action;
pub use action::Action;
+use iced_futures::MaybeSend;
+
use std::fmt;
use std::future::Future;
@@ -24,8 +26,8 @@ impl<T> Command<T> {
/// Creates a [`Command`] that performs the action of the given future.
pub fn perform<A>(
- future: impl Future<Output = T> + 'static + Send,
- f: impl Fn(T) -> A + 'static + Send,
+ future: impl Future<Output = T> + 'static + MaybeSend,
+ f: impl Fn(T) -> A + 'static + MaybeSend,
) -> Command<A> {
use iced_futures::futures::FutureExt;
@@ -45,7 +47,7 @@ impl<T> Command<T> {
/// Applies a transformation to the result of a [`Command`].
pub fn map<A>(
self,
- f: impl Fn(T) -> A + 'static + Send + Sync + Clone,
+ f: impl Fn(T) -> A + 'static + MaybeSend + Sync + Clone,
) -> Command<A>
where
T: 'static,
diff --git a/native/src/command/action.rs b/native/src/command/action.rs
index 77be1b59..5c7509c8 100644
--- a/native/src/command/action.rs
+++ b/native/src/command/action.rs
@@ -1,6 +1,8 @@
use crate::clipboard;
use crate::window;
+use iced_futures::MaybeSend;
+
use std::fmt;
/// An action that a [`Command`] can perform.
@@ -19,7 +21,10 @@ pub enum Action<T> {
impl<T> Action<T> {
/// Applies a transformation to the result of a [`Command`].
- pub fn map<A>(self, f: impl Fn(T) -> A + 'static + Send + Sync) -> Action<A>
+ pub fn map<A>(
+ self,
+ f: impl Fn(T) -> A + 'static + MaybeSend + Sync,
+ ) -> Action<A>
where
T: 'static,
{
diff --git a/native/src/debug/basic.rs b/native/src/debug/basic.rs
index a42f66ea..d706bb00 100644
--- a/native/src/debug/basic.rs
+++ b/native/src/debug/basic.rs
@@ -1,5 +1,7 @@
#![allow(missing_docs)]
-use std::{collections::VecDeque, time};
+use crate::time;
+
+use std::collections::VecDeque;
/// A bunch of time measurements for debugging purposes.
#[derive(Debug)]
diff --git a/native/src/element.rs b/native/src/element.rs
index 6afa3f62..119b7892 100644
--- a/native/src/element.rs
+++ b/native/src/element.rs
@@ -4,7 +4,7 @@ use crate::mouse;
use crate::overlay;
use crate::renderer;
use crate::{
- Clipboard, Color, Hasher, Layout, Length, Point, Rectangle, Shell, Widget,
+ Clipboard, Color, Layout, Length, Point, Rectangle, Shell, Widget,
};
/// A generic [`Widget`].
@@ -269,11 +269,6 @@ where
)
}
- /// Computes the _layout_ hash of the [`Element`].
- pub fn hash_layout(&self, state: &mut Hasher) {
- self.widget.hash_layout(state);
- }
-
/// Returns the overlay of the [`Element`], if there is any.
pub fn overlay<'b>(
&'b mut self,
@@ -379,10 +374,6 @@ where
)
}
- fn hash_layout(&self, state: &mut Hasher) {
- self.widget.hash_layout(state);
- }
-
fn overlay(
&mut self,
layout: Layout<'_>,
@@ -504,10 +495,6 @@ where
)
}
- fn hash_layout(&self, state: &mut Hasher) {
- self.element.widget.hash_layout(state);
- }
-
fn overlay(
&mut self,
layout: Layout<'_>,
diff --git a/native/src/lib.rs b/native/src/lib.rs
index a5526e6d..5c9c24c9 100644
--- a/native/src/lib.rs
+++ b/native/src/lib.rs
@@ -69,9 +69,10 @@ mod debug;
mod debug;
pub use iced_core::alignment;
+pub use iced_core::time;
pub use iced_core::{
- Alignment, Background, Color, Font, Length, Padding, Point, Rectangle,
- Size, Vector,
+ Alignment, Background, Color, ContentFit, Font, Length, Padding, Point,
+ Rectangle, Size, Vector,
};
pub use iced_futures::{executor, futures};
diff --git a/native/src/mouse/click.rs b/native/src/mouse/click.rs
index 6c8b61a5..4a7d796c 100644
--- a/native/src/mouse/click.rs
+++ b/native/src/mouse/click.rs
@@ -1,6 +1,6 @@
//! Track mouse clicks.
+use crate::time::Instant;
use crate::Point;
-use std::time::Instant;
/// A mouse click.
#[derive(Debug, Clone, Copy)]
@@ -62,9 +62,14 @@ impl Click {
}
fn is_consecutive(&self, new_position: Point, time: Instant) -> bool {
+ let duration = if time > self.time {
+ Some(time - self.time)
+ } else {
+ None
+ };
+
self.position == new_position
- && time
- .checked_duration_since(self.time)
+ && duration
.map(|duration| duration.as_millis() <= 300)
.unwrap_or(false)
}
diff --git a/native/src/overlay.rs b/native/src/overlay.rs
index d4b641af..124bcac2 100644
--- a/native/src/overlay.rs
+++ b/native/src/overlay.rs
@@ -10,7 +10,7 @@ use crate::event::{self, Event};
use crate::layout;
use crate::mouse;
use crate::renderer;
-use crate::{Clipboard, Hasher, Layout, Point, Rectangle, Shell, Size};
+use crate::{Clipboard, Layout, Point, Rectangle, Shell, Size};
/// An interactive component that can be displayed on top of other widgets.
pub trait Overlay<Message, Renderer>
@@ -39,19 +39,6 @@ where
cursor_position: Point,
);
- /// Computes the _layout_ hash of the [`Overlay`].
- ///
- /// The produced hash is used by the runtime to decide if the [`Layout`]
- /// needs to be recomputed between frames. Therefore, to ensure maximum
- /// efficiency, the hash should only be affected by the properties of the
- /// [`Overlay`] that can affect layouting.
- ///
- /// For example, the [`Text`] widget does not hash its color property, as
- /// its value cannot affect the overall [`Layout`] of the user interface.
- ///
- /// [`Text`]: crate::widget::Text
- fn hash_layout(&self, state: &mut Hasher, position: Point);
-
/// Processes a runtime [`Event`].
///
/// It receives:
diff --git a/native/src/overlay/element.rs b/native/src/overlay/element.rs
index e7621600..b60881e3 100644
--- a/native/src/overlay/element.rs
+++ b/native/src/overlay/element.rs
@@ -4,7 +4,7 @@ use crate::event::{self, Event};
use crate::layout;
use crate::mouse;
use crate::renderer;
-use crate::{Clipboard, Hasher, Layout, Point, Rectangle, Shell, Size, Vector};
+use crate::{Clipboard, Layout, Point, Rectangle, Shell, Size, Vector};
/// A generic [`Overlay`].
#[allow(missing_debug_implementations)]
@@ -100,11 +100,6 @@ where
) {
self.overlay.draw(renderer, style, layout, cursor_position)
}
-
- /// Computes the _layout_ hash of the [`Element`].
- pub fn hash_layout(&self, state: &mut Hasher) {
- self.overlay.hash_layout(state, self.position);
- }
}
struct Map<'a, A, B, Renderer> {
@@ -184,8 +179,4 @@ where
) {
self.content.draw(renderer, style, layout, cursor_position)
}
-
- fn hash_layout(&self, state: &mut Hasher, position: Point) {
- self.content.hash_layout(state, position);
- }
}
diff --git a/native/src/overlay/menu.rs b/native/src/overlay/menu.rs
index 2deef551..13fa7beb 100644
--- a/native/src/overlay/menu.rs
+++ b/native/src/overlay/menu.rs
@@ -10,8 +10,8 @@ use crate::touch;
use crate::widget::scrollable::{self, Scrollable};
use crate::widget::Container;
use crate::{
- Clipboard, Color, Element, Hasher, Layout, Length, Padding, Point,
- Rectangle, Shell, Size, Vector, Widget,
+ Clipboard, Color, Element, Layout, Length, Padding, Point, Rectangle,
+ Shell, Size, Vector, Widget,
};
pub use iced_style::menu::Style;
@@ -204,17 +204,6 @@ where
node
}
- fn hash_layout(&self, state: &mut Hasher, position: Point) {
- use std::hash::Hash;
-
- struct Marker;
- std::any::TypeId::of::<Marker>().hash(state);
-
- (position.x as u32).hash(state);
- (position.y as u32).hash(state);
- self.container.hash_layout(state);
- }
-
fn on_event(
&mut self,
event: Event,
@@ -320,17 +309,6 @@ where
layout::Node::new(size)
}
- fn hash_layout(&self, state: &mut Hasher) {
- use std::hash::Hash as _;
-
- struct Marker;
- std::any::TypeId::of::<Marker>().hash(state);
-
- self.options.len().hash(state);
- self.text_size.hash(state);
- self.padding.hash(state);
- }
-
fn on_event(
&mut self,
event: Event,
diff --git a/native/src/subscription.rs b/native/src/subscription.rs
index 63834654..9775c84b 100644
--- a/native/src/subscription.rs
+++ b/native/src/subscription.rs
@@ -3,7 +3,7 @@ use crate::event::{self, Event};
use crate::Hasher;
use iced_futures::futures::{self, Future, Stream};
-use iced_futures::BoxStream;
+use iced_futures::{BoxStream, MaybeSend};
use std::hash::Hash;
@@ -56,7 +56,7 @@ pub fn events_with<Message>(
f: fn(Event, event::Status) -> Option<Message>,
) -> Subscription<Message>
where
- Message: 'static + Send,
+ Message: 'static + MaybeSend,
{
Subscription::from_recipe(Runner {
id: f,
@@ -78,7 +78,7 @@ where
pub fn run<I, S, Message>(id: I, stream: S) -> Subscription<Message>
where
I: Hash + 'static,
- S: Stream<Item = Message> + Send + 'static,
+ S: Stream<Item = Message> + MaybeSend + 'static,
Message: 'static,
{
Subscription::from_recipe(Runner {
@@ -159,13 +159,13 @@ where
pub fn unfold<I, T, Fut, Message>(
id: I,
initial: T,
- mut f: impl FnMut(T) -> Fut + Send + Sync + 'static,
+ mut f: impl FnMut(T) -> Fut + MaybeSend + Sync + 'static,
) -> Subscription<Message>
where
I: Hash + 'static,
- T: Send + 'static,
- Fut: Future<Output = (Option<Message>, T)> + Send + 'static,
- Message: 'static + Send,
+ T: MaybeSend + 'static,
+ Fut: Future<Output = (Option<Message>, T)> + MaybeSend + 'static,
+ Message: 'static + MaybeSend,
{
use futures::future::{self, FutureExt};
use futures::stream::StreamExt;
@@ -191,7 +191,7 @@ impl<I, S, F, Message> Recipe<Hasher, (Event, event::Status)>
where
I: Hash + 'static,
F: FnOnce(EventStream) -> S,
- S: Stream<Item = Message> + Send + 'static,
+ S: Stream<Item = Message> + MaybeSend + 'static,
{
type Output = Message;
@@ -201,8 +201,6 @@ where
}
fn stream(self: Box<Self>, input: EventStream) -> BoxStream<Self::Output> {
- use futures::stream::StreamExt;
-
- (self.spawn)(input).boxed()
+ iced_futures::boxed_stream((self.spawn)(input))
}
}
diff --git a/native/src/user_interface.rs b/native/src/user_interface.rs
index f6ec96bb..6fc6a479 100644
--- a/native/src/user_interface.rs
+++ b/native/src/user_interface.rs
@@ -2,12 +2,9 @@
use crate::event::{self, Event};
use crate::layout;
use crate::mouse;
-use crate::overlay;
use crate::renderer;
use crate::{Clipboard, Element, Layout, Point, Rectangle, Shell, Size};
-use std::hash::Hasher;
-
/// A set of interactive graphical elements with a specific [`Layout`].
///
/// It can be updated and drawn.
@@ -23,8 +20,8 @@ use std::hash::Hasher;
#[allow(missing_debug_implementations)]
pub struct UserInterface<'a, Message, Renderer> {
root: Element<'a, Message, Renderer>,
- base: Layer,
- overlay: Option<Layer>,
+ base: layout::Node,
+ overlay: Option<layout::Node>,
bounds: Size,
}
@@ -89,41 +86,18 @@ where
pub fn build<E: Into<Element<'a, Message, Renderer>>>(
root: E,
bounds: Size,
- cache: Cache,
+ _cache: Cache,
renderer: &mut Renderer,
) -> Self {
let root = root.into();
- let (base, overlay) = {
- let hash = {
- let hasher = &mut crate::Hasher::default();
- root.hash_layout(hasher);
-
- hasher.finish()
- };
-
- let layout_is_cached =
- hash == cache.base.hash && bounds == cache.bounds;
-
- let (layout, overlay) = if layout_is_cached {
- (cache.base.layout, cache.overlay)
- } else {
- (
- renderer.layout(
- &root,
- &layout::Limits::new(Size::ZERO, bounds),
- ),
- None,
- )
- };
-
- (Layer { layout, hash }, overlay)
- };
+ let base =
+ renderer.layout(&root, &layout::Limits::new(Size::ZERO, bounds));
UserInterface {
root,
base,
- overlay,
+ overlay: None,
bounds,
}
}
@@ -206,16 +180,10 @@ where
let mut state = State::Updated;
let (base_cursor, overlay_statuses) = if let Some(mut overlay) =
- self.root.overlay(Layout::new(&self.base.layout), renderer)
+ self.root.overlay(Layout::new(&self.base), renderer)
{
let bounds = self.bounds;
-
- let mut layer = Self::overlay_layer(
- self.overlay.take(),
- bounds,
- &mut overlay,
- renderer,
- );
+ let mut layout = overlay.layout(renderer, bounds);
let event_statuses = events
.iter()
@@ -225,7 +193,7 @@ where
let event_status = overlay.on_event(
event,
- Layout::new(&layer.layout),
+ Layout::new(&layout),
cursor_position,
renderer,
clipboard,
@@ -233,12 +201,7 @@ where
);
shell.revalidate_layout(|| {
- layer = Self::overlay_layer(
- None,
- bounds,
- &mut overlay,
- renderer,
- );
+ layout = overlay.layout(renderer, bounds);
});
if shell.are_widgets_invalid() {
@@ -249,15 +212,14 @@ where
})
.collect();
- let base_cursor = if layer.layout.bounds().contains(cursor_position)
- {
+ let base_cursor = if layout.bounds().contains(cursor_position) {
// TODO: Type-safe cursor availability
Point::new(-1.0, -1.0)
} else {
cursor_position
};
- self.overlay = Some(layer);
+ self.overlay = Some(layout);
(base_cursor, event_statuses)
} else {
@@ -273,7 +235,7 @@ where
let event_status = self.root.widget.on_event(
event,
- Layout::new(&self.base.layout),
+ Layout::new(&self.base),
base_cursor,
renderer,
clipboard,
@@ -281,19 +243,11 @@ where
);
shell.revalidate_layout(|| {
- let hash = {
- let hasher = &mut crate::Hasher::default();
- self.root.hash_layout(hasher);
-
- hasher.finish()
- };
-
- let layout = renderer.layout(
+ self.base = renderer.layout(
&self.root,
&layout::Limits::new(Size::ZERO, self.bounds),
);
- self.base = Layer { layout, hash };
self.overlay = None;
});
@@ -391,48 +345,38 @@ where
let viewport = Rectangle::with_size(self.bounds);
- self.overlay = if let Some(mut overlay) =
- self.root.overlay(Layout::new(&self.base.layout), renderer)
+ let base_cursor = if let Some(overlay) =
+ self.root.overlay(Layout::new(&self.base), renderer)
{
- let layer = Self::overlay_layer(
- self.overlay.take(),
- self.bounds,
- &mut overlay,
- renderer,
- );
-
- Some(layer)
- } else {
- None
- };
+ let overlay_layout = self
+ .overlay
+ .take()
+ .unwrap_or_else(|| overlay.layout(renderer, self.bounds));
- if let Some(layer) = &self.overlay {
- let base_cursor = if layer.layout.bounds().contains(cursor_position)
- {
- Point::new(-1.0, -1.0)
- } else {
- cursor_position
- };
+ let new_cursor_position =
+ if overlay_layout.bounds().contains(cursor_position) {
+ Point::new(-1.0, -1.0)
+ } else {
+ cursor_position
+ };
+
+ self.overlay = Some(overlay_layout);
- self.root.widget.draw(
- renderer,
- &renderer::Style::default(),
- Layout::new(&self.base.layout),
- base_cursor,
- &viewport,
- );
+ new_cursor_position
} else {
- self.root.widget.draw(
- renderer,
- &renderer::Style::default(),
- Layout::new(&self.base.layout),
- cursor_position,
- &viewport,
- );
+ cursor_position
};
+ self.root.widget.draw(
+ renderer,
+ &renderer::Style::default(),
+ Layout::new(&self.base),
+ base_cursor,
+ &viewport,
+ );
+
let base_interaction = self.root.widget.mouse_interaction(
- Layout::new(&self.base.layout),
+ Layout::new(&self.base),
cursor_position,
&viewport,
renderer,
@@ -452,34 +396,32 @@ where
// avoid this additional call.
overlay
.as_ref()
- .and_then(|layer| {
- root.overlay(Layout::new(&base.layout), renderer).map(
- |overlay| {
- let overlay_interaction = overlay.mouse_interaction(
- Layout::new(&layer.layout),
- cursor_position,
- &viewport,
+ .and_then(|layout| {
+ root.overlay(Layout::new(&base), renderer).map(|overlay| {
+ let overlay_interaction = overlay.mouse_interaction(
+ Layout::new(layout),
+ cursor_position,
+ &viewport,
+ renderer,
+ );
+
+ let overlay_bounds = layout.bounds();
+
+ renderer.with_layer(overlay_bounds, |renderer| {
+ overlay.draw(
renderer,
+ &renderer::Style::default(),
+ Layout::new(layout),
+ cursor_position,
);
+ });
- let overlay_bounds = layer.layout.bounds();
-
- renderer.with_layer(overlay_bounds, |renderer| {
- overlay.draw(
- renderer,
- &renderer::Style::default(),
- Layout::new(&layer.layout),
- cursor_position,
- );
- });
-
- if overlay_bounds.contains(cursor_position) {
- overlay_interaction
- } else {
- base_interaction
- }
- },
- )
+ if overlay_bounds.contains(cursor_position) {
+ overlay_interaction
+ } else {
+ base_interaction
+ }
+ })
})
.unwrap_or(base_interaction)
}
@@ -487,66 +429,19 @@ where
/// Relayouts and returns a new [`UserInterface`] using the provided
/// bounds.
pub fn relayout(self, bounds: Size, renderer: &mut Renderer) -> Self {
- Self::build(
- self.root,
- bounds,
- Cache {
- base: self.base,
- overlay: self.overlay,
- bounds: self.bounds,
- },
- renderer,
- )
+ Self::build(self.root, bounds, Cache, renderer)
}
/// Extract the [`Cache`] of the [`UserInterface`], consuming it in the
/// process.
pub fn into_cache(self) -> Cache {
- Cache {
- base: self.base,
- overlay: self.overlay,
- bounds: self.bounds,
- }
- }
-
- fn overlay_layer(
- cache: Option<Layer>,
- bounds: Size,
- overlay: &mut overlay::Element<'_, Message, Renderer>,
- renderer: &Renderer,
- ) -> Layer {
- let new_hash = {
- let hasher = &mut crate::Hasher::default();
- overlay.hash_layout(hasher);
-
- hasher.finish()
- };
-
- let layout = match cache {
- Some(Layer { hash, layout }) if new_hash == hash => layout,
- _ => overlay.layout(renderer, bounds),
- };
-
- Layer {
- layout,
- hash: new_hash,
- }
+ Cache
}
}
-#[derive(Debug, Clone)]
-struct Layer {
- layout: layout::Node,
- hash: u64,
-}
-
/// Reusable data of a specific [`UserInterface`].
#[derive(Debug, Clone)]
-pub struct Cache {
- base: Layer,
- overlay: Option<Layer>,
- bounds: Size,
-}
+pub struct Cache;
impl Cache {
/// Creates an empty [`Cache`].
@@ -554,14 +449,7 @@ impl Cache {
/// You should use this to initialize a [`Cache`] before building your first
/// [`UserInterface`].
pub fn new() -> Cache {
- Cache {
- base: Layer {
- layout: layout::Node::new(Size::new(0.0, 0.0)),
- hash: 0,
- },
- overlay: None,
- bounds: Size::ZERO,
- }
+ Cache
}
}
diff --git a/native/src/widget.rs b/native/src/widget.rs
index acd2e580..aacdc3d9 100644
--- a/native/src/widget.rs
+++ b/native/src/widget.rs
@@ -75,7 +75,7 @@ use crate::layout;
use crate::mouse;
use crate::overlay;
use crate::renderer;
-use crate::{Clipboard, Hasher, Layout, Length, Point, Rectangle, Shell};
+use crate::{Clipboard, Layout, Length, Point, Rectangle, Shell};
/// A component that displays information and allows interaction.
///
@@ -131,19 +131,6 @@ where
viewport: &Rectangle,
);
- /// Computes the _layout_ hash of the [`Widget`].
- ///
- /// The produced hash is used by the runtime to decide if the [`Layout`]
- /// needs to be recomputed between frames. Therefore, to ensure maximum
- /// efficiency, the hash should only be affected by the properties of the
- /// [`Widget`] that can affect layouting.
- ///
- /// For example, the [`Text`] widget does not hash its color property, as
- /// its value cannot affect the overall [`Layout`] of the user interface.
- ///
- /// [`Text`]: crate::widget::Text
- fn hash_layout(&self, state: &mut Hasher);
-
/// Processes a runtime [`Event`].
///
/// It receives:
diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs
index b4a3adc3..57fdd7d4 100644
--- a/native/src/widget/button.rs
+++ b/native/src/widget/button.rs
@@ -8,12 +8,10 @@ use crate::overlay;
use crate::renderer;
use crate::touch;
use crate::{
- Background, Clipboard, Color, Element, Hasher, Layout, Length, Padding,
- Point, Rectangle, Shell, Vector, Widget,
+ Background, Clipboard, Color, Element, Layout, Length, Padding, Point,
+ Rectangle, Shell, Vector, Widget,
};
-use std::hash::Hash;
-
pub use iced_style::button::{Style, StyleSheet};
/// A generic widget that produces a message when pressed.
@@ -333,14 +331,6 @@ where
);
}
- fn hash_layout(&self, state: &mut Hasher) {
- struct Marker;
- std::any::TypeId::of::<Marker>().hash(state);
-
- self.width.hash(state);
- self.content.hash_layout(state);
- }
-
fn overlay(
&mut self,
layout: Layout<'_>,
diff --git a/native/src/widget/checkbox.rs b/native/src/widget/checkbox.rs
index fff65a40..15cbf93a 100644
--- a/native/src/widget/checkbox.rs
+++ b/native/src/widget/checkbox.rs
@@ -1,6 +1,4 @@
//! Show toggle controls using checkboxes.
-use std::hash::Hash;
-
use crate::alignment;
use crate::event::{self, Event};
use crate::layout;
@@ -10,8 +8,8 @@ use crate::text;
use crate::touch;
use crate::widget::{self, Row, Text};
use crate::{
- Alignment, Clipboard, Element, Hasher, Layout, Length, Point, Rectangle,
- Shell, Widget,
+ Alignment, Clipboard, Element, Layout, Length, Point, Rectangle, Shell,
+ Widget,
};
pub use iced_style::checkbox::{Style, StyleSheet};
@@ -262,13 +260,6 @@ where
);
}
}
-
- fn hash_layout(&self, state: &mut Hasher) {
- struct Marker;
- std::any::TypeId::of::<Marker>().hash(state);
-
- self.label.hash(state);
- }
}
impl<'a, Message, Renderer> From<Checkbox<'a, Message, Renderer>>
diff --git a/native/src/widget/column.rs b/native/src/widget/column.rs
index 66ed5e23..f161d1f2 100644
--- a/native/src/widget/column.rs
+++ b/native/src/widget/column.rs
@@ -1,14 +1,12 @@
//! Distribute content vertically.
-use std::hash::Hash;
-
use crate::event::{self, Event};
use crate::layout;
use crate::mouse;
use crate::overlay;
use crate::renderer;
use crate::{
- Alignment, Clipboard, Element, Hasher, Layout, Length, Padding, Point,
- Rectangle, Shell, Widget,
+ Alignment, Clipboard, Element, Layout, Length, Padding, Point, Rectangle,
+ Shell, Widget,
};
use std::u32;
@@ -199,23 +197,6 @@ where
}
}
- fn hash_layout(&self, state: &mut Hasher) {
- struct Marker;
- std::any::TypeId::of::<Marker>().hash(state);
-
- self.width.hash(state);
- self.height.hash(state);
- self.max_width.hash(state);
- self.max_height.hash(state);
- self.align_items.hash(state);
- self.spacing.hash(state);
- self.padding.hash(state);
-
- for child in &self.children {
- child.widget.hash_layout(state);
- }
- }
-
fn overlay(
&mut self,
layout: Layout<'_>,
diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs
index 4444732a..ca85a425 100644
--- a/native/src/widget/container.rs
+++ b/native/src/widget/container.rs
@@ -1,6 +1,4 @@
//! Decorate content and apply alignment.
-use std::hash::Hash;
-
use crate::alignment::{self, Alignment};
use crate::event::{self, Event};
use crate::layout;
@@ -8,8 +6,8 @@ use crate::mouse;
use crate::overlay;
use crate::renderer;
use crate::{
- Background, Clipboard, Color, Element, Hasher, Layout, Length, Padding,
- Point, Rectangle, Shell, Widget,
+ Background, Clipboard, Color, Element, Layout, Length, Padding, Point,
+ Rectangle, Shell, Widget,
};
use std::u32;
@@ -219,21 +217,6 @@ where
);
}
- fn hash_layout(&self, state: &mut Hasher) {
- struct Marker;
- std::any::TypeId::of::<Marker>().hash(state);
-
- self.padding.hash(state);
- self.width.hash(state);
- self.height.hash(state);
- self.max_width.hash(state);
- self.max_height.hash(state);
- self.horizontal_alignment.hash(state);
- self.vertical_alignment.hash(state);
-
- self.content.hash_layout(state);
- }
-
fn overlay(
&mut self,
layout: Layout<'_>,
diff --git a/native/src/widget/image.rs b/native/src/widget/image.rs
index b8fb662e..de0ffbc0 100644
--- a/native/src/widget/image.rs
+++ b/native/src/widget/image.rs
@@ -5,7 +5,9 @@ pub use viewer::Viewer;
use crate::image;
use crate::layout;
use crate::renderer;
-use crate::{Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget};
+use crate::{
+ ContentFit, Element, Layout, Length, Point, Rectangle, Size, Vector, Widget,
+};
use std::hash::Hash;
@@ -26,6 +28,7 @@ pub struct Image<Handle> {
handle: Handle,
width: Length,
height: Length,
+ content_fit: ContentFit,
}
impl<Handle> Image<Handle> {
@@ -35,6 +38,7 @@ impl<Handle> Image<Handle> {
handle: handle.into(),
width: Length::Shrink,
height: Length::Shrink,
+ content_fit: ContentFit::Contain,
}
}
@@ -49,6 +53,16 @@ impl<Handle> Image<Handle> {
self.height = height;
self
}
+
+ /// Sets the [`ContentFit`] of the [`Image`].
+ ///
+ /// Defaults to [`ContentFit::Contain`]
+ pub fn content_fit(self, content_fit: ContentFit) -> Self {
+ Self {
+ content_fit,
+ ..self
+ }
+ }
}
impl<Message, Renderer, Handle> Widget<Message, Renderer> for Image<Handle>
@@ -69,24 +83,32 @@ where
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
+ // The raw w/h of the underlying image
let (width, height) = renderer.dimensions(&self.handle);
+ let image_size = Size::new(width as f32, height as f32);
- let aspect_ratio = width as f32 / height as f32;
-
- let mut size = limits
+ // The size to be available to the widget prior to `Shrink`ing
+ let raw_size = limits
.width(self.width)
.height(self.height)
- .resolve(Size::new(width as f32, height as f32));
-
- let viewport_aspect_ratio = size.width / size.height;
-
- if viewport_aspect_ratio > aspect_ratio {
- size.width = width as f32 * size.height / height as f32;
- } else {
- size.height = height as f32 * size.width / width as f32;
- }
-
- layout::Node::new(size)
+ .resolve(image_size);
+
+ // The uncropped size of the image when fit to the bounds above
+ let full_size = self.content_fit.fit(image_size, raw_size);
+
+ // Shrink the widget to fit the resized image, if requested
+ let final_size = Size {
+ width: match self.width {
+ Length::Shrink => f32::min(raw_size.width, full_size.width),
+ _ => raw_size.width,
+ },
+ height: match self.height {
+ Length::Shrink => f32::min(raw_size.height, full_size.height),
+ _ => raw_size.height,
+ },
+ };
+
+ layout::Node::new(final_size)
}
fn draw(
@@ -97,16 +119,34 @@ where
_cursor_position: Point,
_viewport: &Rectangle,
) {
- renderer.draw(self.handle.clone(), layout.bounds());
- }
-
- fn hash_layout(&self, state: &mut Hasher) {
- struct Marker;
- std::any::TypeId::of::<Marker>().hash(state);
-
- self.handle.hash(state);
- self.width.hash(state);
- self.height.hash(state);
+ let (width, height) = renderer.dimensions(&self.handle);
+ let image_size = Size::new(width as f32, height as f32);
+
+ let bounds = layout.bounds();
+ let adjusted_fit = self.content_fit.fit(image_size, bounds.size());
+
+ let render = |renderer: &mut Renderer| {
+ let offset = Vector::new(
+ (bounds.width - adjusted_fit.width).max(0.0) / 2.0,
+ (bounds.height - adjusted_fit.height).max(0.0) / 2.0,
+ );
+
+ let drawing_bounds = Rectangle {
+ width: adjusted_fit.width,
+ height: adjusted_fit.height,
+ ..bounds
+ };
+
+ renderer.draw(self.handle.clone(), drawing_bounds + offset)
+ };
+
+ if adjusted_fit.width > bounds.width
+ || adjusted_fit.height > bounds.height
+ {
+ renderer.with_layer(bounds, render);
+ } else {
+ render(renderer)
+ }
}
}
diff --git a/native/src/widget/image/viewer.rs b/native/src/widget/image/viewer.rs
index 486d5fa5..840b88e5 100644
--- a/native/src/widget/image/viewer.rs
+++ b/native/src/widget/image/viewer.rs
@@ -5,8 +5,8 @@ use crate::layout;
use crate::mouse;
use crate::renderer;
use crate::{
- Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Shell, Size,
- Vector, Widget,
+ Clipboard, Element, Layout, Length, Point, Rectangle, Shell, Size, Vector,
+ Widget,
};
use std::hash::Hash;
@@ -335,17 +335,6 @@ where
});
});
}
-
- fn hash_layout(&self, state: &mut Hasher) {
- struct Marker;
- std::any::TypeId::of::<Marker>().hash(state);
-
- self.width.hash(state);
- self.height.hash(state);
- self.padding.hash(state);
-
- self.handle.hash(state);
- }
}
/// The local state of a [`Viewer`].
diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs
index 5f293fa6..8ad63cf1 100644
--- a/native/src/widget/pane_grid.rs
+++ b/native/src/widget/pane_grid.rs
@@ -34,8 +34,8 @@ use crate::overlay;
use crate::renderer;
use crate::touch;
use crate::{
- Clipboard, Color, Element, Hasher, Layout, Length, Point, Rectangle, Shell,
- Size, Vector, Widget,
+ Clipboard, Color, Element, Layout, Length, Point, Rectangle, Shell, Size,
+ Vector, Widget,
};
pub use iced_style::pane_grid::{Line, StyleSheet};
@@ -666,21 +666,6 @@ where
}
}
- fn hash_layout(&self, state: &mut Hasher) {
- use std::hash::Hash;
-
- struct Marker;
- std::any::TypeId::of::<Marker>().hash(state);
-
- self.width.hash(state);
- self.height.hash(state);
- self.state.hash_layout(state);
-
- for (_, element) in &self.elements {
- element.hash_layout(state);
- }
- }
-
fn overlay(
&mut self,
layout: Layout<'_>,
diff --git a/native/src/widget/pane_grid/content.rs b/native/src/widget/pane_grid/content.rs
index 6d12aa2d..8b0e8d2a 100644
--- a/native/src/widget/pane_grid/content.rs
+++ b/native/src/widget/pane_grid/content.rs
@@ -5,9 +5,7 @@ use crate::overlay;
use crate::renderer;
use crate::widget::container;
use crate::widget::pane_grid::TitleBar;
-use crate::{
- Clipboard, Element, Hasher, Layout, Point, Rectangle, Shell, Size,
-};
+use crate::{Clipboard, Element, Layout, Point, Rectangle, Shell, Size};
/// The content of a [`Pane`].
///
@@ -236,14 +234,6 @@ where
.max(title_bar_interaction)
}
- pub(crate) fn hash_layout(&self, state: &mut Hasher) {
- if let Some(title_bar) = &self.title_bar {
- title_bar.hash_layout(state);
- }
-
- self.body.hash_layout(state);
- }
-
pub(crate) fn overlay(
&mut self,
layout: Layout<'_>,
diff --git a/native/src/widget/pane_grid/state.rs b/native/src/widget/pane_grid/state.rs
index bcc724a8..feea0dec 100644
--- a/native/src/widget/pane_grid/state.rs
+++ b/native/src/widget/pane_grid/state.rs
@@ -1,7 +1,7 @@
use crate::widget::pane_grid::{
Axis, Configuration, Direction, Node, Pane, Split,
};
-use crate::{Hasher, Point, Rectangle, Size};
+use crate::{Point, Rectangle, Size};
use std::collections::{BTreeMap, HashMap};
@@ -292,10 +292,4 @@ impl Internal {
pub fn idle(&mut self) {
self.action = Action::Idle;
}
-
- pub fn hash_layout(&self, hasher: &mut Hasher) {
- use std::hash::Hash;
-
- self.layout.hash(hasher);
- }
}
diff --git a/native/src/widget/pane_grid/title_bar.rs b/native/src/widget/pane_grid/title_bar.rs
index ea9ebfc4..d56972ec 100644
--- a/native/src/widget/pane_grid/title_bar.rs
+++ b/native/src/widget/pane_grid/title_bar.rs
@@ -5,7 +5,7 @@ use crate::overlay;
use crate::renderer;
use crate::widget::container;
use crate::{
- Clipboard, Element, Hasher, Layout, Padding, Point, Rectangle, Shell, Size,
+ Clipboard, Element, Layout, Padding, Point, Rectangle, Shell, Size,
};
/// The title bar of a [`Pane`].
@@ -157,17 +157,6 @@ where
}
}
- pub(crate) fn hash_layout(&self, hasher: &mut Hasher) {
- use std::hash::Hash;
-
- self.content.hash_layout(hasher);
- self.padding.hash(hasher);
-
- if let Some(controls) = &self.controls {
- controls.hash_layout(hasher);
- }
- }
-
pub(crate) fn layout(
&self,
renderer: &Renderer,
diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs
index a200fb13..3be6c20c 100644
--- a/native/src/widget/pick_list.rs
+++ b/native/src/widget/pick_list.rs
@@ -10,8 +10,8 @@ use crate::renderer;
use crate::text::{self, Text};
use crate::touch;
use crate::{
- Clipboard, Element, Hasher, Layout, Length, Padding, Point, Rectangle,
- Shell, Size, Widget,
+ Clipboard, Element, Layout, Length, Padding, Point, Rectangle, Shell, Size,
+ Widget,
};
use std::borrow::Cow;
@@ -220,24 +220,6 @@ where
layout::Node::new(size)
}
- fn hash_layout(&self, state: &mut Hasher) {
- use std::hash::Hash as _;
-
- match self.width {
- Length::Shrink => {
- self.placeholder.hash(state);
-
- self.options
- .iter()
- .map(ToString::to_string)
- .for_each(|label| label.hash(state));
- }
- _ => {
- self.width.hash(state);
- }
- }
- }
-
fn on_event(
&mut self,
event: Event,
diff --git a/native/src/widget/progress_bar.rs b/native/src/widget/progress_bar.rs
index 69eb8c09..c26c38fa 100644
--- a/native/src/widget/progress_bar.rs
+++ b/native/src/widget/progress_bar.rs
@@ -1,11 +1,9 @@
//! Provide progress feedback to your users.
use crate::layout;
use crate::renderer;
-use crate::{
- Color, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget,
-};
+use crate::{Color, Element, Layout, Length, Point, Rectangle, Size, Widget};
-use std::{hash::Hash, ops::RangeInclusive};
+use std::ops::RangeInclusive;
pub use iced_style::progress_bar::{Style, StyleSheet};
@@ -141,14 +139,6 @@ where
);
}
}
-
- fn hash_layout(&self, state: &mut Hasher) {
- struct Marker;
- std::any::TypeId::of::<Marker>().hash(state);
-
- self.width.hash(state);
- self.height.hash(state);
- }
}
impl<'a, Message, Renderer> From<ProgressBar<'a>>
diff --git a/native/src/widget/radio.rs b/native/src/widget/radio.rs
index c4992764..fed2925b 100644
--- a/native/src/widget/radio.rs
+++ b/native/src/widget/radio.rs
@@ -1,6 +1,4 @@
//! Create choices using radio buttons.
-use std::hash::Hash;
-
use crate::alignment;
use crate::event::{self, Event};
use crate::layout;
@@ -10,8 +8,8 @@ use crate::text;
use crate::touch;
use crate::widget::{self, Row, Text};
use crate::{
- Alignment, Clipboard, Color, Element, Hasher, Layout, Length, Point,
- Rectangle, Shell, Widget,
+ Alignment, Clipboard, Color, Element, Layout, Length, Point, Rectangle,
+ Shell, Widget,
};
pub use iced_style::radio::{Style, StyleSheet};
@@ -280,13 +278,6 @@ where
);
}
}
-
- fn hash_layout(&self, state: &mut Hasher) {
- struct Marker;
- std::any::TypeId::of::<Marker>().hash(state);
-
- self.label.hash(state);
- }
}
impl<'a, Message, Renderer> From<Radio<'a, Message, Renderer>>
diff --git a/native/src/widget/row.rs b/native/src/widget/row.rs
index c423d31f..e34befb2 100644
--- a/native/src/widget/row.rs
+++ b/native/src/widget/row.rs
@@ -5,11 +5,10 @@ use crate::mouse;
use crate::overlay;
use crate::renderer;
use crate::{
- Alignment, Clipboard, Element, Hasher, Layout, Length, Padding, Point,
- Rectangle, Shell, Widget,
+ Alignment, Clipboard, Element, Layout, Length, Padding, Point, Rectangle,
+ Shell, Widget,
};
-use std::hash::Hash;
use std::u32;
/// A container that distributes its contents horizontally.
@@ -198,23 +197,6 @@ where
}
}
- fn hash_layout(&self, state: &mut Hasher) {
- struct Marker;
- std::any::TypeId::of::<Marker>().hash(state);
-
- self.width.hash(state);
- self.height.hash(state);
- self.max_width.hash(state);
- self.max_height.hash(state);
- self.align_items.hash(state);
- self.spacing.hash(state);
- self.padding.hash(state);
-
- for child in &self.children {
- child.widget.hash_layout(state);
- }
- }
-
fn overlay(
&mut self,
layout: Layout<'_>,
diff --git a/native/src/widget/rule.rs b/native/src/widget/rule.rs
index 7c8c5dbc..b0cc3768 100644
--- a/native/src/widget/rule.rs
+++ b/native/src/widget/rule.rs
@@ -1,11 +1,7 @@
//! Display a horizontal or vertical rule for dividing content.
use crate::layout;
use crate::renderer;
-use crate::{
- Color, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget,
-};
-
-use std::hash::Hash;
+use crate::{Color, Element, Layout, Length, Point, Rectangle, Size, Widget};
pub use iced_style::rule::{FillMode, Style, StyleSheet};
@@ -122,14 +118,6 @@ where
style.color,
);
}
-
- fn hash_layout(&self, state: &mut Hasher) {
- struct Marker;
- std::any::TypeId::of::<Marker>().hash(state);
-
- self.width.hash(state);
- self.height.hash(state);
- }
}
impl<'a, Message, Renderer> From<Rule<'a>> for Element<'a, Message, Renderer>
diff --git a/native/src/widget/scrollable.rs b/native/src/widget/scrollable.rs
index 4b005c6b..ce734ad8 100644
--- a/native/src/widget/scrollable.rs
+++ b/native/src/widget/scrollable.rs
@@ -7,11 +7,11 @@ use crate::renderer;
use crate::touch;
use crate::widget::Column;
use crate::{
- Alignment, Background, Clipboard, Color, Element, Hasher, Layout, Length,
- Padding, Point, Rectangle, Shell, Size, Vector, Widget,
+ Alignment, Background, Clipboard, Color, Element, Layout, Length, Padding,
+ Point, Rectangle, Shell, Size, Vector, Widget,
};
-use std::{f32, hash::Hash, u32};
+use std::{f32, u32};
pub use iced_style::scrollable::StyleSheet;
@@ -570,16 +570,6 @@ where
}
}
- fn hash_layout(&self, state: &mut Hasher) {
- struct Marker;
- std::any::TypeId::of::<Marker>().hash(state);
-
- self.height.hash(state);
- self.max_height.hash(state);
-
- self.content.hash_layout(state)
- }
-
fn overlay(
&mut self,
layout: Layout<'_>,
diff --git a/native/src/widget/slider.rs b/native/src/widget/slider.rs
index 917bfc14..289f75f5 100644
--- a/native/src/widget/slider.rs
+++ b/native/src/widget/slider.rs
@@ -7,11 +7,10 @@ use crate::mouse;
use crate::renderer;
use crate::touch;
use crate::{
- Background, Clipboard, Color, Element, Hasher, Layout, Length, Point,
- Rectangle, Shell, Size, Widget,
+ Background, Clipboard, Color, Element, Layout, Length, Point, Rectangle,
+ Shell, Size, Widget,
};
-use std::hash::Hash;
use std::ops::RangeInclusive;
pub use iced_style::slider::{Handle, HandleShape, Style, StyleSheet};
@@ -374,13 +373,6 @@ where
mouse::Interaction::default()
}
}
-
- fn hash_layout(&self, state: &mut Hasher) {
- struct Marker;
- std::any::TypeId::of::<Marker>().hash(state);
-
- self.width.hash(state);
- }
}
impl<'a, T, Message, Renderer> From<Slider<'a, T, Message>>
diff --git a/native/src/widget/space.rs b/native/src/widget/space.rs
index 3373f3b7..4135d1b8 100644
--- a/native/src/widget/space.rs
+++ b/native/src/widget/space.rs
@@ -1,9 +1,7 @@
//! Distribute content vertically.
use crate::layout;
use crate::renderer;
-use crate::{Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget};
-
-use std::hash::Hash;
+use crate::{Element, Layout, Length, Point, Rectangle, Size, Widget};
/// An amount of empty space.
///
@@ -68,13 +66,6 @@ where
_viewport: &Rectangle,
) {
}
-
- fn hash_layout(&self, state: &mut Hasher) {
- std::any::TypeId::of::<Space>().hash(state);
-
- self.width.hash(state);
- self.height.hash(state);
- }
}
impl<'a, Message, Renderer> From<Space> for Element<'a, Message, Renderer>
diff --git a/native/src/widget/svg.rs b/native/src/widget/svg.rs
index f212dfcb..008ab356 100644
--- a/native/src/widget/svg.rs
+++ b/native/src/widget/svg.rs
@@ -2,9 +2,10 @@
use crate::layout;
use crate::renderer;
use crate::svg::{self, Handle};
-use crate::{Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget};
+use crate::{
+ ContentFit, Element, Layout, Length, Point, Rectangle, Size, Vector, Widget,
+};
-use std::hash::Hash;
use std::path::PathBuf;
/// A vector graphics image.
@@ -18,6 +19,7 @@ pub struct Svg {
handle: Handle,
width: Length,
height: Length,
+ content_fit: ContentFit,
}
impl Svg {
@@ -27,6 +29,7 @@ impl Svg {
handle: handle.into(),
width: Length::Fill,
height: Length::Shrink,
+ content_fit: ContentFit::Contain,
}
}
@@ -47,6 +50,16 @@ impl Svg {
self.height = height;
self
}
+
+ /// Sets the [`ContentFit`] of the [`Svg`].
+ ///
+ /// Defaults to [`ContentFit::Contain`]
+ pub fn content_fit(self, content_fit: ContentFit) -> Self {
+ Self {
+ content_fit,
+ ..self
+ }
+ }
}
impl<Message, Renderer> Widget<Message, Renderer> for Svg
@@ -66,24 +79,32 @@ where
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
+ // The raw w/h of the underlying image
let (width, height) = renderer.dimensions(&self.handle);
+ let image_size = Size::new(width as f32, height as f32);
- let aspect_ratio = width as f32 / height as f32;
-
- let mut size = limits
+ // The size to be available to the widget prior to `Shrink`ing
+ let raw_size = limits
.width(self.width)
.height(self.height)
- .resolve(Size::new(width as f32, height as f32));
-
- let viewport_aspect_ratio = size.width / size.height;
-
- if viewport_aspect_ratio > aspect_ratio {
- size.width = width as f32 * size.height / height as f32;
- } else {
- size.height = height as f32 * size.width / width as f32;
- }
-
- layout::Node::new(size)
+ .resolve(image_size);
+
+ // The uncropped size of the image when fit to the bounds above
+ let full_size = self.content_fit.fit(image_size, raw_size);
+
+ // Shrink the widget to fit the resized image, if requested
+ let final_size = Size {
+ width: match self.width {
+ Length::Shrink => f32::min(raw_size.width, full_size.width),
+ _ => raw_size.width,
+ },
+ height: match self.height {
+ Length::Shrink => f32::min(raw_size.height, full_size.height),
+ _ => raw_size.height,
+ },
+ };
+
+ layout::Node::new(final_size)
}
fn draw(
@@ -94,15 +115,34 @@ where
_cursor_position: Point,
_viewport: &Rectangle,
) {
- renderer.draw(self.handle.clone(), layout.bounds())
- }
-
- fn hash_layout(&self, state: &mut Hasher) {
- std::any::TypeId::of::<Svg>().hash(state);
-
- self.handle.hash(state);
- self.width.hash(state);
- self.height.hash(state);
+ let (width, height) = renderer.dimensions(&self.handle);
+ let image_size = Size::new(width as f32, height as f32);
+
+ let bounds = layout.bounds();
+ let adjusted_fit = self.content_fit.fit(image_size, bounds.size());
+
+ let render = |renderer: &mut Renderer| {
+ let offset = Vector::new(
+ (bounds.width - adjusted_fit.width).max(0.0) / 2.0,
+ (bounds.height - adjusted_fit.height).max(0.0) / 2.0,
+ );
+
+ let drawing_bounds = Rectangle {
+ width: adjusted_fit.width,
+ height: adjusted_fit.height,
+ ..bounds
+ };
+
+ renderer.draw(self.handle.clone(), drawing_bounds + offset)
+ };
+
+ if adjusted_fit.width > bounds.width
+ || adjusted_fit.height > bounds.height
+ {
+ renderer.with_layer(bounds, render);
+ } else {
+ render(renderer)
+ }
}
}
diff --git a/native/src/widget/text.rs b/native/src/widget/text.rs
index f0b29d15..6f00c9c8 100644
--- a/native/src/widget/text.rs
+++ b/native/src/widget/text.rs
@@ -3,11 +3,7 @@ use crate::alignment;
use crate::layout;
use crate::renderer;
use crate::text;
-use crate::{
- Color, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget,
-};
-
-use std::hash::Hash;
+use crate::{Color, Element, Layout, Length, Point, Rectangle, Size, Widget};
/// A paragraph of text.
///
@@ -151,16 +147,6 @@ where
self.vertical_alignment,
);
}
-
- fn hash_layout(&self, state: &mut Hasher) {
- struct Marker;
- std::any::TypeId::of::<Marker>().hash(state);
-
- self.content.hash(state);
- self.size.hash(state);
- self.width.hash(state);
- self.height.hash(state);
- }
}
/// Draws text using the same logic as the [`Text`] widget.
diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs
index 03adeb12..e30e2343 100644
--- a/native/src/widget/text_input.rs
+++ b/native/src/widget/text_input.rs
@@ -20,8 +20,8 @@ use crate::renderer;
use crate::text::{self, Text};
use crate::touch;
use crate::{
- Clipboard, Color, Element, Hasher, Layout, Length, Padding, Point,
- Rectangle, Shell, Size, Vector, Widget,
+ Clipboard, Color, Element, Layout, Length, Padding, Point, Rectangle,
+ Shell, Size, Vector, Widget,
};
use std::u32;
@@ -783,17 +783,6 @@ where
) {
self.draw(renderer, layout, cursor_position, None)
}
-
- fn hash_layout(&self, state: &mut Hasher) {
- use std::{any::TypeId, hash::Hash};
- struct Marker;
- TypeId::of::<Marker>().hash(state);
-
- self.width.hash(state);
- self.max_width.hash(state);
- self.padding.hash(state);
- self.size.hash(state);
- }
}
impl<'a, Message, Renderer> From<TextInput<'a, Message, Renderer>>
diff --git a/native/src/widget/toggler.rs b/native/src/widget/toggler.rs
index 002e0a4f..48237edb 100644
--- a/native/src/widget/toggler.rs
+++ b/native/src/widget/toggler.rs
@@ -1,5 +1,4 @@
//! Show toggle controls using togglers.
-use std::hash::Hash;
use crate::alignment;
use crate::event;
@@ -9,8 +8,8 @@ use crate::renderer;
use crate::text;
use crate::widget::{Row, Text};
use crate::{
- Alignment, Clipboard, Element, Event, Hasher, Layout, Length, Point,
- Rectangle, Shell, Widget,
+ Alignment, Clipboard, Element, Event, Layout, Length, Point, Rectangle,
+ Shell, Widget,
};
pub use iced_style::toggler::{Style, StyleSheet};
@@ -295,13 +294,6 @@ where
style.foreground,
);
}
-
- fn hash_layout(&self, state: &mut Hasher) {
- struct Marker;
- std::any::TypeId::of::<Marker>().hash(state);
-
- self.label.hash(state)
- }
}
impl<'a, Message, Renderer> From<Toggler<'a, Message, Renderer>>
diff --git a/native/src/widget/tooltip.rs b/native/src/widget/tooltip.rs
index f6630864..7989c768 100644
--- a/native/src/widget/tooltip.rs
+++ b/native/src/widget/tooltip.rs
@@ -1,8 +1,4 @@
//! Display a widget over another.
-use std::hash::Hash;
-
-use iced_core::Rectangle;
-
use crate::event;
use crate::layout;
use crate::mouse;
@@ -11,8 +7,8 @@ use crate::text;
use crate::widget::container;
use crate::widget::text::Text;
use crate::{
- Clipboard, Element, Event, Hasher, Layout, Length, Padding, Point, Shell,
- Size, Vector, Widget,
+ Clipboard, Element, Event, Layout, Length, Padding, Point, Rectangle,
+ Shell, Size, Vector, Widget,
};
/// An element to display a widget over another.
@@ -268,13 +264,6 @@ where
});
}
}
-
- fn hash_layout(&self, state: &mut Hasher) {
- struct Marker;
- std::any::TypeId::of::<Marker>().hash(state);
-
- self.content.hash_layout(state);
- }
}
impl<'a, Message, Renderer> From<Tooltip<'a, Message, Renderer>>