Skip to content

Commit

Permalink
Update to iced v0.10 and palette v0.7 (#1)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
meew0 authored Sep 24, 2023
1 parent f8b98d6 commit 30ac432
Show file tree
Hide file tree
Showing 10 changed files with 1,050 additions and 792 deletions.
1,530 changes: 895 additions & 635 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/basic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
iced = "0.7.0"
iced = "0.10.0"
iced_node_editor = { path = "../../iced_node_editor"}

[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand Down
4 changes: 2 additions & 2 deletions examples/basic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ impl Sandbox for Example {
.center_x()
.center_y()
.on_translate(move |p| Message::MoveNode(i, p.0, p.1))
.width(Length::Units(200))
.height(Length::Units(75))
.width(Length::Fixed(200.0))
.height(Length::Fixed(75.0))
.position(n.position)
.into(),
);
Expand Down
6 changes: 2 additions & 4 deletions iced_node_editor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
iced = { version ="0.7.0" }
iced_native = "0.8.0"
iced_graphics = "0.6.0"
palette = "0.6.1"
iced = { version = "0.10.0", features = ["advanced"] }
palette = "0.7.3"
39 changes: 18 additions & 21 deletions iced_node_editor/src/connection.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use std::sync::Mutex;

use iced::{Length, Point, Size, Vector};
use iced_graphics::triangle::{ColoredVertex2D, Mesh2D};
use iced_native::{
renderer::{self},
Widget,
};
use iced::advanced::graphics::mesh::{Indexed, SolidVertex2D};
use iced::advanced::renderer;
use iced::{advanced::Widget, Length, Point, Size, Vector};

use crate::{
mesh_renderer::MeshRenderer,
Expand Down Expand Up @@ -72,9 +69,9 @@ where
fn layout(
&self,
_renderer: &Renderer,
_limits: &iced_native::layout::Limits,
_limits: &iced::advanced::layout::Limits,
scale: f32,
) -> iced_native::layout::Node {
) -> iced::advanced::layout::Node {
let spline = generate_spline(
Vector::new(self.from.x, self.from.y) * scale,
1.0,
Expand All @@ -90,7 +87,7 @@ where
.map(|p| Vector::new(p.x - spline_bounds.x, p.y - spline_bounds.y))
.collect();

let node = iced_native::layout::Node::new(Size::new(
let node = iced::advanced::layout::Node::new(Size::new(
(spline_bounds.width + self.width).ceil(),
(spline_bounds.height + self.width).ceil(),
));
Expand All @@ -110,19 +107,19 @@ where
fn layout(
&self,
_renderer: &Renderer,
_limits: &iced_native::layout::Limits,
) -> iced_native::layout::Node {
_limits: &iced::advanced::layout::Limits,
) -> iced::advanced::layout::Node {
todo!("This should never be called.")
}

fn draw(
&self,
_tree: &iced_native::widget::Tree,
_tree: &iced::advanced::widget::Tree,
renderer: &mut Renderer,
theme: &<Renderer as iced_native::Renderer>::Theme,
theme: &<Renderer as iced::advanced::Renderer>::Theme,
_renderer_style: &renderer::Style,
layout: iced_native::Layout<'_>,
_cursor_position: iced::Point,
layout: iced::advanced::Layout<'_>,
_cursor: iced::mouse::Cursor,
_viewport: &iced::Rectangle,
) {
let bounds = layout.bounds();
Expand All @@ -131,28 +128,28 @@ where
let spline = self.spline.lock().unwrap();
let (vertices, indices) = line_to_polygon(&spline, self.width / 2.0);

let mesh = Mesh2D {
let buffers = Indexed {
vertices: vertices
.iter()
.map(|p| ColoredVertex2D {
.map(|p| SolidVertex2D {
position: [p.x, p.y],
color: style.color.unwrap().into_linear(),
color: iced::advanced::graphics::color::pack(style.color.unwrap()),
})
.collect(),
indices,
};

renderer.with_translation(Vector::new(bounds.x, bounds.y), |renderer| {
renderer.draw_mesh(mesh);
renderer.draw_buffers(buffers);
});
}

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

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

Expand Down
117 changes: 61 additions & 56 deletions iced_node_editor/src/graph_container.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use iced::{
advanced::{
layout,
renderer::{self},
widget::{self, Operation},
Clipboard, Layout, Shell, Widget,
},
event, mouse, Background, Color, Element, Event, Length, Point, Rectangle, Size, Vector,
};
use iced_native::{
layout,
renderer::{self},
widget::{self, Operation},
Clipboard, Layout, Shell, Widget,
};

use crate::{
matrix::Matrix,
Expand All @@ -21,8 +21,8 @@ where
{
width: Length,
height: Length,
max_width: u32,
max_height: u32,
max_width: f32,
max_height: f32,
style: <Renderer::Theme as StyleSheet>::Style,
content: Vec<GraphNodeElement<'a, Message, Renderer>>,
matrix: Matrix,
Expand All @@ -39,16 +39,15 @@ where
Renderer: renderer::Renderer,
Renderer::Theme: StyleSheet,
{
pub fn new(content: Vec<GraphNodeElement<'a, Message, Renderer>>) -> Self
{
pub fn new(content: Vec<GraphNodeElement<'a, Message, Renderer>>) -> Self {
GraphContainer {
on_translate: None,
on_scale: None,
matrix: Matrix::identity(),
width: Length::Shrink,
height: Length::Shrink,
max_width: u32::MAX,
max_height: u32::MAX,
max_width: f32::MAX,
max_height: f32::MAX,
style: Default::default(),
content,
}
Expand Down Expand Up @@ -85,12 +84,12 @@ where
self
}

pub fn max_width(mut self, max_width: u32) -> Self {
pub fn max_width(mut self, max_width: f32) -> Self {
self.max_width = max_width;
self
}

pub fn max_height(mut self, max_height: u32) -> Self {
pub fn max_height(mut self, max_height: f32) -> Self {
self.max_height = max_height;
self
}
Expand Down Expand Up @@ -138,6 +137,10 @@ where
self.height
}

fn tag(&self) -> widget::tree::Tag {
widget::tree::Tag::of::<widget::tree::State>()
}

fn state(&self) -> widget::tree::State {
widget::tree::State::new(GraphContainerState {
drag_start_position: None,
Expand Down Expand Up @@ -176,7 +179,7 @@ where
renderer: &Renderer,
operation: &mut dyn Operation<Message>,
) {
operation.container(None, &mut |operation| {
operation.container(None, layout.bounds(), &mut |operation| {
self.content
.iter()
.zip(&mut tree.children)
Expand All @@ -194,29 +197,32 @@ where
tree: &mut widget::Tree,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
cursor: mouse::Cursor,
renderer: &Renderer,
clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>,
viewport: &Rectangle<f32>,
) -> event::Status {
let mut status = event::Status::Ignored;
let mut state = tree.state.downcast_mut::<GraphContainerState>();

if let Some(start) = state.drag_start_position {
match event {
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => {
state.drag_start_position = None;
}
Event::Mouse(mouse::Event::CursorMoved { .. }) => {
let delta = cursor_position - start;
state.drag_start_position = Some(cursor_position);
if let Some(f) = &self.on_translate {
let message = f((delta.x, delta.y));
shell.publish(message);
if let Some(cursor_position) = cursor.position() {
match event {
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => {
state.drag_start_position = None;
}
Event::Mouse(mouse::Event::CursorMoved { .. }) => {
let delta = cursor_position - start;
state.drag_start_position = Some(cursor_position);
if let Some(f) = &self.on_translate {
let message = f((delta.x, delta.y));
shell.publish(message);
}
status = event::Status::Captured;
}
status = event::Status::Captured;
_ => {}
}
_ => {}
}
} else {
status = self
Expand All @@ -229,37 +235,40 @@ where
state,
event.clone(),
layout,
cursor_position,
cursor,
renderer,
clipboard,
shell,
viewport,
)
})
.fold(event::Status::Ignored, event::Status::merge);
}

if status == event::Status::Ignored {
match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
state.drag_start_position = Some(cursor_position);
status = event::Status::Captured;
}
Event::Mouse(mouse::Event::WheelScrolled { delta }) => {
if let Some(f) = &self.on_scale {
match delta {
mouse::ScrollDelta::Lines { y, .. } => {
let message = f(cursor_position.x, cursor_position.y, y);
shell.publish(message);
}
mouse::ScrollDelta::Pixels { y, .. } => {
let message = f(cursor_position.x, cursor_position.y, y);
shell.publish(message);
if let Some(cursor_position) = cursor.position() {
match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
state.drag_start_position = Some(cursor_position);
status = event::Status::Captured;
}
Event::Mouse(mouse::Event::WheelScrolled { delta }) => {
if let Some(f) = &self.on_scale {
match delta {
mouse::ScrollDelta::Lines { y, .. } => {
let message = f(cursor_position.x, cursor_position.y, y);
shell.publish(message);
}
mouse::ScrollDelta::Pixels { y, .. } => {
let message = f(cursor_position.x, cursor_position.y, y);
shell.publish(message);
}
}
status = event::Status::Captured;
}
status = event::Status::Captured;
}
_ => {}
}
_ => {}
}
}

Expand All @@ -270,7 +279,7 @@ where
&self,
tree: &widget::Tree,
layout: Layout<'_>,
cursor_position: Point,
cursor: mouse::Cursor,
viewport: &Rectangle,
renderer: &Renderer,
) -> mouse::Interaction {
Expand All @@ -279,13 +288,9 @@ where
.zip(&tree.children)
.zip(layout.children())
.map(|((child, state), layout)| {
child.as_widget().mouse_interaction(
state,
layout,
cursor_position,
viewport,
renderer,
)
child
.as_widget()
.mouse_interaction(state, layout, cursor, viewport, renderer)
})
.max()
.unwrap_or_default()
Expand All @@ -298,7 +303,7 @@ where
theme: &Renderer::Theme,
renderer_style: &renderer::Style,
layout: Layout<'_>,
cursor_position: Point,
cursor: mouse::Cursor,
viewport: &Rectangle,
) {
let style = theme.appearance(&self.style);
Expand Down Expand Up @@ -372,7 +377,7 @@ where
theme,
renderer_style,
layout,
cursor_position,
cursor,
&viewport,
);
}
Expand Down
Loading

0 comments on commit 30ac432

Please sign in to comment.