#![no_std]
use serde::Deserialize;
use standard_plugin::prelude::*;
use standard_plugin::{claims, view};
#[derive(Debug, Default, Deserialize)]
pub struct Status {
pub state: String,
pub builds: u32,
}
#[standard_plugin::ui]
pub struct Together {
card: Surface<Cells>,
status: Status,
}
impl Together {
fn reload(&mut self, cx: &Context) {
if let Ok(Some(status)) = cx.values().get::<Status>("together.status") {
self.status = status;
}
}
fn paint(&mut self) {
let (cols, _) = self.card.size();
self.card.clear();
let line = format!("{} ยท {} builds", self.status.state, self.status.builds);
self.card.text(0, 0, &line, Style::fg(Colour::FG));
let hint = "rebuild";
let x = cols.saturating_sub(hint.len() as u32);
self.card
.text(x, 0, hint, Style::fg(Colour::ACCENT).underline());
let _ = self.card.commit();
}
}
impl UiPlugin for Together {
fn activate(cx: &mut Context) -> Self {
let _ = cx.events().on("together.*");
let mut plugin = Self {
card: Surface::new("card"),
status: Status::default(),
};
plugin.reload(cx);
plugin
}
fn frame(&mut self, _frame: &Frame) {
self.paint();
}
fn event(&mut self, event: Event, cx: &mut Context) {
match event {
Event::Plugin { name, .. } if name == "together.changed" => {
self.reload(cx);
self.paint();
cx.request_frame();
}
Event::Resize { .. } => {
self.paint();
cx.request_frame();
}
Event::Pointer(pointer) if pointer.kind == PointerKind::Down => {
if !view::is_driving() {
return;
}
let _ = claims().once("together.rebuild", 5_000, || {
let _ = cx.calls().send("rebuild", &(), Target::Singleton);
});
}
_ => {}
}
}
}