From 3c5a8af353c797b699847a7cc15568bd46b1c67f Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Mon, 14 Sep 2020 21:32:08 +0200 Subject: [PATCH] Tree: clean-up: less pubs, impl Default if possible --- src/app.rs | 31 ++++--------------------------- src/tree.rs | 49 +++++++++++++++++++++++++++++++++---------------- 2 files changed, 37 insertions(+), 43 deletions(-) diff --git a/src/app.rs b/src/app.rs index df9ca23..bf76b29 100644 --- a/src/app.rs +++ b/src/app.rs @@ -36,16 +36,8 @@ impl Component for App { let root_id = tree .insert( Node::new(NodeData { - icon: None, - icon_color: None, - icon_intent: None, - label: "".into(), - secondary_label: None, - is_selected: false, - is_expanded: false, - has_caret: true, - disabled: false, data: 0, + ..Default::default() }), InsertBehavior::AsRoot, ) @@ -54,15 +46,10 @@ impl Component for App { .insert( Node::new(NodeData { icon: Some(IconName::FolderClose), - icon_color: None, - icon_intent: None, label: "Directory 1".into(), - secondary_label: None, - is_selected: false, - is_expanded: false, has_caret: true, - disabled: false, data: 1, + ..Default::default() }), InsertBehavior::UnderNode(&root_id), ) @@ -70,15 +57,9 @@ impl Component for App { tree.insert( Node::new(NodeData { icon: Some(IconName::Document), - icon_color: None, - icon_intent: None, label: "File 1".into(), - secondary_label: None, - is_selected: false, - is_expanded: false, - has_caret: false, - disabled: false, data: 2, + ..Default::default() }), InsertBehavior::UnderNode(&root_id), ) @@ -86,15 +67,11 @@ impl Component for App { tree.insert( Node::new(NodeData { icon: Some(IconName::Tag), - icon_color: None, icon_intent: Some(Intent::Primary), label: "File 2".into(), secondary_label: Some(html!()), - is_selected: false, - is_expanded: false, - has_caret: false, - disabled: false, data: 3, + ..Default::default() }), InsertBehavior::UnderNode(&dir1), ) diff --git a/src/tree.rs b/src/tree.rs index 702542e..873a256 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -68,6 +68,23 @@ pub struct NodeData { pub data: T, } +impl Default for NodeData { + fn default() -> Self { + Self { + disabled: false, + has_caret: false, + icon: None, + icon_color: None, + icon_intent: None, + is_expanded: false, + is_selected: false, + label: Default::default(), + secondary_label: None, + data: Default::default(), + } + } +} + impl Component for Tree { type Message = (); type Properties = Props; @@ -170,26 +187,26 @@ impl Tree { } } -pub struct TreeNode { +struct TreeNode { props: TreeNodeProps, } #[derive(Clone, PartialEq, Properties)] -pub struct TreeNodeProps { - pub disabled: bool, - pub has_caret: bool, - pub icon: Option, - pub icon_color: Option, - pub icon_intent: Option, - pub is_expanded: bool, - pub is_selected: bool, - pub label: yew::virtual_dom::VNode, - pub secondary_label: Option, - pub on_collapse: Option>, - pub on_expand: Option>, - pub onclick: Option>, - pub children: html::Children, - pub depth: u32, +struct TreeNodeProps { + disabled: bool, + has_caret: bool, + icon: Option, + icon_color: Option, + icon_intent: Option, + is_expanded: bool, + is_selected: bool, + label: yew::virtual_dom::VNode, + secondary_label: Option, + on_collapse: Option>, + on_expand: Option>, + onclick: Option>, + children: html::Children, + depth: u32, } impl Component for TreeNode {