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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
use crate::{Popup, PopupEvent, common::*};
// Notes:
// When user clicks menu, the container should appear
// When container is visible, clicking on a menu item activates the item
// Need the option to close the menu on item press
#[derive(Debug, Clone, PartialEq)]
pub enum MenuEvent {
Open(Entity),
Close(Entity),
Hover(Entity),
CloseAll(Entity),
OpenHover(bool),
}
#[derive(Debug, Copy, Clone)]
pub enum MenuPosition {
Auto, // TODO
Down,
Right,
}
pub struct MenuData {
//open: bool,
}
pub struct Menu {
container: Entity,
open: bool,
text: String,
}
impl Menu {
pub fn new(text: &str) -> Self {
Menu {
container: Entity::default(),
open: false,
text: text.to_string(),
}
}
}
impl Widget for Menu {
type Ret = Entity;
type Data = MenuData;
fn on_build(&mut self, state: &mut State, entity: Entity) -> Self::Ret {
self.container = Popup::new().build(state, entity, |builder| {
builder
.set_position_type(PositionType::SelfDirected)
.set_top(Percentage(100.0))
// .set_width(Auto)
// .set_height(Auto)
.set_width(Pixels(100.0))
.set_height(Pixels(300.0))
.set_background_color(Color::red())
.set_z_order(1)
.set_clip_widget(Entity::root())
.class("container")
});
entity
.set_text(state, &self.text)
.set_element(state, "menu");
self.container
}
fn on_event(&mut self, state: &mut State, entity: Entity, event: &mut Event) {
// if let Some(menu_event) = event.message.downcast::<MenuEvent>() {
// match menu_event {
// MenuEvent::Open(menu) => {
// if *menu == entity {
// entity.set_checked(state, true);
// state.capture(entity);
// self.open = true;
// }
// }
// MenuEvent::Close(menu) => {
// if *menu == entity {
// entity.set_checked(state, false);
// state.release(entity);
// self.open = false;
// }
// }
// _ => {}
// }
// }
if let Some(window_event) = event.message.downcast::<WindowEvent>() {
match window_event {
WindowEvent::MouseDown(button) => {
if *button == MouseButton::Left {
if state.hovered == entity {
if !self.open {
state.insert_event(
Event::new(PopupEvent::Open).target(self.container).propagate(Propagation::Direct),
);
event.consume();
//entity.emit(state, PopupEvent::Switch);
} else {
// state.insert_event(
// Event::new(MenuEvent::Close(entity)).target(entity),
// );
//entity.emit(state, PopupEvent::Close);
}
}
// } else {
// if self.open {
// if state.hovered.is_descendant_of(&state.tree, entity) {
// state.insert_event(
// Event::new(WindowEvent::MouseDown(*button))
// .target(state.hovered),
// );
// self.open = false;
// }
// state.insert_event(
// Event::new(MenuEvent::Close(entity)).target(entity),
// );
// }
// }
}
}
// WindowEvent::MouseOver => {
// if event.target == entity {
// state.insert_event(Event::new(MenuEvent::Hover(entity)).target(entity));
// }
// }
_ => {}
}
}
}
}
pub struct MenuBar {
open_menu: Entity,
}
impl MenuBar {
pub fn new() -> Self {
Self {
open_menu: Entity::default(),
}
}
}
impl Widget for MenuBar {
type Ret = Entity;
type Data = ();
fn on_build(&mut self, state: &mut State, entity: Entity) -> Self::Ret {
entity.set_element(state, "menu_bar")
}
fn on_event(&mut self, state: &mut State, entity: Entity, event: &mut Event) {
if let Some(menu_event) = event.message.downcast::<MenuEvent>() {
match menu_event {
MenuEvent::Open(menu) => {
self.open_menu = *menu;
}
MenuEvent::Close(_) => {
self.open_menu = Entity::default();
}
MenuEvent::Hover(menu) => {
if self.open_menu != Entity::default() {
state.insert_event(
Event::new(MenuEvent::Close(self.open_menu))
.target(entity)
.propagate(Propagation::Fall),
);
state.insert_event(
Event::new(MenuEvent::Open(*menu))
.target(entity)
.propagate(Propagation::Fall),
);
self.open_menu = *menu;
}
}
_ => {}
}
}
}
}