1use egui::{Atom, Button, Color32, Id, Rect, Response, RichText, Shape, Stroke, Ui, Widget, vec2};
20use egui_pop_core::icon::Icon;
21use egui_pop_core::kit::{Kit, Part, State};
22use egui_pop_core::motion::Busy;
23use egui_pop_core::progress::paint_spinner;
24use egui_pop_core::strings;
25use egui_pop_core::theme::Tokens;
26use egui_pop_model::ids::CommandId;
27use serde_json::Value as Json;
28
29use crate::app::AppHandle;
30use crate::strings as txt;
31
32pub fn busy_shown(app: &AppHandle, ctx: &egui::Context, id: Id, command: &CommandId) -> bool {
36 let running = app.running(command);
37 let since = app.running_since(command).unwrap_or(0.0);
38 Busy::new(id, since).visible(ctx, running)
39}
40
41pub fn paint_slot(ui: &Ui, rect: Rect, busy: bool, icon: Option<Icon>, enabled: bool) {
43 let t = Tokens::of(ui.ctx());
44 let color = if enabled {
45 t.text.default
46 } else {
47 t.text.muted
48 };
49 if busy {
50 paint_spinner(ui, rect, color);
51 } else if let Some(i) = icon {
52 i.paint(ui.painter(), rect, color);
53 }
54}
55
56#[derive(Debug)]
59#[must_use = "add it with `ui.add`"]
60pub struct CommandButton<'a> {
61 app: &'a AppHandle,
62 command: CommandId,
63 args: Json,
64 label: Option<String>,
65 icon: Option<Icon>,
66}
67
68impl<'a> CommandButton<'a> {
69 pub fn new(app: &'a AppHandle, command: CommandId) -> Self {
70 Self {
71 app,
72 command,
73 args: Json::Null,
74 label: None,
75 icon: None,
76 }
77 }
78
79 pub fn args(mut self, args: Json) -> Self {
81 self.args = args;
82 self
83 }
84
85 pub fn label(mut self, label: impl Into<String>) -> Self {
87 self.label = Some(label.into());
88 self
89 }
90
91 pub fn icon(mut self, icon: Icon) -> Self {
93 self.icon = Some(icon);
94 self
95 }
96}
97
98impl Widget for CommandButton<'_> {
99 fn ui(self, ui: &mut Ui) -> Response {
100 let ctx = ui.ctx().clone();
101 let t = Tokens::of(&ctx);
102 let app = self.app;
103 let id = &self.command;
104 let spec = app.command(id);
105 let title = self.label.clone().unwrap_or_else(|| {
106 spec.as_ref()
107 .map_or_else(|| id.to_string(), |s| app.command_menu_title(s))
108 });
109 let running = app.running(id);
110 let applies = spec.is_some() && app.applies(id, &app.when_context());
111 let enabled = applies && !running;
112 let slot = Id::unique(("egui_pop_command_button", id.as_str(), ui.scope_id()));
113 let side = t.size.icon_inline;
114 let kit = Kit::of(&ctx);
119 let look = &kit.command_button;
120 let r = ui
121 .add_enabled_ui(enabled, |ui| {
122 let under = ui.painter().add(Shape::Noop);
123 let state = ui
124 .ctx()
125 .read_response(ui.next_auto_id())
126 .map_or(State::REST, |r| State::from_response(&r))
127 .with(State::DISABLED, !ui.is_enabled());
128 let label = RichText::new(title.as_str()).color(look.style(state).text);
129 let border = ui.visuals().widgets.inactive.bg_stroke.width;
131 let r = Button::new((
132 Atom::custom(egui::IdSalt::new(slot), vec2(side, side)),
133 label,
134 ))
135 .fill(Color32::TRANSPARENT)
136 .stroke(Stroke::new(border, Color32::TRANSPARENT))
137 .atom_ui(ui);
138 let rect = r.response.rect;
139 let part = Part {
140 rect,
141 room: rect,
142 state,
143 label: None,
144 };
145 look.paint_under(ui, under, &part, None);
146 r
147 })
148 .inner;
149 let busy = busy_shown(app, &ctx, slot, id);
150 if let Some(rect) = r.rect(egui::IdSalt::new(slot)) {
151 paint_slot(ui, rect, busy, self.icon, enabled);
152 }
153 let mut response = r.response;
154 if running {
155 ctx.accesskit_node_builder(response.id, |node| node.set_busy());
156 response = response.on_disabled_hover_text(strings::tr(&ctx, &txt::MENU_RUNNING));
157 } else if let Some(e) = app
158 .history()
159 .iter()
160 .rev()
161 .find(|run| run.id == *id)
162 .and_then(|run| run.result.clone().err())
163 {
164 response = response.on_hover_text(strings::tr_args(
165 &ctx,
166 &txt::MENU_LAST_RUN_FAILED,
167 &[("error", &e)],
168 ));
169 }
170 if response.clicked() {
171 app.run(id.clone(), self.args.clone());
172 }
173 response
174 }
175}