|
|
|
@ -1,35 +1,46 @@
|
|
|
|
|
use crate::ui::{
|
|
|
|
|
component::{Component, ComponentExt, Event, EventCtx, Never, Pad, PageMsg, Paginate},
|
|
|
|
|
component::{
|
|
|
|
|
base::ComponentExt, paginated::PageMsg, Component, Event, EventCtx, Never, Pad, Paginate,
|
|
|
|
|
},
|
|
|
|
|
display::{self, Color},
|
|
|
|
|
geometry::{Offset, Point, Rect},
|
|
|
|
|
geometry::{Dimensions, Offset, Point, Rect},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use super::{theme, Swipe, SwipeDirection};
|
|
|
|
|
use super::{theme, Button, Swipe, SwipeDirection};
|
|
|
|
|
|
|
|
|
|
pub struct SwipePage<T> {
|
|
|
|
|
pub struct SwipePage<T, U> {
|
|
|
|
|
content: T,
|
|
|
|
|
buttons: U,
|
|
|
|
|
pad: Pad,
|
|
|
|
|
swipe: Swipe,
|
|
|
|
|
scrollbar: ScrollBar,
|
|
|
|
|
fade: Option<i32>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> SwipePage<T>
|
|
|
|
|
impl<T, U> SwipePage<T, U>
|
|
|
|
|
where
|
|
|
|
|
T: Paginate,
|
|
|
|
|
T: Component,
|
|
|
|
|
T: Dimensions,
|
|
|
|
|
U: Component,
|
|
|
|
|
{
|
|
|
|
|
pub fn new(area: Rect, content: impl FnOnce(Rect) -> T, background: Color) -> Self {
|
|
|
|
|
// Content occupies the whole area.
|
|
|
|
|
let mut content = content(area);
|
|
|
|
|
pub fn new(
|
|
|
|
|
area: Rect,
|
|
|
|
|
background: Color,
|
|
|
|
|
content: impl FnOnce(Rect) -> T,
|
|
|
|
|
controls: impl FnOnce(Rect) -> U,
|
|
|
|
|
) -> Self {
|
|
|
|
|
let layout = PageLayout::new(area);
|
|
|
|
|
let mut content = Self::make_content(&layout, content);
|
|
|
|
|
|
|
|
|
|
// Always start at the first page.
|
|
|
|
|
let scrollbar = ScrollBar::vertical_right(area, content.page_count(), 0);
|
|
|
|
|
let scrollbar = ScrollBar::vertical_right(layout.scrollbar, content.page_count(), 0);
|
|
|
|
|
|
|
|
|
|
let swipe = Self::make_swipe(area, &scrollbar);
|
|
|
|
|
let pad = Pad::with_background(area, background);
|
|
|
|
|
Self {
|
|
|
|
|
content,
|
|
|
|
|
buttons: controls(layout.buttons),
|
|
|
|
|
scrollbar,
|
|
|
|
|
swipe,
|
|
|
|
|
pad,
|
|
|
|
@ -44,6 +55,16 @@ where
|
|
|
|
|
swipe
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn make_content(layout: &PageLayout, content: impl FnOnce(Rect) -> T) -> T {
|
|
|
|
|
// Check if content fits on single page.
|
|
|
|
|
let mut content = content(layout.content_single_page);
|
|
|
|
|
if content.page_count() > 1 {
|
|
|
|
|
// Reduce area to make space for scrollbar if it doesn't fit.
|
|
|
|
|
content.set_area(layout.content);
|
|
|
|
|
}
|
|
|
|
|
content
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn change_page(&mut self, ctx: &mut EventCtx, page: usize) {
|
|
|
|
|
// Adjust the swipe parameters.
|
|
|
|
|
self.swipe = Self::make_swipe(self.swipe.area, &self.scrollbar);
|
|
|
|
@ -58,14 +79,26 @@ where
|
|
|
|
|
// paint.
|
|
|
|
|
self.fade = Some(theme::BACKLIGHT_NORMAL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn paint_hint(&mut self) {
|
|
|
|
|
display::text_center(
|
|
|
|
|
Point::new(self.pad.area.center().x, self.pad.area.bottom_right().y - 3),
|
|
|
|
|
b"SWIPE TO CONTINUE",
|
|
|
|
|
theme::FONT_BOLD, // FIXME: Figma has this as 14px but bold is 16px
|
|
|
|
|
theme::GREY_LIGHT,
|
|
|
|
|
theme::BG,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> Component for SwipePage<T>
|
|
|
|
|
impl<T, U> Component for SwipePage<T, U>
|
|
|
|
|
where
|
|
|
|
|
T: Paginate,
|
|
|
|
|
T: Component,
|
|
|
|
|
T: Dimensions,
|
|
|
|
|
U: Component,
|
|
|
|
|
{
|
|
|
|
|
type Msg = PageMsg<T::Msg, Never>;
|
|
|
|
|
type Msg = PageMsg<T::Msg, U::Msg>;
|
|
|
|
|
|
|
|
|
|
fn event(&mut self, ctx: &mut EventCtx, event: Event) -> Option<Self::Msg> {
|
|
|
|
|
if let Some(swipe) = self.swipe.event(ctx, event) {
|
|
|
|
@ -90,13 +123,25 @@ where
|
|
|
|
|
if let Some(msg) = self.content.event(ctx, event) {
|
|
|
|
|
return Some(PageMsg::Content(msg));
|
|
|
|
|
}
|
|
|
|
|
if !self.scrollbar.has_next_page() {
|
|
|
|
|
if let Some(msg) = self.buttons.event(ctx, event) {
|
|
|
|
|
return Some(PageMsg::Controls(msg));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn paint(&mut self) {
|
|
|
|
|
self.pad.paint();
|
|
|
|
|
self.content.paint();
|
|
|
|
|
self.scrollbar.paint();
|
|
|
|
|
if self.scrollbar.has_pages() {
|
|
|
|
|
self.scrollbar.paint();
|
|
|
|
|
}
|
|
|
|
|
if self.scrollbar.has_next_page() {
|
|
|
|
|
self.paint_hint();
|
|
|
|
|
} else {
|
|
|
|
|
self.buttons.paint();
|
|
|
|
|
}
|
|
|
|
|
if let Some(val) = self.fade.take() {
|
|
|
|
|
// Note that this is blocking and takes some time.
|
|
|
|
|
display::fade_backlight(val);
|
|
|
|
@ -105,9 +150,10 @@ where
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "ui_debug")]
|
|
|
|
|
impl<T> crate::trace::Trace for SwipePage<T>
|
|
|
|
|
impl<T, U> crate::trace::Trace for SwipePage<T, U>
|
|
|
|
|
where
|
|
|
|
|
T: crate::trace::Trace,
|
|
|
|
|
U: crate::trace::Trace,
|
|
|
|
|
{
|
|
|
|
|
fn trace(&self, t: &mut dyn crate::trace::Tracer) {
|
|
|
|
|
t.open("SwipePage");
|
|
|
|
@ -116,6 +162,15 @@ where
|
|
|
|
|
t.field("content", &self.content);
|
|
|
|
|
t.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn bounds(&self, sink: &dyn Fn(Rect)) {
|
|
|
|
|
sink(self.scrollbar.area);
|
|
|
|
|
sink(self.pad.area);
|
|
|
|
|
self.content.bounds(sink);
|
|
|
|
|
if !self.scrollbar.has_next_page() {
|
|
|
|
|
self.buttons.bounds(sink);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct ScrollBar {
|
|
|
|
@ -135,12 +190,16 @@ impl ScrollBar {
|
|
|
|
|
|
|
|
|
|
pub fn vertical_right(area: Rect, page_count: usize, active_page: usize) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
area: area.cut_from_right(Self::DOT_SIZE.x),
|
|
|
|
|
area,
|
|
|
|
|
page_count,
|
|
|
|
|
active_page,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn has_pages(&self) -> bool {
|
|
|
|
|
self.page_count > 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn has_next_page(&self) -> bool {
|
|
|
|
|
self.active_page < self.page_count - 1
|
|
|
|
|
}
|
|
|
|
@ -211,3 +270,33 @@ impl Component for ScrollBar {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct PageLayout {
|
|
|
|
|
pub content_single_page: Rect,
|
|
|
|
|
pub content: Rect,
|
|
|
|
|
pub scrollbar: Rect,
|
|
|
|
|
pub buttons: Rect,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PageLayout {
|
|
|
|
|
const BUTTON_SPACE: i32 = 6;
|
|
|
|
|
const SCROLLBAR_WIDTH: i32 = 10;
|
|
|
|
|
const SCROLLBAR_SPACE: i32 = 10;
|
|
|
|
|
|
|
|
|
|
pub fn new(area: Rect) -> Self {
|
|
|
|
|
let (content, buttons) = area.hsplit(-Button::HEIGHT);
|
|
|
|
|
let (content, _space) = content.hsplit(-Self::BUTTON_SPACE);
|
|
|
|
|
let (buttons, _space) = buttons.vsplit(-theme::CONTENT_BORDER);
|
|
|
|
|
let (_space, content) = content.vsplit(theme::CONTENT_BORDER);
|
|
|
|
|
let (content_single_page, _space) = content.vsplit(-theme::CONTENT_BORDER);
|
|
|
|
|
let (content, scrollbar) = content.vsplit(-(Self::SCROLLBAR_SPACE + Self::SCROLLBAR_WIDTH));
|
|
|
|
|
let (_space, scrollbar) = scrollbar.vsplit(Self::SCROLLBAR_SPACE);
|
|
|
|
|
|
|
|
|
|
Self {
|
|
|
|
|
content_single_page,
|
|
|
|
|
content,
|
|
|
|
|
scrollbar,
|
|
|
|
|
buttons,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|