Skip to content

Commit

Permalink
feat: add theme scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Feb 13, 2024
1 parent abb520c commit 8d496ac
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/target
.fleet
.idea
flamegraph.svg
perf.data
perf.data.old
perf.data.old
13 changes: 13 additions & 0 deletions src/app/style/app.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::app::style::scrollable::scroller::ScrollerStyles;
use crate::app::style::scrollable::RowContainerStyle;
use crate::app::style::search::SearchContainerStyles;
use crate::app::style::Scale;
use crate::config::color::OnagreColor;
use crate::config::padding::OnagrePadding;
use iced_core::{Background, BorderRadius};
Expand Down Expand Up @@ -29,6 +30,18 @@ pub struct AppContainerStyles {
pub scrollable: ScrollerStyles,
}

impl Scale for AppContainerStyles {
fn scale(mut self, scale: f32) -> Self {
self.search = self.search.scale(scale);
self.padding = self.padding * scale;
self.rows = self.rows.scale(scale);
self.search = self.search.scale(scale);
self.scrollable = self.scrollable.scale(scale);
self.border_width = self.border_width.scale(scale);
self
}
}

impl StyleSheet for &AppContainerStyles {
type Style = iced::Theme;

Expand Down
47 changes: 45 additions & 2 deletions src/app/style/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use crate::app::style::search::SearchContainerStyles;
use crate::config::color::OnagreColor;
use crate::config::padding::OnagrePadding;
use crate::THEME_PATH;
use crate::THEME_SCALE;
use iced::widget::container::Appearance;
use iced::Background;
use iced_core::BorderRadius;
use iced_core::{BorderRadius, Length};
use tracing::{error, warn};

pub mod app;
Expand All @@ -25,10 +26,19 @@ impl Theme {
warn!("Failing back to default theme");
};

theme.unwrap_or_default()
let mut theme = theme.unwrap_or_default();
if let Some(scale) = THEME_SCALE.get() {
theme = theme.scale(*scale)
}

theme
}
}

pub(crate) trait Scale {
fn scale(self, scale: f32) -> Self;
}

