Skip to content

Commit

Permalink
feat: use markdown render body
Browse files Browse the repository at this point in the history
  • Loading branch information
Decodetalkers committed Sep 21, 2024
1 parent 3d3c297 commit 9c828eb
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
39 changes: 39 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lala_bar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ iced = { version = "0.13.1", features = [
"image",
"advanced",
"svg",
"markdown"
] }
iced_runtime = "0.13.0"
iced_layershell = "0.7.1"
Expand Down
39 changes: 30 additions & 9 deletions lala_bar/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<Message> = 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))
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -207,6 +220,7 @@ struct LalaMusicBar {
hiddenid_lock: bool,
right_panel: Option<iced::window::Id>,
notifications: HashMap<u32, NotifyUnitWidgetInfo>,
notifications_markdown: HashMap<u32, Vec<markdown::Item>>,
showned_notifications: HashMap<iced::window::Id, u32>,
cached_notifications: HashMap<iced::window::Id, NotifyUnitWidgetInfo>,
cached_hidden_notifications: Vec<NotifyUnitWidgetInfo>,
Expand Down Expand Up @@ -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()
})
Expand Down Expand Up @@ -555,6 +569,8 @@ enum Message {
QuiteMode(bool),
CloseErrorNotification(iced::window::Id),
Ready(Sender<NotifyCommand>),
#[allow(unused)]
LinkClicked(markdown::Url),
}

impl From<NotifyMessage> for Message {
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -980,6 +998,8 @@ impl MultiApplication for LalaMusicBar {
))
}

self.notifications_markdown
.insert(notify.id, markdown::parse(&notify.body).collect());
self.notifications.insert(
notify.id,
NotifyUnitWidgetInfo {
Expand Down Expand Up @@ -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));
Expand All @@ -1218,7 +1239,7 @@ impl MultiApplication for LalaMusicBar {
}
}
LaLaInfo::Notify(unitwidgetinfo) => {
let btnwidgets: Element<Message> = unitwidgetinfo.notify_button();
let btnwidgets: Element<Message> = unitwidgetinfo.notify_button(self);

let notify = &unitwidgetinfo.unit;
if notify.inline_reply_support() {
Expand Down

0 comments on commit 9c828eb

Please sign in to comment.