1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-08-17 23:01:04 +02:00

feat: swaync notifications module

Adds a new module which connects to SwayNC to display information from the notifications deamon.

Resolves #7
This commit is contained in:
Jake Stanger 2024-03-03 22:42:57 +00:00
parent 660dc81e7d
commit 7742a46578
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61
15 changed files with 592 additions and 3 deletions

View file

@ -34,6 +34,8 @@ pub mod label;
pub mod launcher;
#[cfg(feature = "music")]
pub mod music;
#[cfg(feature = "notifications")]
pub mod notifications;
pub mod script;
#[cfg(feature = "sys_info")]
pub mod sysinfo;

View file

@ -0,0 +1,190 @@
use crate::clients::swaync;
use crate::config::CommonConfig;
use crate::gtk_helpers::IronbarGtkExt;
use crate::modules::{Module, ModuleInfo, ModuleParts, ModuleUpdateEvent, WidgetContext};
use crate::{glib_recv, send_async, spawn, try_send};
use gtk::prelude::*;
use gtk::{Align, Button, Label, Overlay};
use serde::Deserialize;
use tokio::sync::mpsc::Receiver;
use tracing::error;
#[derive(Debug, Deserialize, Clone)]
pub struct NotificationsModule {
#[serde(default = "crate::config::default_true")]
show_count: bool,
#[serde(default)]
icons: Icons,
#[serde(flatten)]
pub common: Option<CommonConfig>,
}
#[derive(Debug, Deserialize, Clone)]
struct Icons {
#[serde(default = "default_icon_closed_none")]
closed_none: String,
#[serde(default = "default_icon_closed_some")]
closed_some: String,
#[serde(default = "default_icon_closed_dnd")]
closed_dnd: String,
#[serde(default = "default_icon_open_none")]
open_none: String,
#[serde(default = "default_icon_open_some")]
open_some: String,
#[serde(default = "default_icon_open_dnd")]
open_dnd: String,
}
impl Default for Icons {
fn default() -> Self {
Self {
closed_none: default_icon_closed_none(),
closed_some: default_icon_closed_some(),
closed_dnd: default_icon_closed_dnd(),
open_none: default_icon_open_none(),
open_some: default_icon_open_some(),
open_dnd: default_icon_open_dnd(),
}
}
}
fn default_icon_closed_none() -> String {
String::from("󰍥")
}
fn default_icon_closed_some() -> String {
String::from("󱥂")
}
fn default_icon_closed_dnd() -> String {
String::from("󱅯")
}
fn default_icon_open_none() -> String {
String::from("󰍡")
}
fn default_icon_open_some() -> String {
String::from("󱥁")
}
fn default_icon_open_dnd() -> String {
String::from("󱅮")
}
impl Icons {
fn icon(&self, value: &swaync::Event) -> &str {
match (value.cc_open, value.count > 0, value.dnd) {
(true, _, true) => &self.open_dnd,
(true, true, false) => &self.open_some,
(true, false, false) => &self.open_none,
(false, _, true) => &self.closed_dnd,
(false, true, false) => &self.closed_some,
(false, false, false) => &self.closed_none,
}
.as_str()
}
}
#[derive(Debug, Clone, Copy)]
pub enum UiEvent {
ToggleVisibility,
}
impl Module<Overlay> for NotificationsModule {
type SendMessage = swaync::Event;
type ReceiveMessage = UiEvent;
fn name() -> &'static str {
"notifications"
}
fn spawn_controller(
&self,
_info: &ModuleInfo,
context: &WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
mut rx: Receiver<Self::ReceiveMessage>,
) -> color_eyre::Result<()>
where
<Self as Module<Overlay>>::SendMessage: Clone,
{
let client = context.client::<swaync::Client>();
{
let client = client.clone();
let mut rx = client.subscribe();
let tx = context.tx.clone();
spawn(async move {
let initial_state = client.state().await;
match initial_state {
Ok(ev) => send_async!(tx, ModuleUpdateEvent::Update(ev)),
Err(err) => error!("{err:?}"),
};
while let Ok(ev) = rx.recv().await {
send_async!(tx, ModuleUpdateEvent::Update(ev));
}
});
}
spawn(async move {
while let Some(event) = rx.recv().await {
match event {
UiEvent::ToggleVisibility => client.toggle_visibility().await,
}
}
});
Ok(())
}
fn into_widget(
self,
context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
_info: &ModuleInfo,
) -> color_eyre::Result<ModuleParts<Overlay>>
where
<Self as Module<Overlay>>::SendMessage: Clone,
{
let overlay = Overlay::new();
let button = Button::with_label(&self.icons.closed_none);
overlay.add(&button);
let label = Label::builder()
.label("0")
.halign(Align::End)
.valign(Align::Start)
.build();
if self.show_count {
label.add_class("count");
overlay.add_overlay(&label);
}
let ctx = context.controller_tx.clone();
button.connect_clicked(move |_| {
try_send!(ctx, UiEvent::ToggleVisibility);
});
{
let button = button.clone();
glib_recv!(context.subscribe(), ev => {
let icon = self.icons.icon(&ev);
button.set_label(icon);
label.set_label(&ev.count.to_string());
label.set_visible(self.show_count && ev.count > 0);
});
}
Ok(ModuleParts {
widget: overlay,
popup: None,
})
}
}