Skip to content

Commit

Permalink
Clippy + fmt run
Browse files Browse the repository at this point in the history
  • Loading branch information
zakarumych committed Jul 11, 2024
1 parent 7a8dcfd commit 18f42d6
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 162 deletions.
180 changes: 81 additions & 99 deletions examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl DemoNode {

fn string_out(&self) -> &str {
match self {
DemoNode::String(value) => &value,
DemoNode::String(value) => value,
_ => unreachable!(),
}
}
Expand Down Expand Up @@ -289,61 +289,58 @@ impl SnarlViewer<DemoNode> for DemoViewer {
if changed {
let expr_node = snarl[pin.id.node].expr_node();

match syn::parse_str(&expr_node.text) {
Ok(expr) => {
expr_node.expr = expr;
if let Ok(expr) = syn::parse_str(&expr_node.text) {
expr_node.expr = expr;

let values = Iterator::zip(
expr_node.bindings.iter().map(String::clone),
expr_node.values.iter().copied(),
)
.collect::<HashMap<String, f64>>();
let values = Iterator::zip(
expr_node.bindings.iter().map(String::clone),
expr_node.values.iter().copied(),
)
.collect::<HashMap<String, f64>>();

let mut new_bindings = Vec::new();
expr_node.expr.extend_bindings(&mut new_bindings);
let mut new_bindings = Vec::new();
expr_node.expr.extend_bindings(&mut new_bindings);

let old_bindings =
std::mem::replace(&mut expr_node.bindings, new_bindings.clone());
let old_bindings =
std::mem::replace(&mut expr_node.bindings, new_bindings.clone());

let new_values = new_bindings
.iter()
.map(|name| values.get(&**name).copied().unwrap_or(0.0))
.collect::<Vec<_>>();
let new_values = new_bindings
.iter()
.map(|name| values.get(&**name).copied().unwrap_or(0.0))
.collect::<Vec<_>>();

expr_node.values = new_values;
expr_node.values = new_values;

let old_inputs = (0..old_bindings.len())
.map(|idx| {
snarl.in_pin(InPinId {
node: pin.id.node,
input: idx + 1,
})
let old_inputs = (0..old_bindings.len())
.map(|idx| {
snarl.in_pin(InPinId {
node: pin.id.node,
input: idx + 1,
})
.collect::<Vec<_>>();
})
.collect::<Vec<_>>();

for (idx, name) in old_bindings.iter().enumerate() {
let new_idx =
new_bindings.iter().position(|new_name| *new_name == *name);
for (idx, name) in old_bindings.iter().enumerate() {
let new_idx =
new_bindings.iter().position(|new_name| *new_name == *name);

match new_idx {
None => {
snarl.drop_inputs(old_inputs[idx].id);
}
Some(new_idx) if new_idx != idx => {
let new_in_pin = InPinId {
node: pin.id.node,
input: new_idx,
};
for &remote in &old_inputs[idx].remotes {
snarl.disconnect(remote, old_inputs[idx].id);
snarl.connect(remote, new_in_pin);
}
match new_idx {
None => {
snarl.drop_inputs(old_inputs[idx].id);
}
Some(new_idx) if new_idx != idx => {
let new_in_pin = InPinId {
node: pin.id.node,
input: new_idx,
};
for &remote in &old_inputs[idx].remotes {
snarl.disconnect(remote, old_inputs[idx].id);
snarl.connect(remote, new_in_pin);
}
_ => {}
}
_ => {}
}
}
Err(_) => {}
}
}
PinInfo::triangle().with_fill(STRING_COLOR).with_wire_style(
Expand Down Expand Up @@ -523,19 +520,17 @@ impl SnarlViewer<DemoNode> for DemoViewer {
];

for (name, ctor, in_ty) in dst_in_candidates {
if src_out_ty & in_ty != 0 {
if ui.button(name).clicked() {
// Create new node.
let new_node = snarl.insert_node(pos, ctor());
let dst_pin = InPinId {
node: new_node,
input: 0,
};

// Connect the wire.
snarl.connect(src_pin, dst_pin);
ui.close_menu();
}
if src_out_ty & in_ty != 0 && ui.button(name).clicked() {
// Create new node.
let new_node = snarl.insert_node(pos, ctor());
let dst_pin = InPinId {
node: new_node,
input: 0,
};

// Connect the wire.
snarl.connect(src_pin, dst_pin);
ui.close_menu();
}
}
}
Expand All @@ -556,31 +551,27 @@ impl SnarlViewer<DemoNode> for DemoViewer {
];

for (name, ctor, out_ty) in dst_out_candidates {
if all_src_types & out_ty != 0 {
if ui.button(name).clicked() {
// Create new node.
let new_node = ctor();
let dst_ty = pin_out_compat(&new_node);

let new_node = snarl.insert_node(pos, new_node);
let dst_pin = OutPinId {
node: new_node,
output: 0,
};

// Connect the wire.
for src_pin in pins {
let src_ty = pin_in_compat(
snarl.get_node(src_pin.node).unwrap(),
src_pin.input,
);
if src_ty & dst_ty != 0 {
// In this demo, input pin MUST be unique ...
// Therefore here we drop inputs of source input pin.
snarl.drop_inputs(*src_pin);
snarl.connect(dst_pin, *src_pin);
ui.close_menu();
}
if all_src_types & out_ty != 0 && ui.button(name).clicked() {
// Create new node.
let new_node = ctor();
let dst_ty = pin_out_compat(&new_node);

let new_node = snarl.insert_node(pos, new_node);
let dst_pin = OutPinId {
node: new_node,
output: 0,
};

// Connect the wire.
for src_pin in pins {
let src_ty =
pin_in_compat(snarl.get_node(src_pin.node).unwrap(), src_pin.input);
if src_ty & dst_ty != 0 {
// In this demo, input pin MUST be unique ...
// Therefore here we drop inputs of source input pin.
snarl.drop_inputs(*src_pin);
snarl.connect(dst_pin, *src_pin);
ui.close_menu();
}
}
}
Expand Down Expand Up @@ -653,7 +644,7 @@ struct ExprNode {
impl ExprNode {
fn new() -> Self {
ExprNode {
text: format!("0"),
text: "0".to_string(),
bindings: Vec::new(),
values: Vec::new(),
expr: Expr::Val(0.0),
Expand Down Expand Up @@ -946,27 +937,19 @@ impl DemoApp {

let snarl = match cx.storage {
None => Snarl::new(),
Some(storage) => {
let snarl = storage
.get_string("snarl")
.and_then(|snarl| serde_json::from_str(&snarl).ok())
.unwrap_or_else(Snarl::new);

snarl
}
Some(storage) => storage
.get_string("snarl")
.and_then(|snarl| serde_json::from_str(&snarl).ok())
.unwrap_or_else(Snarl::new),
};
// let snarl = Snarl::new();

let style = match cx.storage {
None => SnarlStyle::new(),
Some(storage) => {
let style = storage
.get_string("style")
.and_then(|style| serde_json::from_str(&style).ok())
.unwrap_or_else(SnarlStyle::new);

style
}
Some(storage) => storage
.get_string("style")
.and_then(|style| serde_json::from_str(&style).ok())
.unwrap_or_else(SnarlStyle::new),
};
// let style = SnarlStyle::new();

Expand All @@ -976,7 +959,6 @@ impl DemoApp {

impl App for DemoApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {

egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
// The top panel is often a good place for a menu bar:

Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ impl<T> Snarl<T> {
pos,
open: true,
});
let id = NodeId(idx);
id

NodeId(idx)
}

/// Adds a node to the Snarl in collapsed state.
Expand All @@ -258,8 +258,8 @@ impl<T> Snarl<T> {
pos,
open: false,
});
let id = NodeId(idx);
id

NodeId(idx)
}

/// Opens or collapses a node.
Expand Down
62 changes: 29 additions & 33 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,9 @@ impl SnarlStyle {
}

fn header_frame(&self, scale: f32, ui: &Ui) -> Frame {
self._header_frame.zoomed(scale).unwrap_or_else(|| {
self.node_frame(scale, ui)
.shadow(Shadow::NONE)
})
self._header_frame
.zoomed(scale)
.unwrap_or_else(|| self.node_frame(scale, ui).shadow(Shadow::NONE))
}

fn centering(&self) -> bool {
Expand Down Expand Up @@ -861,24 +860,23 @@ impl<T> Snarl<T> {
);
});
}
} else {
if snarl_state.is_link_menu_open() || viewer.has_graph_menu(interact_pos, self)
{
bg_r.context_menu(|ui| {
is_menu_visible = true;
if !snarl_state.is_link_menu_open() {
// Mark link menu is now visible.
snarl_state.open_link_menu();
}
} else if snarl_state.is_link_menu_open()
|| viewer.has_graph_menu(interact_pos, self)
{
bg_r.context_menu(|ui| {
is_menu_visible = true;
if !snarl_state.is_link_menu_open() {
// Mark link menu is now visible.
snarl_state.open_link_menu();
}

viewer.show_graph_menu(
snarl_state.screen_pos_to_graph(ui.cursor().min, viewport),
ui,
snarl_state.scale(),
self,
);
});
}
viewer.show_graph_menu(
snarl_state.screen_pos_to_graph(ui.cursor().min, viewport),
ui,
snarl_state.scale(),
self,
);
});
}
}

Expand Down Expand Up @@ -1033,7 +1031,9 @@ impl<T> Snarl<T> {

let pin_size = style.pin_size(snarl_state.scale(), ui).max(0.0);

let header_drag_space = style.header_drag_space(snarl_state.scale(), ui).max(Vec2::ZERO);
let header_drag_space = style
.header_drag_space(snarl_state.scale(), ui)
.max(Vec2::ZERO);

let inputs = (0..inputs_count)
.map(|idx| InPin::new(self, InPinId { node, input: idx }))
Expand Down Expand Up @@ -1100,12 +1100,9 @@ impl<T> Snarl<T> {
None,
);



let mut new_pins_size = Vec2::ZERO;

let r = node_frame.show(node_ui, |ui| {

let min_pin_y = node_rect.min.y + node_state.header_height() * 0.5;

let input_x = node_frame_rect.left() + node_frame.inner_margin.left + pin_size;
Expand All @@ -1122,16 +1119,17 @@ impl<T> Snarl<T> {
let payload_rect = Rect::from_min_max(
pos2(
node_rect.min.x,
node_rect.min.y + node_state.header_height() + header_frame.total_margin().bottom + ui.spacing().item_spacing.y
node_rect.min.y
+ node_state.header_height()
+ header_frame.total_margin().bottom
+ ui.spacing().item_spacing.y
- node_state.payload_offset(openness),
),
node_rect.max,
);

let payload_clip_rect = Rect::from_min_max(
node_rect.min,
pos2(node_rect.max.x, f32::INFINITY),
);
let payload_clip_rect =
Rect::from_min_max(node_rect.min, pos2(node_rect.max.x, f32::INFINITY));

// Show input pins.

Expand Down Expand Up @@ -1470,11 +1468,10 @@ impl<T> Snarl<T> {
}
}


// Render header frame.
let mut header_rect = Rect::NAN;

let mut header_frame_rect = Rect::NAN;//node_rect + header_frame.total_margin();
let mut header_frame_rect = Rect::NAN; //node_rect + header_frame.total_margin();

// Show node's header
let header_ui: &mut Ui = &mut ui.child_ui_with_id_source(
Expand Down Expand Up @@ -1528,7 +1525,6 @@ impl<T> Snarl<T> {
+ ui.spacing().item_spacing.y
+ new_pins_size.y,
));

});

if !self.nodes.contains(node.0) {
Expand Down
Loading

0 comments on commit 18f42d6

Please sign in to comment.