You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
1.9 KiB
81 lines
1.9 KiB
3 years ago
|
use super::theme;
|
||
|
use crate::ui::{
|
||
|
component::{Child, Component, ComponentExt, Event, EventCtx},
|
||
|
display,
|
||
2 years ago
|
geometry::Rect,
|
||
3 years ago
|
};
|
||
|
|
||
2 years ago
|
pub struct Frame<T, U> {
|
||
3 years ago
|
area: Rect,
|
||
|
title: U,
|
||
|
content: Child<T>,
|
||
|
}
|
||
|
|
||
2 years ago
|
impl<T, U> Frame<T, U>
|
||
|
where
|
||
|
T: Component,
|
||
|
U: AsRef<[u8]>,
|
||
|
{
|
||
3 years ago
|
pub fn new(area: Rect, title: U, content: impl FnOnce(Rect) -> T) -> Self {
|
||
|
let (title_area, content_area) = Self::areas(area);
|
||
|
Self {
|
||
|
area: title_area,
|
||
|
title,
|
||
|
content: content(content_area).into_child(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fn areas(area: Rect) -> (Rect, Rect) {
|
||
2 years ago
|
const HEADER_SPACE: i32 = 14;
|
||
|
let header_height = theme::FONT_BOLD.line_height() - theme::CONTENT_BORDER;
|
||
3 years ago
|
|
||
|
let (header_area, content_area) = area.hsplit(header_height);
|
||
2 years ago
|
let (_space, header_area) = header_area.vsplit(theme::CONTENT_BORDER);
|
||
3 years ago
|
let (_space, content_area) = content_area.hsplit(HEADER_SPACE);
|
||
|
|
||
|
(header_area, content_area)
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
impl<T, U> Component for Frame<T, U>
|
||
|
where
|
||
|
T: Component,
|
||
|
U: AsRef<[u8]>,
|
||
|
{
|
||
3 years ago
|
type Msg = T::Msg;
|
||
|
|
||
|
fn event(&mut self, ctx: &mut EventCtx, event: Event) -> Option<Self::Msg> {
|
||
|
self.content.event(ctx, event)
|
||
|
}
|
||
|
|
||
|
fn paint(&mut self) {
|
||
|
display::text(
|
||
2 years ago
|
self.area.bottom_left(),
|
||
3 years ago
|
self.title.as_ref(),
|
||
|
theme::FONT_BOLD,
|
||
2 years ago
|
theme::GREY_LIGHT,
|
||
3 years ago
|
theme::BG,
|
||
|
);
|
||
|
self.content.paint();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[cfg(feature = "ui_debug")]
|
||
2 years ago
|
impl<T, U> crate::trace::Trace for Frame<T, U>
|
||
3 years ago
|
where
|
||
|
T: crate::trace::Trace,
|
||
|
U: crate::trace::Trace + AsRef<[u8]>,
|
||
|
{
|
||
|
fn trace(&self, t: &mut dyn crate::trace::Tracer) {
|
||
2 years ago
|
t.open("Frame");
|
||
3 years ago
|
t.field("title", &self.title);
|
||
|
t.field("content", &self.content);
|
||
|
t.close();
|
||
|
}
|
||
2 years ago
|
|
||
|
fn bounds(&self, sink: &dyn Fn(Rect)) {
|
||
|
sink(self.area);
|
||
|
self.content.bounds(sink);
|
||
|
}
|
||
3 years ago
|
}
|