feat(core/rust): make Checklist component usable by more models

pull/3011/head
grdddj 1 year ago committed by Martin Milata
parent 610c832dee
commit db38b708b4

@ -540,13 +540,15 @@ pub struct Checklist<T> {
current: usize,
icon_current: Icon,
icon_done: Icon,
/// How wide will the left icon column be
check_width: i16,
/// Offset of the icon representing DONE
done_offset: Offset,
/// Offset of the icon representing CURRENT
current_offset: Offset,
}
impl<T> Checklist<T> {
const CHECK_WIDTH: i16 = 16;
const DONE_OFFSET: Offset = Offset::new(-2, 6);
const CURRENT_OFFSET: Offset = Offset::new(2, 3);
pub fn from_paragraphs(
icon_current: Icon,
icon_done: Icon,
@ -559,9 +561,27 @@ impl<T> Checklist<T> {
current,
icon_current,
icon_done,
check_width: 0,
done_offset: Offset::zero(),
current_offset: Offset::zero(),
}
}
pub fn with_check_width(mut self, check_width: i16) -> Self {
self.check_width = check_width;
self
}
pub fn with_done_offset(mut self, done_offset: Offset) -> Self {
self.done_offset = done_offset;
self
}
pub fn with_current_offset(mut self, current_offset: Offset) -> Self {
self.current_offset = current_offset;
self
}
fn paint_icon(&self, layout: &TextLayout, icon: Icon, offset: Offset) {
let top_left = Point::new(self.area.x0, layout.bounds.y0);
icon.draw(
@ -581,7 +601,7 @@ where
fn place(&mut self, bounds: Rect) -> Rect {
self.area = bounds;
let para_area = bounds.inset(Insets::left(Self::CHECK_WIDTH));
let para_area = bounds.inset(Insets::left(self.check_width));
self.paragraphs.place(para_area);
self.area
}
@ -595,10 +615,10 @@ where
let current_visible = self.current.saturating_sub(self.paragraphs.offset.par);
for layout in self.paragraphs.visible.iter().take(current_visible) {
self.paint_icon(layout, self.icon_done, Self::DONE_OFFSET);
self.paint_icon(layout, self.icon_done, self.done_offset);
}
if let Some(layout) = self.paragraphs.visible.iter().nth(current_visible) {
self.paint_icon(layout, self.icon_current, Self::CURRENT_OFFSET);
self.paint_icon(layout, self.icon_current, self.current_offset);
}
}

@ -1327,7 +1327,10 @@ extern "C" fn new_show_checklist(n_args: usize, args: *const Obj, kwargs: *mut M
paragraphs
.into_paragraphs()
.with_spacing(theme::CHECKLIST_SPACING),
),
)
.with_check_width(theme::CHECKLIST_CHECK_WIDTH)
.with_current_offset(theme::CHECKLIST_CURRENT_OFFSET)
.with_done_offset(theme::CHECKLIST_DONE_OFFSET),
theme::button_bar(Button::with_text(button).map(|msg| {
(matches!(msg, ButtonMsg::Clicked)).then(|| CancelConfirmMsg::Confirmed)
})),

@ -6,7 +6,7 @@ use crate::{
FixedHeightBar,
},
display::{Color, Font, Icon},
geometry::Insets,
geometry::{Insets, Offset},
},
};
@ -507,8 +507,8 @@ pub const TEXT_BOLD: TextStyle = TextStyle::new(Font::BOLD, FG, BG, GREY_LIGHT,
pub const TEXT_MONO: TextStyle = TextStyle::new(Font::MONO, FG, BG, GREY_LIGHT, GREY_LIGHT)
.with_line_breaking(LineBreaking::BreakWordsNoHyphen)
.with_page_breaking(PageBreaking::CutAndInsertEllipsisBoth)
.with_ellipsis_icon(Icon::new(ICON_PAGE_NEXT))
.with_prev_page_icon(Icon::new(ICON_PAGE_PREV));
.with_ellipsis_icon(Icon::new(ICON_PAGE_NEXT), 0)
.with_prev_page_icon(Icon::new(ICON_PAGE_PREV), 0);
/// Convert Python-side numeric id to a `TextStyle`.
pub fn textstyle_number(num: i32) -> &'static TextStyle {
@ -553,6 +553,11 @@ pub const RESULT_PADDING: i16 = 6;
pub const RESULT_FOOTER_START: i16 = 171;
pub const RESULT_FOOTER_HEIGHT: i16 = 62;
// checklist settings
pub const CHECKLIST_CHECK_WIDTH: i16 = 16;
pub const CHECKLIST_DONE_OFFSET: Offset = Offset::new(-2, 6);
pub const CHECKLIST_CURRENT_OFFSET: Offset = Offset::new(2, 3);
pub const fn button_bar<T>(inner: T) -> FixedHeightBar<T> {
FixedHeightBar::bottom(inner, BUTTON_HEIGHT)
}

Loading…
Cancel
Save