main menu: fix int overflow

Signed-off-by: Frank Villaro-Dixon <frank@villaro-dixon.eu>
This commit is contained in:
Frank Villaro-Dixon 2024-05-16 15:23:34 +02:00
parent 91172deddd
commit 40b0cd7a90
2 changed files with 19 additions and 2 deletions

View file

@ -8,6 +8,23 @@ pub struct MainMenu {
pub names: Vec<String>, pub names: Vec<String>,
} }
impl MainMenu {
pub fn selected_name(&self) -> &str {
&self.names[self.selected_id]
}
pub fn go_left(&mut self) {
if self.selected_id > 0 {
self.selected_id -= 1;
}
}
pub fn go_right(&mut self) {
if self.selected_id < self.names.len() - 1 {
self.selected_id += 1;
}
}
}
pub struct AppBase { pub struct AppBase {
name: String, name: String,
pub main_menu: MainMenu, pub main_menu: MainMenu,

View file

@ -44,13 +44,13 @@ impl BeoUi {
Beo5Event::LeftButtonPressed => { Beo5Event::LeftButtonPressed => {
println!("Left button pressed"); println!("Left button pressed");
if let Some(selected_app) = self.current_app_mut() { if let Some(selected_app) = self.current_app_mut() {
selected_app.base_mut().main_menu.selected_id += 1; selected_app.base_mut().main_menu.go_left();
} }
} }
Beo5Event::RightButtonPressed => { Beo5Event::RightButtonPressed => {
println!("Right button pressed"); println!("Right button pressed");
if let Some(selected_app) = self.current_app_mut() { if let Some(selected_app) = self.current_app_mut() {
selected_app.base_mut().main_menu.selected_id -= 1; selected_app.base_mut().main_menu.go_right();
} }
} }
_ => { _ => {