1use std::rc::Rc;
6
7use crabshell_reactive::{Memo, Signal};
8use crabshell_ui::{
9 Align, Button, Color, Cx, Element, Icon, IconSource, IntoProp, Menu, PopupWindow, Prop, Rect,
10 Row, Style, Widget,
11};
12
13use super::{TrayHost, TrayItem};
14use crate::ServicesExt;
15
16const MENU: Style = Style::new()
19 .background(Color::rgb(0x2b, 0x2b, 0x2b))
20 .color(Color::rgb(0xe6, 0xe6, 0xe6))
21 .radius(6.0)
22 .padding(4, 0);
23
24const BUTTON: Style = Style::new().padding(2, 4).radius(4.0);
26
27const ICON_SIZE: u32 = 16;
29
30const NO_ICON: &str = "image-missing";
32
33type Shared<T> = Rc<dyn Fn() -> T>;
34
35#[derive(Default)]
49pub struct Tray {
50 icon_size: Option<Prop<u32>>,
51 menu_style: Option<Prop<Style>>,
52 visible: Option<Prop<bool>>,
53 geometry: Option<Signal<Rect>>,
54 mapped_geometry: Option<Signal<Rect>>,
55}
56
57impl Tray {
58 pub fn new() -> Tray {
60 Tray::default()
61 }
62
63 pub fn icon_size(mut self, size: impl IntoProp<u32>) -> Tray {
65 self.icon_size = Some(size.into_prop());
66 self
67 }
68
69 pub fn menu_style(mut self, style: impl IntoProp<Style>) -> Tray {
73 self.menu_style = Some(style.into_prop());
74 self
75 }
76
77 pub fn visible(mut self, visible: impl IntoProp<bool>) -> Tray {
79 self.visible = Some(visible.into_prop());
80 self
81 }
82
83 pub fn geometry(mut self, geometry: Signal<Rect>) -> Tray {
86 self.geometry = Some(geometry);
87 self
88 }
89
90 pub fn mapped_geometry(mut self, geometry: Signal<Rect>) -> Tray {
93 self.mapped_geometry = Some(geometry);
94 self
95 }
96}
97
98impl Widget for Tray {
99 fn into_element(self: Box<Self>) -> Element {
100 let size: Shared<u32> = self
101 .icon_size
102 .map_or_else(|| Rc::new(|| ICON_SIZE), Prop::shared);
103 let style: Shared<Style> = self
104 .menu_style
105 .map_or_else(|| Rc::new(|| MENU), Prop::shared);
106 let mut tray = Row::new().children_keyed(
109 || vec![()],
110 |_| (),
111 move |cx, _| {
112 let host = cx.tray();
113 let items = host.items();
114 let (size, style) = (Rc::clone(&size), Rc::clone(&style));
115 Row::new().align(Align::Center).children_keyed(
116 move || items.get(),
117 |item| item.id.clone(),
118 move |_, item| item_button(&host, item, &size, &style),
119 )
120 },
121 );
122 if let Some(visible) = self.visible {
123 tray = tray.visible(visible);
124 }
125 if let Some(geometry) = self.geometry {
126 tray = tray.geometry(geometry);
127 }
128 if let Some(geometry) = self.mapped_geometry {
129 tray = tray.mapped_geometry(geometry);
130 }
131 Box::new(tray).into_element()
132 }
133}
134
135fn item_button(
137 host: &TrayHost,
138 item: Memo<TrayItem>,
139 size: &Shared<u32>,
140 style: &Shared<Style>,
141) -> Button {
142 let id: Rc<str> = item.with_untracked(|item| item.id.as_str().into());
144 let (icon_size, pick) = (Rc::clone(size), Rc::clone(size));
145 let icon = Icon::new(move || {
146 let pixels = pick().saturating_mul(2);
150 let source = item.with(|item| item.icon(pixels));
151 source.unwrap_or_else(|| IconSource::Name(NO_ICON.into()))
152 })
153 .size(move || icon_size());
154 let click = {
155 let (host, id, style) = (host.clone(), Rc::clone(&id), Rc::clone(style));
156 move |cx: &Cx| {
157 if item.with_untracked(|item| item.item_is_menu) {
158 open_menu(cx, &host, &id, &style);
159 } else {
160 host.activate(&id);
161 }
162 }
163 };
164 let right_click = {
165 let (host, id, style) = (host.clone(), Rc::clone(&id), Rc::clone(style));
166 move |cx: &Cx| open_menu(cx, &host, &id, &style)
167 };
168 let (middle, scroll) = (host.clone(), host.clone());
169 let middle_id = Rc::clone(&id);
170 Button::new(icon)
171 .style(BUTTON)
172 .accessible_name(move || {
173 item.with(|item| {
174 let name = if item.title.is_empty() {
175 &item.app_id
176 } else {
177 &item.title
178 };
179 name.clone()
180 })
181 })
182 .on_click(click)
183 .on_right_click(right_click)
184 .on_middle_click(move |_| middle.secondary_activate(&middle_id))
185 .on_scroll(move |_, steps| scroll.scroll(&id, steps))
186}
187
188fn open_menu(cx: &Cx, host: &TrayHost, id: &Rc<str>, style: &Shared<Style>) {
192 let Some(service) = host.service.clone() else {
193 return;
194 };
195 let Some(opened) = service.open_menu(id) else {
196 return;
197 };
198 let (host, id, style) = (host.clone(), Rc::clone(id), Rc::clone(style));
199 cx.open_popup(move |cx| {
200 let _following = cx.signal(Following {
201 service: Rc::clone(&service),
202 opened,
203 });
204 let menu = host.menu();
205 let closer = cx.clone();
208 cx.effect(move || {
209 menu.with(|_| ());
210 if !service.follows(opened) {
211 closer.close_popup();
212 }
213 });
214 let shown = {
215 let id = Rc::clone(&id);
216 move || menu.with(|menu| menu.as_ref().is_some_and(|menu| *menu.item == *id))
217 };
218 let entries = {
219 let id = Rc::clone(&id);
220 move || {
221 menu.with(|menu| {
222 let ours = menu.as_ref().filter(|menu| *menu.item == *id);
223 ours.map(|menu| menu.entries.clone()).unwrap_or_default()
224 })
225 }
226 };
227 let (activate, submenu) = (host.clone(), host);
228 let menu = Menu::new(entries)
230 .style(move || if shown() { style() } else { Style::new() })
231 .on_activate(move |_, entry| activate.menu_event(&id, entry))
232 .on_open_submenu(move |_, entry| submenu.open_submenu(entry));
233 PopupWindow::new().child(menu)
234 });
235}
236
237struct Following {
240 service: Rc<super::Service>,
241 opened: u64,
242}
243
244impl Drop for Following {
245 fn drop(&mut self) {
246 self.service.close_menu_opened(self.opened);
247 }
248}