use cranpose::prelude::*;
use super::{
frame_time::{Time, frame_time},
text_style::text_style as style,
};
const ROWS: usize = 40;
const CARD_ROWS: usize = 3;
const ROW_HEIGHT: f32 = 30.0;
const SCROLL_PIXELS_PER_MS: f32 = 0.06;
const SCROLL_PERIOD_MS: u64 = 10_000;
const NAMES: [&str; 8] = [
"Photos", "Audio", "Notes", "Maps", "Inbox", "Books", "Video", "Docs",
];
const INITIALS: [&str; 8] = ["P", "A", "N", "M", "I", "B", "V", "D"];
const BACKGROUND: Color = Color::from_rgb_u8(15, 18, 32);
const CARD: Color = Color::from_rgb_u8(27, 31, 51);
const TRACK: Color = Color::from_rgb_u8(42, 48, 80);
const LINE: Color = Color::from_rgb_u8(60, 68, 110);
const MUTED: Color = Color::from_rgb_u8(140, 147, 176);
const FILLS: [Color; 4] = [
Color::from_rgb_u8(67, 97, 238),
Color::from_rgb_u8(76, 201, 240),
Color::from_rgb_u8(247, 37, 133),
Color::from_rgb_u8(78, 205, 196),
];
fn level(t: u64, period: u64, phase: u64) -> f32 {
let at = (t + phase) % period;
let half = period / 2;
let rise = if at < half { at } else { period - at };
rise as f32 / half as f32
}
fn live_bar(index: usize, time: Time) -> Modifier {
let fill = FILLS[index % FILLS.len()];
let period = 1_200 + 97 * index as u64;
let phase = 131 * index as u64;
Modifier::empty()
.fill_max_width()
.height(2.0)
.background(TRACK)
.draw_behind(move |scope| {
let size = scope.size();
scope.draw_rect_at(
Rect {
x: 0.0,
y: 0.0,
width: size.width * level(time.get(), period, phase),
height: size.height,
},
Brush::solid(fill),
);
})
}
#[composable]
fn PathRow(index: usize, time: Time) {
let kind = index % NAMES.len();
Row(
Modifier::empty()
.fill_max_width()
.height(ROW_HEIGHT)
.padding_vertical(1.0)
.background(CARD)
.rounded_corners(6.0)
.padding_horizontal(6.0),
RowSpec::default()
.vertical_alignment(VerticalAlignment::CenterVertically)
.horizontal_arrangement(LinearArrangement::spaced_by(6.0)),
move || {
Box(
Modifier::empty()
.size(Size::new(16.0, 16.0))
.background(FILLS[index % FILLS.len()])
.rounded_corners(8.0),
BoxSpec::default().content_alignment(Alignment::CENTER),
move || {
Text(INITIALS[kind], Modifier::empty(), style(Color::WHITE, 9.0));
},
);
Column(
Modifier::empty().weight(1.0),
ColumnSpec::default(),
move || {
Text(NAMES[kind], Modifier::empty(), style(MUTED, 10.0));
Box(
Modifier::empty()
.fill_max_width()
.background(LINE)
.rounded_corners(4.0)
.padding(1.0),
BoxSpec::default(),
move || {
Box(
Modifier::empty()
.fill_max_width()
.background(BACKGROUND)
.rounded_corners(3.0)
.padding(2.0),
BoxSpec::default(),
move || {
Box(live_bar(index, time), BoxSpec::default(), || {});
},
);
},
);
},
);
},
);
}
#[composable]
pub fn ChangingPaths(title: &'static str) {
let list = rememberLazyListState();
let time = frame_time(move |t| {
let scroll = (t % SCROLL_PERIOD_MS) as f32 * SCROLL_PIXELS_PER_MS;
list.scroll_to_item((scroll / ROW_HEIGHT) as usize, scroll % ROW_HEIGHT);
});
Column(
Modifier::empty()
.fill_max_size()
.background(BACKGROUND)
.padding(8.0),
ColumnSpec::default().vertical_arrangement(LinearArrangement::spaced_by(6.0)),
move || {
Text(title, Modifier::empty(), style(Color::WHITE, 14.0));
Column(
Modifier::empty()
.fill_max_width()
.graphics_layer_block(move |layer| {
layer.alpha = 0.4 + 0.6 * level(time.get(), 2_000, 0);
})
.background(LINE)
.rounded_corners(8.0)
.padding(4.0),
ColumnSpec::default(),
move || {
for index in ROWS..ROWS + CARD_ROWS {
PathRow(index, time);
}
},
);
LazyColumn(
Modifier::empty()
.fill_max_width()
.weight(1.0)
.clip_to_bounds(),
list,
LazyColumnSpec::default(),
move |scope| {
scope.items(LazyItems::new(ROWS), move |index| PathRow(index, time));
},
);
},
);
}