use std::{
sync::{
Arc,
atomic::{AtomicBool, Ordering},
},
time::Duration,
};
use crate::{
animation::{Easing, Transition},
app::{
component::Component,
ctx::{Ctx, Interval},
window::Haptic,
},
components::{Row, Stack},
core::Signal,
layout::layout_kind::{Justify, ScrollState},
node::{Element, color::Color, dimension::Dimension, transform::Transform2D},
ui::{Recipe, Themed, engine_look::EngineLook, names},
};
const FILL: Dimension = Dimension::Pct(100.0);
const SPOKES: usize = 8;
const STEP: Duration = Duration::from_millis(125);
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ActivityIndicatorProps {
pub size: f32,
pub color: Color,
pub animating: bool,
pub shown: f32,
}
impl Default for ActivityIndicatorProps {
fn default() -> Self {
Self {
size: 20.0,
color: spinner_color(),
animating: true,
shown: 1.0,
}
}
}
impl ActivityIndicatorProps {
pub fn new() -> Self {
Self::default()
}
pub fn size(mut self, size: f32) -> Self {
self.size = size;
self
}
pub fn color(mut self, color: Color) -> Self {
self.color = color;
self
}
pub fn animating(mut self, animating: bool) -> Self {
self.animating = animating;
self
}
}
#[cfg(feature = "devtools")]
impl crate::app::component::DevtoolsInspectable for ActivityIndicatorProps {
fn write_info(&self, _buffer: &mut Vec<crate::app::component::ComponentInfo>) {}
}
#[deprecated(
since = "0.63.0",
note = "use `lurq::ui::Spinner`: the design's spinner, in the color of the text around it, with a size and a label"
)]
pub struct ActivityIndicator {
step: Signal<usize>,
turns: Interval,
turning: AtomicBool,
}
#[allow(deprecated)]
impl ActivityIndicator {
pub fn mount(ctx: &mut Ctx, props: ActivityIndicatorProps) -> Element {
ctx.mount::<Self>(props)
}
}
#[allow(deprecated)]
impl Component for ActivityIndicator {
type Props = ActivityIndicatorProps;
fn create(ctx: &mut Ctx) -> Self {
let step = Signal::new(0usize);
let turns = ctx.create_interval(STEP, {
let step = step.clone();
move || step.update(|step| *step = (*step + 1) % SPOKES)
});
turns.stop();
Self {
step,
turns,
turning: AtomicBool::new(false),
}
}
fn render(&self, ctx: &mut Ctx) -> impl Into<Element> {
let props = *ctx.props::<ActivityIndicatorProps>();
if self.turning.swap(props.animating, Ordering::AcqRel) != props.animating {
if props.animating {
self.turns.start();
} else {
self.turns.stop();
}
}
let step = if props.animating { self.step.get() } else { 0 };
let (width, length) = (props.size * 0.1, props.size * 0.28);
let reach = props.size / 2.0 - length / 2.0;
let there = (props.shown.clamp(0.0, 1.0) * SPOKES as f32).round() as usize;
let mut spokes = Stack::new().width(props.size).height(props.size);
for spoke in 0..SPOKES {
let angle = spoke as f32 * std::f32::consts::TAU / SPOKES as f32;
let behind = (step + SPOKES - spoke) % SPOKES;
let opacity = if props.animating {
1.0 - behind as f32 * 0.1
} else if spoke < there {
0.7
} else {
0.0
};
spokes = spokes.child(
Row::new()
.width(width)
.height(length)
.rounded(width / 2.0)
.background(props.color)
.opacity(opacity)
.transform(
Transform2D::translate(
props.size / 2.0 - width / 2.0 + reach * angle.sin(),
props.size / 2.0 - length / 2.0 - reach * angle.cos(),
)
.then(&Transform2D::rotate(angle)),
),
);
}
spokes
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PullToRefreshStyle {
pub threshold: f32,
pub hold: f32,
pub spinner_size: f32,
pub spinner_color: Color,
pub duration: Duration,
pub haptic: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PullToRefreshPart {
Spinner,
}
names!(PullToRefreshPart { Spinner => "spinner" });
const LEGACY_SPINNER: Color = Color::new(60, 60, 67, 153);
fn spinner_color() -> Color {
EngineLook::of::<PullToRefreshStyle>(&[]).foreground(PullToRefreshPart::Spinner, LEGACY_SPINNER)
}
impl Themed for PullToRefreshStyle {
const NAME: &'static str = "phone_pull_to_refresh";
type Renderer = ();
fn recipe() -> Recipe {
Recipe::new().base(PullToRefreshPart::Spinner, |s| {
s.foreground(crate::app::theme::PaletteColor::MutedForeground)
})
}
}
impl Default for PullToRefreshStyle {
fn default() -> Self {
Self {
threshold: 100.0,
hold: 60.0,
spinner_size: 22.0,
spinner_color: spinner_color(),
duration: Duration::from_millis(300),
haptic: true,
}
}
}
impl PullToRefreshStyle {
pub fn new() -> Self {
Self::default()
}
pub(crate) fn legacy_recipe() -> Recipe {
Recipe::new().base(PullToRefreshPart::Spinner, |s| s.foreground(LEGACY_SPINNER))
}
#[deprecated(
since = "0.63.0",
note = "the theme's appearance gives the dark color: `PullToRefreshStyle::new()` built in a dark theme is dark"
)]
pub fn dark() -> Self {
Self {
spinner_color: Color::new(235, 235, 245, 153),
..Self::default()
}
}
}
#[derive(Clone)]
pub struct PullToRefreshProps {
refreshing: Signal<bool>,
scroll: ScrollState,
on_refresh: Option<Arc<dyn Fn() + Send + Sync>>,
top: f32,
style: PullToRefreshStyle,
}
impl PartialEq for PullToRefreshProps {
fn eq(&self, other: &Self) -> bool {
self.refreshing.id() == other.refreshing.id()
&& self.scroll.same(&other.scroll)
&& self.top == other.top
&& self.style == other.style
&& match (&self.on_refresh, &other.on_refresh) {
(None, None) => true,
(Some(a), Some(b)) => Arc::ptr_eq(a, b),
_ => false,
}
}
}
#[cfg(feature = "devtools")]
impl crate::app::component::DevtoolsInspectable for PullToRefreshProps {
fn write_info(&self, _buffer: &mut Vec<crate::app::component::ComponentInfo>) {}
}
impl PullToRefreshProps {
pub fn new(refreshing: Signal<bool>, scroll: ScrollState) -> Self {
Self {
refreshing,
scroll,
on_refresh: None,
top: 0.0,
style: PullToRefreshStyle::default(),
}
}
pub fn on_refresh(mut self, on_refresh: impl Fn() + Send + Sync + 'static) -> Self {
self.on_refresh = Some(Arc::new(on_refresh));
self
}
pub fn top(mut self, top: f32) -> Self {
self.top = top;
self
}
pub fn style(mut self, style: PullToRefreshStyle) -> Self {
self.style = style;
self
}
}
pub struct PullToRefresh {
pull: Signal<f32>,
armed: AtomicBool,
was_refreshing: AtomicBool,
seen: parking_lot::Mutex<Option<PullToRefreshProps>>,
}
impl PullToRefresh {
pub fn mount(ctx: &mut Ctx, props: PullToRefreshProps, list: impl Into<Element>) -> Element {
ctx.mount_with::<Self>(props, vec![list.into()])
}
}
impl Component for PullToRefresh {
type Props = PullToRefreshProps;
fn create(_ctx: &mut Ctx) -> Self {
Self {
pull: Signal::new(0.0),
armed: AtomicBool::new(true),
was_refreshing: AtomicBool::new(false),
seen: parking_lot::Mutex::new(None),
}
}
fn render(&self, ctx: &mut Ctx) -> impl Into<Element> {
let props = ctx.props::<PullToRefreshProps>().clone();
let style = props.style;
let refreshing = props.refreshing.get();
let pull = self.pull.get();
*self.seen.lock() = Some(props.clone());
if self.was_refreshing.swap(refreshing, Ordering::AcqRel) != refreshing && refreshing && style.haptic {
ctx.window().haptic(Haptic::Light);
}
let held = if refreshing { style.hold } else { 0.0 };
let room = pull.max(held);
let mut list = Stack::new()
.size(FILL, FILL)
.transform(Transform2D::translate(0.0, held))
.transition(Transition::transform().duration(style.duration).easing(Easing::EASE));
for child in ctx.children() {
list = list.child(child.clone());
}
#[allow(deprecated)]
let spinner = ActivityIndicator::mount(
ctx,
ActivityIndicatorProps {
size: style.spinner_size,
color: style.spinner_color,
animating: refreshing,
shown: (pull / style.threshold).clamp(0.0, 1.0),
},
);
Stack::new().size(FILL, FILL).clip().child(list).child(
Row::new()
.width(FILL)
.justify(Justify::Center)
.pointer_events_none()
.padding_top(props.top + (room - style.spinner_size).max(0.0) / 2.0)
.opacity(if refreshing || pull > 4.0 { 1.0 } else { 0.0 })
.child(spinner),
)
}
fn after_layout(&self) {
let props = self.seen.lock().clone();
if let Some(props) = props {
self.watch(&props);
}
}
}
impl PullToRefresh {
fn watch(&self, props: &PullToRefreshProps) {
let pull = (-props.scroll.overscroll().1).max(0.0);
if (self.pull.get_untracked() - pull).abs() > 0.5 {
self.pull.set(pull);
}
if pull < 4.0 {
self.armed.store(true, Ordering::Release);
}
if pull >= props.style.threshold && !props.refreshing.get_untracked() && self.armed.swap(false, Ordering::AcqRel) {
props.refreshing.set(true);
if let Some(on_refresh) = &props.on_refresh {
on_refresh();
}
}
}
}