From 3cd37aaa22a1d5ce8b5f97ad7fa911585892c175 Mon Sep 17 00:00:00 2001 From: Elli Howard Date: Sun, 29 Sep 2024 19:48:36 -0700 Subject: [PATCH] Fixed menu icon changing to frontlight * Added a child_lookup HashMap to toolbar that allows for dynamic references to children by name * Changed static references to children to dynamic references based on index in HashMap to make adding and updating children less brittle * Added a test to verify that updating the toolbar does not make the menu icon change to the frontlight icon --- crates/core/src/view/top_bar.rs | 138 ++++++++++++++++++++++++++++---- 1 file changed, 122 insertions(+), 16 deletions(-) diff --git a/crates/core/src/view/top_bar.rs b/crates/core/src/view/top_bar.rs index 0d690373..5297b8d5 100644 --- a/crates/core/src/view/top_bar.rs +++ b/crates/core/src/view/top_bar.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + use crate::framebuffer::{Framebuffer, UpdateMode}; use crate::gesture::GestureEvent; use crate::input::DeviceEvent; @@ -15,13 +17,23 @@ use crate::device::CURRENT_DEVICE; use crate::context::Context; use crate::battery::{Battery, Status}; +// Children names for lookup +pub const MENU_ACTION: &str = "menu_action"; +pub const CLOCK: &str = "clock"; +pub const BATTERY: &str = "battery"; +pub const FRONTLIGHT: &str = "frontlight"; +pub const MENU: &str = "menu"; +pub const TITLE: &str = "title"; +pub const BORDER: &str = "border"; + #[derive(Debug, Clone)] pub struct TopBar { id: Id, rect: Rectangle, children: Vec>, content_height: i32, - border_thickness: i32 + border_thickness: i32, + children_lookup: HashMap } impl TopBar { @@ -37,7 +49,8 @@ impl TopBar { parent_rect.max.x, parent_rect.min.y + content_height + border_thickness], children: Vec::new(), content_height, - border_thickness + border_thickness, + children_lookup: HashMap::new() } } pub fn new(parent_rect: Rectangle, root_event: Event, title: String, @@ -69,6 +82,7 @@ impl TopBar { let root_icon = Icon::new(icon_name, rect![self.rect.min, self.rect.min + self.content_height], root_event); + self.children_lookup.insert(MENU_ACTION.to_string(), self.children.len()); self.children.push(Box::new(root_icon) as Box); } @@ -77,6 +91,8 @@ impl TopBar { self.rect.max.x - (self.content_height * 4), self.rect.min.y, self.rect.max.x - (self.content_height * 3), self.rect.min.y + self.content_height]; let clock_label = Clock::new(&mut clock_rect, format, fonts); + + self.children_lookup.insert(CLOCK.to_string(), self.children.len()); self.children.push(Box::new(clock_label) as Box); } @@ -87,6 +103,8 @@ impl TopBar { self.rect.max.x - (self.content_height * 3), self.rect.min.y, self.rect.max.x - (self.content_height * 2), self.rect.min.y + self.content_height]; let battery_widget = BatteryWidget::new(battery_rect, capacity, status); + + self.children_lookup.insert(BATTERY.to_string(), self.children.len()); self.children.push(Box::new(battery_widget) as Box); } @@ -97,6 +115,8 @@ impl TopBar { self.rect.max.x - (self.content_height), self.rect.min.y + self.content_height]; let frontlight_icon = Icon::new(name, frontlight_rect, Event::Show(ViewId::Frontlight)); + + self.children_lookup.insert(FRONTLIGHT.to_string(), self.children.len()); self.children.push(Box::new(frontlight_icon) as Box); } @@ -107,6 +127,8 @@ impl TopBar { let menu_icon = Icon::new("menu", menu_rect, Event::ToggleNear(ViewId::MainMenu, menu_rect)); + + self.children_lookup.insert(MENU.to_string(), self.children.len()); self.children.push(Box::new(menu_icon) as Box); } @@ -119,6 +141,8 @@ impl TopBar { self.rect.max.x - used_width, self.rect.min.y + self.content_height]; let title_label = Label::new(title_rect, title, Align::Center) .event(Some(Event::ToggleNear(ViewId::TitleMenu, title_rect))); + + self.children_lookup.insert(TITLE.to_string(), self.children.len()); self.children.push(Box::new(title_label) as Box); } @@ -127,39 +151,70 @@ impl TopBar { self.rect.min.x, self.rect.max.y - self.border_thickness, self.rect.max.x, self.rect.max.y]; let separator = Filler::new(border_rect, BLACK); + + self.children_lookup.insert(BORDER.to_string(), self.children.len()); self.children.push(Box::new(separator) as Box); } pub fn update_root_icon(&mut self, name: &str, rq: &mut RenderQueue) { - let icon = self.child_mut(0).downcast_mut::().unwrap(); - if icon.name != name { - icon.name = name.to_string(); - rq.add(RenderData::new(icon.id(), *icon.rect(), UpdateMode::Gui)); + match self.children_lookup.get(MENU_ACTION) { + Some(index) => { + let icon = self.child_mut(*index).downcast_mut::().unwrap(); + if icon.name != name { + icon.name = name.to_string(); + rq.add(RenderData::new(icon.id(), *icon.rect(), UpdateMode::Gui)); + } + }, + None => () } } pub fn update_title_label(&mut self, title: &str, rq: &mut RenderQueue) { - let title_label = self.children[1].as_mut().downcast_mut::