refactor shit

Signed-off-by: Frank Villaro-Dixon <frank@villaro-dixon.eu>
This commit is contained in:
Frank Villaro-Dixon 2024-05-09 00:00:45 +02:00
parent 8341b9d54c
commit ff510a8692
3 changed files with 128 additions and 33 deletions

View file

@ -1,31 +1,122 @@
pub struct BeoApps {
pub apps: Vec<Box<dyn App>>,
}
struct MainMenu {
pub last_id: usize,
pub default_id: usize,
pub names: Vec<String>,
}
pub struct AppBase {
name: String,
pub main_menu: MainMenu,
}
impl AppBase {
pub fn name(&self) -> &str {
&self.name
}
}
pub trait App { pub trait App {
fn name(&self) -> &str; fn base(&self) -> &AppBase;
// fn main_menu(&self) -> &MainMenu;
}
struct Spotify {
base: AppBase,
}
impl Spotify {
fn new() -> Self {
Spotify {
base: AppBase {
name: "Spotify".to_string(),
main_menu: MainMenu {
last_id: 0,
default_id: 0,
names: vec![
"Playlists".to_string(),
"Artists".to_string(),
"Albums".to_string(),
],
},
},
}
}
} }
struct Spotify;
impl App for Spotify { impl App for Spotify {
fn name(&self) -> &str { fn base(&self) -> &AppBase {
"Spotify" &self.base
} }
} }
struct Radio;
// Similar implementations for other apps like Radio and Settings
struct Radio {
base: AppBase,
}
impl Radio {
fn new() -> Self {
Radio {
base: AppBase {
name: "Radio".to_string(),
main_menu: MainMenu {
last_id: 0,
default_id: 0,
names: vec![
"Favorites".to_string(),
"Local".to_string(),
"Global".to_string(),
],
},
},
}
}
}
impl App for Radio { impl App for Radio {
fn name(&self) -> &str { fn base(&self) -> &AppBase {
"Radio" &self.base
}
}
struct Settings {
base: AppBase,
}
impl Settings {
fn new() -> Self {
Settings {
base: AppBase {
name: "Settings".to_string(),
main_menu: MainMenu {
last_id: 0,
default_id: 0,
names: vec![
"Display".to_string(),
"Sound".to_string(),
"Network".to_string(),
],
},
},
}
} }
} }
struct Settings;
impl App for Settings { impl App for Settings {
fn name(&self) -> &str { fn base(&self) -> &AppBase {
"Settings" &self.base
} }
} }
pub fn get_apps() -> Vec<Box<dyn App>> { pub fn get_beo_apps() -> BeoApps {
vec![ let apps: Vec<Box<dyn App>> = vec![
Box::new(Spotify {}), Box::new(Spotify::new()),
Box::new(Radio {}), Box::new(Radio::new()),
Box::new(Settings {}), Box::new(Settings::new()),
] ];
BeoApps { apps }
} }

View file

@ -1,3 +1,4 @@
use apps::get_beo_apps;
use femtovg::{ use femtovg::{
renderer::OpenGl, Align, Baseline, Canvas, Color, FontId, ImageId, Paint, Path, Renderer, renderer::OpenGl, Align, Baseline, Canvas, Color, FontId, ImageId, Paint, Path, Renderer,
}; };
@ -49,10 +50,7 @@ fn run(
.expect("Cannot add font"), .expect("Cannot add font"),
}; };
let mut beo = ui::Beo::new(); let mut beo = ui::BeoUi::new(get_beo_apps());
for app in &beo.apps {
println!(">> {}", app.name());
}
let start = Instant::now(); let start = Instant::now();
let mut prevt = start; let mut prevt = start;

View file

@ -2,11 +2,11 @@ use femtovg::{Align, Baseline, Canvas, Color, Paint, Path, Renderer};
use crate::{hid::Beo5Event, Fonts}; use crate::{hid::Beo5Event, Fonts};
use crate::apps::{get_apps, App}; use crate::apps::{App, BeoApps};
use crate::roundy_math; use crate::roundy_math;
pub struct Beo { pub struct BeoUi {
pub apps: Vec<Box<dyn App>>, pub beo_apps: BeoApps,
pub laser_pct: f32, pub laser_pct: f32,
} }
@ -20,10 +20,10 @@ fn laser_pct_to_y_pos(pct: f32) -> f32 {
(pct * 1.5 - 0.25) * CANVAS_HEIGHT (pct * 1.5 - 0.25) * CANVAS_HEIGHT
} }
impl Beo { impl BeoUi {
pub fn new() -> Beo { pub fn new(apps: BeoApps) -> BeoUi {
Beo { BeoUi {
apps: get_apps(), beo_apps: apps,
laser_pct: 0.0, laser_pct: 0.0,
} }
} }
@ -43,19 +43,24 @@ impl Beo {
pub fn draw<T: Renderer>(&self, canvas: &mut Canvas<T>, fonts: &Fonts) { pub fn draw<T: Renderer>(&self, canvas: &mut Canvas<T>, fonts: &Fonts) {
self.draw_main_menu(canvas, fonts); self.draw_main_menu(canvas, fonts);
if let Some(selected_app_id) = self.lasered_application_id() { if let Some(selected_app_id) = self.lasered_application_id() {
let selected_app = self.apps.get(selected_app_id).unwrap(); let selected_app = self.beo_apps.apps.get(selected_app_id).unwrap();
let mut paint_title = Paint::color(Color::hex("FFFFFF")); let mut paint_title = Paint::color(Color::hex("FFFFFF"));
paint_title.set_font(&[fonts.bold]); paint_title.set_font(&[fonts.bold]);
paint_title.set_text_baseline(Baseline::Top); paint_title.set_text_baseline(Baseline::Top);
paint_title.set_text_align(Align::Center); paint_title.set_text_align(Align::Center);
paint_title.set_font_size(20.); paint_title.set_font_size(20.);
let _ = canvas.fill_text(CANVAS_WIDTH / 2., 10., selected_app.name(), &paint_title); let _ = canvas.fill_text(
CANVAS_WIDTH / 2.,
10.,
selected_app.base().name(),
&paint_title,
);
} }
} }
fn lasered_application_id(&self) -> Option<usize> { fn lasered_application_id(&self) -> Option<usize> {
let app_count = self.apps.len(); let app_count = self.beo_apps.apps.len();
if app_count == 0 { if app_count == 0 {
return None; return None;
} }
@ -91,7 +96,7 @@ impl Beo {
y: canvas_height as f32, y: canvas_height as f32,
}; };
let apps = &self.apps; let apps = &self.beo_apps.apps;
// draw the main apps in the circle // draw the main apps in the circle
let pts = main_menu_circle.get_equidistant_points(apps.len(), canvas_size); let pts = main_menu_circle.get_equidistant_points(apps.len(), canvas_size);
@ -104,9 +109,10 @@ impl Beo {
paint_selected.set_text_baseline(Baseline::Top); paint_selected.set_text_baseline(Baseline::Top);
for i in 0..apps.len() { for i in 0..apps.len() {
if self.lasered_application_id() == Some(i) { if self.lasered_application_id() == Some(i) {
let _ = canvas.fill_text(pts[i].x, pts[i].y, apps[i].name(), &paint_selected); let _ =
canvas.fill_text(pts[i].x, pts[i].y, apps[i].base().name(), &paint_selected);
} else { } else {
let _ = canvas.fill_text(pts[i].x, pts[i].y, apps[i].name(), &paint_normal); let _ = canvas.fill_text(pts[i].x, pts[i].y, apps[i].base().name(), &paint_normal);
} }
} }