Add notifications support to SAO UI

This commit is contained in:
mars 2022-12-07 15:06:25 -07:00
parent a6b675dd11
commit 482a0de030
3 changed files with 87 additions and 0 deletions

View File

@ -11,7 +11,9 @@ crate-type = ["cdylib"]
glam = "^0.21"
keyframe = "1"
canary-music-player = { path = "../../apps/music-player" }
canary-notifications = { path = "../../apps/notifications" }
canary-script = { path = "../../crates/script" }
canary-textwrap = { path = "../../crates/textwrap" }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
wee_alloc = "^0.4"

View File

@ -7,6 +7,7 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
pub mod anim;
pub mod main_menu;
pub mod music_player;
pub mod notifications;
pub mod style;
pub mod widgets;
@ -14,6 +15,7 @@ use api::*;
use canary_script::*;
use main_menu::MainMenuPanel;
use music_player::MusicPlayerPanel;
use notifications::NotificationPanel;
use widgets::Widget;
export_abi!(bind_panel_impl);
@ -24,6 +26,7 @@ fn bind_panel_impl(panel: Panel, protocol: Message, msg: Message) -> Box<dyn Pan
match protocol.as_str() {
"tebibyte-media.desktop.music-player-controller" => MusicPlayerPanel::bind(panel, msg),
"tebibyte-media.desktop.notification" => NotificationPanel::bind(panel, msg),
"wip-dialog" => ConfirmationDialogPanel::bind(panel, msg),
_ => MainMenuPanel::bind(panel, msg),
}

View File

@ -0,0 +1,82 @@
// Copyright (c) 2022 Marceline Crmaer
// SPDX-License-Identifier: AGPL-3.0-or-later
use super::widgets::prelude::*;
use api::*;
use canary_script::*;
use canary_textwrap::{Content, Layout, TextCache};
use shell::Offset;
use text::{Label, LabelText};
pub struct NotificationPanel {
panel: Panel,
summary: Offset<Label>,
text_cache: TextCache,
body: Content,
body_layout: Layout,
body_rect: Rect,
}
impl PanelImpl for NotificationPanel {
fn update(&mut self, dt: f32) {}
fn draw(&mut self) {
let ctx = DrawContext::new(self.panel);
ctx.draw_rounded_rect(self.body_rect, 5.0, THEME.palette.base);
self.summary.draw(&ctx);
let ctx = ctx.with_offset(Vec2::new(5.0, 20.0));
self.body_layout
.draw(&self.text_cache, &ctx, 5.0, 8.0, THEME.palette.text);
}
fn on_resize(&mut self, new_size: Vec2) {
self.body_rect = Rect::from_xy_size(Vec2::ZERO, new_size);
let width = (new_size.x - 10.0) / 5.0;
self.body_layout = self.body.layout(&mut self.text_cache, width);
}
fn on_cursor_event(&mut self, kind: CursorEventKind, at: Vec2) {}
fn on_message(&mut self, msg: Message) {}
}
impl NotificationPanel {
pub fn bind(panel: Panel, msg: Message) -> Box<dyn PanelImpl> {
let msg = msg.to_vec();
let msg: canary_notifications::Contents = serde_json::from_slice(&msg).unwrap();
let font = Font::new(crate::DISPLAY_FONT);
let text = msg.summary;
let text = LabelText { font, text };
let summary = Label::new(
text,
text::HorizontalAlignment::Left,
10.0,
THEME.palette.text,
0.0,
0.0,
0.0,
);
let summary = Offset::new(summary, Vec2::new(5.0, 12.0));
let font = Font::new(crate::CONTENT_FONT);
let text = msg.body.unwrap_or(String::new());
let mut text_cache = TextCache::default();
let body = Content::from_plain(&mut text_cache, font, &text);
let body_layout = body.layout(&text_cache, 0.0);
let body_rect = Rect::from_xy_size(Vec2::ZERO, Vec2::ZERO);
Box::new(Self {
panel,
summary,
text_cache,
body,
body_layout,
body_rect,
})
}
}