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}
55
56impl Tray {
57 pub fn new() -> Tray {
59 Tray::default()
60 }
61
62 pub fn icon_size(mut self, size: impl IntoProp<u32>) -> Tray {
64 self.icon_size = Some(size.into_prop());
65 self
66 }
67
68 pub fn menu_style(mut self, style: impl IntoProp<Style>) -> Tray {
72 self.menu_style = Some(style.into_prop());
73 self
74 }
75
76 pub fn visible(mut self, visible: impl IntoProp<bool>) -> Tray {
78 self.visible = Some(visible.into_prop());
79 self
80 }
81
82 pub fn geometry(mut self, geometry: Signal<Rect>) -> Tray {
85 self.geometry = Some(geometry);
86 self
87 }
88}
89
90impl Widget for Tray {
91 fn into_element(self: Box<Self>) -> Element {
92 let size: Shared<u32> = self
93 .icon_size
94 .map_or_else(|| Rc::new(|| ICON_SIZE), Prop::shared);
95 let style: Shared<Style> = self
96 .menu_style
97 .map_or_else(|| Rc::new(|| MENU), Prop::shared);
98 let mut tray = Row::new().children_keyed(
101 || vec![()],
102 |_| (),
103 move |cx, _| {
104 let host = cx.tray();
105 let items = host.items();
106 let (size, style) = (Rc::clone(&size), Rc::clone(&style));
107 Row::new().align(Align::Center).children_keyed(
108 move || items.get(),
109 |item| item.id.clone(),
110 move |_, item| item_button(&host, item, &size, &style),
111 )
112 },
113 );
114 if let Some(visible) = self.visible {
115 tray = tray.visible(visible);
116 }
117 if let Some(geometry) = self.geometry {
118 tray = tray.geometry(geometry);
119 }
120 Box::new(tray).into_element()
121 }
122}
123
124fn item_button(
126 host: &TrayHost,
127 item: Memo<TrayItem>,
128 size: &Shared<u32>,
129 style: &Shared<Style>,
130) -> Button {
131 let id: Rc<str> = item.with_untracked(|item| item.id.as_str().into());
133 let (icon_size, pick) = (Rc::clone(size), Rc::clone(size));
134 let icon = Icon::new(move || {
135 let pixels = pick().saturating_mul(2);
139 let source = item.with(|item| item.icon(pixels));
140 source.unwrap_or_else(|| IconSource::Name(NO_ICON.into()))
141 })
142 .size(move || icon_size());
143 let click = {
144 let (host, id, style) = (host.clone(), Rc::clone(&id), Rc::clone(style));
145 move |cx: &Cx| {
146 if item.with_untracked(|item| item.item_is_menu) {
147 open_menu(cx, &host, &id, &style);
148 } else {
149 host.activate(&id);
150 }
151 }
152 };
153 let right_click = {
154 let (host, id, style) = (host.clone(), Rc::clone(&id), Rc::clone(style));
155 move |cx: &Cx| open_menu(cx, &host, &id, &style)
156 };
157 let (middle, scroll) = (host.clone(), host.clone());
158 let middle_id = Rc::clone(&id);
159 Button::new(icon)
160 .style(BUTTON)
161 .on_click(click)
162 .on_right_click(right_click)
163 .on_middle_click(move |_| middle.secondary_activate(&middle_id))
164 .on_scroll(move |_, steps| scroll.scroll(&id, steps))
165}
166
167fn open_menu(cx: &Cx, host: &TrayHost, id: &Rc<str>, style: &Shared<Style>) {
171 let Some(service) = host.service.clone() else {
172 return;
173 };
174 let Some(opened) = service.open_menu(id) else {
175 return;
176 };
177 let (host, id, style) = (host.clone(), Rc::clone(id), Rc::clone(style));
178 cx.open_popup(move |cx| {
179 let _following = cx.signal(Following {
180 service: Rc::clone(&service),
181 opened,
182 });
183 let menu = host.menu();
184 let closer = cx.clone();
187 cx.effect(move || {
188 menu.with(|_| ());
189 if !service.follows(opened) {
190 closer.close_popup();
191 }
192 });
193 let shown = {
194 let id = Rc::clone(&id);
195 move || menu.with(|menu| menu.as_ref().is_some_and(|menu| *menu.item == *id))
196 };
197 let entries = {
198 let id = Rc::clone(&id);
199 move || {
200 menu.with(|menu| {
201 let ours = menu.as_ref().filter(|menu| *menu.item == *id);
202 ours.map(|menu| menu.entries.clone()).unwrap_or_default()
203 })
204 }
205 };
206 let (activate, submenu) = (host.clone(), host);
207 let menu = Menu::new(entries)
209 .style(move || if shown() { style() } else { Style::new() })
210 .on_activate(move |_, entry| activate.menu_event(&id, entry))
211 .on_open_submenu(move |_, entry| submenu.open_submenu(entry));
212 PopupWindow::new().child(menu)
213 });
214}
215
216struct Following {
219 service: Rc<super::Service>,
220 opened: u64,
221}
222
223impl Drop for Following {
224 fn drop(&mut self) {
225 self.service.close_menu_opened(self.opened);
226 }
227}