Merge branch 'master' into virtual-widgets

This commit is contained in:
Héctor Ramón Jiménez 2022-03-07 15:29:43 +07:00
commit c35496d80f
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
58 changed files with 446 additions and 909 deletions

119
core/src/content_fit.rs Normal file
View file

@ -0,0 +1,119 @@
//! Control the fit of some content (like an image) within a space.
use crate::Size;
/// The strategy used to fit the contents of a widget to its bounding box.
///
/// Each variant of this enum is a strategy that can be applied for resolving
/// differences in aspect ratio and size between the image being displayed and
/// the space its being displayed in.
///
/// For an interactive demonstration of these properties as they are implemented
/// in CSS, see [Mozilla's docs][1], or run the `tour` example
///
/// [1]: https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)]
pub enum ContentFit {
/// Scale as big as it can be without needing to crop or hide parts.
///
/// The image will be scaled (preserving aspect ratio) so that it just fits
/// within the window. This won't distort the image or crop/hide any edges,
/// but if the image doesn't fit perfectly, there may be whitespace on the
/// top/bottom or left/right.
///
/// This is a great fit for when you need to display an image without losing
/// any part of it, particularly when the image itself is the focus of the
/// screen.
Contain,
/// Scale the image to cover all of the bounding box, cropping if needed.
///
/// This doesn't distort the image, and it ensures that the widget's area is
/// completely covered, but it might crop off a bit of the edges of the
/// widget, particularly when there is a big difference between the aspect
/// ratio of the widget and the aspect ratio of the image.
///
/// This is best for when you're using an image as a background, or to fill
/// space, and any details of the image around the edge aren't too
/// important.
Cover,
/// Distort the image so the widget is 100% covered without cropping.
///
/// This stretches the image to fit the widget, without any whitespace or
/// cropping. However, because of the stretch, the image may look distorted
/// or elongated, particularly when there's a mismatch of aspect ratios.
Fill,
/// Don't resize or scale the image at all.
///
/// This will not apply any transformations to the provided image, but also
/// means that unless you do the math yourself, the widget's area will not
/// be completely covered, or the image might be cropped.
///
/// This is best for when you've sized the image yourself.
None,
/// Scale the image down if it's too big for the space, but never scale it
/// up.
///
/// This works much like [`Contain`](Self::Contain), except that if the
/// image would have been scaled up, it keeps its original resolution to
/// avoid the bluring that accompanies upscaling images.
ScaleDown,
}
impl ContentFit {
/// Attempt to apply the given fit for a content size within some bounds.
///
/// The returned value is the recommended scaled size of the content.
pub fn fit(&self, content: Size, bounds: Size) -> Size {
let content_ar = content.width / content.height;
let bounds_ar = bounds.width / bounds.height;
match self {
Self::Contain => {
if bounds_ar > content_ar {
Size {
width: content.width * bounds.height / content.height,
..bounds
}
} else {
Size {
height: content.height * bounds.width / content.width,
..bounds
}
}
}
Self::Cover => {
if bounds_ar < content_ar {
Size {
width: content.width * bounds.height / content.height,
..bounds
}
} else {
Size {
height: content.height * bounds.width / content.width,
..bounds
}
}
}
Self::Fill => bounds,
Self::None => content,
Self::ScaleDown => {
if bounds_ar > content_ar && bounds.height < content.height {
Size {
width: content.width * bounds.height / content.height,
..bounds
}
} else if bounds.width < content.width {
Size {
height: content.height * bounds.width / content.width,
..bounds
}
} else {
content
}
}
}
}
}

View file

@ -21,6 +21,7 @@ pub mod time;
mod background;
mod color;
mod content_fit;
mod font;
mod length;
mod padding;
@ -32,6 +33,7 @@ mod vector;
pub use alignment::Alignment;
pub use background::Background;
pub use color::Color;
pub use content_fit::ContentFit;
pub use font::Font;
pub use length::Length;
pub use padding::Padding;

View file

