Skip to content

Commit

Permalink
feat: rename sway_mode to bindmode and add Hyprland support
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrigodd committed Feb 4, 2025
1 parent 03e6f10 commit 0b907ce
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 174 deletions.
1 change: 1 addition & 0 deletions docs/_Sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

# Modules

- [Bindmode](bindmode)
- [Cairo](cairo)
- [Clipboard](clipboard)
- [Clock](clock)
Expand Down
75 changes: 75 additions & 0 deletions docs/modules/Bindmode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
Displays Sway's current binding mode or [Hyprland's current submap](https://wiki.hyprland.org/Configuring/Binds/#submaps)
in a label. If the current sway mode is "default", nothing is displayed.

## Configuration

> Type: `bindmode`
| Name | Type | Default | Description |
| --------------------- | ------------------------------------------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
| `truncate` | `'start'` or `'middle'` or `'end'` or `Map` | `null` | The location of the ellipses and where to truncate text from. Leave null to avoid truncating. Use the long-hand `Map` version if specifying a length. |
| `truncate.mode` | `'start'` or `'middle'` or `'end'` | `null` | The location of the ellipses and where to truncate text from. Leave null to avoid truncating. |
| `truncate.length` | `integer` | `null` | The fixed width (in chars) of the widget. Leave blank to let GTK automatically handle. |
| `truncate.max_length` | `integer` | `null` | The maximum number of characters before truncating. Leave blank to let GTK automatically handle. |

<details>
<summary>JSON</summary>

```json
{
"end": [
{
"type": "bindmode",
"truncate": "start"
}
]
}
```

</details>

<details>
<summary>TOML</summary>

```toml
[[end]]
type = "bindmode"
truncate = "start"
```

</details>

<details>
<summary>YAML</summary>

```yaml
end:
- type: "bindmode"
truncate: "start"
```
</details>
<details>
<summary>Corn</summary>
```corn
{
end = [
{
type = "bindmode"
truncate = "start"
}
]
}
```

</details>

## Styling

| Selector | Description |
| ----------- | ---------------------- |
| `.bindmode` | Bind mode label widget |

For more information on styling, please see the [styling guide](styling-guide).
77 changes: 5 additions & 72 deletions docs/modules/Sway-mode.md
Original file line number Diff line number Diff line change
@@ -1,78 +1,11 @@
Displays the current sway mode in a label. If the current sway mode is
Displays Sway's current binding mode in a label. If the current sway mode is
"default", nothing is displayed.

> [!NOTE]
> This module only works under the [Sway](https://swaywm.org/) compositor.
> This module is now only a alias for the [Bindmode](Bindmode.md) module, which
> supports both Sway and Hyprland. See the mentioned module for more
> information.
## Configuration

> Type: `sway-mode`
| Name | Type | Default | Description |
| --------------------- | ------------------------------------------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
| `truncate` | `'start'` or `'middle'` or `'end'` or `Map` | `null` | The location of the ellipses and where to truncate text from. Leave null to avoid truncating. Use the long-hand `Map` version if specifying a length. |
| `truncate.mode` | `'start'` or `'middle'` or `'end'` | `null` | The location of the ellipses and where to truncate text from. Leave null to avoid truncating. |
| `truncate.length` | `integer` | `null` | The fixed width (in chars) of the widget. Leave blank to let GTK automatically handle. |
| `truncate.max_length` | `integer` | `null` | The maximum number of characters before truncating. Leave blank to let GTK automatically handle. |

<details>
<summary>JSON</summary>

```json
{
"end": [
{
"type": "sway-mode",
"truncate": "start"
}
]
}
```

</details>

<details>
<summary>TOML</summary>

```toml
[[end]]
type = "sway-mode"
truncate = "start"
```

</details>

<details>
<summary>YAML</summary>

```yaml
end:
- type: "sway-mode"
truncate: "start"
```
</details>
<details>
<summary>Corn</summary>
```corn
{
end = [
{
type = "sway-mode"
truncate = "start"
}
]
}
```

</details>

## Styling

| Selector | Description |
| ------------ | ---------------------- |
| `.sway_mode` | Sway mode label widget |

For more information on styling, please see the [styling guide](styling-guide).
> Type: `sway_mode`
25 changes: 20 additions & 5 deletions src/clients/compositor/hyprland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct Client {
}

impl Client {
pub(crate) fn new() -> Self {
pub(crate) fn new() -> Result<Self> {
let (workspace_tx, workspace_rx) = channel(16);
let (keyboard_layout_tx, keyboard_layout_rx) = channel(4);

Expand All @@ -34,24 +34,37 @@ impl Client {
_keyboard_layout_rx: keyboard_layout_rx,
};

instance.listen_workspace_events();
instance
instance.listen_workspace_events()?;
Ok(instance)
}

fn listen_workspace_events(&self) {
pub(crate) fn listen_submap_events(&self, callback: impl Fn(String) + Send + 'static) {
spawn_blocking(move || {
let mut event_listener = EventListener::new();

event_listener.add_sub_map_change_handler(callback);

event_listener
.start_listener()
.expect("Failed to start listener");
});
}

fn listen_workspace_events(&self) -> Result<()> {
info!("Starting Hyprland event listener");

let tx = self.workspace_tx.clone();
let keyboard_layout_tx = self.keyboard_layout_tx.clone();

let active = Self::get_active_workspace()?;

spawn_blocking(move || {
let mut event_listener = EventListener::new();

// we need a lock to ensure events don't run at the same time
let lock = arc_mut!(());

// cache the active workspace since Hyprland doesn't give us the prev active
let active = Self::get_active_workspace().expect("Failed to get active workspace");
let active = arc_mut!(Some(active));

{
Expand Down Expand Up @@ -273,6 +286,8 @@ impl Client {
.start_listener()
.expect("Failed to start listener");
});

Ok(())
}

/// Sends a `WorkspaceUpdate::Focus` event
Expand Down
2 changes: 1 addition & 1 deletion src/clients/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl Clients {
let client = if let Some(client) = &self.hyprland {
client.clone()
} else {
let client = Arc::new(compositor::hyprland::Client::new());
let client = Arc::new(compositor::hyprland::Client::new()?);
self.hyprland.replace(client.clone());
client
};
Expand Down
10 changes: 7 additions & 3 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ mod common;
mod r#impl;
mod truncate;

#[cfg(any(feature = "hyprland", feature = "sway"))]
use crate::modules::bindmode::Bindmode;
#[cfg(feature = "cairo")]
use crate::modules::cairo::CairoModule;
#[cfg(feature = "clipboard")]
Expand All @@ -23,8 +25,6 @@ use crate::modules::networkmanager::NetworkManagerModule;
#[cfg(feature = "notifications")]
use crate::modules::notifications::NotificationsModule;
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")]
Expand Down Expand Up @@ -76,7 +76,9 @@ pub enum ModuleConfig {
#[cfg(feature = "sys_info")]
SysInfo(Box<SysInfoModule>),
#[cfg(feature = "sway")]
SwayMode(Box<SwayModeModule>),
SwayMode(Box<Bindmode>),
#[cfg(any(feature = "sway", feature = "hyprland"))]
Bindmode(Box<Bindmode>),
#[cfg(feature = "tray")]
Tray(Box<TrayModule>),
#[cfg(feature = "upower")]
Expand Down Expand Up @@ -134,6 +136,8 @@ impl ModuleConfig {
Self::Volume(module) => create!(module),
#[cfg(feature = "workspaces")]
Self::Workspaces(module) => create!(module),
#[cfg(any(feature = "hyprland", feature = "sway"))]
Self::Bindmode(module) => create!(module),
}
}
}
Expand Down
Loading

0 comments on commit 0b907ce

Please sign in to comment.