summaryrefslogtreecommitdiffstats
path: root/examples/custom_widget/src
diff options
context:
space:
mode:
Diffstat (limited to 'examples/custom_widget/src')
-rw-r--r--examples/custom_widget/src/main.rs65
1 files changed, 36 insertions, 29 deletions
diff --git a/examples/custom_widget/src/main.rs b/examples/custom_widget/src/main.rs
index 0a570745..36f468c7 100644
--- a/examples/custom_widget/src/main.rs
+++ b/examples/custom_widget/src/main.rs
@@ -9,23 +9,26 @@ mod circle {
// Of course, you can choose to make the implementation renderer-agnostic,
// if you wish to, by creating your own `Renderer` trait, which could be
// implemented by `iced_wgpu` and other renderers.
+ use iced_graphics::{Backend, Defaults, Primitive, Renderer};
use iced_native::{
- layout, Background, Color, Element, Hasher, Layout, Length,
- MouseCursor, Point, Size, Widget,
+ layout, mouse, Background, Color, Element, Hasher, Layout, Length,
+ Point, Rectangle, Size, Widget,
};
- use iced_wgpu::{Defaults, Primitive, Renderer};
pub struct Circle {
- radius: u16,
+ radius: f32,
}
impl Circle {
- pub fn new(radius: u16) -> Self {
+ pub fn new(radius: f32) -> Self {
Self { radius }
}
}
- impl<Message> Widget<Message, Renderer> for Circle {
+ impl<Message, B> Widget<Message, Renderer<B>> for Circle
+ where
+ B: Backend,
+ {
fn width(&self) -> Length {
Length::Shrink
}
@@ -36,43 +39,44 @@ mod circle {
fn layout(
&self,
- _renderer: &Renderer,
+ _renderer: &Renderer<B>,
_limits: &layout::Limits,
) -> layout::Node {
- layout::Node::new(Size::new(
- f32::from(self.radius) * 2.0,
- f32::from(self.radius) * 2.0,
- ))
+ 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.hash(state);
+ self.radius.to_bits().hash(state);
}
fn draw(
&self,
- _renderer: &mut Renderer,
+ _renderer: &mut Renderer<B>,
_defaults: &Defaults,
layout: Layout<'_>,
_cursor_position: Point,
- ) -> (Primitive, MouseCursor) {
+ _viewport: &Rectangle,
+ ) -> (Primitive, mouse::Interaction) {
(
Primitive::Quad {
bounds: layout.bounds(),
background: Background::Color(Color::BLACK),
border_radius: self.radius,
- border_width: 0,
+ border_width: 0.0,
border_color: Color::TRANSPARENT,
},
- MouseCursor::OutOfBounds,
+ mouse::Interaction::default(),
)
}
}
- impl<'a, Message> Into<Element<'a, Message, Renderer>> for Circle {
- fn into(self) -> Element<'a, Message, Renderer> {
+ impl<'a, Message, B> Into<Element<'a, Message, Renderer<B>>> for Circle
+ where
+ B: Backend,
+ {
+ fn into(self) -> Element<'a, Message, Renderer<B>> {
Element::new(self)
}
}
@@ -84,12 +88,12 @@ use iced::{
Slider, Text,
};
-pub fn main() {
+pub fn main() -> iced::Result {
Example::run(Settings::default())
}
struct Example {
- radius: u16,
+ radius: f32,
slider: slider::State,
}
@@ -103,7 +107,7 @@ impl Sandbox for Example {
fn new() -> Self {
Example {
- radius: 50,
+ radius: 50.0,
slider: slider::State::new(),
}
}
@@ -115,7 +119,7 @@ impl Sandbox for Example {
fn update(&mut self, message: Message) {
match message {
Message::RadiusChanged(radius) => {
- self.radius = radius.round() as u16;
+ self.radius = radius;
}
}
}
@@ -127,13 +131,16 @@ impl Sandbox for Example {
.max_width(500)
.align_items(Align::Center)
.push(Circle::new(self.radius))
- .push(Text::new(format!("Radius: {}", self.radius.to_string())))
- .push(Slider::new(
- &mut self.slider,
- 1.0..=100.0,
- f32::from(self.radius),
- Message::RadiusChanged,
- ));
+ .push(Text::new(format!("Radius: {:.2}", self.radius)))
+ .push(
+ Slider::new(
+ &mut self.slider,
+ 1.0..=100.0,
+ self.radius,
+ Message::RadiusChanged,
+ )
+ .step(0.01),
+ );
Container::new(content)
.width(Length::Fill)