Skip to content

Commit

Permalink
feat: make title and button font size customizable
Browse files Browse the repository at this point in the history
  • Loading branch information
goweiwen committed Jan 27, 2024
1 parent b67197e commit 7c72818
Show file tree
Hide file tree
Showing 19 changed files with 102 additions and 19 deletions.
2 changes: 2 additions & 0 deletions activity-tracker/src/view/activity_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ impl ActivityTracker {
let locale = res.get::<Locale>();
vec![
ButtonHint::new(
res.clone(),
Point::zero(),
Key::B,
locale.t("button-back"),
Alignment::Right,
),
ButtonHint::new(
res.clone(),
Point::zero(),
Key::Y,
Sort::MostPlayed.button_hint(&locale),
Expand Down
53 changes: 41 additions & 12 deletions allium-launcher/src/view/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ pub struct App<B>
where
B: Battery + 'static,
{
res: Resources,
rect: Rect,
battery_indicator: BatteryIndicator<B>,
views: (Recents, Games, Apps, Settings),
selected: usize,
tabs: Row<Label<String>>,
title: Label<String>,
dirty: bool,
}

Expand All @@ -59,6 +61,7 @@ where
) -> Result<Self> {
let Rect { x, y, w, h: _h } = rect;
let styles = res.get::<Stylesheet>();
let locale = res.get::<Locale>();

let battery_indicator = BatteryIndicator::new(
Point::new(w as i32 - 12, y + 8),
Expand All @@ -69,7 +72,6 @@ where
let mut tabs = Row::new(
Point::new(x + 12, y + 8),
{
let locale = res.get::<Locale>();
vec![
Label::new(
Point::zero(),
Expand All @@ -94,24 +96,38 @@ where
.unwrap()
.color(StylesheetColor::Highlight);

let mut title = Label::new(
Point::new(x + 24, y + 8),
title(&locale, selected),
Alignment::Left,
None,
);
title.font_size(styles.title_font_size);

drop(styles);
drop(locale);

Ok(Self {
res,
rect,
views,
selected,
battery_indicator,
tabs,
title,
dirty: true,
})
}

pub fn load_or_new(rect: Rect, res: Resources, battery: B) -> Result<Self> {
let tab_rect = {
let styles = res.get::<Stylesheet>();
let font_size = (styles.ui_font.size as f32 * styles.title_font_size) as u32;
Rect::new(
rect.x,
rect.y + styles.ui_font.size as i32 + 8,
rect.y + font_size as i32 + 8,
rect.w,
rect.h - styles.ui_font.size - 8,
rect.h - font_size - 8,
)
};

Expand Down Expand Up @@ -196,6 +212,7 @@ where
.get_mut(self.selected)
.unwrap()
.color(StylesheetColor::Highlight);
self.title.set_text(self.title());
}

fn next(&mut self) {
Expand All @@ -218,6 +235,10 @@ where
self.views.0.search(query)?;
Ok(())
}

fn title(&self) -> String {
title(&self.res.get::<Locale>(), self.selected)
}
}

#[async_trait(?Send)]
Expand All @@ -236,17 +257,15 @@ where
}

let mut drawn = false;
if self.battery_indicator.should_draw() && self.battery_indicator.draw(display, styles)? {
drawn = true;
}
drawn |=
self.battery_indicator.should_draw() && self.battery_indicator.draw(display, styles)?;
drawn |= self.title.should_draw() && self.title.draw(display, styles)?;
drawn |= self.view().should_draw() && self.view_mut().draw(display, styles)?;

if self.tabs.should_draw() && self.tabs.draw(display, styles)? {
drawn = true;
}
// if self.tabs.should_draw() && self.tabs.draw(display, styles)? {
// drawn = true;
// }

if self.view().should_draw() && self.view_mut().draw(display, styles)? {
drawn = true;
}
Ok(drawn)
}

Expand Down Expand Up @@ -313,3 +332,13 @@ where
unimplemented!()
}
}

fn title(locale: &Locale, selected: usize) -> String {
match selected {
0 => locale.t("tab-recents"),
1 => locale.t("tab-games"),
2 => locale.t("tab-apps"),
3 => locale.t("tab-settings"),
_ => unreachable!(),
}
}
2 changes: 2 additions & 0 deletions allium-launcher/src/view/entry_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,15 @@ where
let locale = res.get::<Locale>();

button_hints.push(ButtonHint::new(
res.clone(),
Point::zero(),
Key::A,
locale.t("button-select"),
Alignment::Right,
));
if S::HAS_BUTTON_HINTS {
button_hints.push(ButtonHint::new(
res.clone(),
Point::zero(),
Key::Y,
sort.button_hint(&locale),
Expand Down
1 change: 1 addition & 0 deletions allium-launcher/src/view/games.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl Games {
{
let locale = res.get::<Locale>();
vec![ButtonHint::new(
res.clone(),
Point::zero(),
Key::X,
locale.t("sort-search"),
Expand Down
1 change: 1 addition & 0 deletions allium-launcher/src/view/recents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl Recents {
{
let locale = res.get::<Locale>();
vec![ButtonHint::new(
res.clone(),
Point::zero(),
Key::X,
locale.t("sort-search"),
Expand Down
1 change: 1 addition & 0 deletions allium-launcher/src/view/settings/about.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl About {
rect.y + rect.h as i32 - ButtonIcon::diameter(&styles) as i32 - 8,
),
vec![ButtonHint::new(
res.clone(),
Point::zero(),
Key::B,
locale.t("button-back"),
Expand Down
1 change: 1 addition & 0 deletions allium-launcher/src/view/settings/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ impl Clock {
rect.y + rect.h as i32 - ButtonIcon::diameter(&styles) as i32 - 8,
),
vec![ButtonHint::new(
res.clone(),
Point::zero(),
Key::B,
locale.t("button-back"),
Expand Down
2 changes: 2 additions & 0 deletions allium-launcher/src/view/settings/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ impl Display {
rect.y + rect.h as i32 - ButtonIcon::diameter(&styles) as i32 - 8,
),
vec![ButtonHint::new(
res.clone(),
Point::zero(),
Key::B,
locale.t("button-back"),
Expand All @@ -119,6 +120,7 @@ impl Display {
12,
);
let edit_button = Some(ButtonHint::new(
res.clone(),
Point::zero(),
Key::A,
locale.t("button-edit"),
Expand Down
2 changes: 2 additions & 0 deletions allium-launcher/src/view/settings/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@ impl Language {
),
vec![
ButtonHint::new(
res.clone(),
Point::zero(),
Key::A,
locale.t("button-edit"),
Alignment::Right,
),
ButtonHint::new(
res.clone(),
Point::zero(),
Key::B,
locale.t("button-back"),
Expand Down
2 changes: 2 additions & 0 deletions allium-launcher/src/view/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,14 @@ impl Settings {
),
vec![
ButtonHint::new(
res.clone(),
Point::zero(),
Key::A,
locale.t("button-select"),
Alignment::Right,
),
ButtonHint::new(
res.clone(),
Point::zero(),
Key::B,
locale.t("button-back"),
Expand Down
2 changes: 2 additions & 0 deletions allium-launcher/src/view/settings/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,14 @@ impl Theme {
),
vec![
ButtonHint::new(
res.clone(),
Point::zero(),
Key::A,
locale.t("button-edit"),
Alignment::Right,
),
ButtonHint::new(
res.clone(),
Point::zero(),
Key::B,
locale.t("button-back"),
Expand Down
2 changes: 2 additions & 0 deletions allium-launcher/src/view/settings/wifi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@ impl Wifi {
),
vec![
ButtonHint::new(
res.clone(),
Point::zero(),
Key::A,
locale.t("button-edit"),
Alignment::Right,
),
ButtonHint::new(
res.clone(),
Point::zero(),
Key::B,
locale.t("button-back"),
Expand Down
2 changes: 2 additions & 0 deletions allium-menu/src/view/ingame_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,14 @@ where
),
vec![
ButtonHint::new(
res.clone(),
Point::zero(),
Key::A,
locale.t("button-select"),
Alignment::Right,
),
ButtonHint::new(
res.clone(),
Point::zero(),
Key::B,
locale.t("button-back"),
Expand Down
6 changes: 6 additions & 0 deletions allium-menu/src/view/text_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ impl TextReader {
),
vec![
ButtonHint::new(
res.clone(),
Point::zero(),
Key::X,
locale.t("guide-button-search"),
Alignment::Right,
),
ButtonHint::new(
res.clone(),
Point::zero(),
Key::B,
locale.t("button-back"),
Expand Down Expand Up @@ -220,12 +222,14 @@ impl TextReader {
if self.button_hints.children().len() <= 2 {
let locale = self.res.get::<Locale>();
self.button_hints.push(ButtonHint::new(
self.res.clone(),
Point::zero(),
Key::L2,
locale.t("guide-next"),
Alignment::Right,
));
self.button_hints.push(ButtonHint::new(
self.res.clone(),
Point::zero(),
Key::R2,
locale.t("guide-prev"),
Expand All @@ -250,12 +254,14 @@ impl TextReader {
if self.button_hints.children().len() <= 2 {
let locale = self.res.get::<Locale>();
self.button_hints.push(ButtonHint::new(
self.res.clone(),
Point::zero(),
Key::L,
locale.t("guide-next"),
Alignment::Right,
));
self.button_hints.push(ButtonHint::new(
self.res.clone(),
Point::zero(),
Key::R,
locale.t("guide-prev"),
Expand Down
17 changes: 16 additions & 1 deletion common/src/stylesheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ pub struct Stylesheet {
pub guide_font: StylesheetFont,
#[serde(skip, default = "StylesheetFont::cjk_font")]
pub cjk_font: StylesheetFont,

#[serde(default = "Stylesheet::default_title_font_size")]
pub title_font_size: f32,
#[serde(default = "Stylesheet::default_button_hint_font_size")]
pub button_hint_font_size: f32,
#[serde(default = "Stylesheet::default_alt_foreground_color")]
alt_foreground_color: Color,
#[serde(default = "Stylesheet::default_alt_background_color")]
Expand Down Expand Up @@ -263,6 +266,16 @@ rgui_particle_color = "0xFF{highlight:X}"
Ok(())
}

#[inline]
fn default_title_font_size() -> f32 {
1.3
}

#[inline]
fn default_button_hint_font_size() -> f32 {
1.0
}

#[inline]
fn default_foreground_color() -> Color {
Color::new(255, 255, 255)
Expand Down Expand Up @@ -360,6 +373,8 @@ impl Default for Stylesheet {
ui_font: StylesheetFont::ui_font(),
guide_font: StylesheetFont::guide_font(),
cjk_font: StylesheetFont::cjk_font(),
title_font_size: Self::default_title_font_size(),
button_hint_font_size: Self::default_button_hint_font_size(),
alt_foreground_color: Self::default_alt_foreground_color(),
alt_background_color: Self::default_alt_background_color(),
alt_highlight_color: Self::default_alt_highlight_color(),
Expand Down
7 changes: 5 additions & 2 deletions common/src/view/button_hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tokio::sync::mpsc::Sender;
use crate::display::Display;
use crate::geom::{Alignment, Point, Rect};
use crate::platform::{DefaultPlatform, Key, KeyEvent, Platform};
use crate::resources::Resources;
use crate::stylesheet::Stylesheet;
use crate::view::{ButtonIcon, Command, Label, View};

Expand All @@ -28,8 +29,10 @@ impl<S> ButtonHint<S>
where
S: AsRef<str> + PartialEq + Send,
{
pub fn new(point: Point, button: Key, text: S, alignment: Alignment) -> Self {
let label = Label::new(Point::zero(), text, alignment, None);
pub fn new(res: Resources, point: Point, button: Key, text: S, alignment: Alignment) -> Self {
let styles = res.get::<Stylesheet>();
let mut label = Label::new(Point::zero(), text, alignment, None);
label.font_size(styles.button_hint_font_size);
let button = ButtonIcon::new(Point::zero(), button, alignment);

Self {
Expand Down
2 changes: 1 addition & 1 deletion common/src/view/button_icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl ButtonIcon {
}

pub fn diameter(styles: &Stylesheet) -> u32 {
styles.ui_font.size
(styles.ui_font.size as f32 * styles.button_hint_font_size) as u32
}
}

Expand Down
Loading

0 comments on commit 7c72818

Please sign in to comment.