impl AsRef<Theme> for Theme {
fn as_ref(&self) -> &Theme {
self
Expand Down Expand Up @@ -57,6 +67,39 @@ pub struct Theme {
pub app_container: AppContainerStyles,
}

impl Scale for Theme {
fn scale(mut self, scale: f32) -> Self {
self.app_container = self.app_container.scale(scale);
self.icon_size = (self.icon_size as f32 * scale) as u16;
self.size.0 = (self.size.0 as f32 * scale) as u32;
self.size.1 = (self.size.1 as f32 * scale) as u32;
self.padding = self.padding * scale;
self.font_size = (self.font_size as f32 * scale) as u16;
self
}
}

impl Scale for Length {
fn scale(self, scale: f32) -> Self {
match self {
Length::Fixed(size) => Length::Fixed(size * scale),
_ => self,
}
}
}

impl Scale for u16 {
fn scale(self, scale: f32) -> Self {
(self as f32 * scale) as u16
}
}

impl Scale for f32 {
fn scale(self, scale: f32) -> Self {
self * scale
}
}

impl Theme {
pub fn search(&self) -> &SearchContainerStyles {
&self.app_container.search
Expand Down
11 changes: 11 additions & 0 deletions src/app/style/rows/generic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::app::style::Scale;
use crate::config::color::OnagreColor;
use crate::config::padding::OnagrePadding;
use iced::alignment::{Horizontal, Vertical};
Expand All @@ -23,6 +24,16 @@ pub struct GenericContainerStyle {
pub height: Length,
}

impl Scale for GenericContainerStyle {
fn scale(mut self, scale: f32) -> Self {
self.height = self.height.scale(scale);
self.width = self.width.scale(scale);
self.font_size = self.font_size.scale(scale);
self.border_width = self.border_width.scale(scale);
self.padding = self.padding.scale(scale);
self
}
}
impl Default for GenericContainerStyle {
fn default() -> Self {
GenericContainerStyle {
Expand Down
12 changes: 12 additions & 0 deletions src/app/style/rows/icon.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::app::style::Scale;
use crate::config::color::OnagreColor;
use crate::config::padding::OnagrePadding;
use iced::alignment::{Horizontal, Vertical};
Expand All @@ -21,6 +22,17 @@ pub struct IconStyle {
pub icon_size: u16,
}

impl Scale for IconStyle {
fn scale(mut self, scale: f32) -> Self {
self.height = self.height.scale(scale);
self.width = self.width.scale(scale);
self.padding = self.padding.scale(scale);
self.border_width = self.border_width.scale(scale);
self.icon_size = self.icon_size.scale(scale);
self
}
}

impl Eq for IconStyle {}

impl Default for IconStyle {
Expand Down
14 changes: 14 additions & 0 deletions src/app/style/rows/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::app::style::Scale;
use crate::config::color::OnagreColor;
use crate::config::padding::OnagrePadding;
use generic::GenericContainerStyle;
Expand Down Expand Up @@ -37,6 +38,19 @@ pub struct RowStyles {
pub category_icon: IconStyle,
}

impl Scale for RowStyles {
fn scale(mut self, scale: f32) -> Self {
self.height = self.height.scale(scale);
self.width = self.width.scale(scale);
self.spacing = self.spacing.scale(scale);
self.border_width = self.border_width.scale(scale);
self.title = self.title.scale(scale);
self.description = self.description.scale(scale);
self.icon = self.icon.scale(scale);
self.category_icon = self.category_icon.scale(scale);
self
}
}
impl StyleSheet for &RowStyles {
type Style = iced::Theme;

Expand Down
13 changes: 13 additions & 0 deletions src/app/style/scrollable/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::app::style::rows::RowStyles;
use crate::app::style::Scale;
use crate::config::color::OnagreColor;
use crate::config::padding::OnagrePadding;
use iced::Length;
Expand Down Expand Up @@ -26,6 +27,18 @@ pub struct RowContainerStyle {
pub row_selected: RowStyles,
}

impl Scale for RowContainerStyle {
fn scale(mut self, scale: f32) -> Self {
self.padding = self.padding.scale(scale);
self.border_width = self.border_width.scale(scale);
self.width = self.width.scale(scale);
self.height = self.height.scale(scale);
self.row = self.row.scale(scale);
self.row_selected = self.row_selected.scale(scale);
self
}
}

impl Eq for RowContainerStyle {}

impl StyleSheet for &RowContainerStyle {
Expand Down
12 changes: 12 additions & 0 deletions src/app/style/scrollable/scroller.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::app::style::Scale;
use crate::config::color::OnagreColor;
use iced::widget::scrollable::Scrollbar;
use iced::Background;
Expand All @@ -20,6 +21,17 @@ pub struct ScrollerStyles {
pub scroller_width: u16,
}

impl Scale for ScrollerStyles {
fn scale(mut self, scale: f32) -> Self {
self.border_width = self.border_width.scale(scale);
self.scroller_border_width = self.scroller_border_width.scale(scale);
self.scrollbar_margin = self.scrollbar_margin.scale(scale);
self.scrollbar_width = self.scrollbar_width.scale(scale);
self.scroller_width = self.scroller_width.scale(scale);
self
}
}

impl Eq for ScrollerStyles {}

impl Default for ScrollerStyles {
Expand Down
12 changes: 12 additions & 0 deletions src/app/style/search/input.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::app::style::Scale;
use crate::config::color::OnagreColor;
use crate::config::padding::OnagrePadding;
use iced::alignment::{Horizontal, Vertical};
Expand Down Expand Up @@ -26,6 +27,17 @@ pub struct SearchInputStyles {
pub padding: OnagrePadding,
}

impl Scale for SearchInputStyles {
fn scale(mut self, scale: f32) -> Self {
self.height = self.height.scale(scale);
self.width = self.width.scale(scale);
self.padding = self.padding.scale(scale);
self.padding = self.padding.scale(scale);
self.padding = self.padding.scale(scale);
self
}
}

impl Eq for SearchInputStyles {}

impl StyleSheet for &SearchInputStyles {
Expand Down
21 changes: 18 additions & 3 deletions src/app/style/search/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use crate::app::style::rows::generic::GenericContainerStyle;
use crate::config::color::OnagreColor;
use crate::config::padding::OnagrePadding;
use iced::alignment::{Horizontal, Vertical};
use iced::Length;
use iced_core::{Background, BorderRadius};
use iced_style::container::{Appearance, StyleSheet};

use input::SearchInputStyles;

use crate::app::style::rows::generic::GenericContainerStyle;
use crate::app::style::Scale;
use crate::config::color::OnagreColor;
use crate::config::padding::OnagrePadding;

pub mod hint;
pub mod input;

Expand All @@ -32,6 +35,18 @@ pub struct SearchContainerStyles {
pub plugin_hint: Option<GenericContainerStyle>,
}

impl Scale for SearchContainerStyles {
fn scale(mut self, scale: f32) -> Self {
self.padding = self.padding.scale(scale);
self.border_width = self.border_width.scale(scale);
self.spacing = self.spacing.scale(scale);
self.width = self.width.scale(scale);
self.height = self.height.scale(scale);
self.input = self.input.scale(scale);
self
}
}

impl Eq for SearchContainerStyles {}

impl StyleSheet for &SearchContainerStyles {
Expand Down
21 changes: 21 additions & 0 deletions src/config/padding.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::app::style::Scale;
use iced::Padding;
use std::ops::Mul;

#[derive(Debug, PartialEq, Clone)]
pub struct OnagrePadding {
Expand All @@ -8,6 +10,25 @@ pub struct OnagrePadding {
pub left: u16,
}

impl Mul<f32> for OnagrePadding {
type Output = OnagrePadding;

fn mul(self, rhs: f32) -> Self::Output {
OnagrePadding {
top: (self.top as f32 * rhs) as u16,
right: (self.right as f32 * rhs) as u16,
bottom: (self.bottom as f32 * rhs) as u16,
left: (self.left as f32 * rhs) as u16,
}
}
}

impl Scale for OnagrePadding {
fn scale(self, scale: f32) -> Self {
self * scale
}
}

impl OnagrePadding {
pub const ZERO: OnagrePadding = OnagrePadding {
top: 0,
Expand Down
15 changes: 13 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Mutex;

use anyhow::anyhow;
use clap::Parser;
use once_cell::sync::Lazy;
use once_cell::sync::{Lazy, OnceCell};
use tracing::{debug, info};
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
Expand All @@ -25,10 +25,12 @@ pub static THEME_PATH: Lazy<Mutex<PathBuf>> = Lazy::new(|| {
)
});

static THEME_SCALE: OnceCell<f32> = OnceCell::new();

pub static THEME: Lazy<Theme> = Lazy::new(Theme::load);

#[derive(Parser)]
#[structopt(name = "onagre", author = "Paul D. <paul.delafosse@protonmail.com>")]
#[command(name = "onagre", author = "Paul D. <paul.delafosse@protonmail.com>")]
struct Cli {
#[arg(
long = "theme",
Expand All @@ -37,6 +39,9 @@ struct Cli {
)]
theme: Option<PathBuf>,

#[arg(long = "scale", short = 's', help = "Change the scale of onagre theme")]
scale: Option<f32>,

#[arg(long = "mode", short = 'm', help = "The mode parameter as a string")]
mode: Option<String>,
}
Expand All @@ -51,6 +56,7 @@ pub fn main() -> iced::Result {

info!("Starting onagre");
let cli = Cli::parse();

// User defined theme config, $XDG_CONFIG_DIR/onagre/theme.toml otherwise
if let Some(theme_path) = cli.theme {
let path = theme_path.canonicalize();
Expand All @@ -61,6 +67,11 @@ pub fn main() -> iced::Result {
info!("Using alternate theme : {:?}", THEME_PATH.lock().unwrap());
}

if let Some(scale) = cli.scale {
THEME_SCALE.get_or_init(|| scale);
info!("Using scale value : {:?}", scale);
}

if let Some(mode) = cli.mode {
debug!("Mode parameter: {:?}", mode);

Expand Down

0 comments on commit 8d496ac

Please sign in to comment.