Skip to content

Commit

Permalink
feat: global icon theme setting
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This removes the `icon_theme` option from `launcher` and `focused`. You will need to set this at the top of your config instead.
  • Loading branch information
JakeStanger committed Jan 30, 2023
1 parent 393800a commit 3cf9be8
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 40 deletions.
1 change: 1 addition & 0 deletions docs/Configuration guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ The following table lists each of the top-level bar config options:
| `position` | `top` or `bottom` or `left` or `right` | `bottom` | The bar's position on screen. |
| `anchor_to_edges` | `boolean` | `false` | Whether to anchor the bar to the edges of the screen. Setting to false centres the bar. |
| `height` | `integer` | `42` | The bar's height in pixels. |
| `icon_theme` | `string` | `null` | Name of the GTK icon theme to use. Leave blank to use default. |
| `start` | `Module[]` | `[]` | Array of left or top modules. |
| `center` | `Module[]` | `[]` | Array of center modules. |
| `end` | `Module[]` | `[]` | Array of right or bottom modules. |
Expand Down
1 change: 0 additions & 1 deletion docs/modules/Focused.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Displays the title and/or icon of the currently focused window.
| `show_icon` | `boolean` | `true` | Whether to show the app's icon |
| `show_title` | `boolean` | `true` | Whether to show the app's title |
| `icon_size` | `integer` | `32` | Size of icon in pixels |
| `icon_theme` | `string` | `null` | GTK icon theme to use |
| `truncate` or `truncate.mode` | `start` or `middle` or `end` | `null` | The location of the ellipses and where to truncate text from. Leave null to avoid truncating. Use the long-hand version if specifying a length. |
| `truncate.length` | `integer` | `null` | The maximum number of characters before truncating. Leave blank to let GTK automatically handle. |

Expand Down
1 change: 0 additions & 1 deletion docs/modules/Launcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Optionally displays a launchable set of favourites.
| `favorites` | `string[]` | `[]` | List of app IDs (or classes) to always show at the start of the launcher |
| `show_names` | `boolean` | `false` | Whether to show app names on the button label. Names will still show on tooltips when set to false. |
| `show_icons` | `boolean` | `true` | Whether to show app icons on the button. |
| `icon_theme` | `string` | `null` | GTK icon theme to use. |

