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 13ca448
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 120 deletions.
99 changes: 42 additions & 57 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 @@ -523,19 +523,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 +554,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 +647,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 +940,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 +962,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
17 changes: 3 additions & 14 deletions src/ui/pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ impl From<BasicPinShape> for PinShape {
///
/// All fields are optional.
/// If a field is `None`, the default value is used derived from the graph style.
#[derive(Default)]
pub struct PinInfo {
/// Shape of the pin.
pub shape: Option<PinShape>,
Expand All @@ -102,18 +103,6 @@ pub struct PinInfo {
pub wire_style: Option<WireStyle>,
}

impl Default for PinInfo {
fn default() -> Self {
PinInfo {
shape: None,
size: None,
fill: None,
stroke: None,
wire_style: None,
}
}
}

impl PinInfo {
/// Sets the shape of the pin.
pub fn with_shape(mut self, shape: PinShape) -> Self {
Expand Down Expand Up @@ -211,7 +200,7 @@ pub fn draw_pin(
painter.add(Shape::Path(PathShape {
points,
closed: true,
fill: fill,
fill,
stroke: stroke.into(),
}));
}
Expand All @@ -226,7 +215,7 @@ pub fn draw_pin(
painter.add(Shape::Path(PathShape {
points,
closed: true,
fill: fill,
fill,
stroke: stroke.into(),
}));
}
Expand Down
16 changes: 4 additions & 12 deletions src/ui/wire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,28 @@ use egui::{epaint::PathShape, pos2, Color32, Pos2, Rect, Shape, Stroke, Ui};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "egui-probe", derive(egui_probe::EguiProbe))]
#[derive(Default)]
pub enum WireLayer {
/// Wires are rendered behind nodes.
/// This is default.
#[default]
BehindNodes,

/// Wires are rendered above nodes.
AboveNodes,
}

impl Default for WireLayer {
fn default() -> Self {
WireLayer::BehindNodes
}
}

/// Controls style in which wire is rendered.
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "egui-probe", derive(egui_probe::EguiProbe))]
#[derive(Default)]
pub enum WireStyle {
/// Draw wire as 3rd degree Bezier curve.
Bezier3,

/// Draw wire as 5th degree Bezier curve.
#[default]
Bezier5,

/// Draw wire as straight lines with 90 degree turns.
Expand All @@ -40,12 +38,6 @@ pub enum WireStyle {
},
}

impl Default for WireStyle {
fn default() -> Self {
WireStyle::Bezier5
}
}

pub(crate) fn pick_wire_style(
default: WireStyle,
left: Option<WireStyle>,
Expand Down

0 comments on commit 13ca448

Please sign in to comment.