forked from iced-rs/iced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
138 lines (122 loc) · 3.56 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//! This example showcases a simple native custom widget that draws a circle.
mod circle {
// For now, to implement a custom native widget you will need to add
// `iced_native` and `iced_wgpu` to your dependencies.
//
// Then, you simply need to define your widget type and implement the
// `iced_native::Widget` trait with the `iced_wgpu::Renderer`.
//
// 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::advanced::layout::{self, Layout};
use iced::advanced::renderer;
use iced::advanced::widget::{self, Widget};
use iced::mouse;
use iced::{Border, Color, Element, Length, Rectangle, Size};
pub struct Circle {
radius: f32,
}
impl Circle {
pub fn new(radius: f32) -> Self {
Self { radius }
}
}
pub fn circle(radius: f32) -> Circle {
Circle::new(radius)
}
impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer> for Circle
where
Renderer: renderer::Renderer,
{
fn size(&self) -> Size<Length> {
Size {
width: Length::Shrink,
height: Length::Shrink,
}
}
fn layout(
&self,
_tree: &mut widget::Tree,
_renderer: &Renderer,
_limits: &layout::Limits,
) -> layout::Node {
layout::Node::new(Size::new(self.radius * 2.0, self.radius * 2.0))
}
fn draw(
&self,
_state: &widget::Tree,
renderer: &mut Renderer,
_theme: &Theme,
_style: &renderer::Style,
layout: Layout<'_>,
_cursor: mouse::Cursor,
_viewport: &Rectangle,
) {
renderer.fill_quad(
renderer::Quad {
bounds: layout.bounds(),
border: Border::rounded(self.radius),
..renderer::Quad::default()
},
Color::BLACK,
);
}
}
impl<'a, Message, Theme, Renderer> From<Circle>
for Element<'a, Message, Theme, Renderer>
where
Renderer: renderer::Renderer,
{
fn from(circle: Circle) -> Self {
Self::new(circle)
}
}
}
use circle::circle;
use iced::widget::{column, container, slider, text};
use iced::{Alignment, Element, Length};
pub fn main() -> iced::Result {
iced::run("Custom Widget - Iced", Example::update, Example::view)
}
struct Example {
radius: f32,
}
#[derive(Debug, Clone, Copy)]
enum Message {
RadiusChanged(f32),
}
impl Example {
fn new() -> Self {
Example { radius: 50.0 }
}
fn update(&mut self, message: Message) {
match message {
Message::RadiusChanged(radius) => {
self.radius = radius;
}
}
}
fn view(&self) -> Element<Message> {
let content = column![
circle(self.radius),
text(format!("Radius: {:.2}", self.radius)),
slider(1.0..=100.0, self.radius, Message::RadiusChanged).step(0.01),
]
.padding(20)
.spacing(20)
.max_width(500)
.align_items(Alignment::Center);
container(content)
.width(Length::Fill)
.height(Length::Fill)
.center_x()
.center_y()
.into()
}
}
impl Default for Example {
fn default() -> Self {
Self::new()
}
}