@ -11,9 +11,7 @@ mod circle {
// implemented by `iced_wgpu` and other renderers.
use iced_native::layout::{self, Layout};
use iced_native::renderer;
use iced_native::{
Color, Element, Hasher, Length, Point, Rectangle, Size, Widget,
};
use iced_native::{Color, Element, Length, Point, Rectangle, Size, Widget};
pub struct Circle {
radius: f32,
@ -45,12 +43,6 @@ mod circle {
layout::Node::new(Size::new(self.radius * 2.0, self.radius * 2.0))
}
fn hash_layout(&self, state: &mut Hasher) {
use std::hash::Hash;
self.radius.to_bits().hash(state);
}
fn draw(
&self,
renderer: &mut Renderer,

View file

@ -14,8 +14,7 @@ mod rainbow {
use iced_graphics::{Backend, Primitive};
use iced_native::{
layout, Element, Hasher, Layout, Length, Point, Rectangle, Size,
Vector, Widget,
layout, Element, Layout, Length, Point, Rectangle, Size, Vector, Widget,
};
pub struct Rainbow;
@ -48,8 +47,6 @@ mod rainbow {
layout::Node::new(Size::new(size.width, size.width))
}
fn hash_layout(&self, _state: &mut Hasher) {}
fn draw(
&self,
renderer: &mut Renderer<B>,

View file

@ -1,7 +1,7 @@
use iced::{
alignment, button, scrollable, slider, text_input, Button, Checkbox, Color,
Column, Container, Element, Image, Length, Radio, Row, Sandbox, Scrollable,
Settings, Slider, Space, Text, TextInput, Toggler,
Column, Container, ContentFit, Element, Image, Length, Radio, Row, Sandbox,
Scrollable, Settings, Slider, Space, Text, TextInput, Toggler,
};
pub fn main() -> iced::Result {
@ -139,7 +139,8 @@ impl Steps {
can_continue: false,
},
Step::Image {
width: 300,
height: 200,
current_fit: ContentFit::Contain,
slider: slider::State::new(),
},
Step::Scrollable,
@ -213,8 +214,9 @@ enum Step {
can_continue: bool,
},
Image {
width: u16,
height: u16,
slider: slider::State,
current_fit: ContentFit,
},
Scrollable,
TextInput {
@ -234,7 +236,8 @@ pub enum StepMessage {
TextSizeChanged(u16),
TextColorChanged(Color),
LanguageSelected(Language),
ImageWidthChanged(u16),
ImageHeightChanged(u16),
ImageFitSelected(ContentFit),
InputChanged(String),
ToggleSecureInput(bool),
DebugToggled(bool),
@ -279,9 +282,14 @@ impl<'a> Step {
*spacing = new_spacing;
}
}
StepMessage::ImageWidthChanged(new_width) => {
if let Step::Image { width, .. } = self {
*width = new_width;
StepMessage::ImageHeightChanged(new_height) => {
if let Step::Image { height, .. } = self {
*height = new_height;
}
}
StepMessage::ImageFitSelected(fit) => {
if let Step::Image { current_fit, .. } = self {
*current_fit = fit;
}
}
StepMessage::InputChanged(new_value) => {
@ -346,7 +354,11 @@ impl<'a> Step {
color_sliders,
color,
} => Self::text(size_slider, *size, color_sliders, *color),
Step::Image { width, slider } => Self::image(*width, slider),
Step::Image {
height,
slider,
current_fit,
} => Self::image(*height, slider, *current_fit),
Step::RowsAndColumns {
layout,
spacing_slider,
@ -574,23 +586,44 @@ impl<'a> Step {
}
fn image(
width: u16,
height: u16,
slider: &'a mut slider::State,
current_fit: ContentFit,
) -> Column<'a, StepMessage> {
const FIT_MODES: [(ContentFit, &str); 3] = [
(ContentFit::Contain, "Contain"),
(ContentFit::Cover, "Cover"),
(ContentFit::Fill, "Fill"),
];
let mode_selector = FIT_MODES.iter().fold(
Column::new().padding(10).spacing(20),
|choices, (mode, name)| {
choices.push(Radio::new(
*mode,
*name,
Some(current_fit),
StepMessage::ImageFitSelected,
))
},
);
Self::container("Image")
.push(Text::new("An image that tries to keep its aspect ratio."))
.push(ferris(width))
.push(Text::new("Pictures of things in all shapes and sizes!"))
.push(ferris(height, current_fit))
.push(Slider::new(
slider,
100..=500,
width,
StepMessage::ImageWidthChanged,
50..=500,
height,
StepMessage::ImageHeightChanged,
))
.push(
Text::new(format!("Width: {} px", width.to_string()))
Text::new(format!("Height: {} px", height))
.width(Length::Fill)
.horizontal_alignment(alignment::Horizontal::Center),
)
.push(Text::new("Pick a content fit strategy:"))
.push(mode_selector)
}
fn scrollable() -> Column<'a, StepMessage> {
@ -613,7 +646,7 @@ impl<'a> Step {
.horizontal_alignment(alignment::Horizontal::Center),
)
.push(Column::new().height(Length::Units(4096)))
.push(ferris(300))
.push(ferris(200, ContentFit::Contain))
.push(
Text::new("You made it!")
.width(Length::Fill)
@ -699,7 +732,10 @@ impl<'a> Step {
}
}
fn ferris<'a>(width: u16) -> Container<'a, StepMessage> {
fn ferris<'a>(
height: u16,
content_fit: ContentFit,
) -> Container<'a, StepMessage> {
Container::new(
// This should go away once we unify resource loading on native
// platforms
@ -708,10 +744,11 @@ fn ferris<'a>(width: u16) -> Container<'a, StepMessage> {
} else {
Image::new(format!(
"{}/images/ferris.png",
env!("CARGO_MANIFEST_DIR")
env!("CARGO_MANIFEST_DIR"),
))
}
.width(Length::Units(width)),
.height(Length::Units(height))
.content_fit(content_fit),
)
.width(Length::Fill)
.center_x()
@ -783,7 +820,8 @@ pub enum Layout {
}
mod style {
use iced::{button, Background, Color, Vector};
use iced::button;
use iced::{Background, Color, Vector};
pub enum Button {
Primary,

View file

@ -9,10 +9,9 @@ use crate::{Backend, Primitive};
use iced_native::layout;
use iced_native::mouse;
use iced_native::{
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;
use std::marker::PhantomData;
pub mod event;
@ -226,14 +225,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, P, B> From<Canvas<Message, P>>

View file

@ -5,8 +5,7 @@ use crate::Backend;
use iced_native::layout;
use iced_native::{
Color, Element, Hasher, Layout, Length, Point, Rectangle, Size, Vector,
Widget,
Color, Element, Layout, Length, Point, Rectangle, Size, Vector, Widget,
};
use thiserror::Error;
@ -74,12 +73,6 @@ where
))
}
fn hash_layout(&self, state: &mut Hasher) {
use std::hash::Hash;
self.state.contents.hash(state);
}
fn draw(
&self,
renderer: &mut Renderer<B>,

View file

@ -7,12 +7,11 @@ use iced_native::mouse;
use iced_native::overlay;
use iced_native::renderer;
use iced_native::{
Clipboard, Element, Hasher, Length, Point, Rectangle, Shell, Size, Widget,
Clipboard, Element, Length, Point, Rectangle, Shell, Size, Widget,
};
use ouroboros::self_referencing;
use std::cell::RefCell;
use std::hash::Hash;
use std::marker::PhantomData;
/// A reusable, custom widget that uses The Elm Architecture.
@ -217,12 +216,6 @@ where
});
}
fn hash_layout(&self, state: &mut Hasher) {
self.with_element(|element| {
element.hash_layout(state);
});
}
fn mouse_interaction(
&self,
layout: Layout<'_>,
@ -371,18 +364,6 @@ where
.unwrap_or_default()
}
fn hash_layout(&self, state: &mut Hasher, position: Point) {
struct Marker;
std::any::TypeId::of::<Marker>().hash(state);
(position.x as u32).hash(state);
(position.y as u32).hash(state);
self.with_overlay_maybe(|overlay| {
overlay.hash_layout(state);
});
}
fn on_event(
&mut self,
event: iced_native::Event,

View file

@ -4,13 +4,12 @@ use iced_native::layout::{self, Layout};
use iced_native::mouse;
use iced_native::overlay;
use iced_native::renderer;
use iced_native::{Clipboard, Hasher, Length, Point, Rectangle, Shell, Size};
use iced_native::{Clipboard, Length, Point, Rectangle, Shell, Size};
use iced_pure::widget::tree::{self, Tree};
use iced_pure::{Element, Widget};
use ouroboros::self_referencing;
use std::cell::{Ref, RefCell};
use std::hash::Hash;
use std::marker::PhantomData;
/// A reusable, custom widget that uses The Elm Architecture.
@ -250,12 +249,6 @@ where
});
}
fn hash_layout(&self, state: &mut Hasher) {
self.with_element(|element| {
element.as_widget().hash_layout(state);
});
}
fn mouse_interaction(
&self,
tree: &Tree,
@ -409,18 +402,6 @@ where
.unwrap_or_default()
}
fn hash_layout(&self, state: &mut Hasher, position: Point) {
struct Marker;
std::any::TypeId::of::<Marker>().hash(state);
(position.x as u32).hash(state);
(position.y as u32).hash(state);
self.with_overlay_maybe(|overlay| {
overlay.hash_layout(state);
});
}
fn on_event(
&mut self,
event: iced_native::Event,

View file

@ -8,11 +8,10 @@ use iced_native::overlay;
use iced_native::renderer;
use iced_native::window;
use iced_native::{
Clipboard, Element, Hasher, Length, Point, Rectangle, Shell, Size, Widget,
Clipboard, Element, Length, Point, Rectangle, Shell, Size, Widget,
};
use std::cell::RefCell;
use std::hash::{Hash, Hasher as _};
use std::ops::Deref;
/// The state of a [`Responsive`] widget.
@ -20,7 +19,6 @@ use std::ops::Deref;
pub struct State {
last_size: Option<Size>,
last_layout: layout::Node,
last_layout_hash: u64,
}
impl State {
@ -76,8 +74,6 @@ where
Length::Fill
}
fn hash_layout(&self, _hasher: &mut Hasher) {}
fn layout(
&self,
_renderer: &Renderer,
@ -178,7 +174,7 @@ where
let content_layout = state.layout(layout);
match content {
Content::Pending(_) => false,
Content::Pending(_) => None,
Content::Ready(cache) => {
*cache = Some(
CacheBuilder {
@ -190,14 +186,19 @@ where
.build(),
);
cache.as_ref().unwrap().borrow_overlay().is_some()
cache
.as_ref()
.unwrap()
.borrow_overlay()
.as_ref()
.map(|overlay| overlay.position())
}
}
};
has_overlay.then(|| {
has_overlay.map(|position| {
overlay::Element::new(
layout.position(),
position,
Box::new(Overlay { instance: self }),
)
})
@ -265,26 +266,13 @@ where
let element =
view.take().unwrap()(state.last_size.unwrap_or(Size::ZERO));
let new_layout_hash = {
let mut hasher = Hasher::default();
element.hash_layout(&mut hasher);
hasher.finish()
};
if state.last_size != Some(state.last_layout.size())
|| new_layout_hash != state.last_layout_hash
{
state.last_layout = element.layout(
renderer.deref(),
&layout::Limits::new(
Size::ZERO,
state.last_size.unwrap_or(Size::ZERO),
),
);
state.last_layout_hash = new_layout_hash;
}
state.last_layout = element.layout(
renderer.deref(),
&layout::Limits::new(
Size::ZERO,
state.last_size.unwrap_or(Size::ZERO),
),
);
*self = Content::Ready(Some(
CacheBuilder {
@ -395,18 +383,6 @@ where
.unwrap_or_default()
}
fn hash_layout(&self, state: &mut Hasher, position: Point) {
struct Marker;
std::any::TypeId::of::<Marker>().hash(state);
(position.x as u32).hash(state);
(position.y as u32).hash(state);
self.with_overlay_maybe(|overlay| {
overlay.hash_layout(state);
});
}
fn on_event(
&mut self,
event: iced_native::Event,

View file

@ -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<'_>,

View file

@ -71,8 +71,8 @@ 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};

View file

@ -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:

View file

@ -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);
}
}

View file

@ -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,

View file

@ -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,24 +345,8 @@ 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 layer = Self::overlay_layer(
self.overlay.take(),
self.bounds,
&mut overlay,
renderer,
);
Some(layer)
} else {
None
};
if let Some(layer) = &self.overlay {
let base_cursor = if layer.layout.bounds().contains(cursor_position)
{
if let Some(layout) = &self.overlay {
let base_cursor = if layout.bounds().contains(cursor_position) {
Point::new(-1.0, -1.0)
} else {
cursor_position
@ -417,7 +355,7 @@ where
self.root.widget.draw(
renderer,
&renderer::Style::default(),
Layout::new(&self.base.layout),
Layout::new(&self.base),
base_cursor,
&viewport,
);
@ -425,14 +363,14 @@ where
self.root.widget.draw(
renderer,
&renderer::Style::default(),
Layout::new(&self.base.layout),
Layout::new(&self.base),
cursor_position,
&viewport,
);
};
let base_interaction = self.root.widget.mouse_interaction(
Layout::new(&self.base.layout),
Layout::new(&self.base),
cursor_position,
&viewport,
renderer,
@ -452,34 +390,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 +423,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,
}
Cache
}
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,
}
}
}
#[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 +443,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
}
}

