BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / format_toolbar.rs

format_toolbar.rs

62bd9112 History
format_toolbar.rs 10.3 KiB Download Raw
1use std::rc::Rc;
2
3use gpui_kit::component::input::{Input, InputState};
4use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex};
5use gpui_kit::prelude::FluentBuilder as _;
6use gpui_kit::*;
7use teams_core::FormatState;
8
9use super::widgets::symbol;
10use crate::theme;
11
12const PADDING: f32 = 4.;
13const BUTTON: f32 = 28.;
14const BUTTON_GAP: f32 = 2.;
15const SEPARATOR_MARGIN: f32 = 4.;
16const GAP: f32 = 8.;
17const ARROW_WIDTH: f32 = 14.;
18const ARROW_HEIGHT: f32 = 7.;
19const ARROW_MARGIN: f32 = 12.;
20const LINK_FIELD_WIDTH: f32 = 240.;
21const APPLY_WIDTH: f32 = 64.;
22const ACTIVE_ALPHA: f32 = 0.22;
23
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub enum FormatButton {
26 Bold,
27 Italic,
28 Underline,
29 Strike,
30 Code,
31 Link,
32 Bulleted,
33 Numbered,
34 Quote,
35}
36
37pub const GROUPS: [&[FormatButton]; 3] = [
38 &[
39 FormatButton::Bold,
40 FormatButton::Italic,
41 FormatButton::Underline,
42 FormatButton::Strike,
43 ],
44 &[FormatButton::Code, FormatButton::Link],
45 &[
46 FormatButton::Bulleted,
47 FormatButton::Numbered,
48 FormatButton::Quote,
49 ],
50];
51
52impl FormatButton {
53 fn tooltip(self) -> &'static str {
54 match self {
55 FormatButton::Bold => "Bold (Ctrl+B)",
56 FormatButton::Italic => "Italic (Ctrl+I)",
57 FormatButton::Underline => "Underline (Ctrl+U)",
58 FormatButton::Strike => "Strikethrough (Ctrl+Shift+X)",
59 FormatButton::Code => "Code (Ctrl+Shift+C)",
60 FormatButton::Link => "Link (Ctrl+K)",
61 FormatButton::Bulleted => "Bulleted list",
62 FormatButton::Numbered => "Numbered list",
63 FormatButton::Quote => "Quote",
64 }
65 }
66
67 fn content(self, color: Hsla) -> AnyElement {
68 let letter = |text: &'static str| div().text_size(px(15.)).text_color(color).child(text);
69 match self {
70 FormatButton::Bold => letter("B").font_weight(FontWeight::BOLD).into_any_element(),
71 FormatButton::Italic => letter("I").italic().into_any_element(),
72 FormatButton::Underline => letter("U").underline().into_any_element(),
73 FormatButton::Strike => letter("S").line_through().into_any_element(),
74 FormatButton::Code => symbol("code", 18., color).into_any_element(),
75 FormatButton::Link => symbol("link", 18., color).into_any_element(),
76 FormatButton::Bulleted => symbol("format_list_bulleted", 18., color).into_any_element(),
77 FormatButton::Numbered => symbol("format_list_numbered", 18., color).into_any_element(),
78 FormatButton::Quote => symbol("format_quote", 18., color).into_any_element(),
79 }
80 }
81}
82
83pub fn bar_width() -> f32 {
84 let buttons: usize = GROUPS.iter().map(|group| group.len()).sum();
85 let gaps: usize = GROUPS.iter().map(|group| group.len() - 1).sum();
86 let separators = (GROUPS.len() - 1) as f32 * (1. + 2. * SEPARATOR_MARGIN);
87 2. * PADDING + buttons as f32 * BUTTON + gaps as f32 * BUTTON_GAP + separators
88}
89
90pub fn link_width() -> f32 {
91 2. * PADDING + LINK_FIELD_WIDTH + BUTTON_GAP * 2. + APPLY_WIDTH
92}
93
94#[derive(Debug, Clone, Copy, PartialEq)]
95pub struct Placement {
96 pub left: Pixels,
97 pub bottom: Pixels,
98 pub arrow_x: Pixels,
99}
100
101/// Centered over `center_x`, `GAP` above `top`, kept between the composer edges.
102pub fn place(center_x: Pixels, top: Pixels, edges: (Pixels, Pixels), width: f32) -> Placement {
103 let width = px(width);
104 let (left_edge, right_edge) = edges;
105 let left = (center_x - width / 2.)
106 .min(right_edge - width)
107 .max(left_edge);
108 Placement {
109 left,
110 bottom: top - px(GAP),
111 arrow_x: (center_x - left)
112 .max(px(ARROW_MARGIN))
113 .min(width - px(ARROW_MARGIN)),
114 }
115}
116
117pub type PressHandler = Rc<dyn Fn(FormatButton, &mut Window, &mut App)>;
118pub type ApplyHandler = Rc<dyn Fn(&mut Window, &mut App)>;
119
120pub enum Mode<'a> {
121 Bar {
122 state: &'a dyn Fn(FormatButton) -> FormatState,
123 on_press: PressHandler,
124 },
125 Link {
126 field: &'a Entity<InputState>,
127 on_apply: ApplyHandler,
128 },
129}
130
131fn button(format: FormatButton, state: FormatState, on_press: PressHandler) -> impl IntoElement {
132 let on = state == FormatState::On;
133 let color = if on {
134 theme::accent_soft()
135 } else {
136 theme::text_soft()
137 };
138 div()
139 .id(ElementId::Name(format!("format-{format:?}").into()))
140 .relative()
141 .size(px(BUTTON))
142 .flex()
143 .flex_none()
144 .items_center()
145 .justify_center()
146 .rounded(px(6.))
147 .border_1()
148 .border_color(transparent_black())
149 .cursor_pointer()
150 .when(on, |button| {
151 button.bg(theme::accent().opacity(ACTIVE_ALPHA))
152 })
153 .hover(|button| button.border_color(theme::border_strong()))
154 .tooltip(move |window, cx| Tooltip::new(format.tooltip()).build(window, cx))
155 .child(format.content(color))
156 .when(state == FormatState::Mixed, |button| {
157 button.child(
158 div()
159 .absolute()
160 .bottom(px(2.))
161 .left(px(7.))
162 .w(px(BUTTON - 16.))
163 .h(px(2.))
164 .rounded(px(1.))
165 .bg(theme::accent()),
166 )
167 })
168 .on_mouse_down(MouseButton::Left, |_, window, cx| {
169 window.prevent_default();
170 cx.stop_propagation();
171 })
172 .on_click(move |_, window, cx| on_press(format, window, cx))
173}
174
175fn separator() -> Div {
176 div()
177 .w(px(1.))
178 .h(px(BUTTON - 8.))
179 .mx(px(SEPARATOR_MARGIN))
180 .bg(theme::border_strong())
181}
182
183fn bar(state: &dyn Fn(FormatButton) -> FormatState, on_press: PressHandler) -> Div {
184 let mut row = h_flex().gap(px(BUTTON_GAP)).items_center();
185 for (index, group) in GROUPS.iter().enumerate() {
186 if index > 0 {
187 row = row.child(separator());
188 }
189 for format in group.iter().copied() {
190 row = row.child(button(format, state(format), on_press.clone()));
191 }
192 }
193 row
194}
195
196fn link_editor(field: &Entity<InputState>, on_apply: ApplyHandler) -> Div {
197 h_flex()
198 .gap(px(BUTTON_GAP * 2.))
199 .items_center()
200 .child(
201 div()
202 .w(px(LINK_FIELD_WIDTH))
203 .h(px(BUTTON))
204 .rounded(px(6.))
205 .border_1()
206 .border_color(theme::accent())
207 .bg(theme::surface())
208 .child(Input::new(field).appearance(false).bordered(false)),
209 )
210 .child(
211 div()
212 .id("format-link-apply")
213 .w(px(APPLY_WIDTH))
214 .h(px(BUTTON))
215 .flex()
216 .items_center()
217 .justify_center()
218 .rounded(px(6.))
219 .bg(theme::accent())
220 .text_color(theme::on_accent())
221 .text_size(px(13.))
222 .font_weight(FontWeight::SEMIBOLD)
223 .cursor_pointer()
224 .hover(|button| button.opacity(0.9))
225 .child("Apply")
226 .on_mouse_down(MouseButton::Left, |_, window, cx| {
227 window.prevent_default();
228 cx.stop_propagation();
229 })
230 .on_click(move |_, window, cx| on_apply(window, cx)),
231 )
232}
233
234fn arrow(offset: Pixels) -> impl IntoElement {
235 canvas(
236 |_, _, _| {},
237 move |bounds, _, window, _| {
238 let tip = point(bounds.origin.x + offset, bounds.origin.y + px(ARROW_HEIGHT));
239 for (inset, color) in [(0., theme::border_strong()), (1.5, theme::surface_raised())] {
240 let mut builder = PathBuilder::fill();
241 builder.move_to(point(
242 tip.x - px(ARROW_WIDTH / 2. - inset),
243 bounds.origin.y - px(1.),
244 ));
245 builder.line_to(point(
246 tip.x + px(ARROW_WIDTH / 2. - inset),
247 bounds.origin.y - px(1.),
248 ));
249 builder.line_to(point(tip.x, tip.y - px(inset)));
250 builder.close();
251 if let Ok(path) = builder.build() {
252 window.paint_path(path, color);
253 }
254 }
255 },
256 )
257 .w_full()
258 .h(px(ARROW_HEIGHT))
259}
260
261pub fn render(placement: Placement, mode: Mode<'_>) -> AnyElement {
262 let content = match mode {
263 Mode::Bar { state, on_press } => bar(state, on_press),
264 Mode::Link { field, on_apply } => link_editor(field, on_apply),
265 };
266 deferred(
267 anchored()
268 .position(point(placement.left, placement.bottom))
269 .anchor(Anchor::BottomLeft)
270 .child(
271 v_flex()
272 .id("format-toolbar")
273 .occlude()
274 .child(
275 div()
276 .p(px(PADDING))
277 .rounded(px(8.))
278 .border_1()
279 .border_color(theme::border_strong())
280 .bg(theme::surface_raised())
281 .shadow_lg()
282 .child(content),
283 )
284 .child(arrow(placement.arrow_x)),
285 ),
286 )
287 .with_priority(1)
288 .into_any_element()
289}
290
291#[cfg(test)]
292mod tests {
293 use gpui_kit::px;
294
295 use super::{ARROW_MARGIN, BUTTON, GROUPS, bar_width, place};
296
297 #[test]
298 fn the_bar_centers_over_the_selection() {
299 let placement = place(px(400.), px(300.), (px(100.), px(900.)), 200.);
300 assert_eq!(placement.left, px(300.));
301 assert_eq!(placement.bottom, px(292.));
302 assert_eq!(placement.arrow_x, px(100.));
303 }
304
305 #[test]
306 fn the_bar_stays_inside_the_composer_and_the_arrow_follows_the_selection() {
307 let near_left = place(px(120.), px(300.), (px(100.), px(900.)), 200.);
308 assert_eq!(near_left.left, px(100.));
309 assert_eq!(near_left.arrow_x, px(20.));
310 let near_right = place(px(899.), px(300.), (px(100.), px(900.)), 200.);
311 assert_eq!(near_right.left, px(700.));
312 assert_eq!(near_right.arrow_x, px(200. - ARROW_MARGIN));
313 }
314
315 #[test]
316 fn the_bar_holds_nine_buttons_in_three_groups() {
317 assert_eq!(GROUPS.iter().map(|group| group.len()).sum::<usize>(), 9);
318 assert!(bar_width() > 9. * BUTTON);
319 }
320}