Skip to content

Commit

Permalink
feat: support wallpaper, translucent colors
Browse files Browse the repository at this point in the history
  • Loading branch information
goweiwen committed Jan 27, 2024
1 parent 7c72818 commit a64137a
Show file tree
Hide file tree
Showing 13 changed files with 255 additions and 143 deletions.
1 change: 1 addition & 0 deletions activity-tracker/src/view/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ where
let locale = res.get::<Locale>();

let battery_indicator = BatteryIndicator::new(
res.clone(),
Point::new(w as i32 - 12, y + 8),
battery,
styles.show_battery_level,
Expand Down
60 changes: 54 additions & 6 deletions allium-launcher/src/allium_launcher.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
use std::collections::VecDeque;
use std::path::Path;
use std::process;
use std::time::Instant;

use anyhow::Result;
use common::command::Command;
use common::constants::ALLIUM_GAMES_DIR;
use common::constants::{ALLIUM_GAMES_DIR, ALLIUM_SD_ROOT};
use common::display::color::Color;
use common::geom;
use common::locale::{Locale, LocaleSettings};
use common::resources::Resources;
use common::view::View;
use embedded_graphics::image::ImageRaw;
use embedded_graphics::prelude::*;
use enum_map::EnumMap;
use log::{info, trace, warn};
use log::{error, info, trace, warn};

use common::database::Database;
use common::display::Display;
Expand Down Expand Up @@ -62,8 +64,19 @@ impl AlliumLauncher<DefaultPlatform> {
}

pub async fn run_event_loop(&mut self) -> Result<()> {
self.display
.clear(self.res.get::<Stylesheet>().background_color)?;
{
let styles = self.res.get::<Stylesheet>();

if let Some(wallpaper) = styles.wallpaper.as_deref() {
let path = ALLIUM_SD_ROOT.join(wallpaper);
if let Err(e) = set_wallpaper(&mut self.display, &path) {
error!("Failed to set wallpaper: {}", e);
}
}

self.display.clear(styles.background_color)?;
}

self.display.save()?;

#[cfg(unix)]
Expand Down Expand Up @@ -180,8 +193,23 @@ impl AlliumLauncher<DefaultPlatform> {
trace!("saving stylesheet");
styles.load_fonts()?;
styles.save()?;
self.display.clear(styles.background_color)?;
self.display.save()?;

{
let old_styles = self.res.get::<Stylesheet>();
if old_styles.wallpaper != styles.wallpaper
|| old_styles.background_color != styles.background_color
{
if let Some(wallpaper) = styles.wallpaper.as_deref() {
let path = ALLIUM_SD_ROOT.join(wallpaper);
if let Err(e) = set_wallpaper(&mut self.display, &path) {
error!("Failed to set wallpaper: {}", e);
}
}
self.display.clear(styles.background_color)?;
self.display.save()?;
}
}

self.res.insert(*styles);
self.view.save()?;
self.view = App::load_or_new(
Expand Down Expand Up @@ -296,3 +324,23 @@ impl AlliumLauncher<DefaultPlatform> {
Ok(())
}
}

fn set_wallpaper(display: &mut impl Display, path: &Path) -> Result<()> {
if !path.exists() {
return Ok(());
}

let rect = display.bounding_box().size;

let image = ::image::open(path)?;
let image = image.resize_to_fill(
rect.width,
rect.height,
image::imageops::FilterType::Lanczos3,
);
let image = image.into_rgba8();
let image: ImageRaw<'_, Color> = ImageRaw::new(&image, rect.width);
let image = embedded_graphics::image::Image::new(&image, display.bounding_box().top_left);
image.draw(display)?;
Ok(())
}
1 change: 1 addition & 0 deletions allium-launcher/src/view/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ where
let locale = res.get::<Locale>();

let battery_indicator = BatteryIndicator::new(
res.clone(),
Point::new(w as i32 - 12, y + 8),
battery,
styles.show_battery_level,
Expand Down
14 changes: 14 additions & 0 deletions allium-launcher/src/view/settings/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,36 +64,50 @@ impl Display {
)),
Box::new(Percentage::new(
Point::zero(),
0,
100,
i32::from(settings.luminance),
Alignment::Right,
)),
Box::new(Percentage::new(
Point::zero(),
0,
100,
i32::from(settings.hue),
Alignment::Right,
)),
Box::new(Percentage::new(
Point::zero(),
0,
100,
i32::from(settings.saturation),
Alignment::Right,
)),
Box::new(Percentage::new(
Point::zero(),
0,
100,
i32::from(settings.contrast),
Alignment::Right,
)),
Box::new(Percentage::new(
Point::zero(),
0,
100,
i32::from(settings.r),
Alignment::Right,
)),
Box::new(Percentage::new(
Point::zero(),
0,
100,
i32::from(settings.g),
Alignment::Right,
)),
Box::new(Percentage::new(
Point::zero(),
0,
100,
i32::from(settings.b),
Alignment::Right,
)),
Expand Down
66 changes: 50 additions & 16 deletions allium-launcher/src/view/settings/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use common::platform::{DefaultPlatform, Key, KeyEvent, Platform};
use common::resources::Resources;
use common::stylesheet::{Stylesheet, StylesheetFont};
use common::view::{
ButtonHint, ButtonIcon, ColorPicker, Number, Row, Select, SettingsList, Toggle, View,
ButtonHint, ButtonIcon, ColorPicker, Number, Percentage, Row, Select, SettingsList, Toggle,
View,
};
use tokio::sync::mpsc::Sender;

Expand Down Expand Up @@ -59,6 +60,9 @@ impl Theme {
locale.t("settings-theme-ui-font-size"),
locale.t("settings-theme-guide-font"),
locale.t("settings-theme-guide-font-size"),
locale.t("settings-theme-title-font-size"),
locale.t("settings-theme-status-bar-font-size"),
locale.t("settings-theme-button-hint-font-size"),
locale.t("settings-theme-highlight-color"),
locale.t("settings-theme-foreground-color"),
locale.t("settings-theme-background-color"),
Expand Down Expand Up @@ -111,6 +115,27 @@ impl Theme {
60,
Alignment::Right,
)),
Box::new(Percentage::new(
Point::zero(),
(stylesheet.title_font_size * 100.0) as i32,
50,
200,
Alignment::Right,
)),
Box::new(Percentage::new(
Point::zero(),
(stylesheet.status_bar_font_size * 100.0) as i32,
50,
200,
Alignment::Right,
)),
Box::new(Percentage::new(
Point::zero(),
(stylesheet.button_hint_font_size * 100.0) as i32,
50,
200,
Alignment::Right,
)),
Box::new(ColorPicker::new(
Point::zero(),
stylesheet.highlight_color,
Expand Down Expand Up @@ -239,55 +264,55 @@ impl View for Theme {
0 => {
self.stylesheet.toggle_dark_mode();
self.list.set_right(
6,
10,
Box::new(ColorPicker::new(
Point::zero(),
self.stylesheet.foreground_color,
Alignment::Right,
)),
);
self.list.set_right(
7,
11,
Box::new(ColorPicker::new(
Point::zero(),
self.stylesheet.background_color,
Alignment::Right,
)),
);
self.list.set_right(
8,
12,
Box::new(ColorPicker::new(
Point::zero(),
self.stylesheet.disabled_color,
Alignment::Right,
)),
);
self.list.set_right(
9,
13,
Box::new(ColorPicker::new(
Point::zero(),
self.stylesheet.button_a_color,
Alignment::Right,
)),
);
self.list.set_right(
10,
14,
Box::new(ColorPicker::new(
Point::zero(),
self.stylesheet.button_b_color,
Alignment::Right,
)),
);
self.list.set_right(
11,
15,
Box::new(ColorPicker::new(
Point::zero(),
self.stylesheet.button_x_color,
Alignment::Right,
)),
);
self.list.set_right(
12,
16,
Box::new(ColorPicker::new(
Point::zero(),
self.stylesheet.button_y_color,
Expand All @@ -306,14 +331,23 @@ impl View for Theme {
self.fonts[val.as_int().unwrap() as usize].clone()
}
5 => self.stylesheet.guide_font.size = val.as_int().unwrap() as u32,
6 => self.stylesheet.highlight_color = val.as_color().unwrap(),
7 => self.stylesheet.foreground_color = val.as_color().unwrap(),
8 => self.stylesheet.background_color = val.as_color().unwrap(),
9 => self.stylesheet.disabled_color = val.as_color().unwrap(),
10 => self.stylesheet.button_a_color = val.as_color().unwrap(),
11 => self.stylesheet.button_b_color = val.as_color().unwrap(),
12 => self.stylesheet.button_x_color = val.as_color().unwrap(),
13 => self.stylesheet.button_y_color = val.as_color().unwrap(),
6 => self.stylesheet.title_font_size = val.as_int().unwrap() as f32 / 100.0,
7 => {
self.stylesheet.status_bar_font_size =
val.as_int().unwrap() as f32 / 100.0
}
8 => {
self.stylesheet.button_hint_font_size =
val.as_int().unwrap() as f32 / 100.0
}
9 => self.stylesheet.highlight_color = val.as_color().unwrap(),
10 => self.stylesheet.foreground_color = val.as_color().unwrap(),
11 => self.stylesheet.background_color = val.as_color().unwrap(),
12 => self.stylesheet.disabled_color = val.as_color().unwrap(),
13 => self.stylesheet.button_a_color = val.as_color().unwrap(),
14 => self.stylesheet.button_b_color = val.as_color().unwrap(),
15 => self.stylesheet.button_x_color = val.as_color().unwrap(),
16 => self.stylesheet.button_y_color = val.as_color().unwrap(),
_ => unreachable!("Invalid index"),
}

Expand Down
1 change: 1 addition & 0 deletions allium-menu/src/view/ingame_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ where
name.color(common::stylesheet::StylesheetColor::Highlight);

let battery_indicator = BatteryIndicator::new(
res.clone(),
Point::new(w as i32 - 12, y + 8),
battery,
styles.show_battery_level,
Expand Down
22 changes: 19 additions & 3 deletions common/src/display/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ impl Color {
Self((b as u32) << 8 | self.0 & 0xFFFF00FF)
}

#[inline]
pub fn with_a(&self, a: u8) -> Self {
Self((a as u32) << 0 | self.0 & 0xFFFFFF00)
}

pub fn char(&self, i: usize) -> String {
format!(
"{:X}",
Expand All @@ -64,6 +69,8 @@ impl Color {
3 => self.g() % 16,
4 => self.b() / 16,
5 => self.b() % 16,
6 => self.a() / 16,
7 => self.a() % 16,
_ => unreachable!(),
}
)
Expand Down Expand Up @@ -99,8 +106,12 @@ impl Color {

impl Serialize for Color {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let (r, g, b) = (self.r(), self.g(), self.b());
let hex = format!("#{:02x}{:02x}{:02x}", r, g, b);
let (r, g, b, a) = (self.r(), self.g(), self.b(), self.a());
let hex = if a < 255 {
format!("#{:02x}{:02x}{:02x}{:02x}", r, g, b, a)
} else {
format!("#{:02x}{:02x}{:02x}", r, g, b)
};
serializer.serialize_str(&hex)
}
}
Expand All @@ -112,7 +123,12 @@ impl<'de> Deserialize<'de> for Color {
let r = u8::from_str_radix(&hex[0..2], 16).map_err(serde::de::Error::custom)?;
let g = u8::from_str_radix(&hex[2..4], 16).map_err(serde::de::Error::custom)?;
let b = u8::from_str_radix(&hex[4..6], 16).map_err(serde::de::Error::custom)?;
Ok(Color::new(r, g, b))
Ok(if hex.len() == 8 {
let a = u8::from_str_radix(&hex[6..8], 16).map_err(serde::de::Error::custom)?;
Color::rgba(r, g, b, a)
} else {
Color::new(r, g, b)
})
}
}

Expand Down
Loading

0 comments on commit a64137a

Please sign in to comment.