utilities/widgets: ensure ProgressSpinner is cleaned up

memfd
Manos Pitsidianakis 2020-09-15 13:01:44 +03:00
parent 0e2641f7ed
commit f83df69d2f
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
2 changed files with 23 additions and 14 deletions

View File

@ -663,7 +663,7 @@ impl StatusBar {
height: 1, height: 1,
id: ComponentId::new_v4(), id: ComponentId::new_v4(),
auto_complete: AutoComplete::new(Vec::new()), auto_complete: AutoComplete::new(Vec::new()),
progress_spinner: ProgressSpinner::new(3), progress_spinner: ProgressSpinner::new(1),
in_progress_jobs: HashSet::default(), in_progress_jobs: HashSet::default(),
done_jobs: HashSet::default(), done_jobs: HashSet::default(),
cmd_history: crate::command::history::old_cmd_history(), cmd_history: crate::command::history::old_cmd_history(),
@ -1182,8 +1182,8 @@ impl Component for StatusBar {
self.in_progress_jobs.remove(job_id); self.in_progress_jobs.remove(job_id);
if self.in_progress_jobs.is_empty() { if self.in_progress_jobs.is_empty() {
self.progress_spinner.stop(); self.progress_spinner.stop();
self.set_dirty(true);
} }
self.progress_spinner.set_dirty(true);
} }
UIEvent::StatusEvent(StatusEvent::NewJob(ref job_id)) UIEvent::StatusEvent(StatusEvent::NewJob(ref job_id))
if !self.done_jobs.contains(job_id) => if !self.done_jobs.contains(job_id) =>
@ -1191,6 +1191,7 @@ impl Component for StatusBar {
if self.in_progress_jobs.is_empty() { if self.in_progress_jobs.is_empty() {
self.progress_spinner.start(); self.progress_spinner.start();
} }
self.progress_spinner.set_dirty(true);
self.in_progress_jobs.insert(*job_id); self.in_progress_jobs.insert(*job_id);
} }
UIEvent::Timer(_) => { UIEvent::Timer(_) => {

View File

@ -996,12 +996,13 @@ pub struct ProgressSpinner {
timer: crate::timer::PosixTimer, timer: crate::timer::PosixTimer,
stage: usize, stage: usize,
kind: usize, kind: usize,
active: bool,
dirty: bool, dirty: bool,
id: ComponentId, id: ComponentId,
} }
impl ProgressSpinner { impl ProgressSpinner {
const KINDS: &'static [&'static [&'static str]] = &[ const KINDS: [&'static [&'static str]; 15] = [
&["", "", "", "", "", "", "", ""], &["", "", "", "", "", "", "", ""],
&["", "", "", "", "", "", ""], &["", "", "", "", "", "", ""],
&["", "", "", "", "", "", ""], &["", "", "", "", "", "", ""],
@ -1029,16 +1030,19 @@ impl ProgressSpinner {
nix::sys::signal::Signal::SIGALRM, nix::sys::signal::Signal::SIGALRM,
) )
.unwrap(); .unwrap();
debug!("Requested timer {:?} for ProgressSpinner", timer.si_value);
ProgressSpinner { ProgressSpinner {
timer, timer,
stage: 0, stage: 0,
kind: kind % Self::KINDS.len(), kind: kind % Self::KINDS.len(),
dirty: true, dirty: true,
active: false,
id: ComponentId::new_v4(), id: ComponentId::new_v4(),
} }
} }
pub fn start(&mut self) { pub fn start(&mut self) {
self.active = true;
self.timer self.timer
.set_interval(Self::INTERVAL) .set_interval(Self::INTERVAL)
.set_value(Self::VALUE) .set_value(Self::VALUE)
@ -1046,6 +1050,8 @@ impl ProgressSpinner {
} }
pub fn stop(&mut self) { pub fn stop(&mut self) {
self.active = false;
self.stage = 0;
self.timer self.timer
.set_interval(std::time::Duration::from_millis(0)) .set_interval(std::time::Duration::from_millis(0))
.set_value(std::time::Duration::from_millis(0)) .set_value(std::time::Duration::from_millis(0))
@ -1064,17 +1070,19 @@ impl Component for ProgressSpinner {
if self.dirty { if self.dirty {
let theme_attr = crate::conf::value(context, "status.bar"); let theme_attr = crate::conf::value(context, "status.bar");
clear_area(grid, area, theme_attr); clear_area(grid, area, theme_attr);
let stage = self.stage; if self.active {
self.stage = (self.stage + 1).wrapping_rem(Self::KINDS[self.kind].len()); let stage = self.stage;
write_string_to_grid( self.stage = (self.stage + 1).wrapping_rem(Self::KINDS[self.kind].len());
Self::KINDS[self.kind][stage], write_string_to_grid(
grid, Self::KINDS[self.kind][stage],
theme_attr.fg, grid,
theme_attr.bg, theme_attr.fg,
theme_attr.attrs, theme_attr.bg,
area, theme_attr.attrs,
None, area,
); None,
);
}
context.dirty_areas.push_back(area); context.dirty_areas.push_back(area);
self.dirty = false; self.dirty = false;
} }