use crate::component::ComponentId;
use crate::event::EventArgs;
use crate::prop::PropValue;
use crate::ui::Ui;
const INDICATOR_AFTER: f64 = 3.0;
#[derive(Default)]
pub(crate) struct Busy {
outstanding: Vec<(u64, ComponentId)>,
since: Option<f64>,
waiting_user: usize,
shown: Option<f64>,
busy_form: Option<ComponentId>,
stage: Stage,
pub(crate) freed_hook: Option<std::rc::Rc<dyn Fn(&mut Ui, &[ComponentId])>>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
enum Stage {
#[default]
Idle,
Cursor,
Indicator,
}
struct Watch {
id: Option<ComponentId>,
delay: f64,
action: String,
busy_form: Option<ComponentId>,
block_input: bool,
min_visible: f64,
}
impl Ui {
fn watch(&self) -> Watch {
let id = self.components().find(|&c| self.class_name(c) == Some("BusyWatcher"));
let num = |p: &str, d: f64| match id.map(|i| self.get_prop(i, p)) {
Some(Ok(PropValue::Int(n))) => n as f64 / 1000.0,
Some(Ok(PropValue::Float(n))) => n / 1000.0,
_ => d,
};
let action = match id.map(|i| self.get_prop(i, "action")) {
Some(Ok(PropValue::Enum(a))) => a,
_ => "default".into(),
};
let busy_form = match id.map(|i| self.get_prop(i, "busy_form")) {
Some(Ok(PropValue::Component(c))) => c,
_ => None,
};
let block_input = !matches!(id.map(|i| self.get_prop(i, "block_input")), Some(Ok(PropValue::Bool(false))));
Watch { id, delay: num("delay", 1.0), action, busy_form, block_input, min_visible: num("min_visible", 0.3) }
}
pub fn backend_event_sent(&mut self, seq: u64, sender: ComponentId) {
let form = self.form_of(sender);
self.busy.outstanding.push((seq, form));
if self.busy.since.is_none() && self.busy.waiting_user == 0 {
self.busy.since = Some(self.time);
}
}
pub fn backend_done(&mut self, seq: u64) {
self.busy.outstanding.retain(|(s, _)| *s != seq);
if !self.busy.outstanding.is_empty() {
return;
}
self.busy.since = None;
self.end_busy(false);
let roots: Vec<ComponentId> = self.input.keys().copied().collect();
for root in roots {
self.release_held(root);
}
self.request_repaint();
}
pub fn backend_busy(&self) -> bool {
!self.busy.outstanding.is_empty() && self.busy.waiting_user == 0
}
pub(crate) fn busy_blocks_input(&self) -> bool {
self.backend_busy() && self.watch().block_input
}
pub fn backend_waiting_user(&mut self, waiting: bool) {
if waiting {
self.busy.waiting_user += 1;
self.busy.since = None;
self.end_busy(true);
} else {
self.busy.waiting_user = self.busy.waiting_user.saturating_sub(1);
if self.busy.waiting_user == 0 && !self.busy.outstanding.is_empty() {
self.busy.since = Some(self.time);
}
}
self.request_repaint();
}
fn end_busy(&mut self, now: bool) {
let Some(shown) = self.busy.shown else {
self.busy.stage = Stage::Idle;
return;
};
let watch = self.watch();
if !now && self.busy.busy_form.is_some() && self.time - shown < watch.min_visible {
return;
}
self.busy.shown = None;
self.busy.stage = Stage::Idle;
if let Some(form) = self.busy.busy_form.take() {
if self.contains(form) {
self.close(form);
}
}
if let Some(id) = watch.id {
self.fire(id, "on_busy_end", &EventArgs::None);
}
}
pub(crate) fn tick_busy(&mut self) {
if self.busy.since.is_none() {
if self.busy.shown.is_some() && self.busy.outstanding.is_empty() {
self.end_busy(false);
}
return;
}
let Some(since) = self.busy.since else { return };
let watch = self.watch();
let elapsed = self.time - since;
let stage = if elapsed >= watch.delay.max(INDICATOR_AFTER) && watch.action == "default" {
Stage::Indicator
} else if elapsed >= watch.delay {
Stage::Cursor
} else {
Stage::Idle
};
if stage != Stage::Idle && self.busy.shown.is_none() {
self.busy.shown = Some(self.time);
let mut skip_form = false;
if let Some(id) = watch.id {
skip_form = self.ask(id, "on_busy_start", &EventArgs::None) == crate::question::Asked::Answered(crate::question::Answer::Refused(String::new()));
}
if watch.action == "form" && !skip_form {
if let Some(form) = watch.busy_form.filter(|&f| self.contains(f)) {
let _ = self.show(form);
self.busy.busy_form = Some(form);
}
}
}
if stage != self.busy.stage {
self.busy.stage = stage;
self.request_repaint();
}
let next = if stage == Stage::Idle { since + watch.delay } else { since + watch.delay.max(INDICATOR_AFTER) };
if next > self.time {
let at = self.wake_at.get().map_or(next, |t| t.min(next));
self.wake_at.set(Some(at));
}
}
pub(crate) fn busy_cursor(&self) -> bool {
self.busy.since.is_some() && self.busy.stage != Stage::Idle && self.watch().action != "none"
}
pub(crate) fn paint_busy(&self, root: ComponentId, canvas: &mut dyn crate::canvas::Canvas) {
if self.busy.stage != Stage::Indicator {
return;
}
let Some(form) = self.control(root).map(|cb| cb.bounds()) else { return };
let style = self.theme.resolve(&["Hint"], "", crate::component::ControlState::default());
let ts = crate::canvas::TextStyle { font: style.font.clone(), color: style.text, ..Default::default() };
let text = crate::messages::t("Aguardando o programa…");
let pad = style.padding;
let w = canvas.measure_text(&text, &ts).width + pad.left + pad.right + 1.0;
let h = (ts.font.size * 1.4).round() + pad.top + pad.bottom;
let rect = crate::geometry::RectF::new(form.width - w - 12.0, form.height - h - 12.0, w, h);
if let Some(shadow) = &style.shadow {
canvas.draw_shadow(rect, style.radius, shadow);
}
canvas.fill_rect(rect, style.radius, &crate::canvas::Brush::Solid(style.fill.with_opacity(0.92)));
canvas.draw_text(&text, crate::geometry::RectF::new(rect.x + pad.left, rect.y + pad.top, w - pad.left - pad.right, h - pad.top - pad.bottom), &ts);
}
fn release_held(&mut self, root: ComponentId) {
crate::keyflow::release_after_busy(self, root);
}
}
impl Ui {
pub fn on_forms_freed(&mut self, f: impl Fn(&mut Ui, &[ComponentId]) + 'static) {
self.busy.freed_hook = Some(std::rc::Rc::new(f));
}
}