Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2024"
[dependencies]
openaction = "2.6"
discord-ipc-rust = { git = "https://github.com/nekename/discord-ipc-rust.git" }
base64 = "0.22"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1.52", features = ["rt-multi-thread", "macros"] }
Expand Down
50 changes: 43 additions & 7 deletions src/actions/notifications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,51 @@ use discord_ipc_rust::models::send::commands::{SelectTextChannelArgs, SentComman
use openaction::{Action, ActionUuid, Instance, OpenActionResult, async_trait};
use serde::{Deserialize, Serialize};

pub async fn update_title(instance: &Instance) -> OpenActionResult<()> {
const NOTIFICATION_SVG: &str = include_str!("../../assets/actions/notifications.svg");

fn generate_badge_xml(count: usize) -> String {
if count == 0 {
return String::new();
}
let title = format!("{}", count);

if count < 100 {
format!(
r#"<circle cx="114" cy="114" r="18" fill="red"/>
<text x="114" y="120" font-size="20" font-weight="bold" fill="white" text-anchor="middle" font-family="Arial, sans-serif">{}</text>"#,
title
)
} else {
let digit_count = title.len() as i32;
let width = 20 + (digit_count * 9);
let rect_x = 105 - (width / 2);
format!(
r#"<rect x="{}" y="96" width="{}" height="36" rx="18" ry="18" fill="red"/>
<text x="105" y="120" font-size="20" font-weight="bold" fill="white" text-anchor="middle" font-family="Arial, sans-serif">{}</text>"#,
rect_x, width, title
)
}
}
Comment on lines +22 to +32

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this if you want : )


pub async fn update_image(instance: &Instance) -> OpenActionResult<()> {
use base64::{Engine, prelude::BASE64_STANDARD};

let cache = notification_cache().read().await;
let title = format!("{}", cache.len());
let count = cache.len();
let final_svg = if count > 0 {
let badge_xml = generate_badge_xml(count);
NOTIFICATION_SVG.replace("</svg>", &format!("{}\n</svg>", badge_xml))
} else {
NOTIFICATION_SVG.to_string()
};

if let Err(e) = instance.set_title(Some(title), None).await {
let b64_encoded = BASE64_STANDARD.encode(final_svg.as_bytes());
let data_url = format!("data:image/svg+xml;base64,{}", b64_encoded);

if let Err(e) = instance.set_image(Some(data_url), None).await {
log::error!("Failed to update notifications action title: {}", e);
instance.show_alert().await?;
}

Ok(())
}

Expand Down Expand Up @@ -44,7 +80,7 @@ impl Action for NotificationsAction {
instance: &Instance,
_settings: &Self::Settings,
) -> OpenActionResult<()> {
update_title(instance).await
update_image(instance).await
}

async fn key_down(
Expand All @@ -56,7 +92,7 @@ impl Action for NotificationsAction {
NotificationsActionType::DoNothing => return Ok(()),
NotificationsActionType::Clear => {
notification_cache().write().await.clear();
update_title(instance).await?;
update_image(instance).await?;
return Ok(());
}
NotificationsActionType::OpenAndClear => {
Expand All @@ -78,7 +114,7 @@ impl Action for NotificationsAction {
return Ok(());
};

update_title(instance).await?;
update_image(instance).await?;

let mut client_lock = discord_client().write().await;
let Some(client) = client_lock.as_mut() else {
Expand Down
2 changes: 1 addition & 1 deletion src/rpc_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ async fn handle_select_voice_channel(channel_id: Option<String>) {
async fn handle_notification(notification: NotificationCreateData) {
crate::cache::add_notification_to_cache(notification).await;
for instance in visible_instances(crate::actions::NotificationsAction::UUID).await {
let _ = crate::actions::notifications::update_title(&instance).await;
let _ = crate::actions::notifications::update_image(&instance).await;
}
}

Expand Down