chore(core/rust): Try to simplify pagination a bit

[no changelog]
pull/2116/head
Jan Pochyla 3 years ago committed by matejcik
parent 8fb28e4af5
commit d979efc3ca

@ -13,7 +13,7 @@ pub use base::{Child, Component, ComponentExt, Event, EventCtx, Never, TimerToke
pub use empty::Empty; pub use empty::Empty;
pub use label::{Label, LabelStyle}; pub use label::{Label, LabelStyle};
pub use pad::Pad; pub use pad::Pad;
pub use paginated::{Paginate, Paginated, PaginatedMsg}; pub use paginated::{PageMsg, Paginate};
pub use text::{ pub use text::{
formatted::FormattedText, formatted::FormattedText,
layout::{LineBreaking, PageBreaking, TextLayout}, layout::{LineBreaking, PageBreaking, TextLayout},

@ -1,113 +1,18 @@
use crate::ui::{ use crate::ui::component::{
component::{ text::layout::{LayoutFit, TextNoOp},
text::layout::{LayoutFit, TextNoOp}, FormattedText,
Component, ComponentExt, Event, EventCtx, FormattedText, Pad,
},
display::Color,
geometry::Rect,
}; };
/// Implementations of `Page` wrap the component being paged. They also contain /// Common message type for pagination components.
/// model-dependent logic like:
///
/// * rendering scrollbar
/// * detecting swipe on TT
/// * buttons for changing pages on T1
/// * fading backlight
pub trait Page {
type Content;
fn new(area: Rect, page: Self::Content, page_count: usize, active_page: usize) -> Self;
fn inner_mut(&mut self) -> &mut Self::Content;
fn page_count(&self) -> usize;
fn active_page(&self) -> usize;
fn fade_after_next_paint(&mut self);
fn content_area(area: Rect) -> Rect;
}
/// Implementation of `Page` is a `Component` returning this message.
pub enum PageMsg<T, U> { pub enum PageMsg<T, U> {
/// Pass-through from paged component. /// Pass-through from paged component.
Content(T), Content(T),
/// Pass-through from other `Component`s.
Controls(U),
/// Page change requested.
ChangePage(usize),
}
/// Handles page redraw on `ChangePage` message, and other model-agnostic logic.
pub struct Paginated<P> {
page: P,
pad: Pad,
}
pub enum PaginatedMsg<T, U> {
/// Pass-through from the paged `Component`.
Content(T),
/// Messages from page controls outside the paged component. Currently only /// Messages from page controls outside the paged component. Currently only
/// used on T1 for "OK" and "Cancel" buttons. /// used on T1 for "OK" and "Cancel" buttons.
Controls(U), Controls(U),
} }
impl<P> Paginated<P>
where
P: Page,
P::Content: Paginate,
{
pub fn new(area: Rect, content: impl FnOnce(Rect) -> P::Content, background: Color) -> Self {
let active_page = 0;
let mut content = content(P::content_area(area));
let page_count = content.page_count();
Self {
page: P::new(area, content, page_count, active_page),
pad: Pad::with_background(area, background),
}
}
}
// C is type of message returned by page controls.
impl<P, C> Component for Paginated<P>
where
P: Page,
P: Component<Msg = PageMsg<<<P as Page>::Content as Component>::Msg, C>>,
P::Content: Paginate,
P::Content: Component,
{
type Msg = PaginatedMsg<<<P as Page>::Content as Component>::Msg, C>;
fn event(&mut self, ctx: &mut EventCtx, event: Event) -> Option<Self::Msg> {
self.page.event(ctx, event).and_then(|msg| match msg {
PageMsg::Content(c) => Some(PaginatedMsg::Content(c)),
PageMsg::Controls(c) => Some(PaginatedMsg::Controls(c)),
PageMsg::ChangePage(page) => {
self.page.fade_after_next_paint();
self.page.inner_mut().change_page(page);
self.page.inner_mut().request_complete_repaint(ctx);
self.pad.clear();
None
}
})
}
fn paint(&mut self) {
self.pad.paint();
self.page.paint();
}
}
#[cfg(feature = "ui_debug")]
impl<P> crate::trace::Trace for Paginated<P>
where
P: Page + crate::trace::Trace,
P::Content: crate::trace::Trace,
{
fn trace(&self, t: &mut dyn crate::trace::Tracer) {
self.page.trace(t);
}
}
pub trait Paginate { pub trait Paginate {
fn page_count(&mut self) -> usize; fn page_count(&mut self) -> usize;
fn change_page(&mut self, active_page: usize); fn change_page(&mut self, active_page: usize);

@ -38,7 +38,7 @@ where
} }
pub fn add<D: DefaultTextTheme>(mut self, text_font: Font, content: T) -> Self { pub fn add<D: DefaultTextTheme>(mut self, text_font: Font, content: T) -> Self {
if content.as_ref().len() == 0 { if content.as_ref().is_empty() {
return self; return self;
} }
let paragraph = Paragraph::new( let paragraph = Paragraph::new(

@ -1,39 +1,35 @@
use crate::ui::{ use crate::ui::{
component::{ component::{Component, ComponentExt, Event, EventCtx, Never, Pad, PageMsg, Paginate},
paginated::{Page, PageMsg}, display::{self, Color},
Component, Event, EventCtx, Never,
},
display,
geometry::{Offset, Point, Rect}, geometry::{Offset, Point, Rect},
}; };
use super::{theme, Button, ButtonMsg, ButtonPos}; use super::{theme, Button, ButtonMsg, ButtonPos};
pub struct ButtonPage<T> { pub struct ButtonPage<T> {
content: T,
scrollbar: ScrollBar, scrollbar: ScrollBar,
pad: Pad,
prev: Button<&'static str>, prev: Button<&'static str>,
next: Button<&'static str>, next: Button<&'static str>,
cancel: Button<&'static str>, cancel: Button<&'static str>,
confirm: Button<&'static str>, confirm: Button<&'static str>,
page: T,
} }
impl<T> ButtonPage<T> { impl<T> ButtonPage<T>
fn areas(area: Rect) -> (Rect, Rect, Rect) { where
let button_height = theme::FONT_BOLD.line_height() + 2; T: Paginate,
let (content_area, button_area) = area.hsplit(-button_height); T: Component,
let (content_area, scrollbar_area) = content_area.vsplit(-ScrollBar::WIDTH); {
let (content_area, _) = content_area.hsplit(-1); pub fn new(area: Rect, content: impl FnOnce(Rect) -> T, background: Color) -> Self {
(content_area, scrollbar_area, button_area) let (content_area, scrollbar_area, button_area) = Self::areas(area);
} let mut content = content(content_area);
} let pad = Pad::with_background(area, background);
impl<T> Page for ButtonPage<T> { // Always start at the first page.
type Content = T; let scrollbar = ScrollBar::vertical_right(scrollbar_area, content.page_count(), 0);
fn new(area: Rect, page: T, page_count: usize, active_page: usize) -> Self { // Create the button controls.
let (_content_area, scrollbar_area, button_area) = Self::areas(area);
let scrollbar = ScrollBar::vertical_right(scrollbar_area, page_count, active_page);
let prev = Button::with_text(button_area, ButtonPos::Left, "BACK", theme::button_cancel()); let prev = Button::with_text(button_area, ButtonPos::Left, "BACK", theme::button_cancel());
let next = Button::with_text( let next = Button::with_text(
button_area, button_area,
@ -53,36 +49,40 @@ impl<T> Page for ButtonPage<T> {
"CONFIRM", "CONFIRM",
theme::button_default(), theme::button_default(),
); );
Self { Self {
content,
scrollbar, scrollbar,
pad,
prev, prev,
next, next,
cancel, cancel,
confirm, confirm,
page,
} }
} }
fn inner_mut(&mut self) -> &mut T { fn areas(area: Rect) -> (Rect, Rect, Rect) {
&mut self.page let button_height = theme::FONT_BOLD.line_height() + 2;
} let (content_area, button_area) = area.hsplit(-button_height);
let (content_area, scrollbar_area) = content_area.vsplit(-ScrollBar::WIDTH);
fn page_count(&self) -> usize { let (content_area, _) = content_area.hsplit(-1);
self.scrollbar.page_count (content_area, scrollbar_area, button_area)
}
fn active_page(&self) -> usize {
self.scrollbar.active_page
} }
fn fade_after_next_paint(&mut self) {} fn change_page(&mut self, ctx: &mut EventCtx, page: usize) {
// Change the page in the content, clear the background under it and make sure
fn content_area(area: Rect) -> Rect { // it gets completely repainted.
Self::areas(area).0 self.content.change_page(page);
self.content.request_complete_repaint(ctx);
self.pad.clear();
} }
} }
impl<T: Component> Component for ButtonPage<T> { impl<T> Component for ButtonPage<T>
where
T: Component,
T: Paginate,
{
type Msg = PageMsg<T::Msg, bool>; type Msg = PageMsg<T::Msg, bool>;
fn event(&mut self, ctx: &mut EventCtx, event: Event) -> Option<Self::Msg> { fn event(&mut self, ctx: &mut EventCtx, event: Event) -> Option<Self::Msg> {
@ -90,34 +90,33 @@ impl<T: Component> Component for ButtonPage<T> {
if let Some(ButtonMsg::Clicked) = self.prev.event(ctx, event) { if let Some(ButtonMsg::Clicked) = self.prev.event(ctx, event) {
// Scroll up. // Scroll up.
self.scrollbar.go_to_previous_page(); self.scrollbar.go_to_previous_page();
return Some(PageMsg::ChangePage(self.active_page())); self.change_page(ctx, self.scrollbar.active_page);
} return None;
} else {
if let Some(ButtonMsg::Clicked) = self.cancel.event(ctx, event) {
return Some(PageMsg::Controls(false));
} }
} else if let Some(ButtonMsg::Clicked) = self.cancel.event(ctx, event) {
return Some(PageMsg::Controls(false));
} }
if self.scrollbar.has_next_page() { if self.scrollbar.has_next_page() {
if let Some(ButtonMsg::Clicked) = self.next.event(ctx, event) { if let Some(ButtonMsg::Clicked) = self.next.event(ctx, event) {
// Scroll down. // Scroll down.
self.scrollbar.go_to_next_page(); self.scrollbar.go_to_next_page();
return Some(PageMsg::ChangePage(self.active_page())); self.change_page(ctx, self.scrollbar.active_page);
} return None;
} else {
if let Some(ButtonMsg::Clicked) = self.confirm.event(ctx, event) {
return Some(PageMsg::Controls(true));
} }
} else if let Some(ButtonMsg::Clicked) = self.confirm.event(ctx, event) {
return Some(PageMsg::Controls(true));
} }
if let Some(msg) = self.page.event(ctx, event) { if let Some(msg) = self.content.event(ctx, event) {
return Some(PageMsg::Content(msg)); return Some(PageMsg::Content(msg));
} }
None None
} }
fn paint(&mut self) { fn paint(&mut self) {
self.page.paint(); self.pad.paint();
self.content.paint();
self.scrollbar.paint(); self.scrollbar.paint();
if self.scrollbar.has_previous_page() { if self.scrollbar.has_previous_page() {
self.prev.paint(); self.prev.paint();
@ -139,9 +138,9 @@ where
{ {
fn trace(&self, t: &mut dyn crate::trace::Tracer) { fn trace(&self, t: &mut dyn crate::trace::Tracer) {
t.open("ButtonPage"); t.open("ButtonPage");
t.field("active_page", &self.active_page()); t.field("active_page", &self.scrollbar.active_page);
t.field("page_count", &self.page_count()); t.field("page_count", &self.scrollbar.page_count);
t.field("content", &self.page); t.field("content", &self.content);
t.close(); t.close();
} }
} }

@ -4,7 +4,7 @@ use crate::{
error::Error, error::Error,
micropython::{buffer::Buffer, map::Map, obj::Obj, qstr::Qstr}, micropython::{buffer::Buffer, map::Map, obj::Obj, qstr::Qstr},
ui::{ ui::{
component::{text::paragraphs::Paragraphs, Child, FormattedText, Paginated, PaginatedMsg}, component::{text::paragraphs::Paragraphs, Child, FormattedText, PageMsg},
display, display,
layout::obj::LayoutObj, layout::obj::LayoutObj,
}, },
@ -16,13 +16,13 @@ use super::{
theme, theme,
}; };
impl<T> TryFrom<PaginatedMsg<T, bool>> for Obj { impl<T> TryFrom<PageMsg<T, bool>> for Obj {
type Error = Error; type Error = Error;
fn try_from(val: PaginatedMsg<T, bool>) -> Result<Self, Self::Error> { fn try_from(val: PageMsg<T, bool>) -> Result<Self, Self::Error> {
match val { match val {
PaginatedMsg::Content(_) => 2.try_into(), PageMsg::Content(_) => 2.try_into(),
PaginatedMsg::Controls(c) => Ok(c.into()), PageMsg::Controls(c) => Ok(c.into()),
} }
} }
} }
@ -57,7 +57,7 @@ extern "C" fn ui_layout_new_confirm_action(
.map(|label| |area, pos| Button::with_text(area, pos, label, theme::button_default())); .map(|label| |area, pos| Button::with_text(area, pos, label, theme::button_default()));
let obj = LayoutObj::new(Child::new(Title::new(display::screen(), title, |area| { let obj = LayoutObj::new(Child::new(Title::new(display::screen(), title, |area| {
Paginated::<ButtonPage<_>>::new( ButtonPage::new(
area, area,
|area| { |area| {
FormattedText::new::<theme::T1DefaultText>(area, format) FormattedText::new::<theme::T1DefaultText>(area, format)
@ -85,7 +85,7 @@ extern "C" fn ui_layout_new_confirm_text(
kwargs.get(Qstr::MP_QSTR_description)?.try_into_option()?; kwargs.get(Qstr::MP_QSTR_description)?.try_into_option()?;
let obj = LayoutObj::new(Child::new(Title::new(display::screen(), title, |area| { let obj = LayoutObj::new(Child::new(Title::new(display::screen(), title, |area| {
Paginated::<ButtonPage<_>>::new( ButtonPage::new(
area, area,
|area| { |area| {
Paragraphs::new(area) Paragraphs::new(area)

@ -1,65 +1,70 @@
use crate::ui::{ use crate::ui::{
component::{ component::{Component, ComponentExt, Event, EventCtx, Never, Pad, PageMsg, Paginate},
paginated::{Page, PageMsg}, display::{self, Color},
Component, Event, EventCtx, Never,
},
display,
geometry::{Offset, Point, Rect}, geometry::{Offset, Point, Rect},
}; };
use super::{theme, Swipe, SwipeDirection}; use super::{theme, Swipe, SwipeDirection};
pub struct SwipePage<T> { pub struct SwipePage<T> {
content: T,
pad: Pad,
swipe: Swipe, swipe: Swipe,
scrollbar: ScrollBar, scrollbar: ScrollBar,
page: T,
fade: Option<i32>, fade: Option<i32>,
} }
impl<T> SwipePage<T> { impl<T> SwipePage<T>
fn setup_swipe(scrollbar: &ScrollBar, swipe: &mut Swipe) { where
swipe.allow_up = scrollbar.has_next_page(); T: Paginate,
swipe.allow_down = scrollbar.has_previous_page(); T: Component,
} {
} pub fn new(area: Rect, content: impl FnOnce(Rect) -> T, background: Color) -> Self {
// Content occupies the whole area.
let mut content = content(area);
impl<T> Page for SwipePage<T> { // Always start at the first page.
type Content = T; let scrollbar = ScrollBar::vertical_right(area, content.page_count(), 0);
fn new(area: Rect, page: T, page_count: usize, active_page: usize) -> Self { let swipe = Self::make_swipe(area, &scrollbar);
let scrollbar = ScrollBar::vertical_right(area, page_count, active_page); let pad = Pad::with_background(area, background);
let mut swipe = Swipe::new(area);
Self::setup_swipe(&scrollbar, &mut swipe);
Self { Self {
swipe, content,
scrollbar, scrollbar,
page, swipe,
pad,
fade: None, fade: None,
} }
} }
fn inner_mut(&mut self) -> &mut T { fn make_swipe(area: Rect, scrollbar: &ScrollBar) -> Swipe {
&mut self.page let mut swipe = Swipe::new(area);
swipe.allow_up = scrollbar.has_next_page();
swipe.allow_down = scrollbar.has_previous_page();
swipe
} }
fn page_count(&self) -> usize { fn change_page(&mut self, ctx: &mut EventCtx, page: usize) {
self.scrollbar.page_count // Adjust the swipe parameters.
} self.swipe = Self::make_swipe(self.swipe.area, &self.scrollbar);
fn active_page(&self) -> usize { // Change the page in the content, make sure it gets completely repainted and
self.scrollbar.active_page // clear the background under it.
} self.content.change_page(page);
self.content.request_complete_repaint(ctx);
self.pad.clear();
fn fade_after_next_paint(&mut self) { // Swipe has dimmed the screen, so fade back to normal backlight after the next
// paint.
self.fade = Some(theme::BACKLIGHT_NORMAL); self.fade = Some(theme::BACKLIGHT_NORMAL);
} }
fn content_area(area: Rect) -> Rect {
area
}
} }
impl<T: Component> Component for SwipePage<T> { impl<T> Component for SwipePage<T>
where
T: Paginate,
T: Component,
{
type Msg = PageMsg<T::Msg, Never>; type Msg = PageMsg<T::Msg, Never>;
fn event(&mut self, ctx: &mut EventCtx, event: Event) -> Option<Self::Msg> { fn event(&mut self, ctx: &mut EventCtx, event: Event) -> Option<Self::Msg> {
@ -68,30 +73,32 @@ impl<T: Component> Component for SwipePage<T> {
SwipeDirection::Up => { SwipeDirection::Up => {
// Scroll down, if possible. // Scroll down, if possible.
self.scrollbar.go_to_next_page(); self.scrollbar.go_to_next_page();
Self::setup_swipe(&self.scrollbar, &mut self.swipe); self.change_page(ctx, self.scrollbar.active_page);
return Some(PageMsg::ChangePage(self.active_page())); return None;
} }
SwipeDirection::Down => { SwipeDirection::Down => {
// Scroll up, if possible. // Scroll up, if possible.
self.scrollbar.go_to_previous_page(); self.scrollbar.go_to_previous_page();
Self::setup_swipe(&self.scrollbar, &mut self.swipe); self.change_page(ctx, self.scrollbar.active_page);
return Some(PageMsg::ChangePage(self.active_page())); return None;
} }
_ => { _ => {
// Ignore other directions. // Ignore other directions.
} }
} }
} }
if let Some(msg) = self.page.event(ctx, event) { if let Some(msg) = self.content.event(ctx, event) {
return Some(PageMsg::Content(msg)); return Some(PageMsg::Content(msg));
} }
None None
} }
fn paint(&mut self) { fn paint(&mut self) {
self.page.paint(); self.pad.paint();
self.content.paint();
self.scrollbar.paint(); self.scrollbar.paint();
if let Some(val) = self.fade.take() { if let Some(val) = self.fade.take() {
// Note that this is blocking and takes some time.
display::fade_backlight(val); display::fade_backlight(val);
} }
} }
@ -104,9 +111,9 @@ where
{ {
fn trace(&self, t: &mut dyn crate::trace::Tracer) { fn trace(&self, t: &mut dyn crate::trace::Tracer) {
t.open("SwipePage"); t.open("SwipePage");
t.field("active_page", &self.active_page()); t.field("active_page", &self.scrollbar.active_page);
t.field("page_count", &self.page_count()); t.field("page_count", &self.scrollbar.page_count);
t.field("content", &self.page); t.field("content", &self.content);
t.close(); t.close();
} }
} }
@ -138,11 +145,15 @@ impl ScrollBar {
} }
pub fn go_to_next_page(&mut self) { pub fn go_to_next_page(&mut self) {
self.active_page = self.active_page.saturating_add(1).min(self.page_count - 1); self.go_to(self.active_page.saturating_add(1).min(self.page_count - 1));
} }
pub fn go_to_previous_page(&mut self) { pub fn go_to_previous_page(&mut self) {
self.active_page = self.active_page.saturating_sub(1); self.go_to(self.active_page.saturating_sub(1));
}
pub fn go_to(&mut self, active_page: usize) {
self.active_page = active_page;
} }
} }

@ -14,7 +14,7 @@ pub enum SwipeDirection {
} }
pub struct Swipe { pub struct Swipe {
area: Rect, pub area: Rect,
pub allow_up: bool, pub allow_up: bool,
pub allow_down: bool, pub allow_down: bool,
pub allow_left: bool, pub allow_left: bool,

Loading…
Cancel
Save