From 6089ca39c78583cd5028d112672bdcc7e5e8225f Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Mon, 14 Sep 2020 06:19:58 +0200 Subject: [PATCH] WIP: Tree: intent & cleanup --- src/app.rs | 9 +++++++++ src/icon.rs | 7 +++++++ src/lib.rs | 19 +++++++++++++++++++ src/tree.rs | 32 +++++++++++++------------------- 4 files changed, 48 insertions(+), 19 deletions(-) diff --git a/src/app.rs b/src/app.rs index c4a2663..e91d71e 100644 --- a/src/app.rs +++ b/src/app.rs @@ -3,6 +3,7 @@ use crate::collapse::Collapse; use crate::forms::controls::Switch; use crate::icon::IconName; use crate::tree::*; +use crate::Intent; use yew::prelude::*; const DARK_BG_COLOR: &str = "#30404d"; @@ -35,6 +36,8 @@ impl Component for App { .insert( Node::new(NodeData { icon: None, + icon_color: None, + icon_intent: None, label: "".into(), is_selected: false, is_expanded: false, @@ -51,6 +54,8 @@ impl Component for App { .insert( Node::new(NodeData { icon: Some(IconName::FolderClose), + icon_color: None, + icon_intent: None, label: "Directory 1".into(), is_selected: false, is_expanded: false, @@ -66,6 +71,8 @@ impl Component for App { tree.insert( Node::new(NodeData { icon: Some(IconName::Document), + icon_color: None, + icon_intent: None, label: "File 1".into(), is_selected: false, is_expanded: false, @@ -81,6 +88,8 @@ 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(), is_selected: false, is_expanded: false, diff --git a/src/icon.rs b/src/icon.rs index 73cdbfe..c5e801d 100644 --- a/src/icon.rs +++ b/src/icon.rs @@ -1,3 +1,4 @@ +use crate::Intent; use yew::prelude::*; include!(concat!(env!("OUT_DIR"), "/icon_svg_paths.rs")); @@ -24,6 +25,8 @@ pub struct Props { pub title: Option, #[prop_or_default] pub color: Option, + #[prop_or_default] + pub intent: Option, #[prop_or(16)] pub icon_size: i32, #[prop_or_default] @@ -54,6 +57,10 @@ impl Component for Icon { fn view(&self) -> Html { let mut class = "bp3-icon ".to_string(); class.push_str(self.props.class.as_str()); + class.push_str(" "); + if let Some(intent) = self.props.intent { + class.push_str(intent.into()); + } let paths = if self.props.icon_size == SIZE_STANDARD { icon_svg_paths_16(self.props.icon) diff --git a/src/lib.rs b/src/lib.rs index 321e146..eb2177a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,3 +23,22 @@ pub fn run_app() -> Result<(), wasm_bindgen::JsValue> { Ok(()) } + +#[derive(Debug, Copy, Clone, PartialEq)] +pub enum Intent { + Primary, + Success, + Warning, + Danger, +} + +impl From for &'static str { + fn from(intent: Intent) -> Self { + match intent { + Intent::Primary => "bp3-intent-primary", + Intent::Success => "bp3-intent-success", + Intent::Warning => "bp3-intent-warning", + Intent::Danger => "bp3-intent-danger", + } + } +} diff --git a/src/tree.rs b/src/tree.rs index e945a9a..bb42670 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -1,5 +1,6 @@ use crate::collapse::Collapse; use crate::icon::{Icon, IconName}; +use crate::Intent; pub use id_tree::*; use std::cell::{Ref, RefCell, RefMut}; use std::rc::Rc; @@ -18,15 +19,6 @@ impl PartialEq for TreeData { } impl TreeData { - pub fn new() -> Self { - let tree = id_tree::Tree::new(); - - Self { - tree: Rc::new(RefCell::new(tree)), - version: 0, - } - } - pub fn borrow(&self) -> Ref>> { self.tree.borrow() } @@ -61,6 +53,8 @@ pub struct NodeData { 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, @@ -134,6 +128,8 @@ impl Tree { disabled=data.disabled has_caret=data.has_caret icon=data.icon + icon_color=data.icon_color.clone() + icon_intent=data.icon_intent is_expanded=data.is_expanded is_selected=data.is_selected label=data.label.clone() @@ -160,23 +156,16 @@ pub struct TreeNode { #[derive(Clone, PartialEq, Properties)] pub struct TreeNodeProps { - #[prop_or_default] pub disabled: bool, - #[prop_or_default] pub has_caret: bool, - #[prop_or_default] pub icon: Option, - #[prop_or_default] + pub icon_color: Option, + pub icon_intent: Option, pub is_expanded: bool, - #[prop_or_default] pub is_selected: bool, - #[prop_or_default] pub label: yew::virtual_dom::VNode, - #[prop_or_default] pub on_collapse: Option>, - #[prop_or_default] pub on_expand: Option>, - #[prop_or_default] pub children: html::Children, pub depth: u32, } @@ -239,7 +228,12 @@ impl Component for TreeNode { } } } - + {self.props.label.clone()}