View file

@ -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:

View file

@ -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.
@ -384,16 +382,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.content.hash_layout(state);
}
fn overlay(
&mut self,
layout: Layout<'_>,

View file

@ -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>>

View file

@ -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<'_>,

View file

@ -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;
@ -233,21 +231,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<'_>,

View file

@ -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
}
}
}
/// Computes the layout of an [`Image`].
@ -58,44 +72,37 @@ pub fn layout<Renderer, Handle>(
handle: &Handle,
width: Length,
height: Length,
content_fit: ContentFit,
) -> layout::Node
where
Renderer: image::Renderer<Handle = Handle>,
{
let (original_width, original_height) = renderer.dimensions(handle);
// The raw w/h of the underlying image
let image_size = {
let (width, height) = renderer.dimensions(handle);
let mut size = limits
.width(width)
.height(height)
.resolve(Size::new(original_width as f32, original_height as f32));
Size::new(width as f32, height as f32)
};
let aspect_ratio = original_width as f32 / original_height as f32;
let viewport_aspect_ratio = size.width / size.height;
// The size to be available to the widget prior to `Shrink`ing
let raw_size = limits.width(width).height(height).resolve(image_size);
if viewport_aspect_ratio > aspect_ratio {
size.width =
original_width as f32 * size.height / original_height as f32;
} else {
size.height =
original_height as f32 * size.width / original_width as f32;
}
// The uncropped size of the image when fit to the bounds above
let full_size = content_fit.fit(image_size, raw_size);
layout::Node::new(size)
}
// Shrink the widget to fit the resized image, if requested
let final_size = Size {
width: match width {
Length::Shrink => f32::min(raw_size.width, full_size.width),
_ => raw_size.width,
},
height: match height {
Length::Shrink => f32::min(raw_size.height, full_size.height),
_ => raw_size.height,
},
};
/// Hashes the layout attributes of an [`Image`].
pub fn hash_layout<Handle: Hash>(
state: &mut Hasher,
handle: &Handle,
width: Length,
height: Length,
) {
struct Marker;
std::any::TypeId::of::<Marker>().hash(state);
handle.hash(state);
width.hash(state);
height.hash(state);
layout::Node::new(final_size)
}
impl<Message, Renderer, Handle> Widget<Message, Renderer> for Image<Handle>
@ -116,7 +123,14 @@ where
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
layout(renderer, limits, &self.handle, self.width, self.height)
layout(
renderer,
limits,
&self.handle,
self.width,
self.height,
self.content_fit,
)
}
fn draw(
@ -127,11 +141,34 @@ where
_cursor_position: Point,
_viewport: &Rectangle,
) {
renderer.draw(self.handle.clone(), layout.bounds());
}
let (width, height) = renderer.dimensions(&self.handle);
let image_size = Size::new(width as f32, height as f32);
fn hash_layout(&self, state: &mut Hasher) {
hash_layout(state, &self.handle, self.width, self.height)
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)
}
}
}

