From 06cfad62e228f7fc63938f2280206450005cb064 Mon Sep 17 00:00:00 2001 From: Jake Stanger Date: Sat, 15 Oct 2022 16:27:25 +0100 Subject: [PATCH] feat: more positioning options (#23) * feat: more positioning options Can now display the bar on the left/right, and avoid anchoring to edges to centre the bar. BREAKING CHANGE: The `left` and `right` config options have been renamed to `start` and `end` --- examples/config.corn | 4 +- examples/config.json | 10 ++- src/bar.rs | 75 ++++++++++++------ src/config.rs | 29 ++++++- src/modules/clock.rs | 14 +++- src/modules/focused.rs | 6 +- src/modules/launcher/item.rs | 10 ++- src/modules/launcher/mod.rs | 6 +- src/modules/mod.rs | 8 +- src/modules/mpd/mod.rs | 14 +++- src/modules/script.rs | 3 +- src/modules/sysinfo.rs | 7 +- src/modules/workspaces.rs | 4 +- src/popup.rs | 147 ++++++++++++++++++++++++++++------- 14 files changed, 254 insertions(+), 83 deletions(-) diff --git a/examples/config.corn b/examples/config.corn index a832b84..7e892df 100644 --- a/examples/config.corn +++ b/examples/config.corn @@ -39,5 +39,7 @@ let { $right = [ $mpd_local $mpd_server $phone_battery $sys_info $clock ] } in { - left = $left right = $right + anchor_to_edges = true + position = "top" + start = $left end = $right } diff --git a/examples/config.json b/examples/config.json index 375d9dd..b3f09a1 100644 --- a/examples/config.json +++ b/examples/config.json @@ -1,16 +1,20 @@ { - "left": [ + "start": [ { "type": "workspaces" }, { "type": "launcher", "icon_theme": "Paper", - "favorites": ["firefox", "discord", "Steam"], + "favorites": [ + "firefox", + "discord", + "Steam" + ], "show_names": false } ], - "right": [ + "end": [ { "type": "mpd" }, diff --git a/src/bar.rs b/src/bar.rs index 688ba43..717315c 100644 --- a/src/bar.rs +++ b/src/bar.rs @@ -28,30 +28,42 @@ pub fn create_bar( ) -> Result<()> { let win = ApplicationWindow::builder().application(app).build(); - setup_layer_shell(&win, monitor, &config.position); + setup_layer_shell(&win, monitor, config.position, config.anchor_to_edges); let content = gtk::Box::builder() - .orientation(Orientation::Horizontal) + .orientation(config.position.get_orientation()) .spacing(0) .hexpand(false) .height_request(config.height) .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(); + let start = gtk::Box::builder() + .orientation(config.position.get_orientation()) + .spacing(0) + .name("start") + .build(); + let center = gtk::Box::builder() + .orientation(config.position.get_orientation()) + .spacing(0) + .name("center") + .build(); + let end = gtk::Box::builder() + .orientation(config.position.get_orientation()) + .spacing(0) + .name("end") + .build(); content.style_context().add_class("container"); - left.style_context().add_class("container"); + start.style_context().add_class("container"); center.style_context().add_class("container"); - right.style_context().add_class("container"); + end.style_context().add_class("container"); - content.add(&left); + content.add(&start); content.set_center_widget(Some(¢er)); - content.pack_end(&right, false, false, 0); + content.pack_end(&end, false, false, 0); - load_modules(&left, ¢er, &right, app, config, monitor, monitor_name)?; + load_modules(&start, ¢er, &end, app, config, monitor, monitor_name)?; win.add(&content); win.connect_destroy_event(|_, _| { @@ -79,11 +91,11 @@ fn load_modules( let mut info_builder = ModuleInfoBuilder::default(); let info_builder = info_builder .app(app) - .bar_position(&config.position) + .bar_position(config.position) .monitor(monitor) .output_name(output_name); - if let Some(modules) = config.left { + if let Some(modules) = config.start { let info_builder = info_builder.location(ModuleLocation::Left); add_modules(left, modules, info_builder)?; @@ -95,7 +107,7 @@ fn load_modules( add_modules(center, modules, info_builder)?; } - if let Some(modules) = config.right { + if let Some(modules) = config.end { let info_builder = info_builder.location(ModuleLocation::Right); add_modules(right, modules, info_builder)?; @@ -160,23 +172,23 @@ fn add_modules( w_tx.send(update).expect("Failed to send update to module"); } - ModuleUpdateEvent::TogglePopup((x, w)) => { + ModuleUpdateEvent::TogglePopup(geometry) => { debug!("Toggling popup for {} [#{}]", $name, $id); let popup = popup.read().expect("Failed to get read lock on popup"); if popup.is_visible() { popup.hide() } else { popup.show_content($id); - popup.show(x, w); + popup.show(geometry); } } - ModuleUpdateEvent::OpenPopup((x, w)) => { + ModuleUpdateEvent::OpenPopup(geometry) => { debug!("Opening popup for {} [#{}]", $name, $id); let popup = popup.read().expect("Failed to get read lock on popup"); popup.hide(); popup.show_content($id); - popup.show(x, w); + popup.show(geometry); } ModuleUpdateEvent::ClosePopup => { debug!("Closing popup for {} [#{}]", $name, $id); @@ -224,7 +236,12 @@ fn add_modules( } /// Sets up GTK layer shell for a provided application window. -fn setup_layer_shell(win: &ApplicationWindow, monitor: &Monitor, position: &BarPosition) { +fn setup_layer_shell( + win: &ApplicationWindow, + monitor: &Monitor, + position: BarPosition, + anchor_to_edges: bool, +) { 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); @@ -235,16 +252,30 @@ fn setup_layer_shell(win: &ApplicationWindow, monitor: &Monitor, position: &BarP gtk_layer_shell::set_margin(win, gtk_layer_shell::Edge::Left, 0); gtk_layer_shell::set_margin(win, gtk_layer_shell::Edge::Right, 0); + let bar_orientation = position.get_orientation(); + gtk_layer_shell::set_anchor( win, gtk_layer_shell::Edge::Top, - position == &BarPosition::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, + 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), ); - gtk_layer_shell::set_anchor(win, gtk_layer_shell::Edge::Left, true); - gtk_layer_shell::set_anchor(win, gtk_layer_shell::Edge::Right, true); } diff --git a/src/config.rs b/src/config.rs index 47bd52f..03ce274 100644 --- a/src/config.rs +++ b/src/config.rs @@ -10,6 +10,7 @@ use color_eyre::eyre::{Context, ContextCompat}; use color_eyre::{eyre, Help, Report}; use dirs::config_dir; use eyre::Result; +use gtk::Orientation; use serde::Deserialize; use std::collections::HashMap; use std::path::{Path, PathBuf}; @@ -35,11 +36,13 @@ pub enum MonitorConfig { Multiple(Vec), } -#[derive(Debug, Deserialize, Clone, PartialEq, Eq)] +#[derive(Debug, Deserialize, Copy, Clone, PartialEq, Eq)] #[serde(rename_all = "kebab-case")] pub enum BarPosition { Top, Bottom, + Left, + Right, } impl Default for BarPosition { @@ -48,16 +51,36 @@ impl Default for BarPosition { } } +impl BarPosition { + pub fn get_orientation(self) -> Orientation { + if self == Self::Top || self == Self::Bottom { + Orientation::Horizontal + } else { + Orientation::Vertical + } + } + + pub const fn get_angle(self) -> f64 { + match self { + Self::Top | Self::Bottom => 0.0, + Self::Left => 90.0, + Self::Right => 270.0, + } + } +} + #[derive(Debug, Deserialize, Clone, Default)] pub struct Config { #[serde(default = "default_bar_position")] pub position: BarPosition, + #[serde(default = "default_true")] + pub anchor_to_edges: bool, #[serde(default = "default_bar_height")] pub height: i32, - pub left: Option>, + pub start: Option>, pub center: Option>, - pub right: Option>, + pub end: Option>, pub monitors: Option>, } diff --git a/src/modules/clock.rs b/src/modules/clock.rs index eedb9fa..c418573 100644 --- a/src/modules/clock.rs +++ b/src/modules/clock.rs @@ -51,23 +51,29 @@ impl Module