Skip to content

Commit 30ac432

Browse files
authored
Update to iced v0.10 and palette v0.7 (#1)
* Update to iced v0.10 and palette v0.7 * Override `GraphContainer::tag` to return a stateful `Tag` This appears to be necessary to prevent “Downcast on stateless state” panics.
1 parent f8b98d6 commit 30ac432

File tree

10 files changed

+1050
-792
lines changed

10 files changed

+1050
-792
lines changed

Cargo.lock

Lines changed: 895 additions & 635 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/basic/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
iced = "0.7.0"
9+
iced = "0.10.0"
1010
iced_node_editor = { path = "../../iced_node_editor"}
1111

1212
[target.'cfg(target_arch = "wasm32")'.dependencies]

examples/basic/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ impl Sandbox for Example {
109109
.center_x()
110110
.center_y()
111111
.on_translate(move |p| Message::MoveNode(i, p.0, p.1))
112-
.width(Length::Units(200))
113-
.height(Length::Units(75))
112+
.width(Length::Fixed(200.0))
113+
.height(Length::Fixed(75.0))
114114
.position(n.position)
115115
.into(),
116116
);

iced_node_editor/Cargo.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,5 @@ edition = "2021"
66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9-
iced = { version ="0.7.0" }
10-
iced_native = "0.8.0"
11-
iced_graphics = "0.6.0"
12-
palette = "0.6.1"
9+
iced = { version = "0.10.0", features = ["advanced"] }
10+
palette = "0.7.3"

iced_node_editor/src/connection.rs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
use std::sync::Mutex;
22

3-
use iced::{Length, Point, Size, Vector};
4-
use iced_graphics::triangle::{ColoredVertex2D, Mesh2D};
5-
use iced_native::{
6-
renderer::{self},
7-
Widget,
8-
};
3+
use iced::advanced::graphics::mesh::{Indexed, SolidVertex2D};
4+
use iced::advanced::renderer;
5+
use iced::{advanced::Widget, Length, Point, Size, Vector};
96

107
use crate::{
118
mesh_renderer::MeshRenderer,
@@ -72,9 +69,9 @@ where
7269
fn layout(
7370
&self,
7471
_renderer: &Renderer,
75-
_limits: &iced_native::layout::Limits,
72+
_limits: &iced::advanced::layout::Limits,
7673
scale: f32,
77-
) -> iced_native::layout::Node {
74+
) -> iced::advanced::layout::Node {
7875
let spline = generate_spline(
7976
Vector::new(self.from.x, self.from.y) * scale,
8077
1.0,
@@ -90,7 +87,7 @@ where
9087
.map(|p| Vector::new(p.x - spline_bounds.x, p.y - spline_bounds.y))
9188
.collect();
9289

93-
let node = iced_native::layout::Node::new(Size::new(
90+
let node = iced::advanced::layout::Node::new(Size::new(
9491
(spline_bounds.width + self.width).ceil(),
9592
(spline_bounds.height + self.width).ceil(),
9693
));
@@ -110,19 +107,19 @@ where
110107
fn layout(
111108
&self,
112109
_renderer: &Renderer,
113-
_limits: &iced_native::layout::Limits,
114-
) -> iced_native::layout::Node {
110+
_limits: &iced::advanced::layout::Limits,
111+
) -> iced::advanced::layout::Node {
115112
todo!("This should never be called.")
116113
}
117114

118115
fn draw(
119116
&self,
120-
_tree: &iced_native::widget::Tree,
117+
_tree: &iced::advanced::widget::Tree,
121118
renderer: &mut Renderer,
122-
theme: &<Renderer as iced_native::Renderer>::Theme,
119+
theme: &<Renderer as iced::advanced::Renderer>::Theme,
123120
_renderer_style: &renderer::Style,
124-
layout: iced_native::Layout<'_>,
125-
_cursor_position: iced::Point,
121+
layout: iced::advanced::Layout<'_>,
122+
_cursor: iced::mouse::Cursor,
126123
_viewport: &iced::Rectangle,
127124
) {
128125
let bounds = layout.bounds();
@@ -131,28 +128,28 @@ where
131128
let spline = self.spline.lock().unwrap();
132129
let (vertices, indices) = line_to_polygon(&spline, self.width / 2.0);
133130

134-
let mesh = Mesh2D {
131+
let buffers = Indexed {
135132
vertices: vertices
136133
.iter()
137-
.map(|p| ColoredVertex2D {
134+
.map(|p| SolidVertex2D {
138135
position: [p.x, p.y],
139-
color: style.color.unwrap().into_linear(),
136+
color: iced::advanced::graphics::color::pack(style.color.unwrap()),
140137
})
141138
.collect(),
142139
indices,
143140
};
144141

145142
renderer.with_translation(Vector::new(bounds.x, bounds.y), |renderer| {
146-
renderer.draw_mesh(mesh);
143+
renderer.draw_buffers(buffers);
147144
});
148145
}
149146

150147
fn width(&self) -> Length {
151-
Length::Units(((self.from.x - self.to.x).abs() + self.width).ceil() as u16)
148+
Length::Fixed((self.from.x - self.to.x).abs() + self.width)
152149
}
153150

154151
fn height(&self) -> Length {
155-
Length::Units(((self.from.y - self.to.y).abs() + self.width).ceil() as u16)
152+
Length::Fixed((self.from.y - self.to.y).abs() + self.width)
156153
}
157154
}
158155

iced_node_editor/src/graph_container.rs

Lines changed: 61 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use iced::{
2+
advanced::{
3+
layout,
4+
renderer::{self},
5+
widget::{self, Operation},
6+
Clipboard, Layout, Shell, Widget,
7+
},
28
event, mouse, Background, Color, Element, Event, Length, Point, Rectangle, Size, Vector,
39
};
4-
use iced_native::{
5-
layout,
6-
renderer::{self},
7-
widget::{self, Operation},
8-
Clipboard, Layout, Shell, Widget,
9-
};
1010

1111
use crate::{
1212
matrix::Matrix,
@@ -21,8 +21,8 @@ where
2121
{
2222
width: Length,
2323
height: Length,
24-
max_width: u32,
25-
max_height: u32,
24+
max_width: f32,
25+
max_height: f32,
2626
style: <Renderer::Theme as StyleSheet>::Style,
2727
content: Vec<GraphNodeElement<'a, Message, Renderer>>,
2828
matrix: Matrix,
@@ -39,16 +39,15 @@ where
3939
Renderer: renderer::Renderer,
4040
Renderer::Theme: StyleSheet,
4141
{
42-
pub fn new(content: Vec<GraphNodeElement<'a, Message, Renderer>>) -> Self
43-
{
42+
pub fn new(content: Vec<GraphNodeElement<'a, Message, Renderer>>) -> Self {
4443
GraphContainer {
4544
on_translate: None,
4645
on_scale: None,
4746
matrix: Matrix::identity(),
4847
width: Length::Shrink,
4948
height: Length::Shrink,
50-
max_width: u32::MAX,
51-
max_height: u32::MAX,
49+
max_width: f32::MAX,
50+
max_height: f32::MAX,
5251
style: Default::default(),
5352
content,
5453
}
@@ -85,12 +84,12 @@ where
8584
self
8685
}
8786

88-
pub fn max_width(mut self, max_width: u32) -> Self {
87+
pub fn max_width(mut self, max_width: f32) -> Self {
8988
self.max_width = max_width;
9089
self
9190
}
9291

93-
pub fn max_height(mut self, max_height: u32) -> Self {
92+
pub fn max_height(mut self, max_height: f32) -> Self {
9493
self.max_height = max_height;
9594
self
9695
}
@@ -138,6 +137,10 @@ where
138137
self.height
139138
}
140139

140+
fn tag(&self) -> widget::tree::Tag {
141+
widget::tree::Tag::of::<widget::tree::State>()
142+
}
143+
141144
fn state(&self) -> widget::tree::State {
142145
widget::tree::State::new(GraphContainerState {
143146
drag_start_position: None,
@@ -176,7 +179,7 @@ where
176179
renderer: &Renderer,
177180
operation: &mut dyn Operation<Message>,
178181
) {
179-
operation.container(None, &mut |operation| {
182+
operation.container(None, layout.bounds(), &mut |operation| {
180183
self.content
181184
.iter()
182185
.zip(&mut tree.children)
@@ -194,29 +197,32 @@ where
194197
tree: &mut widget::Tree,
195198
event: Event,
196199
layout: Layout<'_>,
197-
cursor_position: Point,
200+
cursor: mouse::Cursor,
198201
renderer: &Renderer,
199202
clipboard: &mut dyn Clipboard,
200203
shell: &mut Shell<'_, Message>,
204+
viewport: &Rectangle<f32>,
201205
) -> event::Status {
202206
let mut status = event::Status::Ignored;
203207
let mut state = tree.state.downcast_mut::<GraphContainerState>();
204208

205209
if let Some(start) = state.drag_start_position {
206-
match event {
207-
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => {
208-
state.drag_start_position = None;
209-
}
210-
Event::Mouse(mouse::Event::CursorMoved { .. }) => {
211-
let delta = cursor_position - start;
212-
state.drag_start_position = Some(cursor_position);
213-
if let Some(f) = &self.on_translate {
214-
let message = f((delta.x, delta.y));
215-
shell.publish(message);
210+
if let Some(cursor_position) = cursor.position() {
211+
match event {
212+
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => {
213+
state.drag_start_position = None;
214+
}
215+
Event::Mouse(mouse::Event::CursorMoved { .. }) => {
216+
let delta = cursor_position - start;
217+
state.drag_start_position = Some(cursor_position);
218+
if let Some(f) = &self.on_translate {
219+
let message = f((delta.x, delta.y));
220+
shell.publish(message);
221+
}
222+
status = event::Status::Captured;
216223
}
217-
status = event::Status::Captured;
224+
_ => {}
218225
}
219-
_ => {}
220226
}
221227
} else {
222228
status = self
@@ -229,37 +235,40 @@ where
229235
state,
230236
event.clone(),
231237
layout,
232-
cursor_position,
238+
cursor,
233239
renderer,
234240
clipboard,
235241
shell,
242+
viewport,
236243
)
237244
})
238245
.fold(event::Status::Ignored, event::Status::merge);
239246
}
240247

241248
if status == event::Status::Ignored {
242-
match event {
243-
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
244-
state.drag_start_position = Some(cursor_position);
245-
status = event::Status::Captured;
246-
}
247-
Event::Mouse(mouse::Event::WheelScrolled { delta }) => {
248-
if let Some(f) = &self.on_scale {
249-
match delta {
250-
mouse::ScrollDelta::Lines { y, .. } => {
251-
let message = f(cursor_position.x, cursor_position.y, y);
252-
shell.publish(message);
253-
}
254-
mouse::ScrollDelta::Pixels { y, .. } => {
255-
let message = f(cursor_position.x, cursor_position.y, y);
256-
shell.publish(message);
249+
if let Some(cursor_position) = cursor.position() {
250+
match event {
251+
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
252+
state.drag_start_position = Some(cursor_position);
253+
status = event::Status::Captured;
254+
}
255+
Event::Mouse(mouse::Event::WheelScrolled { delta }) => {
256+
if let Some(f) = &self.on_scale {
257+
match delta {
258+
mouse::ScrollDelta::Lines { y, .. } => {
259+
let message = f(cursor_position.x, cursor_position.y, y);
260+
shell.publish(message);
261+
}
262+
mouse::ScrollDelta::Pixels { y, .. } => {
263+
let message = f(cursor_position.x, cursor_position.y, y);
264+
shell.publish(message);
265+
}
257266
}
267+
status = event::Status::Captured;
258268
}
259-
status = event::Status::Captured;
260269
}
270+
_ => {}
261271
}
262-
_ => {}
263272
}
264273
}
265274

@@ -270,7 +279,7 @@ where
270279
&self,
271280
tree: &widget::Tree,
272281
layout: Layout<'_>,
273-
cursor_position: Point,
282+
cursor: mouse::Cursor,
274283
viewport: &Rectangle,
275284
renderer: &Renderer,
276285
) -> mouse::Interaction {
@@ -279,13 +288,9 @@ where
279288
.zip(&tree.children)
280289
.zip(layout.children())
281290
.map(|((child, state), layout)| {
282-
child.as_widget().mouse_interaction(
283-
state,
284-
layout,
285-
cursor_position,
286-
viewport,
287-
renderer,
288-
)
291+
child
292+
.as_widget()
293+
.mouse_interaction(state, layout, cursor, viewport, renderer)
289294
})
290295
.max()
291296
.unwrap_or_default()
@@ -298,7 +303,7 @@ where
298303
theme: &Renderer::Theme,
299304
renderer_style: &renderer::Style,
300305
layout: Layout<'_>,
301-
cursor_position: Point,
306+
cursor: mouse::Cursor,
302307
viewport: &Rectangle,
303308
) {
304309
let style = theme.appearance(&self.style);
@@ -372,7 +377,7 @@ where
372377
theme,
373378
renderer_style,
374379
layout,
375-
cursor_position,
380+
cursor,
376381
&viewport,
377382
);
378383
}

0 commit comments

Comments
 (0)