Skip to content

Commit

Permalink
Improve color customization
Browse files Browse the repository at this point in the history
  • Loading branch information
thorio committed Mar 24, 2024
1 parent 692e8fa commit 8a8c037
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 23 deletions.
18 changes: 12 additions & 6 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,22 @@ frontend:
# ## Color configuration. Use the hex color: `0xRRGGBB`.
# ## Google has a color picker if you search for, well, "color picker".
# colors:
#
# ## Background color of the window.
# background: 0x202020
#
# ## Text/hit selection and scrollbar color.
# accent: 0xbf6c0d
#
# ## Color of all text.
# text: 0xffffff
#
# ## Alternative representation which is more customizable.
# ## Chose *either* block, not both.
# # colors:
# background: 0x202020
# query_text: 0xffffff
# query_cursor: 0xbf6c0d
# query_highlight: 0xbf6c0d
# hit_title: 0xffffff
# hit_subtitle: 0xffffff
# hit_highlight: 0xbf6c0d
# scrollbar: 0xbf6c0d
#
# behaviour:
#
# ## Whether or not to show the window when starting gravel
Expand Down
18 changes: 12 additions & 6 deletions gravel-frontend-fltk/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,22 @@ layout:
## Color configuration. Use the hex color: `0xRRGGBB`.
## Google has a color picker if you search for, well, "color picker".
colors:

## Background color of the window.
background: 0x202020

## Text/hit selection and scrollbar color.
accent: 0xbf6c0d

## Color of all text.
text: 0xffffff

## Alternative representation which is more customizable.
## Chose *either* block, not both.
# colors:
background: 0x202020
query_text: 0xffffff
query_cursor: 0xbf6c0d
query_highlight: 0xbf6c0d
hit_title: 0xffffff
hit_subtitle: 0xffffff
hit_highlight: 0xbf6c0d
scrollbar: 0xbf6c0d

behaviour:

## Whether or not to show the window when starting gravel
Expand Down
14 changes: 7 additions & 7 deletions gravel-frontend-fltk/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ fn build_input(config: &Config) -> Input {
input.set_text_size(config.layout.query_font_size);
input.set_frame(FrameType::FlatBox);
input.set_color(config.colors.background);
input.set_text_color(config.colors.text);
input.set_selection_color(config.colors.accent);
input.set_cursor_color(config.colors.accent);
input.set_text_color(config.colors.query_text);
input.set_selection_color(config.colors.query_highlight);
input.set_cursor_color(config.colors.query_cursor);

input
}
Expand All @@ -91,7 +91,7 @@ fn build_scrollbar(config: &Config) -> Scrollbar {
.with_pos(config.layout.scrollbar_x, config.layout.scrollbar_y)
.with_size(config.layout.scrollbar_width, config.layout.scrollbar_height)
.with_padding(config.layout.scrollbar_padding)
.with_colors(config.colors.background, config.colors.accent)
.with_colors(config.colors.background, config.colors.scrollbar)
}

fn build_hit(i: i32, config: &Config) -> HitUi {
Expand All @@ -101,7 +101,7 @@ fn build_hit(i: i32, config: &Config) -> HitUi {
.with_pos(config.layout.padding, y)
.with_size(config.layout.hit_width, config.layout.hit_height);

group.set_color(config.colors.accent);
group.set_color(config.colors.hit_highlight);
group.set_frame(FrameType::FlatBox);

let mut title = Frame::default()
Expand All @@ -110,15 +110,15 @@ fn build_hit(i: i32, config: &Config) -> HitUi {
.with_align(Align::BottomLeft | Align::Inside | Align::Clip);

title.set_label_size(config.layout.hit_title_font_size);
title.set_label_color(config.colors.text);
title.set_label_color(config.colors.hit_title);

let mut subtitle = Frame::default()
.with_pos(config.layout.padding, y + config.layout.hit_title_height)
.with_size(config.layout.hit_width, config.layout.hit_subtitle_height)
.with_align(Align::TopLeft | Align::Inside | Align::Clip);

subtitle.set_label_size(config.layout.hit_subtitle_font_size);
subtitle.set_label_color(config.colors.text);
subtitle.set_label_color(config.colors.hit_subtitle);

group.show();
group.end();
Expand Down
64 changes: 60 additions & 4 deletions gravel-frontend-fltk/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,29 @@ pub struct Layout {
#[derive(Deserialize, Debug)]
pub struct Config {
pub layout: Layout,
pub colors: Colors,
#[serde(deserialize_with = "deserialize::colors")]
pub colors: DetailedColors,
pub behaviour: Behaviour,
}

#[derive(Deserialize, Debug)]
pub struct Colors {
pub struct DetailedColors {
#[serde(deserialize_with = "deserialize::color")]
pub background: Color,
#[serde(deserialize_with = "deserialize::color")]
pub accent: Color,
pub query_text: Color,
#[serde(deserialize_with = "deserialize::color")]
pub text: Color,
pub query_cursor: Color,
#[serde(deserialize_with = "deserialize::color")]
pub query_highlight: Color,
#[serde(deserialize_with = "deserialize::color")]
pub hit_title: Color,
#[serde(deserialize_with = "deserialize::color")]
pub hit_subtitle: Color,
#[serde(deserialize_with = "deserialize::color")]
pub hit_highlight: Color,
#[serde(deserialize_with = "deserialize::color")]
pub scrollbar: Color,
}

pub mod deserialize {
Expand All @@ -75,6 +86,51 @@ pub mod deserialize {
u32::deserialize(de).map(Color::from_hex)
}

pub fn colors<'de, D: Deserializer<'de>>(de: D) -> Result<super::DetailedColors, D::Error> {
ColorVariants::deserialize(de).map(|v| v.into())
}

#[derive(Deserialize, Debug)]
#[serde(untagged)]
pub enum ColorVariants {
SimpleColors(SimpleColors),
DetailedColors(super::DetailedColors),
}

#[derive(Deserialize, Debug)]
pub struct SimpleColors {
#[serde(deserialize_with = "color")]
pub background: Color,
#[serde(deserialize_with = "color")]
pub accent: Color,
#[serde(deserialize_with = "color")]
pub text: Color,
}

impl From<ColorVariants> for super::DetailedColors {
fn from(val: ColorVariants) -> Self {
match val {
ColorVariants::SimpleColors(colors) => colors.into(),
ColorVariants::DetailedColors(colors) => colors,
}
}
}

impl From<SimpleColors> for super::DetailedColors {
fn from(val: SimpleColors) -> Self {
super::DetailedColors {
background: val.background,
query_text: val.text,
query_cursor: val.accent,
query_highlight: val.accent,
hit_title: val.text,
hit_subtitle: val.text,
hit_highlight: val.accent,
scrollbar: val.accent,
}
}
}

#[derive(Deserialize, Debug)]
pub struct Layout {
pub scale: f32,
Expand Down

0 comments on commit 8a8c037

Please sign in to comment.