2022-08-14 15:56:21 +01:00
|
|
|
use crate::config::{BarPosition, ModuleConfig};
|
2022-08-14 14:30:13 +01:00
|
|
|
use crate::modules::{Module, ModuleInfo, ModuleLocation};
|
|
|
|
use crate::Config;
|
2022-08-21 23:36:07 +01:00
|
|
|
use color_eyre::Result;
|
2022-08-14 14:30:13 +01:00
|
|
|
use gtk::gdk::Monitor;
|
|
|
|
use gtk::prelude::*;
|
|
|
|
use gtk::{Application, ApplicationWindow, Orientation};
|
2022-08-25 21:53:57 +01:00
|
|
|
use tracing::{debug, info};
|
2022-08-14 14:30:13 +01:00
|
|
|
|
2022-08-21 23:36:07 +01:00
|
|
|
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();
|
|
|
|
|
2022-08-14 15:56:21 +01:00
|
|
|
setup_layer_shell(&win, monitor, &config.position);
|
2022-08-14 14:30:13 +01:00
|
|
|
|
|
|
|
let content = gtk::Box::builder()
|
|
|
|
.orientation(Orientation::Horizontal)
|
|
|
|
.spacing(0)
|
|
|
|
.hexpand(false)
|
2022-08-14 20:41:38 +01:00
|
|
|
.height_request(config.height)
|
2022-08-14 14:30:13 +01:00
|
|
|
.name("bar")
|
|
|
|
.build();
|
|
|
|
|
|
|
|
let left = gtk::Box::builder().spacing(0).name("left").build();
|
|
|
|
let center = gtk::Box::builder().spacing(0).name("center").build();
|
|
|
|
let right = gtk::Box::builder().spacing(0).name("right").build();
|
|
|
|
|
|
|
|
content.style_context().add_class("container");
|
|
|
|
left.style_context().add_class("container");
|
|
|
|
center.style_context().add_class("container");
|
|
|
|
right.style_context().add_class("container");
|
|
|
|
|
|
|
|
content.add(&left);
|
|
|
|
content.set_center_widget(Some(¢er));
|
|
|
|
content.pack_end(&right, false, false, 0);
|
|
|
|
|
2022-08-21 23:36:07 +01:00
|
|
|
load_modules(&left, ¢er, &right, 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");
|
2022-08-14 14:30:13 +01:00
|
|
|
win.show_all();
|
2022-08-21 23:36:07 +01:00
|
|
|
|
|
|
|
Ok(())
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn load_modules(
|
|
|
|
left: >k::Box,
|
|
|
|
center: >k::Box,
|
|
|
|
right: >k::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,
|
2022-08-21 23:36:07 +01:00
|
|
|
) -> Result<()> {
|
2022-08-14 14:30:13 +01:00
|
|
|
if let Some(modules) = config.left {
|
|
|
|
let info = ModuleInfo {
|
|
|
|
app,
|
|
|
|
location: ModuleLocation::Left,
|
2022-08-14 16:23:41 +01:00
|
|
|
bar_position: &config.position,
|
2022-08-15 21:11:00 +01:00
|
|
|
monitor,
|
2022-08-14 20:40:11 +01:00
|
|
|
output_name,
|
2022-08-14 14:30:13 +01:00
|
|
|
};
|
|
|
|
|
2022-08-21 23:36:07 +01:00
|
|
|
add_modules(left, modules, &info)?;
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(modules) = config.center {
|
|
|
|
let info = ModuleInfo {
|
|
|
|
app,
|
|
|
|
location: ModuleLocation::Center,
|
2022-08-14 16:23:41 +01:00
|
|
|
bar_position: &config.position,
|
2022-08-15 21:11:00 +01:00
|
|
|
monitor,
|
2022-08-14 20:40:11 +01:00
|
|
|
output_name,
|
2022-08-14 14:30:13 +01:00
|
|
|
};
|
|
|
|
|
2022-08-21 23:36:07 +01:00
|
|
|
add_modules(center, modules, &info)?;
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(modules) = config.right {
|
|
|
|
let info = ModuleInfo {
|
|
|
|
app,
|
|
|
|
location: ModuleLocation::Right,
|
2022-08-14 16:23:41 +01:00
|
|
|
bar_position: &config.position,
|
2022-08-15 21:11:00 +01:00
|
|
|
monitor,
|
2022-08-14 20:40:11 +01:00
|
|
|
output_name,
|
2022-08-14 14:30:13 +01:00
|
|
|
};
|
|
|
|
|
2022-08-21 23:36:07 +01:00
|
|
|
add_modules(right, modules, &info)?;
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
2022-08-21 23:36:07 +01:00
|
|
|
|
|
|
|
Ok(())
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
|
|
|
|
2022-08-21 23:36:07 +01:00
|
|
|
fn add_modules(content: >k::Box, modules: Vec<ModuleConfig>, info: &ModuleInfo) -> Result<()> {
|
2022-08-14 23:32:20 +01:00
|
|
|
macro_rules! add_module {
|
|
|
|
($module:expr, $name:literal) => {{
|
2022-08-21 23:36:07 +01:00
|
|
|
let widget = $module.into_widget(&info)?;
|
2022-08-14 23:32:20 +01:00
|
|
|
widget.set_widget_name($name);
|
|
|
|
content.add(&widget);
|
2022-08-25 21:53:57 +01:00
|
|
|
debug!("Added module of type {}", $name);
|
2022-08-14 23:32:20 +01:00
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2022-08-14 14:30:13 +01:00
|
|
|
for config in modules {
|
|
|
|
match config {
|
2022-08-14 23:32:20 +01:00
|
|
|
ModuleConfig::Clock(module) => add_module!(module, "clock"),
|
|
|
|
ModuleConfig::Mpd(module) => add_module!(module, "mpd"),
|
|
|
|
ModuleConfig::Tray(module) => add_module!(module, "tray"),
|
|
|
|
ModuleConfig::Workspaces(module) => add_module!(module, "workspaces"),
|
|
|
|
ModuleConfig::SysInfo(module) => add_module!(module, "sysinfo"),
|
|
|
|
ModuleConfig::Launcher(module) => add_module!(module, "launcher"),
|
|
|
|
ModuleConfig::Script(module) => add_module!(module, "script"),
|
|
|
|
ModuleConfig::Focused(module) => add_module!(module, "focused"),
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
|
|
|
}
|
2022-08-21 23:36:07 +01:00
|
|
|
|
|
|
|
Ok(())
|
2022-08-14 14:30:13 +01:00
|
|
|
}
|
|
|
|
|
2022-08-14 15:56:21 +01:00
|
|
|
fn setup_layer_shell(win: &ApplicationWindow, monitor: &Monitor, position: &BarPosition) {
|
2022-08-14 14:30:13 +01:00
|
|
|
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);
|
|
|
|
|
|
|
|
gtk_layer_shell::set_margin(win, gtk_layer_shell::Edge::Top, 0);
|
|
|
|
gtk_layer_shell::set_margin(win, gtk_layer_shell::Edge::Bottom, 0);
|
|
|
|
gtk_layer_shell::set_margin(win, gtk_layer_shell::Edge::Left, 0);
|
|
|
|
gtk_layer_shell::set_margin(win, gtk_layer_shell::Edge::Right, 0);
|
|
|
|
|
2022-08-14 20:40:11 +01:00
|
|
|
gtk_layer_shell::set_anchor(
|
|
|
|
win,
|
|
|
|
gtk_layer_shell::Edge::Top,
|
|
|
|
position == &BarPosition::Top,
|
|
|
|
);
|
|
|
|
gtk_layer_shell::set_anchor(
|
|
|
|
win,
|
|
|
|
gtk_layer_shell::Edge::Bottom,
|
|
|
|
position == &BarPosition::Bottom,
|
|
|
|
);
|
2022-08-14 14:30:13 +01:00
|
|
|
gtk_layer_shell::set_anchor(win, gtk_layer_shell::Edge::Left, true);
|
|
|
|
gtk_layer_shell::set_anchor(win, gtk_layer_shell::Edge::Right, true);
|
|
|
|
}
|