1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
use crate::node::{Node, NodeContainer}; use crate::{ToHtml, DefaultModifiers}; use crate::renderer::Renderable; use std::borrow::BorrowMut; use crate::component::{Appendable, ChildContainer}; #[derive(Debug, Clone)] pub enum MenuStyle { VerticalNavbar, Toolbar, RadioGroup, } #[derive(Debug, Clone)] pub struct MenuItem { node: Node, pub icon: Option<String>, pub label: String, } impl MenuItem { pub fn new(label: &str) -> Self { Self { node: Default::default(), icon: None, label: label.to_string(), } } pub fn icon(&mut self, name: &str) -> Self { self.icon = Some(name.to_string()); self.clone() } pub fn action(&mut self, url: &str) -> Self { self .set_attr("href", url) .tag("a") } } impl NodeContainer for MenuItem { fn get_node(&mut self) -> &mut Node { self.node.borrow_mut() } } impl DefaultModifiers<MenuItem> for MenuItem {} impl Renderable for MenuItem { fn render(&self) -> Node { let mut menu_item = self.clone(); menu_item.get_node().clone() } } #[derive(Debug, Clone)] pub struct Menu { node: Node, style: MenuStyle, options: Vec<Box<dyn Renderable>>, } impl Menu { pub fn new(style: MenuStyle) -> Self { Self { node: Default::default(), style, options: vec![] } } } impl ChildContainer for Menu { fn get_children(&mut self) -> &mut Vec<Box<dyn Renderable>> { return self.options.borrow_mut(); } } impl Appendable<Menu> for Menu {} impl NodeContainer for Menu { fn get_node(&mut self) -> &mut Node { self.node.borrow_mut() } } impl DefaultModifiers<Menu> for Menu {} impl ToHtml for Menu {} impl Renderable for Menu { fn render(&self) -> Node { let mut menu = self .clone() .add_class("menu"); menu.get_node().clone() } }