From 9c828ebe627f8967fdf445da3e07c532b5f5c38f Mon Sep 17 00:00:00 2001 From: ShootingStarDragons Date: Sat, 21 Sep 2024 20:26:58 +0900 Subject: [PATCH] feat: use markdown render body --- Cargo.lock | 39 +++++++++++++++++++++++++++++++++++++++ lala_bar/Cargo.toml | 1 + lala_bar/src/main.rs | 39 ++++++++++++++++++++++++++++++--------- 3 files changed, 70 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 05d0e52..aaa0b1e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1445,6 +1445,15 @@ dependencies = [ "windows-targets 0.48.5", ] +[[package]] +name = "getopts" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5" +dependencies = [ + "unicode-width", +] + [[package]] name = "getrandom" version = "0.2.15" @@ -1957,9 +1966,11 @@ dependencies = [ "iced_runtime", "num-traits", "once_cell", + "pulldown-cmark", "rustc-hash 2.0.0", "thiserror", "unicode-segmentation", + "url", ] [[package]] @@ -3047,6 +3058,25 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43d84d1d7a6ac92673717f9f6d1518374ef257669c24ebc5ac25d5033828be58" +[[package]] +name = "pulldown-cmark" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "679341d22c78c6c649893cbd6c3278dcbe9fc4faa62fea3a9296ae2b50c14625" +dependencies = [ + "bitflags 2.6.0", + "getopts", + "memchr", + "pulldown-cmark-escape", + "unicase", +] + +[[package]] +name = "pulldown-cmark-escape" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "007d8adb5ddab6f8e3f491ac63566a7d5002cc7ed73901f72057943fa71ae1ae" + [[package]] name = "qoi" version = "0.4.1" @@ -3988,6 +4018,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "unicase" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7d2d4dafb69621809a81864c9c1b864479e1235c0dd4e199924b9742439ed89" +dependencies = [ + "version_check", +] + [[package]] name = "unicode-bidi" version = "0.3.15" diff --git a/lala_bar/Cargo.toml b/lala_bar/Cargo.toml index 1e5281e..7dff293 100644 --- a/lala_bar/Cargo.toml +++ b/lala_bar/Cargo.toml @@ -18,6 +18,7 @@ iced = { version = "0.13.1", features = [ "image", "advanced", "svg", + "markdown" ] } iced_runtime = "0.13.0" iced_layershell = "0.7.1" diff --git a/lala_bar/src/main.rs b/lala_bar/src/main.rs index 65e0f68..1ef7662 100644 --- a/lala_bar/src/main.rs +++ b/lala_bar/src/main.rs @@ -4,8 +4,8 @@ use async_trait::async_trait; use futures::future::pending; use futures::StreamExt; use iced::widget::{ - button, checkbox, column, container, image, row, scrollable, slider, svg, text, text_input, - Space, + button, checkbox, column, container, image, markdown, row, scrollable, slider, svg, text, + text_input, Space, }; use iced::{executor, Alignment, Font}; use iced::{Element, Length, Task as Command, Theme}; @@ -97,13 +97,26 @@ struct NotifyUnitWidgetInfo { } impl NotifyUnitWidgetInfo { - fn notify_button<'a>(&self) -> Element<'a, Message> { + fn notify_button<'a>(&self, bar: &'a LalaMusicBar) -> Element<'a, Message> { let notify = &self.unit; let notify_theme = if notify.is_critical() { button::primary } else { button::secondary }; + let markdown_info = bar.notifications_markdown.get(&self.unit.id); + let text_render: Element = match markdown_info { + Some(data) => markdown::view( + data, + markdown::Settings::default(), + markdown::Style::from_palette(bar.theme().palette()), + ) + .map(Message::LinkClicked) + .into(), + None => text(notify.body.clone()) + .shaping(text::Shaping::Advanced) + .into(), + }; match notify.image() { Some(ImageInfo::Svg(path)) => button(row![ svg(svg::Handle::from_path(path)) @@ -118,7 +131,7 @@ impl NotifyUnitWidgetInfo { weight: iced::font::Weight::Bold, ..Default::default() }), - text(notify.body.clone()).shaping(text::Shaping::Advanced) + text_render ] ]) .style(notify_theme) @@ -145,7 +158,7 @@ impl NotifyUnitWidgetInfo { weight: iced::font::Weight::Bold, ..Default::default() }), - text(notify.body.clone()).shaping(text::Shaping::Advanced) + text_render ] ]) .width(Length::Fill) @@ -164,7 +177,7 @@ impl NotifyUnitWidgetInfo { weight: iced::font::Weight::Bold, ..Default::default() }), - text(notify.body.clone()).shaping(text::Shaping::Advanced) + text_render ] ]) .width(Length::Fill) @@ -174,7 +187,7 @@ impl NotifyUnitWidgetInfo { .into(), _ => button(column![ text(notify.summery.clone()).shaping(text::Shaping::Advanced), - text(notify.body.clone()).shaping(text::Shaping::Advanced) + text_render ]) .width(Length::Fill) .height(Length::Fill) @@ -207,6 +220,7 @@ struct LalaMusicBar { hiddenid_lock: bool, right_panel: Option, notifications: HashMap, + notifications_markdown: HashMap>, showned_notifications: HashMap, cached_notifications: HashMap, cached_hidden_notifications: Vec, @@ -458,7 +472,7 @@ impl LalaMusicBar { .hidden_notification() .iter() .map(|wdgetinfo| { - container(wdgetinfo.notify_button()) + container(wdgetinfo.notify_button(self)) .height(Length::Fixed(100.)) .into() }) @@ -555,6 +569,8 @@ enum Message { QuiteMode(bool), CloseErrorNotification(iced::window::Id), Ready(Sender), + #[allow(unused)] + LinkClicked(markdown::Url), } impl From for Message { @@ -743,6 +759,7 @@ impl MultiApplication for LalaMusicBar { hiddenid: None, hiddenid_lock: false, notifications: HashMap::new(), + notifications_markdown: HashMap::new(), showned_notifications: HashMap::new(), cached_notifications: HashMap::new(), cached_hidden_notifications: Vec::new(), @@ -822,6 +839,7 @@ impl MultiApplication for LalaMusicBar { // If the widget is marked to removed // Then delete it self.notifications.remove(&nid); + self.notifications_markdown.remove(&nid); } } self.cached_notifications.remove(&id); @@ -980,6 +998,8 @@ impl MultiApplication for LalaMusicBar { )) } + self.notifications_markdown + .insert(notify.id, markdown::parse(¬ify.body).collect()); self.notifications.insert( notify.id, NotifyUnitWidgetInfo { @@ -1192,6 +1212,7 @@ impl MultiApplication for LalaMusicBar { } } + self.notifications_markdown.clear(); self.notifications.clear(); self.update_hidden_notification(); commands.push(Command::perform(async {}, |_| Message::CheckOutput)); @@ -1218,7 +1239,7 @@ impl MultiApplication for LalaMusicBar { } } LaLaInfo::Notify(unitwidgetinfo) => { - let btnwidgets: Element = unitwidgetinfo.notify_button(); + let btnwidgets: Element = unitwidgetinfo.notify_button(self); let notify = &unitwidgetinfo.unit; if notify.inline_reply_support() {