<details>
<summary>JSON</summary>
Expand Down
8 changes: 7 additions & 1 deletion src/bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{await_sync, read_lock, send, write_lock, Config};
use color_eyre::Result;
use gtk::gdk::{EventMask, Monitor, ScrollDirection};
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, EventBox, Orientation, Widget};
use gtk::{Application, ApplicationWindow, EventBox, IconTheme, Orientation, Widget};
use std::sync::{Arc, RwLock};
use tokio::spawn;
use tokio::sync::mpsc;
Expand Down Expand Up @@ -140,6 +140,11 @@ fn load_modules(
monitor: &Monitor,
output_name: &str,
) -> Result<()> {
let icon_theme = IconTheme::new();
if let Some(ref theme) = config.icon_theme {
icon_theme.set_custom_theme(Some(theme));
}

macro_rules! info {
($location:expr) => {
ModuleInfo {
Expand All @@ -148,6 +153,7 @@ fn load_modules(
monitor,
output_name,
location: $location,
icon_theme: &icon_theme,
}
};
}
Expand Down
3 changes: 3 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ pub struct Config {
#[serde(default = "default_bar_height")]
pub height: i32,

/// GTK icon theme to use.
pub icon_theme: Option<String>,

pub start: Option<Vec<ModuleConfig>>,
pub center: Option<Vec<ModuleConfig>>,
pub end: Option<Vec<ModuleConfig>>,
Expand Down
10 changes: 10 additions & 0 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ impl<'a> ImageProvider<'a> {
Ok(Self { location, size })
}

/// Returns true if the input starts with a prefix
/// that is supported by the parser
/// (ie the parser would not fallback to checking the input).
pub fn is_definitely_image_input(input: &str) -> bool {
input.starts_with("icon:")
|| input.starts_with("file://")
|| input.starts_with("http://")
|| input.starts_with("https://")
}

fn get_location(input: String, theme: &'a IconTheme, size: i32) -> Result<ImageLocation> {
let (input_type, input_name) = input
.split_once(':')
Expand Down
3 changes: 2 additions & 1 deletion src/modules/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl Module<Button> for ClockModule {
});
}

let popup = self.into_popup(context.controller_tx, context.popup_rx);
let popup = self.into_popup(context.controller_tx, context.popup_rx, info);

Ok(ModuleWidget {
widget: button,
Expand All @@ -94,6 +94,7 @@ impl Module<Button> for ClockModule {
self,
_tx: mpsc::Sender<Self::ReceiveMessage>,
rx: glib::Receiver<Self::SendMessage>,
_info: &ModuleInfo,
) -> Option<gtk::Box> {
let container = gtk::Box::builder()
.orientation(Orientation::Vertical)
Expand Down
51 changes: 36 additions & 15 deletions src/modules/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,28 @@ pub enum WidgetType {

impl Widget {
/// Creates this widget and adds it to the parent container
fn add_to(self, parent: &gtk::Box, tx: Sender<ExecEvent>, bar_orientation: Orientation) {
fn add_to(
self,
parent: &gtk::Box,
tx: Sender<ExecEvent>,
bar_orientation: Orientation,
icon_theme: &IconTheme,
) {
match self.widget_type {
WidgetType::Box => parent.add(&self.into_box(&tx, bar_orientation)),
WidgetType::Box => parent.add(&self.into_box(&tx, bar_orientation, icon_theme)),
WidgetType::Label => parent.add(&self.into_label()),
WidgetType::Button => parent.add(&self.into_button(tx, bar_orientation)),
WidgetType::Image => parent.add(&self.into_image()),
WidgetType::Image => parent.add(&self.into_image(icon_theme)),
}
}

/// Creates a `gtk::Box` from this widget
fn into_box(self, tx: &Sender<ExecEvent>, bar_orientation: Orientation) -> gtk::Box {
fn into_box(
self,
tx: &Sender<ExecEvent>,
bar_orientation: Orientation,
icon_theme: &IconTheme,
) -> gtk::Box {
let mut builder = gtk::Box::builder();

if let Some(name) = self.name {
Expand All @@ -92,9 +103,9 @@ impl Widget {
}

if let Some(widgets) = self.widgets {
widgets
.into_iter()
.for_each(|widget| widget.add_to(&container, tx.clone(), bar_orientation));
widgets.into_iter().for_each(|widget| {
widget.add_to(&container, tx.clone(), bar_orientation, icon_theme)
});
}

container
Expand Down Expand Up @@ -163,7 +174,7 @@ impl Widget {
button
}

fn into_image(self) -> gtk::Image {
fn into_image(self, icon_theme: &IconTheme) -> gtk::Image {
let mut builder = gtk::Image::builder();

if let Some(name) = self.name {
Expand All @@ -173,9 +184,8 @@ impl Widget {
let gtk_image = builder.build();

if let Some(src) = self.src {
let theme = IconTheme::new();
let size = self.size.unwrap_or(32);
if let Err(err) = ImageProvider::parse(src, &theme, size)
if let Err(err) = ImageProvider::parse(src, icon_theme, size)
.and_then(|image| image.load_into_image(gtk_image.clone()))
{
error!("{err:?}");
Expand Down Expand Up @@ -248,10 +258,15 @@ impl Module<gtk::Box> for CustomModule {
}

self.bar.clone().into_iter().for_each(|widget| {
widget.add_to(&container, context.controller_tx.clone(), orientation);
widget.add_to(
&container,
context.controller_tx.clone(),
orientation,
info.icon_theme,
);
});

let popup = self.into_popup(context.controller_tx, context.popup_rx);
let popup = self.into_popup(context.controller_tx, context.popup_rx, info);

Ok(ModuleWidget {
widget: container,
Expand All @@ -263,6 +278,7 @@ impl Module<gtk::Box> for CustomModule {
self,
tx: Sender<Self::ReceiveMessage>,
_rx: glib::Receiver<Self::SendMessage>,
info: &ModuleInfo,
) -> Option<gtk::Box>
where
Self: Sized,
Expand All @@ -276,9 +292,14 @@ impl Module<gtk::Box> for CustomModule {
}

if let Some(popup) = self.popup {
popup
.into_iter()
.for_each(|widget| widget.add_to(&container, tx.clone(), Orientation::Horizontal));
popup.into_iter().for_each(|widget| {
widget.add_to(
&container,
tx.clone(),
Orientation::Horizontal,
info.icon_theme,
)
});
}

Some(container)
Expand Down
11 changes: 3 additions & 8 deletions src/modules/focused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{await_sync, read_lock, send_async};
use color_eyre::Result;
use glib::Continue;
use gtk::prelude::*;
use gtk::{IconTheme, Label};
use gtk::Label;
use serde::Deserialize;
use tokio::spawn;
use tokio::sync::mpsc::{Receiver, Sender};
Expand All @@ -24,8 +24,6 @@ pub struct FocusedModule {
/// Icon size in pixels.
#[serde(default = "default_icon_size")]
icon_size: i32,
/// GTK icon theme to use.
icon_theme: Option<String>,

truncate: Option<TruncateMode>,

Expand Down Expand Up @@ -95,11 +93,7 @@ impl Module<gtk::Box> for FocusedModule {
context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
info: &ModuleInfo,
) -> Result<ModuleWidget<gtk::Box>> {
let icon_theme = IconTheme::new();

if let Some(theme) = self.icon_theme {
icon_theme.set_custom_theme(Some(&theme));
}
let icon_theme = info.icon_theme;

let container = gtk::Box::new(info.bar_position.get_orientation(), 5);

Expand All @@ -114,6 +108,7 @@ impl Module<gtk::Box> for FocusedModule {
container.add(&label);

{
let icon_theme = icon_theme.clone();
context.widget_rx.attach(None, move |(name, id)| {
if self.show_icon {
if let Err(err) = ImageProvider::parse(id, &icon_theme, self.icon_size)
Expand Down
19 changes: 8 additions & 11 deletions src/modules/launcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{lock, read_lock, try_send, write_lock};
use color_eyre::{Help, Report};
use glib::Continue;
use gtk::prelude::*;
use gtk::{Button, IconTheme, Orientation};
use gtk::{Button, Orientation};
use indexmap::IndexMap;
use serde::Deserialize;
use std::process::{Command, Stdio};
Expand All @@ -33,9 +33,6 @@ pub struct LauncherModule {
#[serde(default = "crate::config::default_true")]
show_icons: bool,

/// Name of the GTK icon theme to use.
icon_theme: Option<String>,

#[serde(flatten)]
pub common: Option<CommonConfig>,
}
Expand Down Expand Up @@ -309,23 +306,22 @@ impl Module<gtk::Box> for LauncherModule {
context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
info: &ModuleInfo,
) -> crate::Result<ModuleWidget<gtk::Box>> {
let icon_theme = IconTheme::new();
if let Some(ref theme) = self.icon_theme {
icon_theme.set_custom_theme(Some(theme));
}
let icon_theme = info.icon_theme;

let container = gtk::Box::new(info.bar_position.get_orientation(), 0);

{
let container = container.clone();
let icon_theme = icon_theme.clone();

let controller_tx = context.controller_tx.clone();

let show_names = self.show_names;
let show_icons = self.show_icons;
let orientation = info.bar_position.get_orientation();

let mut buttons = IndexMap::<String, ItemButton>::new();

let controller_tx2 = context.controller_tx.clone();
context.widget_rx.attach(None, move |event| {
match event {
LauncherUpdate::AddItem(item) => {
Expand All @@ -341,7 +337,7 @@ impl Module<gtk::Box> for LauncherModule {
orientation,
&icon_theme,
&context.tx,
&controller_tx2,
&controller_tx,
);

container.add(&button.button);
Expand Down Expand Up @@ -400,7 +396,7 @@ impl Module<gtk::Box> for LauncherModule {
});
}

let popup = self.into_popup(context.controller_tx, context.popup_rx);
let popup = self.into_popup(context.controller_tx, context.popup_rx, info);
Ok(ModuleWidget {
widget: container,
popup,
Expand All @@ -411,6 +407,7 @@ impl Module<gtk::Box> for LauncherModule {
self,
controller_tx: Sender<Self::ReceiveMessage>,
rx: glib::Receiver<Self::SendMessage>,
_info: &ModuleInfo,
) -> Option<gtk::Box> {
const MAX_WIDTH: i32 = 250;

Expand Down
4 changes: 3 additions & 1 deletion src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::popup::ButtonGeometry;
use color_eyre::Result;
use glib::IsA;
use gtk::gdk::Monitor;
use gtk::{Application, Widget};
use gtk::{Application, IconTheme, Widget};
use tokio::sync::mpsc;

#[derive(Clone)]
Expand All @@ -34,6 +34,7 @@ pub struct ModuleInfo<'a> {
pub bar_position: BarPosition,
pub monitor: &'a Monitor,
pub output_name: &'a str,
pub icon_theme: &'a IconTheme,
}

#[derive(Debug)]
Expand Down Expand Up @@ -88,6 +89,7 @@ where
self,
_tx: mpsc::Sender<Self::ReceiveMessage>,
_rx: glib::Receiver<Self::SendMessage>,
_info: &ModuleInfo,
) -> Option<gtk::Box>
where
Self: Sized,
Expand Down
3 changes: 2 additions & 1 deletion src/modules/music/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl Module<Button> for MusicModule {
});
};

let popup = self.into_popup(context.controller_tx, context.popup_rx);
let popup = self.into_popup(context.controller_tx, context.popup_rx, info);

Ok(ModuleWidget {
widget: button,
Expand All @@ -212,6 +212,7 @@ impl Module<Button> for MusicModule {
self,
tx: Sender<Self::ReceiveMessage>,
rx: glib::Receiver<Self::SendMessage>,
_info: &ModuleInfo,
) -> Option<gtk::Box> {
let icon_theme = IconTheme::new();

Expand Down

0 comments on commit 3cf9be8

Please sign in to comment.