Skip to main content

cosmic_applet_avestruz/views/components/
badge.rs

1// SPDX-License-Identifier: MIT
2
3//! A small mark over the corner of something: how many new things are
4//! behind it, or only that there are some.
5
6use cosmic::Element;
7use cosmic::iced::{Background, Border, Color, Length};
8use cosmic::widget::{container, text};
9
10use crate::messages::Message;
11
12/// How tall a count is, and how wide at the least: a circle around one
13/// figure.
14const COUNT_SIZE: f32 = 14.0;
15
16/// How wide and tall the mark is that only says there is something.
17const DOT_SIZE: f32 = 7.0;
18
19/// `content`, on the accent colour, with its ends rounded all the way.
20fn on_accent<'a, Message: 'a>(
21    content: impl Into<Element<'a, Message>>,
22    height: f32,
23) -> container::Container<'a, Message, cosmic::Theme> {
24    container(content).height(Length::Fixed(height)).class(cosmic::theme::Container::custom(move |theme| {
25        let cosmic = theme.cosmic();
26        container::Style {
27            text_color: Some(cosmic.on_accent_color().into()),
28            background: Some(Background::Color(cosmic.accent_color().into())),
29            border: Border { radius: (height / 2.0).into(), ..Default::default() },
30            ..Default::default()
31        }
32    }))
33}
34
35/// `count`, small, on the accent colour: for over the corner of a button.
36pub fn count_badge<'a>(count: String) -> Element<'a, Message> {
37    let on_accent_color: Color = cosmic::theme::active().cosmic().on_accent_color().into();
38    let figures = text(count).size(9).font(cosmic::font::bold()).class(cosmic::theme::Text::Color(on_accent_color));
39    on_accent(figures, COUNT_SIZE).padding([0, 4]).center_x(Length::Shrink).center_y(Length::Fixed(COUNT_SIZE)).into()
40}
41
42/// A dot of the accent colour: there is something new, however much.
43pub fn dot_badge<'a, Message: 'a>() -> Element<'a, Message> {
44    on_accent(cosmic::widget::Space::new(), DOT_SIZE).width(Length::Fixed(DOT_SIZE)).into()
45}