use gpui::{App, EntityId, IntoElement, ParentElement, SharedString, Styled, div, px};
use bezel_motion as motion;
use bezel_motion::{GRADIENT_SPIN, PULSE, PULSE_STAGGER};
use bezel_theme::Theme;
pub use bezel_motion::phase::{GSPIN_DIM, GSPIN_ROW_TINTS, MATRIX_SIDE, PULSE_CELLS};
pub fn pulse_loader(
_id: &'static str,
theme: &Theme,
cell_px: f32,
view: EntityId,
cx: &mut App,
) -> impl IntoElement {
let color = theme.text;
let slot = cell_px;
let delta = motion::pulse_delta(&PULSE, view, cx);
div()
.flex()
.flex_row()
.items_center()
.gap(px(slot / 2.0))
.children((0..PULSE_CELLS).map(move |i| {
div()
.size(px(slot))
.flex()
.items_center()
.justify_center()
.child({
let phase = motion::staggered_phase(delta, i, PULSE_STAGGER);
div()
.rounded(px(slot / 4.0))
.bg(color)
.opacity(motion::pulse_opacity(phase))
.size(px(slot * motion::pulse_scale(phase)))
})
}))
}
pub fn gradient_spinner(
_id: &'static str,
_theme: &Theme,
cell_px: f32,
view: EntityId,
cx: &mut App,
) -> impl IntoElement {
let center = (MATRIX_SIDE as f32 - 1.0) / 2.0;
let max = MATRIX_SIDE as f32 - 1.0 + center;
let delta = motion::pulse_delta(&GRADIENT_SPIN, view, cx);
div()
.flex()
.flex_col()
.gap(px(cell_px / 2.0))
.children((0..MATRIX_SIDE).map(move |row| {
let tint: gpui::Hsla = gpui::rgb(GSPIN_ROW_TINTS[row]).into();
div()
.flex()
.flex_row()
.gap(px(cell_px / 2.0))
.children((0..MATRIX_SIDE).map(move |col| {
let d = MATRIX_SIDE as f32 - 1.0 - row as f32 + (col as f32 - center).abs();
let phase = if max == 0.0 { 0.0 } else { d / (max + 1.0) };
div()
.size(px(cell_px))
.rounded(px(cell_px / 2.0))
.bg(tint)
.opacity(motion::gspin_opacity(delta + phase, GSPIN_DIM))
}))
}))
}
pub fn mini_gradient_spinner(
key: impl Into<SharedString>,
cell_px: f32,
view: EntityId,
cx: &mut App,
) -> impl IntoElement {
const COLS: usize = 2;
const ROWS: usize = 3;
const RING: [[usize; COLS]; ROWS] = [[0, 1], [5, 2], [4, 3]];
const RING_LEN: f32 = (COLS * ROWS) as f32;
let _key = key.into();
let delta = motion::pulse_delta(&GRADIENT_SPIN, view, cx);
div()
.flex()
.flex_col()
.gap(px(cell_px / 2.0))
.children((0..ROWS).map(move |row| {
let tint: gpui::Hsla = gpui::rgb(GSPIN_ROW_TINTS[row]).into();
div()
.flex()
.flex_row()
.gap(px(cell_px / 2.0))
.children((0..COLS).map(move |col| {
let phase = RING[row][col] as f32 / RING_LEN;
div()
.size(px(cell_px))
.rounded(px(cell_px / 2.0))
.bg(tint)
.opacity(motion::gspin_opacity(delta + phase, GSPIN_DIM))
}))
}))
}
pub fn loading_word(theme: &Theme) -> impl IntoElement {
div()
.text_size(px(11.0))
.text_color(theme.text_muted.opacity(0.7))
.child(SharedString::from(
"L\u{2009}O\u{2009}A\u{2009}D\u{2009}I\u{2009}N\u{2009}G",
))
}
const _: () = {
assert!(PULSE.duration_ms == 2400);
assert!(GRADIENT_SPIN.duration_ms == 750);
};