1// Spinners and progress bars (decisions/F11.md, F11e, and F12.md,2// F12c): signs of work under way, painted again each frame while they3// move (`Widget::settling`).45import std::string;6import std::vector;7import std::option;8import std::math;9import std::time;10import pkg::alloy::window;11import pkg::alloy::draw;1213import pkg::alloy::ui;14import pkg::alloy::ui::store;15import pkg::alloy::ui::tree;16import pkg::alloy::ui::layout;17import pkg::alloy::ui::painting;18import pkg::alloy::ui::events;19import pkg::alloy::ui::widgets;20import pkg::alloy::ui::fields;21import pkg::alloy::ui::scrolling;22import pkg::alloy::ui::running;23import pkg::alloy::ui::timers;24import pkg::alloy::ui::layers;25import pkg::alloy::ui::motion;26import pkg::alloy::ui::transitions;27import pkg::alloy::ui::languages;28import pkg::alloy::ui::extensions;29import pkg::alloy::ui::views;30import ui_controls;3132/* --- Spinners --- */3334const SPINNER_DOTS: u64 = 8;3536pub fn layout(self s: &var Spinner, arrange: &var Arrange, limits: Limits) -> Extent {37 return limits.constrain(Extent { .width = s.size, .height = s.size });38}3940// The dots round a circle, the brightest going round once a second and41// those behind it fading; under reduced motion all pulsing together42// every two seconds.43exp fn paint(self s: &Spinner, look: &Look, paint: &var Paint) {44 const color = if (s.color.color(&look.theme) is ::Some |c|) c else look.theme.primary;45 const size = min(look.size.width, look.size.height);46 const radius = size / 2.0;47 const dot = size * 0.1;48 const ms = look.since.nanoseconds / 1000000;49 const head = (ms % 1000) to f32 / 1000.0 * SPINNER_DOTS to f32;50 const pulse = 0.5 - 0.5 * cos(PI * 2.0 * (ms % 2000) to f32 / 2000.0);51 for ([..SPINNER_DOTS]) |k| {52 const angle = PI * 2.0 * k to f32 / SPINNER_DOTS to f32 - PI / 2.0;53 const x = radius + (radius - dot) * cos(angle);54 const y = radius + (radius - dot) * sin(angle);55 var behind = head - k to f32;56 if (behind < 0.0) {57 behind += SPINNER_DOTS to f32;58 }59 var ink = color;60 ink.a = ink.a * if (look.reduced) 0.3 + 0.6 * pulse else max(61 0.15,62 1.0 - behind / SPINNER_DOTS to f3263 );64 paint.rounded_rect(65 Rect::at(x - dot, y - dot, 2.0 * dot, 2.0 * dot),66 Radii::all(dot),67 Fill::Color(ink)68 );69 }70}7172exp fn renew(self s: &var Spinner, next: Spinner) -> Change {73 const relayout = s.size != next.size || s.sizing != next.sizing;74 s = move next;75 return if (relayout) Change::Layout else Change::Paint;76}7778pub fn sizing(self s: &Spinner) -> Sizing => s.sizing;7980// It moves while it shows.81pub fn settling(self s: &Spinner, since: Duration) -> bool => true;8283pub fn semantics(self s: &Spinner) -> Semantics => Semantics {84 .role = if (s.accessible.length() > 0) Role::Label else Role::Unknown,85 .label = copy s.accessible86};8788/* --- Progress bars --- */8990pub fn layout(self p: &var ProgressBar, arrange: &var Arrange, limits: Limits) -> Extent {91 return limits.constrain(Extent { .width = p.width, .height = p.height });92}9394// The track, then what is done over it from the start; not known, a95// third of the track going across it once a second.96exp fn paint(self p: &ProgressBar, look: &Look, paint: &var Paint) {97 const color = if (p.color.color(&look.theme) is ::Some |c|) c else look.theme.primary;98 const w = look.size.width;99 const h = look.size.height;100 const radius = Radii::all(h / 2.0);101 paint.rounded_rect(Rect::sized(w, h), radius, Fill::Color(look.theme.neutral));102 if (p.value is ::Some |v|) {103 const done = max(clamp(v, 0.0, 1.0) * w, h);104 if (v > 0.0) {105 paint.rounded_rect(106 Rect::at(look.from_start(0.0, done), 0.0, done, h),107 radius,108 Fill::Color(color)109 );110 }111 return;112 }113 const ms = look.since.nanoseconds / 1000000;114 if (look.reduced) {115 var ink = color;116 ink.a = ink.a * (0.3 + 0.3 * (1.0 - cos(PI * 2.0 * (ms % 2000) to f32 / 2000.0)));117 paint.rounded_rect(Rect::sized(w, h), radius, Fill::Color(ink));118 return;119 }120 const stretch = w / 3.0;121 const at = (ms % 1000) to f32 / 1000.0 * (w + stretch) - stretch;122 const start = max(at, 0.0);123 const end = min(at + stretch, w);124 // across from the start, leftward right to left125 if (end > start) {126 paint.rounded_rect(127 Rect::at(look.from_start(start, end - start), 0.0, end - start, h),128 radius,129 Fill::Color(color)130 );131 }132}133134exp fn renew(self p: &var ProgressBar, next: ProgressBar) -> Change {135 const relayout = p.width != next.width || p.height != next.height || p.sizing != next.sizing;136 p = move next;137 return if (relayout) Change::Layout else Change::Paint;138}139140pub fn sizing(self p: &ProgressBar) -> Sizing => p.sizing;141142// It moves while how much is done is not known.143pub fn settling(self p: &ProgressBar, since: Duration) -> bool => p.value is ::None;144145pub fn semantics(self p: &ProgressBar) -> Semantics {146 var s = Semantics { .role = Role::ProgressBar, .label = copy p.accessible };147 if (p.value is ::Some |v|) {148 const percent = round(clamp(v, 0.0, 1.0) * 100.0);149 s.value = f"{percent to u32}%";150 s.range = ::Some(AccessRange {151 .value = percent to f64,152 .minimum = 0.0,153 .maximum = 100.0,154 .step = 0.0155 });156 }157 return move s;158}