1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-04-20 11:54:23 +02:00
ironbar/src/bar.rs

226 lines
6.9 KiB
Rust
Raw Normal View History

use crate::config::{BarPosition, MarginConfig, ModuleConfig};
use crate::modules::{create_module, wrap_widget, ModuleInfo, ModuleLocation};
use crate::popup::Popup;
use crate::Config;
use color_eyre::Result;
use gtk::gdk::Monitor;
2022-08-14 14:30:13 +01:00
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, IconTheme, Orientation};
use std::sync::{Arc, RwLock};
use tracing::{debug, info};
2022-08-14 14:30:13 +01:00
/// Creates a new window for a bar,
/// sets it up and adds its widgets.
pub fn create_bar(
app: &Application,
monitor: &Monitor,
monitor_name: &str,
config: Config,
) -> Result<()> {
2022-08-14 14:30:13 +01:00
let win = ApplicationWindow::builder().application(app).build();
setup_layer_shell(
&win,
monitor,
config.position,
config.anchor_to_edges,
config.margin,
);
2022-08-14 14:30:13 +01:00
let orientation = config.position.get_orientation();
2022-08-14 14:30:13 +01:00
let content = gtk::Box::builder()
.orientation(orientation)
2022-08-14 14:30:13 +01:00
.spacing(0)
.hexpand(false)
.name("bar");
let content = if orientation == Orientation::Horizontal {
content.height_request(config.height)
} else {
content.width_request(config.height)
}
.build();
2022-08-14 14:30:13 +01:00
content.style_context().add_class("container");
let start = create_container("start", orientation);
let center = create_container("center", orientation);
let end = create_container("end", orientation);
2022-08-14 14:30:13 +01:00
content.add(&start);
2022-08-14 14:30:13 +01:00
content.set_center_widget(Some(&center));
content.pack_end(&end, false, false, 0);
2022-08-14 14:30:13 +01:00
load_modules(&start, &center, &end, app, config, monitor, monitor_name)?;
2022-08-14 14:30:13 +01:00
win.add(&content);
win.connect_destroy_event(|_, _| {
2022-08-25 21:53:57 +01:00
info!("Shutting down");
2022-08-14 14:30:13 +01:00
gtk::main_quit();
Inhibit(false)
});
2022-08-25 21:53:57 +01:00
debug!("Showing bar");
// show each box but do not use `show_all`.
// this ensures `show_if` option works as intended.
start.show();
center.show();
end.show();
content.show();
win.show();
Ok(())
2022-08-14 14:30:13 +01:00
}
/// Sets up GTK layer shell for a provided application window.
fn setup_layer_shell(
win: &ApplicationWindow,
monitor: &Monitor,
position: BarPosition,
anchor_to_edges: bool,
margin: MarginConfig,
) {
gtk_layer_shell::init_for_window(win);
gtk_layer_shell::set_monitor(win, monitor);
gtk_layer_shell::set_layer(win, gtk_layer_shell::Layer::Top);
gtk_layer_shell::auto_exclusive_zone_enable(win);
2023-02-08 17:30:09 +00:00
gtk_layer_shell::set_namespace(win, env!("CARGO_PKG_NAME"));
gtk_layer_shell::set_margin(win, gtk_layer_shell::Edge::Top, margin.top);
gtk_layer_shell::set_margin(win, gtk_layer_shell::Edge::Bottom, margin.bottom);
gtk_layer_shell::set_margin(win, gtk_layer_shell::Edge::Left, margin.left);
gtk_layer_shell::set_margin(win, gtk_layer_shell::Edge::Right, margin.right);
let bar_orientation = position.get_orientation();
gtk_layer_shell::set_anchor(
win,
gtk_layer_shell::Edge::Top,
position == BarPosition::Top
|| (bar_orientation == Orientation::Vertical && anchor_to_edges),
);
gtk_layer_shell::set_anchor(
win,
gtk_layer_shell::Edge::Bottom,
position == BarPosition::Bottom
|| (bar_orientation == Orientation::Vertical && anchor_to_edges),
);
gtk_layer_shell::set_anchor(
win,
gtk_layer_shell::Edge::Left,
position == BarPosition::Left
|| (bar_orientation == Orientation::Horizontal && anchor_to_edges),
);
gtk_layer_shell::set_anchor(
win,
gtk_layer_shell::Edge::Right,
position == BarPosition::Right
|| (bar_orientation == Orientation::Horizontal && anchor_to_edges),
);
}
/// Creates a `gtk::Box` container to place widgets inside.
fn create_container(name: &str, orientation: Orientation) -> gtk::Box {
let container = gtk::Box::builder()
.orientation(orientation)
.spacing(0)
.name(name)
.build();
container.style_context().add_class("container");
container
}
/// Loads the configured modules onto a bar.
2022-08-14 14:30:13 +01:00
fn load_modules(
left: &gtk::Box,
center: &gtk::Box,
right: &gtk::Box,
app: &Application,
config: Config,
2022-08-15 21:11:00 +01:00
monitor: &Monitor,
2022-08-14 20:40:11 +01:00
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 {
app,
bar_position: config.position,
monitor,
output_name,
location: $location,
icon_theme: &icon_theme,
}
};
}
if let Some(modules) = config.start {
let info = info!(ModuleLocation::Left);
2023-04-07 14:36:12 +01:00
add_modules(left, modules, &info, config.popup_gap)?;
2022-08-14 14:30:13 +01:00
}
if let Some(modules) = config.center {
let info = info!(ModuleLocation::Center);
2023-04-07 14:36:12 +01:00
add_modules(center, modules, &info, config.popup_gap)?;
2022-08-14 14:30:13 +01:00
}
if let Some(modules) = config.end {
let info = info!(ModuleLocation::Right);
2023-04-07 14:36:12 +01:00
add_modules(right, modules, &info, config.popup_gap)?;
2022-08-14 14:30:13 +01:00
}
Ok(())
2022-08-14 14:30:13 +01:00
}
/// Adds modules into a provided GTK box,
/// which should be one of its left, center or right containers.
2023-04-07 14:36:12 +01:00
fn add_modules(
content: &gtk::Box,
modules: Vec<ModuleConfig>,
info: &ModuleInfo,
popup_gap: i32,
) -> Result<()> {
let popup = Popup::new(info, popup_gap);
let popup = Arc::new(RwLock::new(popup));
macro_rules! add_module {
($module:expr, $id:expr) => {{
let common = $module.common.take().expect("Common config did not exist");
2023-02-25 14:30:45 +00:00
let widget = create_module(*$module, $id, &info, &Arc::clone(&popup))?;
let container = wrap_widget(&widget, common);
content.add(&container);
}};
}
for (id, config) in modules.into_iter().enumerate() {
match config {
2023-02-25 14:30:45 +00:00
#[cfg(feature = "clipboard")]
ModuleConfig::Clipboard(mut module) => add_module!(module, id),
#[cfg(feature = "clock")]
ModuleConfig::Clock(mut module) => add_module!(module, id),
ModuleConfig::Custom(mut module) => add_module!(module, id),
ModuleConfig::Focused(mut module) => add_module!(module, id),
ModuleConfig::Label(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),
}
}
Ok(())
}