1use std::rc::Rc;
7
8use day_reactive::{Scope, untrack};
9use day_spec::{Icon, Shortcut, Symbol};
10
11use crate::{
12 Button, Decorate, Decorated, IntoReactive, IntoText, MenuEntry, Reactive, TextSource,
13 ToolbarEntry,
14};
15
16#[derive(Clone)]
37pub struct Command {
38 id: day_core::ElementId,
39 label: TextSource,
40 enabled: Reactive<bool>,
41 checked: Option<Reactive<bool>>,
42 icon: Option<Icon>,
43 shortcut: Option<Shortcut>,
44 handler: Rc<dyn Fn()>,
45 owner: Scope,
46}
47
48impl Command {
49 pub fn new<M>(
54 id: day_core::ElementId,
55 label: impl IntoText<M>,
56 action: impl Fn() + 'static,
57 ) -> Command {
58 Command {
59 id,
60 label: label.into_text(),
61 enabled: Reactive::Const(true),
62 checked: None,
63 icon: None,
64 shortcut: None,
65 handler: Rc::new(action),
66 owner: Scope::current(),
67 }
68 }
69
70 pub fn enabled<M>(mut self, enabled: impl IntoReactive<bool, M>) -> Self {
72 self.enabled = enabled.into_reactive();
73 self
74 }
75
76 pub fn checked<M>(mut self, checked: impl IntoReactive<bool, M>) -> Self {
78 self.checked = Some(checked.into_reactive());
79 self
80 }
81
82 pub fn icon(mut self, symbol: Symbol) -> Self {
83 self.icon = Some(Icon::Symbol(symbol));
84 self
85 }
86
87 pub fn image(mut self, name: impl Into<day_spec::ImageName>) -> Self {
88 self.icon = Some(Icon::Image(name.into().as_str().to_owned()));
89 self
90 }
91
92 pub fn shortcut(mut self, shortcut: Shortcut) -> Self {
94 self.shortcut = Some(shortcut);
95 self
96 }
97
98 pub fn id(&self) -> &str {
99 self.id.as_str()
100 }
101
102 pub fn label(&self) -> String {
104 if self.owner.is_alive() {
105 self.label.resolve()
106 } else {
107 String::new()
108 }
109 }
110
111 pub fn is_enabled(&self) -> bool {
112 self.owner.is_alive() && self.enabled.get()
113 }
114
115 pub fn is_checked(&self) -> Option<bool> {
116 self.checked
117 .as_ref()
118 .map(|v| self.owner.is_alive() && v.get())
119 }
120
121 pub fn invoke(&self) -> bool {
124 untrack(|| {
125 if !self.is_enabled() {
126 return false;
127 }
128 (self.handler)();
129 true
130 })
131 }
132
133 pub fn button(&self) -> Decorated<Button> {
136 let (title, enabled, action) = (self.clone(), self.clone(), self.clone());
137 let mut button = crate::button(move || title.label())
138 .enabled(move || enabled.is_enabled())
139 .action(move || {
140 action.invoke();
141 });
142 button = match &self.icon {
143 Some(Icon::Symbol(s)) => button.icon(*s),
144 Some(Icon::Image(name)) => button.image(name.clone()),
145 None => button,
146 };
147 button.id(self.id.clone())
148 }
149
150 pub fn menu_item(&self) -> MenuEntry {
153 let (title, enabled, action) = (self.clone(), self.clone(), self.clone());
154 let mut item = crate::menu_item(move || title.label())
155 .id(self.id.clone())
156 .enabled(move || enabled.is_enabled())
157 .action(move || {
158 action.invoke();
159 });
160 if self.is_checked().is_some() {
161 let checked = self.clone();
162 item = item.checked(move || checked.is_checked().unwrap_or(false));
163 }
164 if let Some(shortcut) = &self.shortcut {
165 item = item.shortcut(shortcut.clone());
166 }
167 match &self.icon {
168 Some(Icon::Symbol(s)) => item.icon(*s),
169 Some(Icon::Image(name)) => item.image(name.clone()),
170 None => item,
171 }
172 }
173
174 pub fn toolbar_item(&self) -> ToolbarEntry {
178 let (enabled, action) = (self.clone(), self.clone());
179 let mut item = crate::toolbar_button(self.id.clone(), self.label())
180 .enabled(move || enabled.is_enabled())
181 .action(move || {
182 action.invoke();
183 });
184 if self.is_checked().is_some() {
185 let checked = self.clone();
186 item = item.command_checked(Rc::new(move || checked.is_checked().unwrap_or(false)));
187 }
188 match &self.icon {
189 Some(Icon::Symbol(s)) => item.icon(*s),
190 Some(Icon::Image(name)) => item.image(name.clone()),
191 None => item,
192 }
193 }
194}