1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-08-16 14:21:03 +02:00

Merge pull request #855 from Rodrigodd/feat/module-bindmode

feat: rename sway_mode to bindmode and add Hyprland support
This commit is contained in:
Jake Stanger 2025-04-22 23:40:47 +01:00 committed by GitHub
commit a9508e6541
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 209 additions and 82 deletions

View file

@ -100,6 +100,9 @@ jobs:
- config+toml
- config+corn
- config+ron
- bindmode+all
- bindmode+sway
- bindmode+hyprland
- cairo
- clipboard
- clock

View file

@ -19,6 +19,7 @@ default = [
"focused",
"http",
"ipc",
"bindmode+all",
"keyboard+all",
"launcher",
"label",
@ -38,6 +39,11 @@ ipc = ["dep:serde_json", "dep:clap"]
http = ["dep:reqwest"]
bindmode = []
"bindmode+all" = ["bindmode+sway", "bindmode+hyprland"]
"bindmode+sway" = ["bindmode", "sway"]
"bindmode+hyprland" = ["bindmode", "hyprland"]
"config+all" = [
"config+json",
"config+yaml",

View file

@ -29,6 +29,7 @@ A full list of feature flags can be found [here](Compiling#features).
| Module | Status | Notes |
|-----------------|--------|------------------------------------------------------------------------------------------------------------------------------------------|
| Bindmode | ❌ | |
| Cairo | ✅ | |
| Clipboard | ✅ | |
| Clock | ✅ | |
@ -41,7 +42,6 @@ A full list of feature flags can be found [here](Compiling#features).
| Network Manager | ❌ | |
| Notifications | ✅ | |
| Script | ✅ | |
| Sway Mode | ❌ | |
| SysInfo | ✅ | |
| Tray | ❌ | GTK4 removes widgets required to move the tray. No `libdbusmenu-gtk4` either. will need to manually re-create menus with custom widgets. |
| UPower | ❌ | |

View file

@ -25,6 +25,7 @@
# Modules
- [Bindmode](bindmode)
- [Cairo](cairo)
- [Clipboard](clipboard)
- [Clock](clock)
@ -37,7 +38,6 @@
- [Network Manager](network-manager)
- [Notifications](notifications)
- [Script](script)
- [Sway-mode](sway-mode)
- [Sys_Info](sys-info)
- [Tray](tray)
- [Upower](upower)

View file

@ -1,12 +1,12 @@
Displays the current sway mode in a label. If the current sway mode is
"default", nothing is displayed.
> [!IMPORTANT]
> This module is currently only available on Sway and Hyprland.
> [!NOTE]
> This module only works under the [Sway](https://swaywm.org/) compositor.
Displays Sway's current binding mode or [Hyprland's current submap](https://wiki.hyprland.org/Configuring/Binds/#submaps)
in a label. Nothing is displayed if no binding mode is active.
## Configuration
> Type: `sway-mode`
> Type: `bindmode`
| Name | Type | Default | Description |
| --------------------- | ------------------------------------------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
@ -22,7 +22,7 @@ Displays the current sway mode in a label. If the current sway mode is
{
"end": [
{
"type": "sway-mode",
"type": "bindmode",
"truncate": "start"
}
]
@ -36,7 +36,7 @@ Displays the current sway mode in a label. If the current sway mode is
```toml
[[end]]
type = "sway-mode"
type = "bindmode"
truncate = "start"
```
@ -47,7 +47,7 @@ truncate = "start"
```yaml
end:
- type: "sway-mode"
- type: "bindmode"
truncate: "start"
```
@ -60,7 +60,7 @@ end:
{
end = [
{
type = "sway-mode"
type = "bindmode"
truncate = "start"
}
]
@ -71,8 +71,8 @@ end:
## Styling
| Selector | Description |
| ------------ | ---------------------- |
| `.sway_mode` | Sway mode label widget |
| Selector | Description |
| ----------- | ---------------------- |
| `.bindmode` | Bind mode label widget |
For more information on styling, please see the [styling guide](styling-guide).

View file

@ -10,11 +10,22 @@ use hyprland::shared::{HyprDataVec, WorkspaceType};
use tokio::sync::broadcast::{Receiver, Sender, channel};
use tracing::{debug, error, info};
#[cfg(feature = "bindmode+hyprland")]
use super::{BindModeClient, BindModeUpdate};
#[cfg(feature = "keyboard+hyprland")]
use super::{KeyboardLayoutClient, KeyboardLayoutUpdate};
#[derive(Debug)]
struct TxRx<T> {
tx: Sender<T>,
_rx: Receiver<T>,
}
impl<T: Clone> TxRx<T> {
fn new() -> Self {
let (tx, rx) = channel(16);
Self { tx, _rx: rx }
}
}
#[derive(Debug)]
pub struct Client {
@ -23,27 +34,20 @@ pub struct Client {
#[cfg(feature = "keyboard+hyprland")]
keyboard_layout: TxRx<KeyboardLayoutUpdate>,
#[cfg(feature = "bindmode+hyprland")]
bindmode: TxRx<BindModeUpdate>,
}
impl Client {
pub(crate) fn new() -> Self {
#[cfg(feature = "workspaces+hyprland")]
let (workspace_tx, workspace_rx) = channel(16);
#[cfg(feature = "keyboard+hyprland")]
let (keyboard_layout_tx, keyboard_layout_rx) = channel(16);
let instance = Self {
#[cfg(feature = "workspaces+hyprland")]
workspace: TxRx {
tx: workspace_tx,
_rx: workspace_rx,
},
workspace: TxRx::new(),
#[cfg(feature = "keyboard+hyprland")]
keyboard_layout: TxRx {
tx: keyboard_layout_tx,
_rx: keyboard_layout_rx,
},
keyboard_layout: TxRx::new(),
#[cfg(feature = "bindmode+hyprland")]
bindmode: TxRx::new(),
};
instance.listen_events();
@ -54,11 +58,14 @@ impl Client {
info!("Starting Hyprland event listener");
#[cfg(feature = "workspaces+hyprland")]
let tx = self.workspace.tx.clone();
let workspace_tx = self.workspace.tx.clone();
#[cfg(feature = "keyboard+hyprland")]
let keyboard_layout_tx = self.keyboard_layout.tx.clone();
#[cfg(feature = "bindmode+hyprland")]
let bindmode_tx = self.bindmode.tx.clone();
spawn_blocking(move || {
let mut event_listener = EventListener::new();
@ -67,10 +74,13 @@ impl Client {
// cache the active workspace since Hyprland doesn't give us the prev active
#[cfg(feature = "workspaces+hyprland")]
Self::listen_workspace_events(tx, &mut event_listener, &lock);
Self::listen_workspace_events(workspace_tx, &mut event_listener, &lock);
#[cfg(feature = "keyboard+hyprland")]
Self::listen_keyboard_events(keyboard_layout_tx, &mut event_listener, lock);
Self::listen_keyboard_events(keyboard_layout_tx, &mut event_listener, &lock);
#[cfg(feature = "bindmode+hyprland")]
Self::listen_bindmode_events(bindmode_tx, &mut event_listener, &lock);
event_listener
.start_listener()
@ -257,7 +267,7 @@ impl Client {
fn listen_keyboard_events(
keyboard_layout_tx: Sender<KeyboardLayoutUpdate>,
event_listener: &mut EventListener,
lock: std::sync::Arc<std::sync::Mutex<()>>,
lock: &std::sync::Arc<std::sync::Mutex<()>>,
) {
let tx = keyboard_layout_tx.clone();
let lock = lock.clone();
@ -307,6 +317,29 @@ impl Client {
});
}
#[cfg(feature = "bindmode+hyprland")]
fn listen_bindmode_events(
bindmode_tx: Sender<BindModeUpdate>,
event_listener: &mut EventListener,
lock: &std::sync::Arc<std::sync::Mutex<()>>,
) {
let tx = bindmode_tx.clone();
let lock = lock.clone();
event_listener.add_sub_map_change_handler(move |bind_mode| {
let _lock = lock!(lock);
debug!("Received bind mode: {bind_mode:?}");
send!(
tx,
BindModeUpdate {
name: bind_mode,
pango_markup: false,
}
);
});
}
/// Sends a `WorkspaceUpdate::Focus` event
/// and updates the active workspace cache.
#[cfg(feature = "workspaces+hyprland")]
@ -392,9 +425,6 @@ impl super::WorkspaceClient for Client {
}
}
#[cfg(feature = "keyboard+hyprland")]
use super::{KeyboardLayoutClient, KeyboardLayoutUpdate};
#[cfg(feature = "keyboard+hyprland")]
impl KeyboardLayoutClient for Client {
fn set_next_active(&self) {
@ -436,6 +466,13 @@ impl KeyboardLayoutClient for Client {
}
}
#[cfg(feature = "bindmode+hyprland")]
impl BindModeClient for Client {
fn subscribe(&self) -> Result<Receiver<BindModeUpdate>> {
Ok(self.bindmode.tx.subscribe())
}
}
fn get_workspace_name(name: WorkspaceType) -> String {
match name {
WorkspaceType::Regular(name) => name,

View file

@ -66,6 +66,28 @@ impl Compositor {
}
}
#[cfg(feature = "bindmode")]
pub fn create_bindmode_client(
clients: &mut super::Clients,
) -> ClientResult<dyn BindModeClient + Send + Sync> {
let current = Self::get_current();
debug!("Getting keyboard_layout client for: {current}");
match current {
#[cfg(feature = "bindmode+sway")]
Self::Sway => Ok(clients.sway()?),
#[cfg(feature = "bindmode+hyprland")]
Self::Hyprland => Ok(clients.hyprland()),
#[cfg(feature = "niri")]
Self::Niri => Err(Report::msg("Unsupported compositor")
.note("Currently bindmode is only supported by Sway and Hyprland")),
Self::Unsupported => Err(Report::msg("Unsupported compositor")
.note("Currently bindmode is only supported by Sway and Hyprland")),
#[allow(unreachable_patterns)]
_ => Err(Report::msg("Unsupported compositor")
.note("Bindmode feature is disabled for this compositor")),
}
}
#[cfg(feature = "keyboard")]
pub fn create_keyboard_layout_client(
clients: &mut super::Clients,
@ -74,9 +96,7 @@ impl Compositor {
debug!("Getting keyboard_layout client for: {current}");
match current {
#[cfg(feature = "keyboard+sway")]
Self::Sway => clients
.sway()
.map(|client| client as Arc<dyn KeyboardLayoutClient + Send + Sync>),
Self::Sway => Ok(clients.sway()?),
#[cfg(feature = "keyboard+hyprland")]
Self::Hyprland => Ok(clients.hyprland()),
#[cfg(feature = "niri")]
@ -102,9 +122,7 @@ impl Compositor {
debug!("Getting workspace client for: {current}");
match current {
#[cfg(feature = "workspaces+sway")]
Self::Sway => clients
.sway()
.map(|client| client as Arc<dyn WorkspaceClient + Send + Sync>),
Self::Sway => Ok(clients.sway()?),
#[cfg(feature = "workspaces+hyprland")]
Self::Hyprland => Ok(clients.hyprland()),
#[cfg(feature = "workspaces+niri")]
@ -195,6 +213,14 @@ pub enum WorkspaceUpdate {
Unknown,
}
#[derive(Clone, Debug)]
pub struct BindModeUpdate {
/// The binding mode that became active.
pub name: String,
/// Whether the mode should be parsed as pango markup.
pub pango_markup: bool,
}
#[cfg(feature = "workspaces")]
pub trait WorkspaceClient: Debug + Send + Sync {
/// Requests the workspace with this id is focused.
@ -218,3 +244,12 @@ pub trait KeyboardLayoutClient: Debug + Send + Sync {
#[cfg(feature = "keyboard")]
register_fallible_client!(dyn KeyboardLayoutClient, keyboard_layout);
#[cfg(feature = "bindmode")]
pub trait BindModeClient: Debug + Send + Sync {
/// Add a callback for bindmode updates.
fn subscribe(&self) -> Result<broadcast::Receiver<BindModeUpdate>>;
}
#[cfg(feature = "bindmode")]
register_fallible_client!(dyn BindModeClient, bindmode);

View file

@ -231,3 +231,36 @@ impl TryFrom<InputEvent> for KeyboardLayoutUpdate {
}
}
}
#[cfg(feature = "bindmode+sway")]
use super::{BindModeClient, BindModeUpdate};
#[cfg(feature = "bindmode+sway")]
impl BindModeClient for Client {
fn subscribe(&self) -> Result<Receiver<BindModeUpdate>, Report> {
let (tx, rx) = channel(16);
await_sync(async {
self.add_listener::<swayipc_async::ModeEvent>(move |mode| {
tracing::trace!("mode: {:?}", mode);
// when no bindind is active the bindmode is named "default", but we must display
// nothing in this case.
let name = if mode.change == "default" {
String::new()
} else {
mode.change.clone()
};
send!(
tx,
BindModeUpdate {
name,
pango_markup: mode.pango_markup,
}
);
})
.await
})?;
Ok(rx)
}
}

View file

@ -7,7 +7,12 @@ use std::sync::Arc;
#[cfg(feature = "clipboard")]
pub mod clipboard;
#[cfg(any(feature = "keyboard", feature = "workspaces", feature = "hyprland"))]
#[cfg(any(
feature = "bindmode",
feature = "hyprland",
feature = "keyboard",
feature = "workspaces",
))]
pub mod compositor;
#[cfg(feature = "keyboard")]
pub mod libinput;
@ -42,6 +47,8 @@ pub struct Clients {
sway: Option<Arc<sway::Client>>,
#[cfg(feature = "hyprland")]
hyprland: Option<Arc<compositor::hyprland::Client>>,
#[cfg(feature = "bindmode")]
bindmode: Option<Arc<dyn compositor::BindModeClient>>,
#[cfg(feature = "clipboard")]
clipboard: Option<Arc<clipboard::Client>>,
#[cfg(feature = "keyboard")]
@ -114,6 +121,19 @@ impl Clients {
Ok(client)
}
#[cfg(feature = "bindmode")]
pub fn bindmode(&mut self) -> ClientResult<dyn compositor::BindModeClient> {
let client = if let Some(client) = &self.bindmode {
client.clone()
} else {
let client = compositor::Compositor::create_bindmode_client(self)?;
self.bindmode.replace(client.clone());
client
};
Ok(client)
}
#[cfg(feature = "sway")]
pub fn sway(&mut self) -> ClientResult<sway::Client> {
let client = if let Some(client) = &self.sway {

View file

@ -3,6 +3,8 @@ mod r#impl;
mod layout;
mod truncate;
#[cfg(any(feature = "bindmode"))]
use crate::modules::bindmode::Bindmode;
#[cfg(feature = "cairo")]
use crate::modules::cairo::CairoModule;
#[cfg(feature = "clipboard")]
@ -27,8 +29,6 @@ use crate::modules::networkmanager::NetworkManagerModule;
use crate::modules::notifications::NotificationsModule;
#[cfg(feature = "script")]
use crate::modules::script::ScriptModule;
#[cfg(feature = "sway")]
use crate::modules::sway::mode::SwayModeModule;
#[cfg(feature = "sys_info")]
use crate::modules::sysinfo::SysInfoModule;
#[cfg(feature = "tray")]
@ -57,6 +57,8 @@ pub use self::truncate::{EllipsizeMode, TruncateMode};
#[serde(tag = "type", rename_all = "snake_case")]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub enum ModuleConfig {
#[cfg(feature = "bindmode")]
Bindmode(Box<Bindmode>),
#[cfg(feature = "cairo")]
Cairo(Box<CairoModule>),
#[cfg(feature = "clipboard")]
@ -83,8 +85,6 @@ pub enum ModuleConfig {
Script(Box<ScriptModule>),
#[cfg(feature = "sys_info")]
SysInfo(Box<SysInfoModule>),
#[cfg(feature = "sway")]
SwayMode(Box<SwayModeModule>),
#[cfg(feature = "tray")]
Tray(Box<TrayModule>),
#[cfg(feature = "upower")]
@ -109,6 +109,8 @@ impl ModuleConfig {
}
match self {
#[cfg(feature = "bindmode")]
Self::Bindmode(module) => create!(module),
#[cfg(feature = "cairo")]
Self::Cairo(module) => create!(module),
#[cfg(feature = "clipboard")]
@ -135,8 +137,6 @@ impl ModuleConfig {
Self::Script(module) => create!(module),
#[cfg(feature = "sys_info")]
Self::SysInfo(module) => create!(module),
#[cfg(feature = "sway")]
Self::SwayMode(module) => create!(module),
#[cfg(feature = "tray")]
Self::Tray(module) => create!(module),
#[cfg(feature = "upower")]

View file

@ -1,18 +1,18 @@
use crate::clients::compositor::BindModeUpdate;
use crate::config::{CommonConfig, LayoutConfig, TruncateMode};
use crate::gtk_helpers::IronbarLabelExt;
use crate::modules::{Module, ModuleInfo, ModuleParts, ModuleUpdateEvent, WidgetContext};
use crate::{await_sync, glib_recv, module_impl, try_send};
use color_eyre::{Report, Result};
use crate::modules::{Module, ModuleInfo, ModuleParts, WidgetContext};
use crate::{glib_recv, module_impl, module_update, send_async, spawn};
use color_eyre::Result;
use gtk::Label;
use gtk::prelude::*;
use serde::Deserialize;
use swayipc_async::ModeEvent;
use tokio::sync::mpsc;
use tracing::{info, trace};
#[derive(Debug, Deserialize, Clone)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct SwayModeModule {
pub struct Bindmode {
// -- Common --
/// See [truncate options](module-level-options#truncate-mode).
///
@ -28,11 +28,11 @@ pub struct SwayModeModule {
pub common: Option<CommonConfig>,
}
impl Module<Label> for SwayModeModule {
type SendMessage = ModeEvent;
impl Module<Label> for Bindmode {
type SendMessage = BindModeUpdate;
type ReceiveMessage = ();
module_impl!("sway_mode");
module_impl!("bindmode");
fn spawn_controller(
&self,
@ -40,20 +40,18 @@ impl Module<Label> for SwayModeModule {
context: &WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
_rx: mpsc::Receiver<Self::ReceiveMessage>,
) -> Result<()> {
info!("Sway Mode module started");
info!("Bindmode module started");
let client = context.try_client::<dyn crate::clients::compositor::BindModeClient>()?;
let tx = context.tx.clone();
await_sync(async move {
let client = context.ironbar.clients.borrow_mut().sway()?;
client
.add_listener::<swayipc_async::ModeEvent>(move |mode| {
trace!("mode: {:?}", mode);
try_send!(tx, ModuleUpdateEvent::Update(mode.clone()));
})
.await?;
Ok::<(), Report>(())
})?;
let mut rx = client.subscribe()?;
spawn(async move {
while let Ok(ev) = rx.recv().await {
module_update!(tx, ev);
}
});
Ok(())
}
@ -69,24 +67,20 @@ impl Module<Label> for SwayModeModule {
.justify(self.layout.justify.into())
.build();
if let Some(truncate) = self.truncate {
label.truncate(truncate);
}
{
let label = label.clone();
if let Some(truncate) = self.truncate {
label.truncate(truncate);
}
let on_mode = move |mode: ModeEvent| {
let on_mode = move |mode: BindModeUpdate| {
trace!("mode: {:?}", mode);
label.set_use_markup(mode.pango_markup);
if mode.change == "default" {
label.set_label_escaped("");
} else {
label.set_label_escaped(&mode.change);
}
label.set_label_escaped(&mode.name);
};
glib_recv!(context.subscribe(), mode => on_mode(mode));
glib_recv!(context.subscribe(), on_mode);
}
Ok(ModuleParts {

View file

@ -17,6 +17,8 @@ use crate::gtk_helpers::{IronbarGtkExt, WidgetGeometry};
use crate::popup::Popup;
use crate::{Ironbar, glib_recv_mpsc, send};
#[cfg(feature = "bindmode")]
pub mod bindmode;
#[cfg(feature = "cairo")]
pub mod cairo;
#[cfg(feature = "clipboard")]
@ -50,8 +52,6 @@ pub mod notifications;
#[cfg(feature = "script")]
pub mod script;
#[cfg(feature = "sway")]
pub mod sway;
#[cfg(feature = "sys_info")]
pub mod sysinfo;
#[cfg(feature = "tray")]

View file

@ -1 +0,0 @@
pub mod mode;