1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-07-01 02:31:04 +02:00

feat: add feature flags

Flags allow you to disable certain functionality and compile with only select features to reduce build time.

Resolves #54.
This commit is contained in:
Jake Stanger 2023-02-01 20:42:05 +00:00
parent e83618b1d6
commit c347b6c944
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61
16 changed files with 249 additions and 62 deletions

View file

@ -195,15 +195,20 @@ fn add_modules(content: &gtk::Box, modules: Vec<ModuleConfig>, info: &ModuleInfo
for (id, config) in modules.into_iter().enumerate() {
match config {
#[cfg(feature = "clock")]
ModuleConfig::Clock(mut module) => add_module!(module, id),
ModuleConfig::Script(mut module) => add_module!(module, id),
ModuleConfig::SysInfo(mut module) => add_module!(module, id),
ModuleConfig::Focused(mut module) => add_module!(module, id),
ModuleConfig::Workspaces(mut module) => add_module!(module, id),
ModuleConfig::Tray(mut module) => add_module!(module, id),
ModuleConfig::Music(mut module) => add_module!(module, id),
ModuleConfig::Launcher(mut module) => add_module!(module, id),
ModuleConfig::Custom(mut module) => add_module!(module, id),
ModuleConfig::Focused(mut module) => add_module!(module, id),
ModuleConfig::Launcher(mut module) => add_module!(module, id),
#[cfg(feature = "music")]
ModuleConfig::Music(mut module) => add_module!(module, id),
ModuleConfig::Script(mut module) => add_module!(module, id),
#[cfg(feature = "sys_info")]
ModuleConfig::SysInfo(mut module) => add_module!(module, id),
#[cfg(feature = "tray")]
ModuleConfig::Tray(mut module) => add_module!(module, id),
#[cfg(feature = "workspaces")]
ModuleConfig::Workspaces(mut module) => add_module!(module, id),
}
}

View file

@ -1,13 +1,18 @@
use cfg_if::cfg_if;
use color_eyre::{Help, Report, Result};
use std::fmt::{Display, Formatter};
use tokio::sync::broadcast;
use tracing::debug;
#[cfg(feature = "workspaces+hyprland")]
pub mod hyprland;
#[cfg(feature = "workspaces+sway")]
pub mod sway;
pub enum Compositor {
#[cfg(feature = "workspaces+sway")]
Sway,
#[cfg(feature = "workspaces+hyprland")]
Hyprland,
Unsupported,
}
@ -18,7 +23,9 @@ impl Display for Compositor {
f,
"{}",
match self {
#[cfg(feature = "workspaces+sway")]
Self::Sway => "Sway",
#[cfg(feature = "workspaces+hyprland")]
Self::Hyprland => "Hyprland",
Self::Unsupported => "Unsupported",
}
@ -31,9 +38,15 @@ impl Compositor {
/// This is done by checking system env vars.
fn get_current() -> Self {
if std::env::var("SWAYSOCK").is_ok() {
Self::Sway
cfg_if! {
if #[cfg(feature = "workspaces+sway")] { Self::Sway }
else { tracing::error!("Not compiled with Sway support"); Self::Unsupported }
}
} else if std::env::var("HYPRLAND_INSTANCE_SIGNATURE").is_ok() {
Self::Hyprland
cfg_if! {
if #[cfg(feature = "workspaces+hyprland")] { Self::Hyprland}
else { tracing::error!("Not compiled with Hyprland support"); Self::Unsupported }
}
} else {
Self::Unsupported
}
@ -44,7 +57,9 @@ impl Compositor {
let current = Self::get_current();
debug!("Getting workspace client for: {current}");
match current {
#[cfg(feature = "workspaces+sway")]
Self::Sway => Ok(sway::get_sub_client()),
#[cfg(feature = "workspaces+hyprland")]
Self::Hyprland => Ok(hyprland::get_client()),
Self::Unsupported => Err(Report::msg("Unsupported compositor")
.note("Currently workspaces are only supported by Sway and Hyprland")),

View file

@ -1,4 +1,7 @@
#[cfg(feature = "workspaces")]
pub mod compositor;
#[cfg(feature = "music")]
pub mod music;
#[cfg(feature = "tray")]
pub mod system_tray;
pub mod wayland;

View file

@ -4,7 +4,9 @@ use std::sync::Arc;
use std::time::Duration;
use tokio::sync::broadcast;
#[cfg(feature = "music+mpd")]
pub mod mpd;
#[cfg(feature = "music+mpris")]
pub mod mpris;
#[derive(Clone, Debug)]

View file

@ -133,11 +133,16 @@ impl Config {
.unwrap_or_default();
match extension {
#[cfg(feature = "config+json")]
"json" => serde_json::from_str(str).wrap_err("Invalid JSON config"),
#[cfg(feature = "config+toml")]
"toml" => toml::from_str(str).wrap_err("Invalid TOML config"),
#[cfg(feature = "config+yaml")]
"yaml" | "yml" => serde_yaml::from_str(str).wrap_err("Invalid YAML config"),
#[cfg(feature = "config+corn")]
"corn" => libcorn::from_str(str).wrap_err("Invalid Corn config"),
_ => unreachable!(),
_ => Err(Report::msg(format!("Unsupported config type: {extension}"))
.note("You may need to recompile with support if available")),
}
}
}

View file

@ -1,14 +1,19 @@
mod r#impl;
mod truncate;
#[cfg(feature = "clock")]
use crate::modules::clock::ClockModule;
use crate::modules::custom::CustomModule;
use crate::modules::focused::FocusedModule;
use crate::modules::launcher::LauncherModule;
#[cfg(feature = "music")]
use crate::modules::music::MusicModule;
use crate::modules::script::ScriptModule;
#[cfg(feature = "sys_info")]
use crate::modules::sysinfo::SysInfoModule;
#[cfg(feature = "tray")]
use crate::modules::tray::TrayModule;
#[cfg(feature = "workspaces")]
use crate::modules::workspaces::WorkspacesModule;
use crate::script::ScriptInput;
use serde::Deserialize;
@ -32,15 +37,20 @@ pub struct CommonConfig {
#[derive(Debug, Deserialize, Clone)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ModuleConfig {
#[cfg(feature = "clock")]
Clock(ClockModule),
Music(MusicModule),
Tray(TrayModule),
Workspaces(WorkspacesModule),
SysInfo(SysInfoModule),
Launcher(LauncherModule),
Script(ScriptModule),
Focused(FocusedModule),
Custom(CustomModule),
Focused(FocusedModule),
Launcher(LauncherModule),
#[cfg(feature = "music")]
Music(MusicModule),
Script(ScriptModule),
#[cfg(feature = "sys_info")]
SysInfo(SysInfoModule),
#[cfg(feature = "tray")]
Tray(TrayModule),
#[cfg(feature = "workspaces")]
Workspaces(WorkspacesModule),
}
#[derive(Debug, Clone)]

View file

@ -3,6 +3,7 @@ use gtk::prelude::*;
use gtk::{Button, IconTheme, Image, Label, Orientation};
use tracing::error;
#[cfg(any(feature = "music", feature = "workspaces"))]
pub fn new_icon_button(input: &str, icon_theme: &IconTheme, size: i32) -> Button {
let button = Button::new();
@ -27,6 +28,7 @@ pub fn new_icon_button(input: &str, icon_theme: &IconTheme, size: i32) -> Button
button
}
#[cfg(feature = "music")]
pub fn new_icon_label(input: &str, icon_theme: &IconTheme, size: i32) -> gtk::Box {
let container = gtk::Box::new(Orientation::Horizontal, 0);

View file

@ -1,5 +1,7 @@
#[cfg(any(feature = "music", feature = "workspaces"))]
mod gtk;
mod provider;
#[cfg(any(feature = "music", feature = "workspaces"))]
pub use self::gtk::*;
pub use provider::ImageProvider;

View file

@ -1,22 +1,30 @@
use crate::desktop_file::get_desktop_icon_name;
use crate::send;
use color_eyre::{Report, Result};
use glib::Bytes;
use cfg_if::cfg_if;
use color_eyre::{Help, Report, Result};
use gtk::gdk_pixbuf::Pixbuf;
use gtk::gio::{Cancellable, MemoryInputStream};
use gtk::prelude::*;
use gtk::{IconLookupFlags, IconTheme};
use reqwest::Url;
use std::path::{Path, PathBuf};
use tokio::spawn;
use tracing::error;
cfg_if!(
if #[cfg(feature = "http")] {
use crate::send;
use gtk::gio::{Cancellable, MemoryInputStream};
use tokio::spawn;
use tracing::error;
}
);
#[derive(Debug)]
enum ImageLocation<'a> {
Icon { name: String, theme: &'a IconTheme },
Icon {
name: String,
theme: &'a IconTheme,
},
Local(PathBuf),
Steam(String),
Remote(Url),
#[cfg(feature = "http")]
Remote(reqwest::Url),
}
pub struct ImageProvider<'a> {
@ -38,6 +46,7 @@ impl<'a> ImageProvider<'a> {
/// 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).
#[cfg(any(feature = "music", feature = "workspaces"))]
pub fn is_definitely_image_input(input: &str) -> bool {
input.starts_with("icon:")
|| input.starts_with("file://")
@ -58,6 +67,7 @@ impl<'a> ImageProvider<'a> {
Some(input_type) if input_type == "file" => Ok(ImageLocation::Local(PathBuf::from(
input_name[2..].to_string(),
))),
#[cfg(feature = "http")]
Some(input_type) if input_type == "http" || input_type == "https" => {
Ok(ImageLocation::Remote(input.parse()?))
}
@ -73,7 +83,8 @@ impl<'a> ImageProvider<'a> {
theme,
})
}
Some(input_type) => Err(Report::msg(format!("Unsupported image type: {input_type}"))),
Some(input_type) => Err(Report::msg(format!("Unsupported image type: {input_type}"))
.note("You may need to recompile with support if available")),
None if PathBuf::from(input_name).exists() => {
Ok(ImageLocation::Local(PathBuf::from(input_name)))
}
@ -88,6 +99,7 @@ impl<'a> ImageProvider<'a> {
/// and load it into the provided `GTK::Image` widget.
pub fn load_into_image(&self, image: gtk::Image) -> Result<()> {
// handle remote locations async to avoid blocking UI thread while downloading
#[cfg(feature = "http")]
if let ImageLocation::Remote(url) = &self.location {
let url = url.clone();
let (tx, rx) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
@ -120,16 +132,26 @@ impl<'a> ImageProvider<'a> {
});
}
} else {
let pixbuf = match &self.location {
ImageLocation::Icon { name, theme } => self.get_from_icon(name, theme),
ImageLocation::Local(path) => self.get_from_file(path),
ImageLocation::Steam(steam_id) => self.get_from_steam_id(steam_id),
ImageLocation::Remote(_) => unreachable!(), // handled above
}?;
image.set_pixbuf(Some(&pixbuf));
self.load_into_image_sync(image)?;
};
#[cfg(not(feature = "http"))]
self.load_into_image_sync(image)?;
Ok(())
}
fn load_into_image_sync(&self, image: gtk::Image) -> Result<()> {
let pixbuf = match &self.location {
ImageLocation::Icon { name, theme } => self.get_from_icon(name, theme),
ImageLocation::Local(path) => self.get_from_file(path),
ImageLocation::Steam(steam_id) => self.get_from_steam_id(steam_id),
#[cfg(feature = "http")]
_ => unreachable!(), // handled above
}?;
image.set_pixbuf(Some(&pixbuf));
Ok(())
}
@ -169,8 +191,9 @@ impl<'a> ImageProvider<'a> {
}
/// Attempts to get `Bytes` from an HTTP resource asynchronously.
async fn get_bytes_from_http(url: Url) -> Result<Bytes> {
#[cfg(feature = "http")]
async fn get_bytes_from_http(url: reqwest::Url) -> Result<glib::Bytes> {
let bytes = reqwest::get(url).await?.bytes().await?;
Ok(Bytes::from_owned(bytes))
Ok(glib::Bytes::from_owned(bytes))
}
}

View file

@ -294,8 +294,6 @@ impl Module<gtk::Box> for LauncherModule {
}
}
}
Ok::<(), swayipc_async::Error>(())
});
Ok(())

View file

@ -4,14 +4,19 @@
///
/// Clicking the widget opens a popup containing the current time
/// with second-level precision and a calendar.
#[cfg(feature = "clock")]
pub mod clock;
pub mod custom;
pub mod focused;
pub mod launcher;
#[cfg(feature = "music")]
pub mod music;
pub mod script;
#[cfg(feature = "sys_info")]
pub mod sysinfo;
#[cfg(feature = "tray")]
pub mod tray;
#[cfg(feature = "workspaces")]
pub mod workspaces;
use crate::config::BarPosition;