View file

@ -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`].

View file

@ -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<'_>,

View file

@ -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<'_>,

View file

@ -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);
}
}

View file

@ -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,

View file

@ -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;
@ -194,40 +194,6 @@ where
layout::Node::new(size)
}
/// Hashes the layout attributes of a [`PickList`].
pub fn hash_layout<T>(
state: &mut Hasher,
width: Length,
padding: Padding,
text_size: Option<u16>,
placeholder: Option<&str>,
options: &[T],
) where
T: ToString,
{
use std::hash::Hash as _;
struct Marker;
std::any::TypeId::of::<Marker>().hash(state);
padding.hash(state);
text_size.hash(state);
match width {
Length::Shrink => {
placeholder.hash(state);
options
.iter()
.map(ToString::to_string)
.for_each(|label| label.hash(state));
}
_ => {
width.hash(state);
}
}
}
/// Processes an [`Event`] and updates the [`State`] of a [`PickList`]
/// accordingly.
pub fn update<'a, T, Message>(
@ -487,17 +453,6 @@ where
)
}
fn hash_layout(&self, state: &mut Hasher) {
hash_layout(
state,
self.width,
self.padding,
self.text_size,
self.placeholder.as_ref().map(String::as_str),
&self.options,
)
}
fn on_event(
&mut self,
event: Event,

View file

@ -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>>

View file

@ -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>>

View file

@ -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<'_>,

View file

@ -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>

View file

@ -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;
@ -721,16 +721,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<'_>,

View file

@ -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};
@ -344,14 +343,6 @@ pub fn mouse_interaction(
}
}
/// Hashes the layout of a [`Slider`].
pub fn hash_layout(state: &mut Hasher, width: Length) {
struct Marker;
std::any::TypeId::of::<Marker>().hash(state);
width.hash(state);
}
/// The local state of a [`Slider`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct State {
@ -444,10 +435,6 @@ where
) -> mouse::Interaction {
mouse_interaction(layout, cursor_position, &self.state)
}
fn hash_layout(&self, state: &mut Hasher) {
hash_layout(state, self.width)
}
}
impl<'a, T, Message, Renderer> From<Slider<'a, T, Message>>

View file

@ -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>

View file

@ -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));
.resolve(image_size);
let viewport_aspect_ratio = size.width / size.height;
// The uncropped size of the image when fit to the bounds above
let full_size = self.content_fit.fit(image_size, raw_size);
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;
}
// 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(size)
layout::Node::new(final_size)
}
fn draw(
@ -94,15 +115,34 @@ where
_cursor_position: Point,
_viewport: &Rectangle,
) {
renderer.draw(self.handle.clone(), layout.bounds())
}
let (width, height) = renderer.dimensions(&self.handle);
let image_size = Size::new(width as f32, height as f32);
fn hash_layout(&self, state: &mut Hasher) {
std::any::TypeId::of::<Svg>().hash(state);
let bounds = layout.bounds();
let adjusted_fit = self.content_fit.fit(image_size, bounds.size());
self.handle.hash(state);
self.width.hash(state);
self.height.hash(state);
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)
}
}
}

View file

@ -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.

View file

@ -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,
};
pub use iced_style::text_input::{Style, StyleSheet};
@ -725,21 +725,6 @@ pub fn mouse_interaction(
}
}
/// Hashes the layout attributes of a [`TextInput`].
pub fn hash_layout(
state: &mut Hasher,
width: Length,
padding: Padding,
size: Option<u16>,
) {
use std::hash::Hash;
std::any::TypeId::of::<State>().hash(state);
width.hash(state);
padding.hash(state);
size.hash(state);
}
impl<'a, Message, Renderer> Widget<Message, Renderer>
for TextInput<'a, Message, Renderer>
where
@ -819,10 +804,6 @@ where
self.style_sheet.as_ref(),
)
}
fn hash_layout(&self, state: &mut Hasher) {
hash_layout(state, self.width, self.padding, self.size)
}
}
impl<'a, Message, Renderer> From<TextInput<'a, Message, Renderer>>

View file

@ -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>>

View file

@ -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>>

View file

@ -9,7 +9,7 @@ use iced_native::event::{self, Event};
use iced_native::layout::{self, Layout};
use iced_native::mouse;
use iced_native::renderer;
use iced_native::{Clipboard, Hasher, Length, Point, Rectangle, Shell};
use iced_native::{Clipboard, Length, Point, Rectangle, Shell};
pub struct Pure<'a, Message, Renderer> {
state: &'a mut State,
@ -65,10 +65,6 @@ where
self.element.as_widget().height()
}
fn hash_layout(&self, state: &mut Hasher) {
self.element.as_widget().hash_layout(state)
}
fn layout(
&self,
renderer: &Renderer,

View file

@ -38,7 +38,7 @@ use iced_native::layout::{self, Layout};
use iced_native::mouse;
use iced_native::overlay;
use iced_native::renderer;
use iced_native::{Clipboard, Hasher, Length, Point, Rectangle, Shell};
use iced_native::{Clipboard, Length, Point, Rectangle, Shell};
use std::borrow::Cow;
@ -47,8 +47,6 @@ pub trait Widget<Message, Renderer> {
fn height(&self) -> Length;
fn hash_layout(&self, state: &mut Hasher);
fn layout(
&self,
renderer: &Renderer,

View file

@ -8,7 +8,7 @@ use iced_native::mouse;
use iced_native::renderer;
use iced_native::widget::button;
use iced_native::{
Clipboard, Hasher, Layout, Length, Padding, Point, Rectangle, Shell,
Clipboard, Layout, Length, Padding, Point, Rectangle, Shell,
};
use iced_style::button::StyleSheet;
@ -101,16 +101,6 @@ where
self.height
}
fn hash_layout(&self, state: &mut Hasher) {
use std::hash::Hash;
self.tag().hash(state);
self.width.hash(state);
self.height.hash(state);
self.padding.hash(state);
self.content.as_widget().hash_layout(state);
}
fn layout(
&self,
renderer: &Renderer,

View file

@ -5,7 +5,7 @@ use iced_native::layout::{self, Layout};
use iced_native::mouse;
use iced_native::renderer;
use iced_native::text;
use iced_native::{Clipboard, Hasher, Length, Point, Rectangle, Shell};
use iced_native::{Clipboard, Length, Point, Rectangle, Shell};
pub use iced_native::widget::Checkbox;
@ -88,12 +88,6 @@ where
renderer,
)
}
fn hash_layout(&self, state: &mut Hasher) {
<Self as iced_native::Widget<Message, Renderer>>::hash_layout(
self, state,
)
}
}
impl<'a, Message, Renderer> Into<Element<'a, Message, Renderer>>

View file

@ -7,7 +7,7 @@ use iced_native::layout::{self, Layout};
use iced_native::mouse;
use iced_native::renderer;
use iced_native::{
Alignment, Clipboard, Hasher, Length, Padding, Point, Rectangle, Shell,
Alignment, Clipboard, Length, Padding, Point, Rectangle, Shell,
};
use std::u32;
@ -202,22 +202,6 @@ where
}
}
fn hash_layout(&self, state: &mut Hasher) {
use std::hash::Hash;
self.tag().hash(state);
self.width.hash(state);
self.height.hash(state);
self.max_width.hash(state);
self.align_items.hash(state);
self.spacing.hash(state);
self.padding.hash(state);
for child in &self.children {
child.as_widget().hash_layout(state);
}
}
fn overlay<'b>(
&'b self,
tree: &'b mut Tree,

View file

@ -9,10 +9,9 @@ use iced_native::overlay;
use iced_native::renderer;
use iced_native::widget::container;
use iced_native::{
Clipboard, Hasher, Layout, Length, Padding, Point, Rectangle, Shell,
Clipboard, Layout, Length, Padding, Point, Rectangle, Shell,
};
use std::hash::Hash;
use std::u32;
pub use iced_style::container::{Style, StyleSheet};
@ -224,21 +223,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.as_widget().hash_layout(state);
}
fn overlay<'b>(
&'b self,
tree: &'b mut Tree,

View file

@ -5,7 +5,7 @@ use iced_native::event::{self, Event};
use iced_native::layout::{self, Layout};
use iced_native::mouse;
use iced_native::renderer;
use iced_native::{Clipboard, Hasher, Length, Point, Rectangle, Shell};
use iced_native::{Clipboard, Length, Point, Rectangle, Shell};
pub struct Element<'a, Message, Renderer> {
widget: Box<dyn Widget<Message, Renderer> + 'a>,
@ -160,8 +160,4 @@ where
renderer,
)
}
fn hash_layout(&self, state: &mut Hasher) {
self.widget.hash_layout(state);
}
}

View file

@ -4,7 +4,7 @@ use crate::Element;
use iced_native::layout::{self, Layout};
use iced_native::renderer;
use iced_native::widget::image;
use iced_native::{Hasher, Length, Point, Rectangle};
use iced_native::{Length, Point, Rectangle};
use std::hash::Hash;
@ -51,12 +51,6 @@ where
viewport,
)
}
fn hash_layout(&self, state: &mut Hasher) {
<Self as iced_native::Widget<Message, Renderer>>::hash_layout(
self, state,
)
}
}
impl<'a, Message, Renderer, Handle> Into<Element<'a, Message, Renderer>>

View file

@ -10,7 +10,7 @@ use iced_native::renderer;
use iced_native::text;
use iced_native::widget::pick_list;
use iced_native::{
Clipboard, Hasher, Layout, Length, Padding, Point, Rectangle, Shell,
Clipboard, Layout, Length, Padding, Point, Rectangle, Shell,
};
use std::borrow::Cow;
@ -145,17 +145,6 @@ where
)
}
fn hash_layout(&self, state: &mut Hasher) {
pick_list::hash_layout(
state,
self.width,
self.padding,
self.text_size,
self.placeholder.as_ref().map(String::as_str),
&self.options,
)
}
fn on_event(
&mut self,
tree: &mut Tree,

View file

@ -5,7 +5,7 @@ use iced_native::layout::{self, Layout};
use iced_native::mouse;
use iced_native::renderer;
use iced_native::text;
use iced_native::{Clipboard, Hasher, Length, Point, Rectangle, Shell};
use iced_native::{Clipboard, Length, Point, Rectangle, Shell};
pub use iced_native::widget::Radio;
@ -89,12 +89,6 @@ where
renderer,
)
}
fn hash_layout(&self, state: &mut Hasher) {
<Self as iced_native::Widget<Message, Renderer>>::hash_layout(
self, state,
)
}
}
impl<'a, Message, Renderer> Into<Element<'a, Message, Renderer>>

View file

@ -7,7 +7,7 @@ use iced_native::layout::{self, Layout};
use iced_native::mouse;
use iced_native::renderer;
use iced_native::{
Alignment, Clipboard, Hasher, Length, Padding, Point, Rectangle, Shell,
Alignment, Clipboard, Length, Padding, Point, Rectangle, Shell,
};
pub struct Row<'a, Message, Renderer> {
@ -189,21 +189,6 @@ where
}
}
fn hash_layout(&self, state: &mut Hasher) {
use std::hash::Hash;
self.tag().hash(state);
self.width.hash(state);
self.height.hash(state);
self.align_items.hash(state);
self.spacing.hash(state);
self.padding.hash(state);
for child in &self.children {
child.as_widget().hash_layout(state);
}
}
fn overlay<'b>(
&'b self,
tree: &'b mut Tree,

View file

@ -7,7 +7,7 @@ use iced_native::layout::{self, Layout};
use iced_native::mouse;
use iced_native::renderer;
use iced_native::widget::scrollable;
use iced_native::{Clipboard, Hasher, Length, Point, Rectangle, Shell, Vector};
use iced_native::{Clipboard, Length, Point, Rectangle, Shell, Vector};
pub use iced_style::scrollable::StyleSheet;
@ -115,14 +115,6 @@ where
self.height
}
fn hash_layout(&self, state: &mut Hasher) {
use std::hash::Hash;
self.tag().hash(state);
self.height.hash(state);
self.content.as_widget().hash_layout(state)
}
fn layout(
&self,
renderer: &Renderer,

View file

@ -9,9 +9,7 @@ use iced_native::layout;
use iced_native::mouse;
use iced_native::renderer;
use iced_native::widget::slider;
use iced_native::{
Clipboard, Hasher, Layout, Length, Point, Rectangle, Shell, Size,
};
use iced_native::{Clipboard, Layout, Length, Point, Rectangle, Shell, Size};
use std::ops::RangeInclusive;
@ -230,10 +228,6 @@ where
tree.state.downcast_ref::<slider::State>(),
)
}
fn hash_layout(&self, state: &mut Hasher) {
slider::hash_layout(state, self.width)
}
}
impl<'a, T, Message, Renderer> From<Slider<'a, T, Message>>

View file

@ -4,7 +4,7 @@ use iced_native::event::{self, Event};
use iced_native::layout::{self, Layout};
use iced_native::mouse;
use iced_native::renderer;
use iced_native::{Clipboard, Hasher, Length, Point, Rectangle, Shell};
use iced_native::{Clipboard, Length, Point, Rectangle, Shell};
pub use iced_native::widget::Space;
@ -86,12 +86,6 @@ where
renderer,
)
}
fn hash_layout(&self, state: &mut Hasher) {
<Self as iced_native::Widget<Message, Renderer>>::hash_layout(
self, state,
)
}
}
impl<'a, Message, Renderer> Into<Element<'a, Message, Renderer>> for Space

View file

@ -3,7 +3,7 @@ use crate::{Element, Tree, Widget};
use iced_native::layout::{self, Layout};
use iced_native::renderer;
use iced_native::text;
use iced_native::{Hasher, Length, Point, Rectangle};
use iced_native::{Length, Point, Rectangle};
pub use iced_native::widget::Text;
@ -47,12 +47,6 @@ where
viewport,
)
}
fn hash_layout(&self, state: &mut Hasher) {
<Self as iced_native::Widget<Message, Renderer>>::hash_layout(
self, state,
)
}
}
impl<'a, Message, Renderer> Into<Element<'a, Message, Renderer>>

View file

@ -7,9 +7,7 @@ use iced_native::mouse;
use iced_native::renderer;
use iced_native::text;
use iced_native::widget::text_input;
use iced_native::{
Clipboard, Hasher, Length, Padding, Point, Rectangle, Shell,
};
use iced_native::{Clipboard, Length, Padding, Point, Rectangle, Shell};
pub use iced_style::text_input::StyleSheet;
@ -153,10 +151,6 @@ where
Length::Shrink
}
fn hash_layout(&self, state: &mut Hasher) {
text_input::hash_layout(state, self.width, self.padding, self.size);
}
fn layout(
&self,
renderer: &Renderer,

View file

@ -6,7 +6,7 @@ use iced_native::layout::{self, Layout};
use iced_native::mouse;
use iced_native::renderer;
use iced_native::text;
use iced_native::{Clipboard, Hasher, Length, Point, Rectangle, Shell};
use iced_native::{Clipboard, Length, Point, Rectangle, Shell};
pub use iced_native::widget::toggler::{Style, StyleSheet, Toggler};
@ -23,12 +23,6 @@ where
<Self as iced_native::Widget<Message, Renderer>>::height(self)
}
fn hash_layout(&self, state: &mut Hasher) {
<Self as iced_native::Widget<Message, Renderer>>::hash_layout(
self, state,
)
}
fn layout(
&self,
renderer: &Renderer,

View file

@ -225,6 +225,6 @@ pub use settings::Settings;
pub use runtime::alignment;
pub use runtime::futures;
pub use runtime::{
Alignment, Background, Color, Command, Font, Length, Point, Rectangle,
Size, Subscription, Vector,
Alignment, Background, Color, Command, ContentFit, Font, Length, Point,
Rectangle, Size, Subscription, Vector,
};