use crate::reply::offers::OfferRow;
use crate::ui::model::Grade;
use crate::ui::{Model, theme};
pub const WORD: &str = "offers";
pub const MINT_HEADING: &str = "offer a new device a place here";
pub const NAME_HINT: &str = "the new device's name";
pub const NAME_HELP: &str =
"the name the server will know the new device by; one nothing was ever issued under";
pub const GRADE_HELP: &str =
"what the new device may do: an operator asks and acts, a foot only executes";
pub const MINT: &str = "mint offer";
pub const MINT_HELP: &str =
"asks the server for a one-time registration URL for a device of that name and grade";
pub const MINTED: &str = "the new device opens this URL, or scans the symbol — it works once";
pub const UNTIL: &str = "its code expires at";
pub const PENDING: &str = "pending offers";
pub const NOT_ANSWERED: &str = "waiting to hear which offers are pending";
pub const NONE_PENDING: &str = "no offer is pending in this workspace";
pub const APPROVE: &str = "approve";
pub const APPROVE_HELP: &str =
"seats this device in the workspace — only when the word on its screen is this row's word";
pub const APPROVE_DARK: &str =
"cannot be approved yet: the device has not dialled the server with its new certificate";
pub const DECLINE: &str = "decline";
pub const DECLINE_HELP: &str =
"voids this offer; if a device already claimed it, its certificate stays refused for good";
pub const NO_WORD: &str = "no word yet — nothing has claimed it";
const NAME_BOX: &str = "the offer's name box";
const SYMBOL: f32 = 220.0;
pub fn render(ui: &mut egui::Ui, model: &mut Model) -> bool {
if !model.showing(crate::ui::Listing::Offers) {
return false;
}
if let Some(aim) = model.aim.clone() {
ui.colored_label(
theme::INK_WEAK,
format!("on {} — {}", aim.address, aim.channel),
);
}
form(ui, model);
minted(ui, model);
ui.add_space(theme::space::M);
theme::paint::section(ui, PENDING);
match model.pending.clone() {
None => {
ui.label(NOT_ANSWERED);
}
Some(rows) if rows.is_empty() => {
ui.label(NONE_PENDING);
}
Some(rows) => {
for row in &rows {
pending(ui, model, row);
}
}
}
true
}
fn form(ui: &mut egui::Ui, model: &mut Model) {
theme::paint::section(ui, MINT_HEADING);
theme::paint::field(
ui,
egui::Id::new(NAME_BOX),
&mut model.offering.name,
NAME_HINT,
None,
NAME_HELP,
);
ui.horizontal_wrapped(|ui| {
for grade in Grade::BOTH {
let chosen = model.offering.grade == grade;
if theme::paint::seat(ui, chosen, grade.word(), GRADE_HELP).clicked() {
model.offering.grade = grade;
}
}
let mint = theme::paint::button(ui, MINT, model.offering.ready(), MINT_HELP);
crate::ui::act::tag(&mint, &[crate::verbs::OFFER.word]);
if mint.clicked() {
model.post_offer();
}
});
}
fn minted(ui: &mut egui::Ui, model: &Model) {
let Some(minted) = &model.offering.minted else {
return;
};
ui.add_space(theme::space::S);
ui.colored_label(theme::INK_WEAK, MINTED);
ui.add(egui::Label::new(egui::RichText::new(&minted.offered.url).monospace()).wrap());
ui.colored_label(
theme::INK_WEAK,
format!(
"{UNTIL} {}",
crate::reply::offers::clock(minted.offered.expires)
),
);
if let Some(drawn) = &minted.symbol {
let side = SYMBOL.min(ui.available_width());
ui.allocate_ui(egui::vec2(side, side), |ui| symbol::paint(ui, drawn));
}
}
fn pending(ui: &mut egui::Ui, model: &mut Model, row: &OfferRow) {
ui.separator();
ui.horizontal_wrapped(|ui| {
ui.label(format!("{} {} {}", row.name, row.grade, row.state));
match &row.word {
Some(word) => {
ui.label(egui::RichText::new(word).monospace().strong());
}
None => {
ui.colored_label(theme::INK_WEAK, NO_WORD);
}
}
});
ui.horizontal_wrapped(|ui| {
let (live, help) = if row.proved() {
(true, APPROVE_HELP)
} else {
(false, APPROVE_DARK)
};
let approve = theme::paint::button(ui, APPROVE, live, help);
crate::ui::act::tag(&approve, &[crate::verbs::APPROVE.word]);
if approve.clicked() {
model.post_verdict(&row.name, true);
}
let decline = theme::paint::button(ui, DECLINE, true, DECLINE_HELP);
crate::ui::act::tag(&decline, &[crate::verbs::DECLINE.word]);
if decline.clicked() {
model.post_verdict(&row.name, false);
}
});
}
mod symbol;
#[cfg(test)]
pub(crate) mod tests;