canary-rs/scripts/sao-ui/src/notifications.rs

83 lines
2.3 KiB
Rust

// 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,
})
}
}