Implement Image in iced_pure
This commit is contained in:
parent
09c96a6d81
commit
45455be450
4 changed files with 134 additions and 25 deletions
|
|
@ -51,6 +51,53 @@ impl<Handle> Image<Handle> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Computes the layout of an [`Image`].
|
||||||
|
pub fn layout<Renderer, Handle>(
|
||||||
|
renderer: &Renderer,
|
||||||
|
limits: &layout::Limits,
|
||||||
|
handle: &Handle,
|
||||||
|
width: Length,
|
||||||
|
height: Length,
|
||||||
|
) -> layout::Node
|
||||||
|
where
|
||||||
|
Renderer: image::Renderer<Handle = Handle>,
|
||||||
|
{
|
||||||
|
let (original_width, original_height) = renderer.dimensions(handle);
|
||||||
|
|
||||||
|
let mut size = limits
|
||||||
|
.width(width)
|
||||||
|
.height(height)
|
||||||
|
.resolve(Size::new(original_width as f32, original_height as f32));
|
||||||
|
|
||||||
|
let aspect_ratio = original_width as f32 / original_height as f32;
|
||||||
|
let viewport_aspect_ratio = size.width / size.height;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
layout::Node::new(size)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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);
|
||||||
|
}
|
||||||
|
|
||||||
impl<Message, Renderer, Handle> Widget<Message, Renderer> for Image<Handle>
|
impl<Message, Renderer, Handle> Widget<Message, Renderer> for Image<Handle>
|
||||||
where
|
where
|
||||||
Renderer: image::Renderer<Handle = Handle>,
|
Renderer: image::Renderer<Handle = Handle>,
|
||||||
|
|
@ -69,24 +116,7 @@ where
|
||||||
renderer: &Renderer,
|
renderer: &Renderer,
|
||||||
limits: &layout::Limits,
|
limits: &layout::Limits,
|
||||||
) -> layout::Node {
|
) -> layout::Node {
|
||||||
let (width, height) = renderer.dimensions(&self.handle);
|
layout(renderer, limits, &self.handle, self.width, self.height)
|
||||||
|
|
||||||
let aspect_ratio = width as f32 / height as f32;
|
|
||||||
|
|
||||||
let mut 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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw(
|
fn draw(
|
||||||
|
|
@ -101,12 +131,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hash_layout(&self, state: &mut Hasher) {
|
fn hash_layout(&self, state: &mut Hasher) {
|
||||||
struct Marker;
|
hash_layout(state, &self.handle, self.width, self.height)
|
||||||
std::any::TypeId::of::<Marker>().hash(state);
|
|
||||||
|
|
||||||
self.handle.hash(state);
|
|
||||||
self.width.hash(state);
|
|
||||||
self.height.hash(state);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
pub mod image;
|
||||||
|
|
||||||
mod button;
|
mod button;
|
||||||
mod checkbox;
|
mod checkbox;
|
||||||
mod column;
|
mod column;
|
||||||
|
|
@ -14,6 +16,7 @@ pub use checkbox::Checkbox;
|
||||||
pub use column::Column;
|
pub use column::Column;
|
||||||
pub use container::Container;
|
pub use container::Container;
|
||||||
pub use element::Element;
|
pub use element::Element;
|
||||||
|
pub use image::Image;
|
||||||
pub use row::Row;
|
pub use row::Row;
|
||||||
pub use scrollable::Scrollable;
|
pub use scrollable::Scrollable;
|
||||||
pub use text::Text;
|
pub use text::Text;
|
||||||
|
|
@ -145,3 +148,7 @@ where
|
||||||
{
|
{
|
||||||
TextInput::new(placeholder, value, on_change)
|
TextInput::new(placeholder, value, on_change)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn image<Handle>(handle: Handle) -> Image<Handle> {
|
||||||
|
Image::new(handle)
|
||||||
|
}
|
||||||
|
|
|
||||||
74
pure/src/widget/image.rs
Normal file
74
pure/src/widget/image.rs
Normal file
|
|
@ -0,0 +1,74 @@
|
||||||
|
use crate::widget::{Tree, Widget};
|
||||||
|
|
||||||
|
use iced_native::layout::{self, Layout};
|
||||||
|
use iced_native::renderer;
|
||||||
|
use iced_native::widget::image;
|
||||||
|
use iced_native::{Hasher, Length, Point, Rectangle};
|
||||||
|
|
||||||
|
use std::any::{self, Any};
|
||||||
|
use std::hash::Hash;
|
||||||
|
|
||||||
|
pub use image::Image;
|
||||||
|
|
||||||
|
impl<Message, Renderer, Handle> Widget<Message, Renderer> for Image<Handle>
|
||||||
|
where
|
||||||
|
Handle: Clone + Hash,
|
||||||
|
Renderer: iced_native::image::Renderer<Handle = Handle>,
|
||||||
|
{
|
||||||
|
fn tag(&self) -> any::TypeId {
|
||||||
|
any::TypeId::of::<()>()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn state(&self) -> Box<dyn Any> {
|
||||||
|
Box::new(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn children_state(&self) -> Vec<Tree> {
|
||||||
|
Vec::new()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn diff(&self, _tree: &mut Tree) {}
|
||||||
|
|
||||||
|
fn width(&self) -> Length {
|
||||||
|
<Self as iced_native::Widget<Message, Renderer>>::width(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn height(&self) -> Length {
|
||||||
|
<Self as iced_native::Widget<Message, Renderer>>::height(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn layout(
|
||||||
|
&self,
|
||||||
|
renderer: &Renderer,
|
||||||
|
limits: &layout::Limits,
|
||||||
|
) -> layout::Node {
|
||||||
|
<Self as iced_native::Widget<Message, Renderer>>::layout(
|
||||||
|
self, renderer, limits,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw(
|
||||||
|
&self,
|
||||||
|
_tree: &Tree,
|
||||||
|
renderer: &mut Renderer,
|
||||||
|
style: &renderer::Style,
|
||||||
|
layout: Layout<'_>,
|
||||||
|
cursor_position: Point,
|
||||||
|
viewport: &Rectangle,
|
||||||
|
) {
|
||||||
|
<Self as iced_native::Widget<Message, Renderer>>::draw(
|
||||||
|
self,
|
||||||
|
renderer,
|
||||||
|
style,
|
||||||
|
layout,
|
||||||
|
cursor_position,
|
||||||
|
viewport,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hash_layout(&self, state: &mut Hasher) {
|
||||||
|
<Self as iced_native::Widget<Message, Renderer>>::hash_layout(
|
||||||
|
self, state,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -17,7 +17,7 @@
|
||||||
//! [the original widgets]: crate::widget
|
//! [the original widgets]: crate::widget
|
||||||
//! [`button::State`]: crate::widget::button::State
|
//! [`button::State`]: crate::widget::button::State
|
||||||
//! [impure `Application`]: crate::Application
|
//! [impure `Application`]: crate::Application
|
||||||
pub use iced_pure::{Element as _, Text as _, *};
|
pub use iced_pure::{Element as _, Image as _, Text as _, *};
|
||||||
|
|
||||||
/// A generic, pure [`Widget`].
|
/// A generic, pure [`Widget`].
|
||||||
pub type Element<'a, Message> =
|
pub type Element<'a, Message> =
|
||||||
|
|
@ -26,6 +26,9 @@ pub type Element<'a, Message> =
|
||||||
/// A pure text widget.
|
/// A pure text widget.
|
||||||
pub type Text = iced_pure::Text<crate::Renderer>;
|
pub type Text = iced_pure::Text<crate::Renderer>;
|
||||||
|
|
||||||
|
/// A pure image widget.
|
||||||
|
pub type Image = iced_pure::Image<crate::widget::image::Handle>;
|
||||||
|
|
||||||
mod application;
|
mod application;
|
||||||
mod sandbox;
|
mod sandbox;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue