BlueGit

ict rusty-teams

feat(composer): Teams Format button with the full format bar, table selection highlight

- Format button first in the composer row (TextEditStyle icon), tooltip "Show/Hide Formatting options (Ctrl+Shift+X)", Ctrl+Shift+X toggles, open state per conversation like Teams' draft state
- Expanded format bar at the top of the composer box with an edge to edge divider; table group Insert table, Insert column or row, Delete column, row, or table (menus in Teams order, Up/Down/Enter/Escape)
- Ctrl+Alt+= / Ctrl+Alt++ / Ctrl+Alt+- open the row and column menus inside a table
- Strikethrough moves to Ctrl+Alt+X like Teams (Ctrl+Shift+X is Format)
- Selection toolbar keeps text formats only and hides while the format bar is open; format bar shows the typing style at the caret
- Selected table tints every cell in the selection color: the editor leaves a selected token's highlight to its renderer, the table renderer ignored is_selected
- Deviation: narrow bars wrap instead of Teams' overflow menu

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

metis Daniel Däschle · pushed by metis

Authored 2026-10-17 · 6 weeks ago

Hash
8b6a71b06b6b740437369a9e1375e939d3ecc235
Parent
4bb89d42
12 files changed +878 −98
crates/app-base/src/assets.rs +5 −1
@@ -8,7 +8,7 @@ macro_rules! symbols {
8 8 };
9 9 }
10 10
11 const SYMBOLS: [(&str, &[u8]); 55] = symbols![
11 const SYMBOLS: [(&str, &[u8]); 59] = symbols![
12 12 "bot",
13 13 "done",
14 14 "table",
@@ -64,6 +64,10 @@ const SYMBOLS: [(&str, &[u8]); 55] = symbols![
64 64 "fluent_share",
65 65 "fluent_clock",
66 66 "fluent_document_text",
67 "fluent_text_edit_style",
68 "fluent_text_edit_style_filled",
69 "fluent_table_add",
70 "fluent_table_dismiss",
67 71 ];
68 72
69 73 pub struct AppAssets;
@@ -8,7 +8,7 @@ macro_rules! symbols {
8 };
8 };
9}
9}
10
10
11const SYMBOLS: [(&str, &[u8]); 55] = symbols![
11const SYMBOLS: [(&str, &[u8]); 59] = symbols![
12 "bot",
12 "bot",
13 "done",
13 "done",
14 "table",
14 "table",
@@ -64,6 +64,10 @@ const SYMBOLS: [(&str, &[u8]); 55] = symbols![
64 "fluent_share",
64 "fluent_share",
65 "fluent_clock",
65 "fluent_clock",
66 "fluent_document_text",
66 "fluent_document_text",
67 "fluent_text_edit_style",
68 "fluent_text_edit_style_filled",
69 "fluent_table_add",
70 "fluent_table_dismiss",
67];
71];
68
72
69pub struct AppAssets;
73pub struct AppAssets;
crates/app-base/src/theme.rs +4 −0
@@ -216,6 +216,10 @@ pub fn border() -> Hsla {
216 216 color(scheme().border)
217 217 }
218 218
219 pub fn selection() -> Hsla {
220 color_with_alpha(scheme().selection)
221 }
222
219 223 pub fn border_strong() -> Hsla {
220 224 color(scheme().border_strong)
221 225 }
@@ -216,6 +216,10 @@ pub fn border() -> Hsla {
216 color(scheme().border)
216 color(scheme().border)
217}
217}
218
218
219pub fn selection() -> Hsla {
220 color_with_alpha(scheme().selection)
221}
222
219pub fn border_strong() -> Hsla {
223pub fn border_strong() -> Hsla {
220 color(scheme().border_strong)
224 color(scheme().border_strong)
221}
225}
crates/app/assets/symbols/fluent_table_add.svg added +1 −0
@@ -0,0 +1 @@
1 <svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M17 5.5A2.5 2.5 0 0 0 14.5 3h-9A2.5 2.5 0 0 0 3 5.5v9A2.5 2.5 0 0 0 5.5 17h4.1c-.16-.32-.3-.65-.4-1H8v-3h1.2c.1-.35.24-.68.4-1H8V8h4v1.6c.32-.16.65-.3 1-.4V8h3v1.2c.35.1.68.24 1 .4V5.5Zm-13 9V13h3v3H5.36A1.5 1.5 0 0 1 4 14.5ZM12 4v3H8V4h4Zm1 0H14.64A1.5 1.5 0 0 1 16 5.5V7h-3V4ZM7 4v3H4V5.36A1.5 1.5 0 0 1 5.5 4H7Zm0 4v4H4V8h3Zm12 6.5a4.5 4.5 0 1 1-9 0 4.5 4.5 0 0 1 9 0Zm-4-2a.5.5 0 0 0-1 0V14h-1.5a.5.5 0 0 0 0 1H14v1.5a.5.5 0 0 0 1 0V15h1.5a.5.5 0 0 0 0-1H15v-1.5Z"/></svg>
@@ -0,0 +1 @@
1<svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M17 5.5A2.5 2.5 0 0 0 14.5 3h-9A2.5 2.5 0 0 0 3 5.5v9A2.5 2.5 0 0 0 5.5 17h4.1c-.16-.32-.3-.65-.4-1H8v-3h1.2c.1-.35.24-.68.4-1H8V8h4v1.6c.32-.16.65-.3 1-.4V8h3v1.2c.35.1.68.24 1 .4V5.5Zm-13 9V13h3v3H5.36A1.5 1.5 0 0 1 4 14.5ZM12 4v3H8V4h4Zm1 0H14.64A1.5 1.5 0 0 1 16 5.5V7h-3V4ZM7 4v3H4V5.36A1.5 1.5 0 0 1 5.5 4H7Zm0 4v4H4V8h3Zm12 6.5a4.5 4.5 0 1 1-9 0 4.5 4.5 0 0 1 9 0Zm-4-2a.5.5 0 0 0-1 0V14h-1.5a.5.5 0 0 0 0 1H14v1.5a.5.5 0 0 0 1 0V15h1.5a.5.5 0 0 0 0-1H15v-1.5Z"/></svg>
crates/app/assets/symbols/fluent_table_dismiss.svg added +1 −0
@@ -0,0 +1 @@
1 <svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M17 5.5A2.5 2.5 0 0 0 14.5 3h-9A2.5 2.5 0 0 0 3 5.5v9A2.5 2.5 0 0 0 5.5 17h4.1c-.16-.32-.3-.65-.4-1H8v-3h1.2c.1-.35.24-.68.4-1H8V8h4v1.6c.32-.16.65-.3 1-.4V8h3v1.2c.35.1.68.24 1 .4V5.5Zm-13 9V13h3v3H5.36A1.5 1.5 0 0 1 4 14.5ZM12 4v3H8V4h4Zm1 0H14.64A1.5 1.5 0 0 1 16 5.5V7h-3V4ZM7 4v3H4V5.36A1.5 1.5 0 0 1 5.5 4H7Zm0 4v4H4V8h3Zm12 6.5a4.5 4.5 0 1 1-9 0 4.5 4.5 0 0 1 9 0Zm-3.8 0 1.15-1.15a.5.5 0 0 0-.7-.7l-1.15 1.14-1.15-1.14a.5.5 0 0 0-.7.7l1.14 1.15-1.14 1.15a.5.5 0 0 0 .7.7l1.15-1.14 1.15 1.14a.5.5 0 0 0 .7-.7l-1.14-1.15Z"/></svg>
@@ -0,0 +1 @@
1<svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M17 5.5A2.5 2.5 0 0 0 14.5 3h-9A2.5 2.5 0 0 0 3 5.5v9A2.5 2.5 0 0 0 5.5 17h4.1c-.16-.32-.3-.65-.4-1H8v-3h1.2c.1-.35.24-.68.4-1H8V8h4v1.6c.32-.16.65-.3 1-.4V8h3v1.2c.35.1.68.24 1 .4V5.5Zm-13 9V13h3v3H5.36A1.5 1.5 0 0 1 4 14.5ZM12 4v3H8V4h4Zm1 0H14.64A1.5 1.5 0 0 1 16 5.5V7h-3V4ZM7 4v3H4V5.36A1.5 1.5 0 0 1 5.5 4H7Zm0 4v4H4V8h3Zm12 6.5a4.5 4.5 0 1 1-9 0 4.5 4.5 0 0 1 9 0Zm-3.8 0 1.15-1.15a.5.5 0 0 0-.7-.7l-1.15 1.14-1.15-1.14a.5.5 0 0 0-.7.7l1.14 1.15-1.14 1.15a.5.5 0 0 0 .7.7l1.15-1.14 1.15 1.14a.5.5 0 0 0 .7-.7l-1.14-1.15Z"/></svg>
crates/app/assets/symbols/fluent_text_edit_style.svg added +1 −0
@@ -0,0 +1 @@
1 <svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M14.09 6.7a2.26 2.26 0 0 1 3.07-.12l.13.12.12.13c.73.85.73 2.1 0 2.96l-.12.13-6.87 6.9a1.5 1.5 0 0 1-.55.35l-.16.05-3.08.76a.5.5 0 0 1-.62-.53l.01-.08.8-3.07c.05-.21.15-.4.28-.57l.1-.12 6.89-6.9Zm2.5.7a1.26 1.26 0 0 0-1.7-.08l-.1.09-6.88 6.9a.5.5 0 0 0-.1.15l-.03.08-.58 2.27 2.27-.56a.5.5 0 0 0 .12-.05l.05-.03.07-.06 6.87-6.9c.5-.5.5-1.3 0-1.8ZM6.41 2.24l.05.08 2.98 7.63-.77.78L8 9H4l-1.03 2.68a.5.5 0 0 1-.56.3l-.1-.02a.5.5 0 0 1-.3-.56l.02-.08 3.5-9a.5.5 0 0 1 .9-.1ZM6 3.87 4.4 8H7.6l-1.6-4.13Z"/></svg>
@@ -0,0 +1 @@
1<svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M14.09 6.7a2.26 2.26 0 0 1 3.07-.12l.13.12.12.13c.73.85.73 2.1 0 2.96l-.12.13-6.87 6.9a1.5 1.5 0 0 1-.55.35l-.16.05-3.08.76a.5.5 0 0 1-.62-.53l.01-.08.8-3.07c.05-.21.15-.4.28-.57l.1-.12 6.89-6.9Zm2.5.7a1.26 1.26 0 0 0-1.7-.08l-.1.09-6.88 6.9a.5.5 0 0 0-.1.15l-.03.08-.58 2.27 2.27-.56a.5.5 0 0 0 .12-.05l.05-.03.07-.06 6.87-6.9c.5-.5.5-1.3 0-1.8ZM6.41 2.24l.05.08 2.98 7.63-.77.78L8 9H4l-1.03 2.68a.5.5 0 0 1-.56.3l-.1-.02a.5.5 0 0 1-.3-.56l.02-.08 3.5-9a.5.5 0 0 1 .9-.1ZM6 3.87 4.4 8H7.6l-1.6-4.13Z"/></svg>
crates/app/assets/symbols/fluent_text_edit_style_filled.svg added +1 −0
@@ -0,0 +1 @@
1 <svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M14.09 6.7a2.26 2.26 0 0 1 3.07-.12l.13.12.12.13c.73.85.73 2.1 0 2.96l-.12.13-6.87 6.9a1.5 1.5 0 0 1-.55.35l-.16.05-3.08.76a.5.5 0 0 1-.62-.53l.01-.08.8-3.07c.05-.21.15-.4.28-.57l.1-.12 6.89-6.9ZM6.42 2.23l.05.08 2.98 7.63-.77.78L8 9H4l-1.03 2.68a.5.5 0 0 1-.56.3l-.1-.02a.5.5 0 0 1-.3-.56l.02-.08 3.5-9a.5.5 0 0 1 .9-.1ZM6 3.87 4.4 8H7.6l-1.6-4.13Z"/></svg>
@@ -0,0 +1 @@
1<svg width="20" height="20" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M14.09 6.7a2.26 2.26 0 0 1 3.07-.12l.13.12.12.13c.73.85.73 2.1 0 2.96l-.12.13-6.87 6.9a1.5 1.5 0 0 1-.55.35l-.16.05-3.08.76a.5.5 0 0 1-.62-.53l.01-.08.8-3.07c.05-.21.15-.4.28-.57l.1-.12 6.89-6.9ZM6.42 2.23l.05.08 2.98 7.63-.77.78L8 9H4l-1.03 2.68a.5.5 0 0 1-.56.3l-.1-.02a.5.5 0 0 1-.3-.56l.02-.08 3.5-9a.5.5 0 0 1 .9-.1ZM6 3.87 4.4 8H7.6l-1.6-4.13Z"/></svg>
crates/app/src/layout_guard.rs +52 −1
@@ -176,7 +176,10 @@ mod frames {
176 176 use crate::user_activity::UserInput;
177 177 use crate::views::call_transcript::TranscriptPanel;
178 178 use crate::views::call_view::CHAT_PANEL_TAG;
179 use crate::views::composer::{COMPOSER_BOX_TAG, COMPOSER_LEADING_TAG, COMPOSER_SEND_TAG};
179 use crate::views::composer::{
180 COMPOSER_BOX_TAG, COMPOSER_FORMAT_BAR_TAG, COMPOSER_FORMAT_DIVIDER_TAG,
181 COMPOSER_FORMAT_TAG, COMPOSER_LEADING_TAG, COMPOSER_SEND_TAG,
182 };
180 183 use crate::views::conversation::MEMBERS_BUTTON;
181 184 use crate::views::message_row::BUBBLE_TAGS;
182 185 use crate::views::proposal_card::{PROPOSAL_BUTTONS, PROPOSAL_CARD, PROPOSAL_THUMBNAILS};
@@ -1316,6 +1319,54 @@ mod frames {
1316 1319 }
1317 1320 }
1318 1321
1322 #[gpui_kit::test]
1323 fn the_format_bar_divider_runs_edge_to_edge_inside_the_composer(cx: &mut TestAppContext) {
1324 const FORMAT_TAGS: [&str; 6] = [
1325 COMPOSER_BOX_TAG,
1326 COMPOSER_FORMAT_BAR_TAG,
1327 COMPOSER_FORMAT_DIVIDER_TAG,
1328 COMPOSER_LEADING_TAG,
1329 COMPOSER_FORMAT_TAG,
1330 COMPOSER_SEND_TAG,
1331 ];
1332 let (handle, _) = mount(cx);
1333 let mut visual = VisualTestContext::from_window(handle, cx);
1334 for (width, height) in SIZES {
1335 visual.simulate_resize(size(px(width), px(height)));
1336 let window = Bounds {
1337 origin: point(px(0.), px(0.)),
1338 size: size(px(width), px(height)),
1339 };
1340 collect(&mut visual, Some(COMPOSER_FORMAT_TAG));
1341 let toggle = visual
1342 .debug_bounds(COMPOSER_FORMAT_TAG)
1343 .expect("format button");
1344 visual.simulate_click(toggle.center(), Default::default());
1345 collect(&mut visual, Some(COMPOSER_FORMAT_DIVIDER_TAG));
1346 let tags: Vec<Tag> = FORMAT_TAGS
1347 .iter()
1348 .filter_map(|path| visual.debug_bounds(path).map(|bounds| (*path, bounds)))
1349 .collect();
1350 let found: Vec<String> = violations(&tags, window, &FORMAT_TAGS)
1351 .into_iter()
1352 .map(|(key, message)| format!("format@{width}x{height}:{key}: {message}"))
1353 .collect();
1354 let bar = visual.debug_bounds(COMPOSER_FORMAT_BAR_TAG).expect("bar");
1355 let leading = visual.debug_bounds(COMPOSER_LEADING_TAG).expect("leading");
1356 let toggle = visual
1357 .debug_bounds(COMPOSER_FORMAT_TAG)
1358 .expect("format button");
1359 visual.simulate_click(toggle.center(), Default::default());
1360 collect(&mut visual, None);
1361 assert!(found.is_empty(), "{}", found.join("\n"));
1362 assert!(
1363 bar.bottom() <= leading.top(),
1364 "bar {bar:?} over the buttons {leading:?}"
1365 );
1366 assert!(visual.debug_bounds(COMPOSER_FORMAT_BAR_TAG).is_none());
1367 }
1368 }
1369
1319 1370 #[gpui_kit::test]
1320 1371 fn agent_proposals_stay_inside_the_main_island(cx: &mut TestAppContext) {
1321 1372 let (handle, state) = mount_with(cx, true);
@@ -176,7 +176,10 @@ mod frames {
176 use crate::user_activity::UserInput;
176 use crate::user_activity::UserInput;
177 use crate::views::call_transcript::TranscriptPanel;
177 use crate::views::call_transcript::TranscriptPanel;
178 use crate::views::call_view::CHAT_PANEL_TAG;
178 use crate::views::call_view::CHAT_PANEL_TAG;
179 use crate::views::composer::{COMPOSER_BOX_TAG, COMPOSER_LEADING_TAG, COMPOSER_SEND_TAG};
179 use crate::views::composer::{
180 COMPOSER_BOX_TAG, COMPOSER_FORMAT_BAR_TAG, COMPOSER_FORMAT_DIVIDER_TAG,
181 COMPOSER_FORMAT_TAG, COMPOSER_LEADING_TAG, COMPOSER_SEND_TAG,
182 };
180 use crate::views::conversation::MEMBERS_BUTTON;
183 use crate::views::conversation::MEMBERS_BUTTON;
181 use crate::views::message_row::BUBBLE_TAGS;
184 use crate::views::message_row::BUBBLE_TAGS;
182 use crate::views::proposal_card::{PROPOSAL_BUTTONS, PROPOSAL_CARD, PROPOSAL_THUMBNAILS};
185 use crate::views::proposal_card::{PROPOSAL_BUTTONS, PROPOSAL_CARD, PROPOSAL_THUMBNAILS};
@@ -1316,6 +1319,54 @@ mod frames {
1316 }
1319 }
1317 }
1320 }
1318
1321
1322 #[gpui_kit::test]
1323 fn the_format_bar_divider_runs_edge_to_edge_inside_the_composer(cx: &mut TestAppContext) {
1324 const FORMAT_TAGS: [&str; 6] = [
1325 COMPOSER_BOX_TAG,
1326 COMPOSER_FORMAT_BAR_TAG,
1327 COMPOSER_FORMAT_DIVIDER_TAG,
1328 COMPOSER_LEADING_TAG,
1329 COMPOSER_FORMAT_TAG,
1330 COMPOSER_SEND_TAG,
1331 ];
1332 let (handle, _) = mount(cx);
1333 let mut visual = VisualTestContext::from_window(handle, cx);
1334 for (width, height) in SIZES {
1335 visual.simulate_resize(size(px(width), px(height)));
1336 let window = Bounds {
1337 origin: point(px(0.), px(0.)),
1338 size: size(px(width), px(height)),
1339 };
1340 collect(&mut visual, Some(COMPOSER_FORMAT_TAG));
1341 let toggle = visual
1342 .debug_bounds(COMPOSER_FORMAT_TAG)
1343 .expect("format button");
1344 visual.simulate_click(toggle.center(), Default::default());
1345 collect(&mut visual, Some(COMPOSER_FORMAT_DIVIDER_TAG));
1346 let tags: Vec<Tag> = FORMAT_TAGS
1347 .iter()
1348 .filter_map(|path| visual.debug_bounds(path).map(|bounds| (*path, bounds)))
1349 .collect();
1350 let found: Vec<String> = violations(&tags, window, &FORMAT_TAGS)
1351 .into_iter()
1352 .map(|(key, message)| format!("format@{width}x{height}:{key}: {message}"))
1353 .collect();
1354 let bar = visual.debug_bounds(COMPOSER_FORMAT_BAR_TAG).expect("bar");
1355 let leading = visual.debug_bounds(COMPOSER_LEADING_TAG).expect("leading");
1356 let toggle = visual
1357 .debug_bounds(COMPOSER_FORMAT_TAG)
1358 .expect("format button");
1359 visual.simulate_click(toggle.center(), Default::default());
1360 collect(&mut visual, None);
1361 assert!(found.is_empty(), "{}", found.join("\n"));
1362 assert!(
1363 bar.bottom() <= leading.top(),
1364 "bar {bar:?} over the buttons {leading:?}"
1365 );
1366 assert!(visual.debug_bounds(COMPOSER_FORMAT_BAR_TAG).is_none());
1367 }
1368 }
1369
1319 #[gpui_kit::test]
1370 #[gpui_kit::test]
1320 fn agent_proposals_stay_inside_the_main_island(cx: &mut TestAppContext) {
1371 fn agent_proposals_stay_inside_the_main_island(cx: &mut TestAppContext) {
1321 let (handle, state) = mount_with(cx, true);
1372 let (handle, state) = mount_with(cx, true);
crates/app/src/views/actions.rs +4 −1
@@ -21,7 +21,10 @@ actions!(
21 21 InsertTableColumnRight,
22 22 DeleteTableRow,
23 23 DeleteTableColumn,
24 DeleteTable
24 DeleteTable,
25 ToggleFormatBar,
26 InsertTableLine,
27 RemoveTableLine
25 28 ]
26 29 );
27 30
@@ -21,7 +21,10 @@ actions!(
21 InsertTableColumnRight,
21 InsertTableColumnRight,
22 DeleteTableRow,
22 DeleteTableRow,
23 DeleteTableColumn,
23 DeleteTableColumn,
24 DeleteTable
24 DeleteTable,
25 ToggleFormatBar,
26 InsertTableLine,
27 RemoveTableLine
25 ]
28 ]
26);
29);
27
30
crates/app/src/views/composer.rs +371 −31
@@ -35,9 +35,10 @@ mod table;
35 35
36 36 use super::actions::{
37 37 COMPOSER_KEY_CONTEXT, DeleteTable, DeleteTableColumn, DeleteTableRow, EditLink,
38 InsertTableColumnLeft, InsertTableColumnRight, InsertTableRowAbove, InsertTableRowBelow,
39 OpenSwitcher, PastePlainText, ScheduleSend, ToggleBold, ToggleCode, ToggleItalic, ToggleStrike,
40 ToggleSubscript, ToggleSuperscript, ToggleUnderline,
38 InsertTableColumnLeft, InsertTableColumnRight, InsertTableLine, InsertTableRowAbove,
39 InsertTableRowBelow, OpenSwitcher, PastePlainText, RemoveTableLine, ScheduleSend, ToggleBold,
40 ToggleCode, ToggleFormatBar, ToggleItalic, ToggleStrike, ToggleSubscript, ToggleSuperscript,
41 ToggleUnderline,
41 42 };
42 43 use super::attachment_tray::{
43 44 AttachmentTray, DoneFile, JobKind, LoadedFile, MAX_ATTACHMENTS, PasteAction, UploadJob,
@@ -47,7 +48,7 @@ use super::attachment_tray::{
47 48 use super::avatar::{person_avatar, square_avatar};
48 49 use super::draft_style::draft_style;
49 50 use super::emoji_popup::{self, EmojiPopup};
50 use super::format_toolbar::{self, FormatButton};
51 use super::format_toolbar::{self, ButtonState, FormatButton};
51 52 use super::fun_picker::{FunPicker, FunPickerEvent};
52 53 use super::link_preview::{ComposeLink, draft_link, link_preview_card, meeting_join_chip};
53 54 use super::widgets::{icon, symbol};
@@ -59,7 +60,7 @@ use crate::runtime;
59 60 use crate::scheduled_rows::scheduled_label;
60 61 use crate::theme::{self, ElevatedSurface as _};
61 62 use crate::typing::OutgoingTyping;
62 use table::{TableKey, TablePicker, TableViews, is_table_token};
63 use table::{TableKey, TableLineMenu, TableMenu, TablePicker, TableViews, is_table_token};
63 64
64 65 const MIN_ROWS: usize = 1;
65 66 const MAX_ROWS: usize = 8;
@@ -77,6 +78,9 @@ const COMPOSER_BUTTON_INSET: f32 = 6.;
77 78 pub const COMPOSER_BOX_TAG: &str = "composer";
78 79 pub const COMPOSER_LEADING_TAG: &str = "composer/leading";
79 80 pub const COMPOSER_SEND_TAG: &str = "composer/send";
81 pub const COMPOSER_FORMAT_TAG: &str = "composer/leading/format";
82 pub const COMPOSER_FORMAT_BAR_TAG: &str = "composer/format-bar";
83 pub const COMPOSER_FORMAT_DIVIDER_TAG: &str = "composer/format-bar/divider";
80 84 /// gpui-kit draws a `Size::Size` button's icon at this fraction of the button size.
81 85 const BUTTON_ICON_SCALE: f32 = 0.75;
82 86 const DEMO_UPLOAD_STEP: Duration = Duration::from_millis(120);
@@ -216,6 +220,8 @@ pub struct Composer {
216 220 tables: TableViews,
217 221 table_counter: usize,
218 222 table_picker: Option<TablePicker>,
223 table_menu: Option<TableMenu>,
224 format_bar: HashSet<String>,
219 225 active_cell: Option<(String, teams_core::Cell)>,
220 226 caret_before_table: usize,
221 227 caret_text: String,
@@ -425,6 +431,8 @@ impl Composer {
425 431 tables: TableViews::default(),
426 432 table_counter: 0,
427 433 table_picker: None,
434 table_menu: None,
435 format_bar: HashSet::new(),
428 436 active_cell: None,
429 437 caret_before_table: 0,
430 438 caret_text: String::new(),
@@ -948,14 +956,62 @@ impl Composer {
948 956 FormatButton::Numbered => self.toggle_lines(LineKind::Numbered(0), window, cx),
949 957 FormatButton::Quote => self.toggle_lines(LineKind::Quote, window, cx),
950 958 FormatButton::Table => {
959 self.table_menu = None;
951 960 self.toolbar_dismissed = Some(self.selection(cx));
952 961 self.open_table_picker(cx);
953 962 }
963 FormatButton::TableInsertLine => {
964 self.open_table_menu(TableLineMenu::Insert, window, cx);
965 }
966 FormatButton::TableRemoveLine => {
967 self.open_table_menu(TableLineMenu::Remove, window, cx);
968 }
969 }
970 }
971
972 fn typing_state(&self, cursor: usize, kind: &MarkKind) -> FormatState {
973 let style = self.draft.style_at(cursor);
974 let style = match self.pending_style.as_ref() {
975 Some(pending) if pending.at == cursor => pending.apply(style),
976 _ => style,
977 };
978 if style.iter().any(|active| active.same_format(kind)) {
979 FormatState::On
980 } else {
981 FormatState::Off
954 982 }
955 983 }
956 984
957 985 fn format_state(&self, button: FormatButton, selection: &Range<usize>) -> FormatState {
958 986 let range = selection.clone();
987 let mark = match button {
988 FormatButton::Bold => Some(MarkKind::Bold),
989 FormatButton::Italic => Some(MarkKind::Italic),
990 FormatButton::Underline => Some(MarkKind::Underline),
991 FormatButton::Strike => Some(MarkKind::Strike),
992 FormatButton::Superscript => Some(MarkKind::Superscript),
993 FormatButton::Subscript => Some(MarkKind::Subscript),
994 FormatButton::SizeSmall => Some(MarkKind::Size(SizeStep::Small)),
995 FormatButton::SizeLarge => Some(MarkKind::Size(SizeStep::Large)),
996 FormatButton::Code => Some(MarkKind::Code),
997 FormatButton::Link => Some(MarkKind::Link(String::new())),
998 _ => None,
999 };
1000 if range.is_empty() {
1001 if let Some(kind) = &mark {
1002 return self.typing_state(range.start, kind);
1003 }
1004 if button == FormatButton::SizeNormal {
1005 let sized = [SizeStep::Small, SizeStep::Large].into_iter().any(|step| {
1006 self.typing_state(range.start, &MarkKind::Size(step)) == FormatState::On
1007 });
1008 return if sized {
1009 FormatState::Off
1010 } else {
1011 FormatState::On
1012 };
1013 }
1014 }
959 1015 match button {
960 1016 FormatButton::Bold => self.draft.state(range, &MarkKind::Bold),
961 1017 FormatButton::Italic => self.draft.state(range, &MarkKind::Italic),
@@ -972,7 +1028,67 @@ impl Composer {
972 1028 FormatButton::Numbered => self.draft.line_state(range, &LineKind::Numbered(0)),
973 1029 FormatButton::Quote => self.draft.line_state(range, &LineKind::Quote),
974 1030 FormatButton::Table if self.table_picker.is_some() => FormatState::On,
975 FormatButton::Table => FormatState::Off,
1031 FormatButton::TableInsertLine
1032 if self
1033 .table_menu
1034 .is_some_and(|menu| menu.kind == TableLineMenu::Insert) =>
1035 {
1036 FormatState::On
1037 }
1038 FormatButton::TableRemoveLine
1039 if self
1040 .table_menu
1041 .is_some_and(|menu| menu.kind == TableLineMenu::Remove) =>
1042 {
1043 FormatState::On
1044 }
1045 FormatButton::Table | FormatButton::TableInsertLine | FormatButton::TableRemoveLine => {
1046 FormatState::Off
1047 }
1048 }
1049 }
1050
1051 /// Text formats do not apply inside a table cell; the row and column menus only there.
1052 fn button_state(
1053 &self,
1054 button: FormatButton,
1055 selection: &Range<usize>,
1056 in_table: bool,
1057 ) -> ButtonState {
1058 let enabled = match button {
1059 FormatButton::TableInsertLine | FormatButton::TableRemoveLine => in_table,
1060 FormatButton::Table => true,
1061 _ => !in_table,
1062 };
1063 if enabled {
1064 ButtonState::enabled(self.format_state(button, selection))
1065 } else {
1066 ButtonState::disabled()
1067 }
1068 }
1069
1070 pub(super) fn format_bar_open(&self) -> bool {
1071 self.format_bar
1072 .contains(self.conversation_id.as_deref().unwrap_or_default())
1073 }
1074
1075 pub(super) fn set_format_bar(&mut self, open: bool, cx: &mut Context<Self>) {
1076 let key = self.conversation_id.clone().unwrap_or_default();
1077 if open {
1078 self.format_bar.insert(key);
1079 } else {
1080 self.format_bar.remove(&key);
1081 self.table_picker = None;
1082 self.table_menu = None;
1083 }
1084 cx.notify();
1085 }
1086
1087 fn toggle_format_bar(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1088 let open = !self.format_bar_open();
1089 self.set_format_bar(open, cx);
1090 if !self.cell_has_focus(window, cx) {
1091 self.focus(window, cx);
976 1092 }
977 1093 }
978 1094
@@ -1519,6 +1635,8 @@ impl Composer {
1519 1635 self.mention_inputs.clear();
1520 1636 self.close_popup();
1521 1637 self.schedule = None;
1638 self.table_picker = None;
1639 self.table_menu = None;
1522 1640 if !carry {
1523 1641 self.load_stored_draft(conversation_id, window, cx);
1524 1642 }
@@ -2308,7 +2426,8 @@ impl Composer {
2308 2426
2309 2427 fn render_toolbar(&self, focused: bool, cx: &mut Context<Self>) -> Option<AnyElement> {
2310 2428 let composer = cx.entity().downgrade();
2311 if self.link_editor.is_none() && !(focused && self.toolbar_requested(cx)) {
2429 let floating = focused && self.toolbar_requested(cx) && !self.format_bar_open();
2430 if self.link_editor.is_none() && !floating {
2312 2431 return None;
2313 2432 }
2314 2433 let state = self.input.read(cx);
@@ -2349,7 +2468,7 @@ impl Composer {
2349 2468 },
2350 2469 ),
2351 2470 None => {
2352 let state_of = |button| self.format_state(button, &range);
2471 let state_of = |button| ButtonState::enabled(self.format_state(button, &range));
2353 2472 format_toolbar::render(
2354 2473 placement,
2355 2474 format_toolbar::Mode::Bar {
@@ -2366,6 +2485,37 @@ impl Composer {
2366 2485 Some(element)
2367 2486 }
2368 2487
2488 fn render_format_bar(&self, window: &Window, cx: &mut Context<Self>) -> AnyElement {
2489 let selection = self.selection(cx);
2490 let in_table = self.in_table(window, cx);
2491 let state_of = |button| self.button_state(button, &selection, in_table);
2492 let composer = cx.weak_entity();
2493 let on_press: format_toolbar::PressHandler = Rc::new(move |button, window, cx| {
2494 composer
2495 .update(cx, |this, cx| this.press_format(button, window, cx))
2496 .ok();
2497 });
2498 let mut overlay = |button| match button {
2499 FormatButton::Table => self.render_table_picker(cx),
2500 FormatButton::TableInsertLine => self.render_table_menu(TableLineMenu::Insert, cx),
2501 FormatButton::TableRemoveLine => self.render_table_menu(TableLineMenu::Remove, cx),
2502 _ => None,
2503 };
2504 let row = format_toolbar::render_format_bar(&state_of, on_press, &mut overlay);
2505 v_flex()
2506 .debug_selector(|| COMPOSER_FORMAT_BAR_TAG.into())
2507 .w_full()
2508 .child(row.w_full().p(px(COMPOSER_BUTTON_INSET)))
2509 .child(
2510 div()
2511 .debug_selector(|| COMPOSER_FORMAT_DIVIDER_TAG.into())
2512 .w_full()
2513 .h(px(1.))
2514 .bg(theme::border()),
2515 )
2516 .into_any_element()
2517 }
2518
2369 2519 fn render_paste_hint(&self) -> Option<Div> {
2370 2520 self.paste_hint.as_ref()?;
2371 2521 Some(
@@ -2579,6 +2729,37 @@ impl Render for Composer {
2579 2729 ))
2580 2730 .on_click(cx.listener(|this, _, window, cx| this.open_picker(window, cx)))
2581 2731 });
2732 let format_open = self.format_bar_open();
2733 let format_toggle = div()
2734 .id("composer-format")
2735 .debug_selector(|| COMPOSER_FORMAT_TAG.into())
2736 .size(px(COMPOSER_BUTTON_SIZE))
2737 .flex_none()
2738 .flex()
2739 .items_center()
2740 .justify_center()
2741 .rounded(px(8.))
2742 .cursor_pointer()
2743 .when(format_open, |button| button.bg(theme::popup_hover()))
2744 .hover(|button| button.bg(theme::popup_hover()))
2745 .tooltip(move |window, cx| {
2746 Tooltip::new(format_toolbar::toggle_tooltip(format_open)).build(window, cx)
2747 })
2748 .child(symbol(
2749 format_toolbar::toggle_symbol(format_open),
2750 COMPOSER_ICON_SIZE,
2751 if format_open {
2752 theme::accent_soft()
2753 } else {
2754 theme::text_muted()
2755 },
2756 ))
2757 .on_mouse_down(MouseButton::Left, |_, window, cx| {
2758 window.prevent_default();
2759 cx.stop_propagation();
2760 })
2761 .on_click(cx.listener(|this, _, window, cx| this.toggle_format_bar(window, cx)));
2762 let format_bar = format_open.then(|| self.render_format_bar(window, cx));
2582 2763 let composer = cx.weak_entity();
2583 2764 let tokens_composer = composer.clone();
2584 2765 let input = Textarea::new(&self.input)
@@ -2594,8 +2775,11 @@ impl Render for Composer {
2594 2775 if context.is_block() && is_table_token(context.token()) {
2595 2776 let token_id = context.token().id().to_string();
2596 2777 let width = context.available_width();
2778 let selected = context.is_selected();
2597 2779 if let Some(table) = tokens_composer
2598 .read_with(cx, |this, _| this.render_table_token(&token_id, width))
2780 .read_with(cx, |this, _| {
2781 this.render_table_token(&token_id, width, selected)
2782 })
2599 2783 .ok()
2600 2784 .flatten()
2601 2785 {
@@ -2740,6 +2924,15 @@ impl Render for Composer {
2740 2924 this.edit_table(TableEdit::DeleteTable, window, cx)
2741 2925 }))
2742 2926 .on_action(cx.listener(|this, _: &ScheduleSend, _, cx| this.open_schedule_picker(cx)))
2927 .on_action(cx.listener(|this, _: &ToggleFormatBar, window, cx| {
2928 this.toggle_format_bar(window, cx)
2929 }))
2930 .on_action(cx.listener(|this, _: &InsertTableLine, window, cx| {
2931 this.open_table_menu(TableLineMenu::Insert, window, cx);
2932 }))
2933 .on_action(cx.listener(|this, _: &RemoveTableLine, window, cx| {
2934 this.open_table_menu(TableLineMenu::Remove, window, cx);
2935 }))
2743 2936 .capture_action(cx.listener(|this, _: &Undo, window, cx| {
2744 2937 if this.link_editor.is_none() {
2745 2938 this.step_history(true, window, cx);
@@ -2771,7 +2964,11 @@ impl Render for Composer {
2771 2964 }
2772 2965 }))
2773 2966 .capture_action(cx.listener(|this, _: &MoveUp, window, cx| {
2774 if this.cell_has_focus(window, cx) {
2967 if let Some(menu) = this.table_menu.as_mut() {
2968 menu.step(-1);
2969 cx.notify();
2970 cx.stop_propagation();
2971 } else if this.cell_has_focus(window, cx) {
2775 2972 if this.table_key(TableKey::Up, window, cx) {
2776 2973 cx.stop_propagation();
2777 2974 }
@@ -2788,7 +2985,11 @@ impl Render for Composer {
2788 2985 }
2789 2986 }))
2790 2987 .capture_action(cx.listener(|this, _: &MoveDown, window, cx| {
2791 if this.cell_has_focus(window, cx) {
2988 if let Some(menu) = this.table_menu.as_mut() {
2989 menu.step(1);
2990 cx.notify();
2991 cx.stop_propagation();
2992 } else if this.cell_has_focus(window, cx) {
2792 2993 if this.table_key(TableKey::Down, window, cx) {
2793 2994 cx.stop_propagation();
2794 2995 }
@@ -2812,7 +3013,10 @@ impl Render for Composer {
2812 3013 }
2813 3014 }))
2814 3015 .capture_action(cx.listener(|this, action: &Enter, window, cx| {
2815 if this.cell_has_focus(window, cx) {
3016 if this.table_menu.is_some() {
3017 this.accept_table_menu(None, window, cx);
3018 cx.stop_propagation();
3019 } else if this.cell_has_focus(window, cx) {
2816 3020 } else if this.link_editor.is_some() {
2817 3021 this.apply_link(window, cx);
2818 3022 cx.stop_propagation();
@@ -2864,7 +3068,7 @@ impl Render for Composer {
2864 3068 }
2865 3069 }))
2866 3070 .capture_action(cx.listener(|this, _: &Escape, window, cx| {
2867 if this.table_picker.take().is_some() {
3071 if this.table_picker.take().is_some() || this.table_menu.take().is_some() {
2868 3072 cx.notify();
2869 3073 cx.stop_propagation();
2870 3074 return;
@@ -2879,7 +3083,7 @@ impl Render for Composer {
2879 3083 cx.stop_propagation();
2880 3084 return;
2881 3085 }
2882 if this.toolbar_requested(cx) {
3086 if this.toolbar_requested(cx) && !this.format_bar_open() {
2883 3087 this.toolbar_dismissed = Some(this.selection(cx));
2884 3088 cx.notify();
2885 3089 cx.stop_propagation();
@@ -2910,7 +3114,6 @@ impl Render for Composer {
2910 3114 .children(self.render_emoji_popup(window, cx))
2911 3115 .children(self.render_toolbar(focused, cx))
2912 3116 .children(self.render_schedule(cx))
2913 .children(self.render_table_picker(cx))
2914 3117 .child(
2915 3118 v_flex()
2916 3119 .debug_selector(|| COMPOSER_BOX_TAG.into())
@@ -2932,6 +3135,7 @@ impl Render for Composer {
2932 3135 .absolute()
2933 3136 .size_full(),
2934 3137 )
3138 .children(format_bar)
2935 3139 .children(tray)
2936 3140 .child(
2937 3141 h_flex()
@@ -2944,22 +3148,16 @@ impl Render for Composer {
2944 3148 }
2945 3149 })
2946 3150 .py(px(COMPOSER_BUTTON_INSET))
2947 .pl(px(if can_attach {
2948 COMPOSER_BUTTON_INSET
2949 } else {
2950 12.
2951 }))
2952 .pr(px(COMPOSER_BUTTON_INSET))
2953 .when(can_attach, |row| {
2954 row.child(
2955 h_flex()
2956 .debug_selector(|| COMPOSER_LEADING_TAG.into())
2957 .flex_none()
2958 .gap(px(COMPOSER_BUTTON_INSET))
2959 .children(fun)
2960 .children(attach),
2961 )
2962 })
3151 .px(px(COMPOSER_BUTTON_INSET))
3152 .child(
3153 h_flex()
3154 .debug_selector(|| COMPOSER_LEADING_TAG.into())
3155 .flex_none()
3156 .gap(px(COMPOSER_BUTTON_INSET))
3157 .child(format_toggle)
3158 .children(fun)
3159 .children(attach),
3160 )
2963 3161 .child(
2964 3162 div()
2965 3163 .flex_1()
@@ -4270,6 +4468,148 @@ mod tests {
4270 4468 );
4271 4469 }
4272 4470
4471 fn format_bar_open(typist: &mut Typist<'_>) -> bool {
4472 let composer = typist.composer.clone();
4473 typist.cx.update(|cx| composer.read(cx).format_bar_open())
4474 }
4475
4476 #[gpui_kit::test]
4477 fn ctrl_shift_x_opens_the_format_bar_per_conversation(cx: &mut TestAppContext) {
4478 let mut typist = typist(cx);
4479 let composer = typist.composer.clone();
4480 let open_chat = |typist: &mut Typist<'_>, id: &'static str| {
4481 let composer = composer.clone();
4482 typist.act(|window, cx| {
4483 composer.update(cx, |composer, cx| {
4484 composer.set_conversation(id, "", window, cx);
4485 composer.focus(window, cx);
4486 })
4487 });
4488 };
4489 open_chat(&mut typist, "chat-one");
4490 assert!(!format_bar_open(&mut typist));
4491 typist.press("ctrl-shift-x");
4492 assert!(format_bar_open(&mut typist));
4493 open_chat(&mut typist, "chat-two");
4494 assert!(!format_bar_open(&mut typist));
4495 open_chat(&mut typist, "chat-one");
4496 assert!(format_bar_open(&mut typist));
4497 typist.press("ctrl-shift-x");
4498 assert!(!format_bar_open(&mut typist));
4499 }
4500
4501 #[gpui_kit::test]
4502 fn ctrl_alt_x_strikes_through_now_that_ctrl_shift_x_is_format(cx: &mut TestAppContext) {
4503 let mut typist = typist(cx);
4504 typist.type_text("gone");
4505 typist.press("ctrl-a ctrl-alt-x");
4506 assert_eq!(typist.outgoing().html(), "<s>gone</s>");
4507 assert!(!format_bar_open(&mut typist));
4508 }
4509
4510 #[gpui_kit::test]
4511 fn the_format_bar_shows_the_style_at_the_caret_without_a_selection(
4512 cx: &mut TestAppContext,
4513 ) {
4514 use teams_core::FormatState;
4515
4516 use super::super::FormatButton;
4517
4518 let mut typist = typist(cx);
4519 typist.press("ctrl-shift-x ctrl-b");
4520 let composer = typist.composer.clone();
4521 let bold = |typist: &mut Typist<'_>| {
4522 let composer = composer.clone();
4523 typist.cx.update(|cx| {
4524 let composer = composer.read(cx);
4525 let caret = composer.selection(cx);
4526 composer.button_state(FormatButton::Bold, &caret, false)
4527 })
4528 };
4529 assert_eq!(bold(&mut typist).format, FormatState::On);
4530 typist.type_text("x");
4531 assert_eq!(bold(&mut typist).format, FormatState::On);
4532 typist.press("ctrl-b");
4533 assert_eq!(bold(&mut typist).format, FormatState::Off);
4534 assert!(bold(&mut typist).enabled);
4535 }
4536
4537 #[gpui_kit::test]
4538 fn the_format_bar_inserts_a_table_without_a_selection(cx: &mut TestAppContext) {
4539 use super::super::FormatButton;
4540
4541 let mut typist = typist(cx);
4542 typist.press("ctrl-shift-x");
4543 let composer = typist.composer.clone();
4544 typist.act(|window, cx| {
4545 composer.update(cx, |composer, cx| {
4546 composer.press_format(FormatButton::Table, window, cx)
4547 })
4548 });
4549 let picker_open = typist
4550 .cx
4551 .update(|cx| composer.read(cx).table_picker.is_some());
4552 assert!(picker_open);
4553 insert_table(&mut typist, 2, 2);
4554 assert!(typist.draft().lines()[0].is_table());
4555 }
4556
4557 #[gpui_kit::test]
4558 fn ctrl_alt_plus_and_minus_open_the_teams_row_and_column_menus(cx: &mut TestAppContext) {
4559 use super::super::table::TableLineMenu;
4560
4561 let mut typist = typist(cx);
4562 let composer = typist.composer.clone();
4563 let menu = |typist: &mut Typist<'_>| {
4564 let composer = composer.clone();
4565 typist
4566 .cx
4567 .update(|cx| composer.read(cx).table_menu.map(|menu| menu.kind))
4568 };
4569 typist.press("ctrl-alt-=");
4570 assert_eq!(menu(&mut typist), None);
4571 insert_table(&mut typist, 1, 1);
4572 typist.type_text("a");
4573 typist.press("ctrl-alt-=");
4574 assert_eq!(menu(&mut typist), Some(TableLineMenu::Insert));
4575 assert!(format_bar_open(&mut typist));
4576 typist.press("enter");
4577 assert_eq!(menu(&mut typist), None);
4578 let shape = |typist: &mut Typist<'_>| {
4579 let draft = typist.draft();
4580 let table = draft.table(0).expect("table").clone();
4581 (table.row_count(), table.column_count())
4582 };
4583 assert_eq!(shape(&mut typist), (1, 2));
4584 typist.press("ctrl-alt-- enter");
4585 assert_eq!(shape(&mut typist), (1, 1));
4586 typist.press("ctrl-alt-- escape");
4587 assert_eq!(menu(&mut typist), None);
4588 assert_eq!(shape(&mut typist), (1, 1));
4589 }
4590
4591 #[gpui_kit::test]
4592 fn ctrl_a_highlights_a_table_like_selected_text(cx: &mut TestAppContext) {
4593 use gpui_kit::VisualTestContext;
4594
4595 use super::super::table::SELECTED_TABLE_TAG;
4596
4597 let mut typist = typist(cx);
4598 typist.type_text("before");
4599 insert_table(&mut typist, 1, 2);
4600 typist.type_text("a");
4601 typist.press("down");
4602 typist.type_text("after");
4603 let handle = typist.handle;
4604 let mut visual = VisualTestContext::from_window(handle, typist.cx);
4605 visual.update(|window, cx| window.render_frame(cx));
4606 assert!(visual.debug_bounds(SELECTED_TABLE_TAG).is_none());
4607 visual.simulate_keystrokes("ctrl-a");
4608 visual.run_until_parked();
4609 visual.update(|window, cx| window.render_frame(cx));
4610 assert!(visual.debug_bounds(SELECTED_TABLE_TAG).is_some());
4611 }
4612
4273 4613 #[gpui_kit::test]
4274 4614 fn deleting_the_last_column_deletes_the_table(cx: &mut TestAppContext) {
4275 4615 let mut typist = typist(cx);
@@ -35,9 +35,10 @@ mod table;
35
35
36use super::actions::{
36use super::actions::{
37 COMPOSER_KEY_CONTEXT, DeleteTable, DeleteTableColumn, DeleteTableRow, EditLink,
37 COMPOSER_KEY_CONTEXT, DeleteTable, DeleteTableColumn, DeleteTableRow, EditLink,
38 InsertTableColumnLeft, InsertTableColumnRight, InsertTableRowAbove, InsertTableRowBelow,
38 InsertTableColumnLeft, InsertTableColumnRight, InsertTableLine, InsertTableRowAbove,
39 OpenSwitcher, PastePlainText, ScheduleSend, ToggleBold, ToggleCode, ToggleItalic, ToggleStrike,
39 InsertTableRowBelow, OpenSwitcher, PastePlainText, RemoveTableLine, ScheduleSend, ToggleBold,
40 ToggleSubscript, ToggleSuperscript, ToggleUnderline,
40 ToggleCode, ToggleFormatBar, ToggleItalic, ToggleStrike, ToggleSubscript, ToggleSuperscript,
41 ToggleUnderline,
41};
42};
42use super::attachment_tray::{
43use super::attachment_tray::{
43 AttachmentTray, DoneFile, JobKind, LoadedFile, MAX_ATTACHMENTS, PasteAction, UploadJob,
44 AttachmentTray, DoneFile, JobKind, LoadedFile, MAX_ATTACHMENTS, PasteAction, UploadJob,
@@ -47,7 +48,7 @@ use super::attachment_tray::{
47use super::avatar::{person_avatar, square_avatar};
48use super::avatar::{person_avatar, square_avatar};
48use super::draft_style::draft_style;
49use super::draft_style::draft_style;
49use super::emoji_popup::{self, EmojiPopup};
50use super::emoji_popup::{self, EmojiPopup};
50use super::format_toolbar::{self, FormatButton};
51use super::format_toolbar::{self, ButtonState, FormatButton};
51use super::fun_picker::{FunPicker, FunPickerEvent};
52use super::fun_picker::{FunPicker, FunPickerEvent};
52use super::link_preview::{ComposeLink, draft_link, link_preview_card, meeting_join_chip};
53use super::link_preview::{ComposeLink, draft_link, link_preview_card, meeting_join_chip};
53use super::widgets::{icon, symbol};
54use super::widgets::{icon, symbol};
@@ -59,7 +60,7 @@ use crate::runtime;
59use crate::scheduled_rows::scheduled_label;
60use crate::scheduled_rows::scheduled_label;
60use crate::theme::{self, ElevatedSurface as _};
61use crate::theme::{self, ElevatedSurface as _};
61use crate::typing::OutgoingTyping;
62use crate::typing::OutgoingTyping;
62use table::{TableKey, TablePicker, TableViews, is_table_token};
63use table::{TableKey, TableLineMenu, TableMenu, TablePicker, TableViews, is_table_token};
63
64
64const MIN_ROWS: usize = 1;
65const MIN_ROWS: usize = 1;
65const MAX_ROWS: usize = 8;
66const MAX_ROWS: usize = 8;
@@ -77,6 +78,9 @@ const COMPOSER_BUTTON_INSET: f32 = 6.;
77pub const COMPOSER_BOX_TAG: &str = "composer";
78pub const COMPOSER_BOX_TAG: &str = "composer";
78pub const COMPOSER_LEADING_TAG: &str = "composer/leading";
79pub const COMPOSER_LEADING_TAG: &str = "composer/leading";
79pub const COMPOSER_SEND_TAG: &str = "composer/send";
80pub const COMPOSER_SEND_TAG: &str = "composer/send";
81pub const COMPOSER_FORMAT_TAG: &str = "composer/leading/format";
82pub const COMPOSER_FORMAT_BAR_TAG: &str = "composer/format-bar";
83pub const COMPOSER_FORMAT_DIVIDER_TAG: &str = "composer/format-bar/divider";
80/// gpui-kit draws a `Size::Size` button's icon at this fraction of the button size.
84/// gpui-kit draws a `Size::Size` button's icon at this fraction of the button size.
81const BUTTON_ICON_SCALE: f32 = 0.75;
85const BUTTON_ICON_SCALE: f32 = 0.75;
82const DEMO_UPLOAD_STEP: Duration = Duration::from_millis(120);
86const DEMO_UPLOAD_STEP: Duration = Duration::from_millis(120);
@@ -216,6 +220,8 @@ pub struct Composer {
216 tables: TableViews,
220 tables: TableViews,
217 table_counter: usize,
221 table_counter: usize,
218 table_picker: Option<TablePicker>,
222 table_picker: Option<TablePicker>,
223 table_menu: Option<TableMenu>,
224 format_bar: HashSet<String>,
219 active_cell: Option<(String, teams_core::Cell)>,
225 active_cell: Option<(String, teams_core::Cell)>,
220 caret_before_table: usize,
226 caret_before_table: usize,
221 caret_text: String,
227 caret_text: String,
@@ -425,6 +431,8 @@ impl Composer {
425 tables: TableViews::default(),
431 tables: TableViews::default(),
426 table_counter: 0,
432 table_counter: 0,
427 table_picker: None,
433 table_picker: None,
434 table_menu: None,
435 format_bar: HashSet::new(),
428 active_cell: None,
436 active_cell: None,
429 caret_before_table: 0,
437 caret_before_table: 0,
430 caret_text: String::new(),
438 caret_text: String::new(),
@@ -948,14 +956,62 @@ impl Composer {
948 FormatButton::Numbered => self.toggle_lines(LineKind::Numbered(0), window, cx),
956 FormatButton::Numbered => self.toggle_lines(LineKind::Numbered(0), window, cx),
949 FormatButton::Quote => self.toggle_lines(LineKind::Quote, window, cx),
957 FormatButton::Quote => self.toggle_lines(LineKind::Quote, window, cx),
950 FormatButton::Table => {
958 FormatButton::Table => {
959 self.table_menu = None;
951 self.toolbar_dismissed = Some(self.selection(cx));
960 self.toolbar_dismissed = Some(self.selection(cx));
952 self.open_table_picker(cx);
961 self.open_table_picker(cx);
953 }
962 }
963 FormatButton::TableInsertLine => {
964 self.open_table_menu(TableLineMenu::Insert, window, cx);
965 }
966 FormatButton::TableRemoveLine => {
967 self.open_table_menu(TableLineMenu::Remove, window, cx);
968 }
969 }
970 }
971
972 fn typing_state(&self, cursor: usize, kind: &MarkKind) -> FormatState {
973 let style = self.draft.style_at(cursor);
974 let style = match self.pending_style.as_ref() {
975 Some(pending) if pending.at == cursor => pending.apply(style),
976 _ => style,
977 };
978 if style.iter().any(|active| active.same_format(kind)) {
979 FormatState::On
980 } else {
981 FormatState::Off
954 }
982 }
955 }
983 }
956
984
957 fn format_state(&self, button: FormatButton, selection: &Range<usize>) -> FormatState {
985 fn format_state(&self, button: FormatButton, selection: &Range<usize>) -> FormatState {
958 let range = selection.clone();
986 let range = selection.clone();
987 let mark = match button {
988 FormatButton::Bold => Some(MarkKind::Bold),
989 FormatButton::Italic => Some(MarkKind::Italic),
990 FormatButton::Underline => Some(MarkKind::Underline),
991 FormatButton::Strike => Some(MarkKind::Strike),
992 FormatButton::Superscript => Some(MarkKind::Superscript),
993 FormatButton::Subscript => Some(MarkKind::Subscript),
994 FormatButton::SizeSmall => Some(MarkKind::Size(SizeStep::Small)),
995 FormatButton::SizeLarge => Some(MarkKind::Size(SizeStep::Large)),
996 FormatButton::Code => Some(MarkKind::Code),
997 FormatButton::Link => Some(MarkKind::Link(String::new())),
998 _ => None,
999 };
1000 if range.is_empty() {
1001 if let Some(kind) = &mark {
1002 return self.typing_state(range.start, kind);
1003 }
1004 if button == FormatButton::SizeNormal {
1005 let sized = [SizeStep::Small, SizeStep::Large].into_iter().any(|step| {
1006 self.typing_state(range.start, &MarkKind::Size(step)) == FormatState::On
1007 });
1008 return if sized {
1009 FormatState::Off
1010 } else {
1011 FormatState::On
1012 };
1013 }
1014 }
959 match button {
1015 match button {
960 FormatButton::Bold => self.draft.state(range, &MarkKind::Bold),
1016 FormatButton::Bold => self.draft.state(range, &MarkKind::Bold),
961 FormatButton::Italic => self.draft.state(range, &MarkKind::Italic),
1017 FormatButton::Italic => self.draft.state(range, &MarkKind::Italic),
@@ -972,7 +1028,67 @@ impl Composer {
972 FormatButton::Numbered => self.draft.line_state(range, &LineKind::Numbered(0)),
1028 FormatButton::Numbered => self.draft.line_state(range, &LineKind::Numbered(0)),
973 FormatButton::Quote => self.draft.line_state(range, &LineKind::Quote),
1029 FormatButton::Quote => self.draft.line_state(range, &LineKind::Quote),
974 FormatButton::Table if self.table_picker.is_some() => FormatState::On,
1030 FormatButton::Table if self.table_picker.is_some() => FormatState::On,
975 FormatButton::Table => FormatState::Off,
1031 FormatButton::TableInsertLine
1032 if self
1033 .table_menu
1034 .is_some_and(|menu| menu.kind == TableLineMenu::Insert) =>
1035 {
1036 FormatState::On
1037 }
1038 FormatButton::TableRemoveLine
1039 if self
1040 .table_menu
1041 .is_some_and(|menu| menu.kind == TableLineMenu::Remove) =>
1042 {
1043 FormatState::On
1044 }
1045 FormatButton::Table | FormatButton::TableInsertLine | FormatButton::TableRemoveLine => {
1046 FormatState::Off
1047 }
1048 }
1049 }
1050
1051 /// Text formats do not apply inside a table cell; the row and column menus only there.
1052 fn button_state(
1053 &self,
1054 button: FormatButton,
1055 selection: &Range<usize>,
1056 in_table: bool,
1057 ) -> ButtonState {
1058 let enabled = match button {
1059 FormatButton::TableInsertLine | FormatButton::TableRemoveLine => in_table,
1060 FormatButton::Table => true,
1061 _ => !in_table,
1062 };
1063 if enabled {
1064 ButtonState::enabled(self.format_state(button, selection))
1065 } else {
1066 ButtonState::disabled()
1067 }
1068 }
1069
1070 pub(super) fn format_bar_open(&self) -> bool {
1071 self.format_bar
1072 .contains(self.conversation_id.as_deref().unwrap_or_default())
1073 }
1074
1075 pub(super) fn set_format_bar(&mut self, open: bool, cx: &mut Context<Self>) {
1076 let key = self.conversation_id.clone().unwrap_or_default();
1077 if open {
1078 self.format_bar.insert(key);
1079 } else {
1080 self.format_bar.remove(&key);
1081 self.table_picker = None;
1082 self.table_menu = None;
1083 }
1084 cx.notify();
1085 }
1086
1087 fn toggle_format_bar(&mut self, window: &mut Window, cx: &mut Context<Self>) {
1088 let open = !self.format_bar_open();
1089 self.set_format_bar(open, cx);
1090 if !self.cell_has_focus(window, cx) {
1091 self.focus(window, cx);
976 }
1092 }
977 }
1093 }
978
1094
@@ -1519,6 +1635,8 @@ impl Composer {
1519 self.mention_inputs.clear();
1635 self.mention_inputs.clear();
1520 self.close_popup();
1636 self.close_popup();
1521 self.schedule = None;
1637 self.schedule = None;
1638 self.table_picker = None;
1639 self.table_menu = None;
1522 if !carry {
1640 if !carry {
1523 self.load_stored_draft(conversation_id, window, cx);
1641 self.load_stored_draft(conversation_id, window, cx);
1524 }
1642 }
@@ -2308,7 +2426,8 @@ impl Composer {
2308
2426
2309 fn render_toolbar(&self, focused: bool, cx: &mut Context<Self>) -> Option<AnyElement> {
2427 fn render_toolbar(&self, focused: bool, cx: &mut Context<Self>) -> Option<AnyElement> {
2310 let composer = cx.entity().downgrade();
2428 let composer = cx.entity().downgrade();
2311 if self.link_editor.is_none() && !(focused && self.toolbar_requested(cx)) {
2429 let floating = focused && self.toolbar_requested(cx) && !self.format_bar_open();
2430 if self.link_editor.is_none() && !floating {
2312 return None;
2431 return None;
2313 }
2432 }
2314 let state = self.input.read(cx);
2433 let state = self.input.read(cx);
@@ -2349,7 +2468,7 @@ impl Composer {
2349 },
2468 },
2350 ),
2469 ),
2351 None => {
2470 None => {
2352 let state_of = |button| self.format_state(button, &range);
2471 let state_of = |button| ButtonState::enabled(self.format_state(button, &range));
2353 format_toolbar::render(
2472 format_toolbar::render(
2354 placement,
2473 placement,
2355 format_toolbar::Mode::Bar {
2474 format_toolbar::Mode::Bar {
@@ -2366,6 +2485,37 @@ impl Composer {
2366 Some(element)
2485 Some(element)
2367 }
2486 }
2368
2487
2488 fn render_format_bar(&self, window: &Window, cx: &mut Context<Self>) -> AnyElement {
2489 let selection = self.selection(cx);
2490 let in_table = self.in_table(window, cx);
2491 let state_of = |button| self.button_state(button, &selection, in_table);
2492 let composer = cx.weak_entity();
2493 let on_press: format_toolbar::PressHandler = Rc::new(move |button, window, cx| {
2494 composer
2495 .update(cx, |this, cx| this.press_format(button, window, cx))
2496 .ok();
2497 });
2498 let mut overlay = |button| match button {
2499 FormatButton::Table => self.render_table_picker(cx),
2500 FormatButton::TableInsertLine => self.render_table_menu(TableLineMenu::Insert, cx),
2501 FormatButton::TableRemoveLine => self.render_table_menu(TableLineMenu::Remove, cx),
2502 _ => None,
2503 };
2504 let row = format_toolbar::render_format_bar(&state_of, on_press, &mut overlay);
2505 v_flex()
2506 .debug_selector(|| COMPOSER_FORMAT_BAR_TAG.into())
2507 .w_full()
2508 .child(row.w_full().p(px(COMPOSER_BUTTON_INSET)))
2509 .child(
2510 div()
2511 .debug_selector(|| COMPOSER_FORMAT_DIVIDER_TAG.into())
2512 .w_full()
2513 .h(px(1.))
2514 .bg(theme::border()),
2515 )
2516 .into_any_element()
2517 }
2518
2369 fn render_paste_hint(&self) -> Option<Div> {
2519 fn render_paste_hint(&self) -> Option<Div> {
2370 self.paste_hint.as_ref()?;
2520 self.paste_hint.as_ref()?;
2371 Some(
2521 Some(
@@ -2579,6 +2729,37 @@ impl Render for Composer {
2579 ))
2729 ))
2580 .on_click(cx.listener(|this, _, window, cx| this.open_picker(window, cx)))
2730 .on_click(cx.listener(|this, _, window, cx| this.open_picker(window, cx)))
2581 });
2731 });
2732 let format_open = self.format_bar_open();
2733 let format_toggle = div()
2734 .id("composer-format")
2735 .debug_selector(|| COMPOSER_FORMAT_TAG.into())
2736 .size(px(COMPOSER_BUTTON_SIZE))
2737 .flex_none()
2738 .flex()
2739 .items_center()
2740 .justify_center()
2741 .rounded(px(8.))
2742 .cursor_pointer()
2743 .when(format_open, |button| button.bg(theme::popup_hover()))
2744 .hover(|button| button.bg(theme::popup_hover()))
2745 .tooltip(move |window, cx| {
2746 Tooltip::new(format_toolbar::toggle_tooltip(format_open)).build(window, cx)
2747 })
2748 .child(symbol(
2749 format_toolbar::toggle_symbol(format_open),
2750 COMPOSER_ICON_SIZE,
2751 if format_open {
2752 theme::accent_soft()
2753 } else {
2754 theme::text_muted()
2755 },
2756 ))
2757 .on_mouse_down(MouseButton::Left, |_, window, cx| {
2758 window.prevent_default();
2759 cx.stop_propagation();
2760 })
2761 .on_click(cx.listener(|this, _, window, cx| this.toggle_format_bar(window, cx)));
2762 let format_bar = format_open.then(|| self.render_format_bar(window, cx));
2582 let composer = cx.weak_entity();
2763 let composer = cx.weak_entity();
2583 let tokens_composer = composer.clone();
2764 let tokens_composer = composer.clone();
2584 let input = Textarea::new(&self.input)
2765 let input = Textarea::new(&self.input)
@@ -2594,8 +2775,11 @@ impl Render for Composer {
2594 if context.is_block() && is_table_token(context.token()) {
2775 if context.is_block() && is_table_token(context.token()) {
2595 let token_id = context.token().id().to_string();
2776 let token_id = context.token().id().to_string();
2596 let width = context.available_width();
2777 let width = context.available_width();
2778 let selected = context.is_selected();
2597 if let Some(table) = tokens_composer
2779 if let Some(table) = tokens_composer
2598 .read_with(cx, |this, _| this.render_table_token(&token_id, width))
2780 .read_with(cx, |this, _| {
2781 this.render_table_token(&token_id, width, selected)
2782 })
2599 .ok()
2783 .ok()
2600 .flatten()
2784 .flatten()
2601 {
2785 {
@@ -2740,6 +2924,15 @@ impl Render for Composer {
2740 this.edit_table(TableEdit::DeleteTable, window, cx)
2924 this.edit_table(TableEdit::DeleteTable, window, cx)
2741 }))
2925 }))
2742 .on_action(cx.listener(|this, _: &ScheduleSend, _, cx| this.open_schedule_picker(cx)))
2926 .on_action(cx.listener(|this, _: &ScheduleSend, _, cx| this.open_schedule_picker(cx)))
2927 .on_action(cx.listener(|this, _: &ToggleFormatBar, window, cx| {
2928 this.toggle_format_bar(window, cx)
2929 }))
2930 .on_action(cx.listener(|this, _: &InsertTableLine, window, cx| {
2931 this.open_table_menu(TableLineMenu::Insert, window, cx);
2932 }))
2933 .on_action(cx.listener(|this, _: &RemoveTableLine, window, cx| {
2934 this.open_table_menu(TableLineMenu::Remove, window, cx);
2935 }))
2743 .capture_action(cx.listener(|this, _: &Undo, window, cx| {
2936 .capture_action(cx.listener(|this, _: &Undo, window, cx| {
2744 if this.link_editor.is_none() {
2937 if this.link_editor.is_none() {
2745 this.step_history(true, window, cx);
2938 this.step_history(true, window, cx);
@@ -2771,7 +2964,11 @@ impl Render for Composer {
2771 }
2964 }
2772 }))
2965 }))
2773 .capture_action(cx.listener(|this, _: &MoveUp, window, cx| {
2966 .capture_action(cx.listener(|this, _: &MoveUp, window, cx| {
2774 if this.cell_has_focus(window, cx) {
2967 if let Some(menu) = this.table_menu.as_mut() {
2968 menu.step(-1);
2969 cx.notify();
2970 cx.stop_propagation();
2971 } else if this.cell_has_focus(window, cx) {
2775 if this.table_key(TableKey::Up, window, cx) {
2972 if this.table_key(TableKey::Up, window, cx) {
2776 cx.stop_propagation();
2973 cx.stop_propagation();
2777 }
2974 }
@@ -2788,7 +2985,11 @@ impl Render for Composer {
2788 }
2985 }
2789 }))
2986 }))
2790 .capture_action(cx.listener(|this, _: &MoveDown, window, cx| {
2987 .capture_action(cx.listener(|this, _: &MoveDown, window, cx| {
2791 if this.cell_has_focus(window, cx) {
2988 if let Some(menu) = this.table_menu.as_mut() {
2989 menu.step(1);
2990 cx.notify();
2991 cx.stop_propagation();
2992 } else if this.cell_has_focus(window, cx) {
2792 if this.table_key(TableKey::Down, window, cx) {
2993 if this.table_key(TableKey::Down, window, cx) {
2793 cx.stop_propagation();
2994 cx.stop_propagation();
2794 }
2995 }
@@ -2812,7 +3013,10 @@ impl Render for Composer {
2812 }
3013 }
2813 }))
3014 }))
2814 .capture_action(cx.listener(|this, action: &Enter, window, cx| {
3015 .capture_action(cx.listener(|this, action: &Enter, window, cx| {
2815 if this.cell_has_focus(window, cx) {
3016 if this.table_menu.is_some() {
3017 this.accept_table_menu(None, window, cx);
3018 cx.stop_propagation();
3019 } else if this.cell_has_focus(window, cx) {
2816 } else if this.link_editor.is_some() {
3020 } else if this.link_editor.is_some() {
2817 this.apply_link(window, cx);
3021 this.apply_link(window, cx);
2818 cx.stop_propagation();
3022 cx.stop_propagation();
@@ -2864,7 +3068,7 @@ impl Render for Composer {
2864 }
3068 }
2865 }))
3069 }))
2866 .capture_action(cx.listener(|this, _: &Escape, window, cx| {
3070 .capture_action(cx.listener(|this, _: &Escape, window, cx| {
2867 if this.table_picker.take().is_some() {
3071 if this.table_picker.take().is_some() || this.table_menu.take().is_some() {
2868 cx.notify();
3072 cx.notify();
2869 cx.stop_propagation();
3073 cx.stop_propagation();
2870 return;
3074 return;
@@ -2879,7 +3083,7 @@ impl Render for Composer {
2879 cx.stop_propagation();
3083 cx.stop_propagation();
2880 return;
3084 return;
2881 }
3085 }
2882 if this.toolbar_requested(cx) {
3086 if this.toolbar_requested(cx) && !this.format_bar_open() {
2883 this.toolbar_dismissed = Some(this.selection(cx));
3087 this.toolbar_dismissed = Some(this.selection(cx));
2884 cx.notify();
3088 cx.notify();
2885 cx.stop_propagation();
3089 cx.stop_propagation();
@@ -2910,7 +3114,6 @@ impl Render for Composer {
2910 .children(self.render_emoji_popup(window, cx))
3114 .children(self.render_emoji_popup(window, cx))
2911 .children(self.render_toolbar(focused, cx))
3115 .children(self.render_toolbar(focused, cx))
2912 .children(self.render_schedule(cx))
3116 .children(self.render_schedule(cx))
2913 .children(self.render_table_picker(cx))
2914 .child(
3117 .child(
2915 v_flex()
3118 v_flex()
2916 .debug_selector(|| COMPOSER_BOX_TAG.into())
3119 .debug_selector(|| COMPOSER_BOX_TAG.into())
@@ -2932,6 +3135,7 @@ impl Render for Composer {
2932 .absolute()
3135 .absolute()
2933 .size_full(),
3136 .size_full(),
2934 )
3137 )
3138 .children(format_bar)
2935 .children(tray)
3139 .children(tray)
2936 .child(
3140 .child(
2937 h_flex()
3141 h_flex()
@@ -2944,22 +3148,16 @@ impl Render for Composer {
2944 }
3148 }
2945 })
3149 })
2946 .py(px(COMPOSER_BUTTON_INSET))
3150 .py(px(COMPOSER_BUTTON_INSET))
2947 .pl(px(if can_attach {
3151 .px(px(COMPOSER_BUTTON_INSET))
2948 COMPOSER_BUTTON_INSET
3152 .child(
2949 } else {
3153 h_flex()
2950 12.
3154 .debug_selector(|| COMPOSER_LEADING_TAG.into())
2951 }))
3155 .flex_none()
2952 .pr(px(COMPOSER_BUTTON_INSET))
3156 .gap(px(COMPOSER_BUTTON_INSET))
2953 .when(can_attach, |row| {
3157 .child(format_toggle)
2954 row.child(
3158 .children(fun)
2955 h_flex()
3159 .children(attach),
2956 .debug_selector(|| COMPOSER_LEADING_TAG.into())
3160 )
2957 .flex_none()
2958 .gap(px(COMPOSER_BUTTON_INSET))
2959 .children(fun)
2960 .children(attach),
2961 )
2962 })
2963 .child(
3161 .child(
2964 div()
3162 div()
2965 .flex_1()
3163 .flex_1()
@@ -4270,6 +4468,148 @@ mod tests {
4270 );
4468 );
4271 }
4469 }
4272
4470
4471 fn format_bar_open(typist: &mut Typist<'_>) -> bool {
4472 let composer = typist.composer.clone();
4473 typist.cx.update(|cx| composer.read(cx).format_bar_open())
4474 }
4475
4476 #[gpui_kit::test]
4477 fn ctrl_shift_x_opens_the_format_bar_per_conversation(cx: &mut TestAppContext) {
4478 let mut typist = typist(cx);
4479 let composer = typist.composer.clone();
4480 let open_chat = |typist: &mut Typist<'_>, id: &'static str| {
4481 let composer = composer.clone();
4482 typist.act(|window, cx| {
4483 composer.update(cx, |composer, cx| {
4484 composer.set_conversation(id, "", window, cx);
4485 composer.focus(window, cx);
4486 })
4487 });
4488 };
4489 open_chat(&mut typist, "chat-one");
4490 assert!(!format_bar_open(&mut typist));
4491 typist.press("ctrl-shift-x");
4492 assert!(format_bar_open(&mut typist));
4493 open_chat(&mut typist, "chat-two");
4494 assert!(!format_bar_open(&mut typist));
4495 open_chat(&mut typist, "chat-one");
4496 assert!(format_bar_open(&mut typist));
4497 typist.press("ctrl-shift-x");
4498 assert!(!format_bar_open(&mut typist));
4499 }
4500
4501 #[gpui_kit::test]
4502 fn ctrl_alt_x_strikes_through_now_that_ctrl_shift_x_is_format(cx: &mut TestAppContext) {
4503 let mut typist = typist(cx);
4504 typist.type_text("gone");
4505 typist.press("ctrl-a ctrl-alt-x");
4506 assert_eq!(typist.outgoing().html(), "<s>gone</s>");
4507 assert!(!format_bar_open(&mut typist));
4508 }
4509
4510 #[gpui_kit::test]
4511 fn the_format_bar_shows_the_style_at_the_caret_without_a_selection(
4512 cx: &mut TestAppContext,
4513 ) {
4514 use teams_core::FormatState;
4515
4516 use super::super::FormatButton;
4517
4518 let mut typist = typist(cx);
4519 typist.press("ctrl-shift-x ctrl-b");
4520 let composer = typist.composer.clone();
4521 let bold = |typist: &mut Typist<'_>| {
4522 let composer = composer.clone();
4523 typist.cx.update(|cx| {
4524 let composer = composer.read(cx);
4525 let caret = composer.selection(cx);
4526 composer.button_state(FormatButton::Bold, &caret, false)
4527 })
4528 };
4529 assert_eq!(bold(&mut typist).format, FormatState::On);
4530 typist.type_text("x");
4531 assert_eq!(bold(&mut typist).format, FormatState::On);
4532 typist.press("ctrl-b");
4533 assert_eq!(bold(&mut typist).format, FormatState::Off);
4534 assert!(bold(&mut typist).enabled);
4535 }
4536
4537 #[gpui_kit::test]
4538 fn the_format_bar_inserts_a_table_without_a_selection(cx: &mut TestAppContext) {
4539 use super::super::FormatButton;
4540
4541 let mut typist = typist(cx);
4542 typist.press("ctrl-shift-x");
4543 let composer = typist.composer.clone();
4544 typist.act(|window, cx| {
4545 composer.update(cx, |composer, cx| {
4546 composer.press_format(FormatButton::Table, window, cx)
4547 })
4548 });
4549 let picker_open = typist
4550 .cx
4551 .update(|cx| composer.read(cx).table_picker.is_some());
4552 assert!(picker_open);
4553 insert_table(&mut typist, 2, 2);
4554 assert!(typist.draft().lines()[0].is_table());
4555 }
4556
4557 #[gpui_kit::test]
4558 fn ctrl_alt_plus_and_minus_open_the_teams_row_and_column_menus(cx: &mut TestAppContext) {
4559 use super::super::table::TableLineMenu;
4560
4561 let mut typist = typist(cx);
4562 let composer = typist.composer.clone();
4563 let menu = |typist: &mut Typist<'_>| {
4564 let composer = composer.clone();
4565 typist
4566 .cx
4567 .update(|cx| composer.read(cx).table_menu.map(|menu| menu.kind))
4568 };
4569 typist.press("ctrl-alt-=");
4570 assert_eq!(menu(&mut typist), None);
4571 insert_table(&mut typist, 1, 1);
4572 typist.type_text("a");
4573 typist.press("ctrl-alt-=");
4574 assert_eq!(menu(&mut typist), Some(TableLineMenu::Insert));
4575 assert!(format_bar_open(&mut typist));
4576 typist.press("enter");
4577 assert_eq!(menu(&mut typist), None);
4578 let shape = |typist: &mut Typist<'_>| {
4579 let draft = typist.draft();
4580 let table = draft.table(0).expect("table").clone();
4581 (table.row_count(), table.column_count())
4582 };
4583 assert_eq!(shape(&mut typist), (1, 2));
4584 typist.press("ctrl-alt-- enter");
4585 assert_eq!(shape(&mut typist), (1, 1));
4586 typist.press("ctrl-alt-- escape");
4587 assert_eq!(menu(&mut typist), None);
4588 assert_eq!(shape(&mut typist), (1, 1));
4589 }
4590
4591 #[gpui_kit::test]
4592 fn ctrl_a_highlights_a_table_like_selected_text(cx: &mut TestAppContext) {
4593 use gpui_kit::VisualTestContext;
4594
4595 use super::super::table::SELECTED_TABLE_TAG;
4596
4597 let mut typist = typist(cx);
4598 typist.type_text("before");
4599 insert_table(&mut typist, 1, 2);
4600 typist.type_text("a");
4601 typist.press("down");
4602 typist.type_text("after");
4603 let handle = typist.handle;
4604 let mut visual = VisualTestContext::from_window(handle, typist.cx);
4605 visual.update(|window, cx| window.render_frame(cx));
4606 assert!(visual.debug_bounds(SELECTED_TABLE_TAG).is_none());
4607 visual.simulate_keystrokes("ctrl-a");
4608 visual.run_until_parked();
4609 visual.update(|window, cx| window.render_frame(cx));
4610 assert!(visual.debug_bounds(SELECTED_TABLE_TAG).is_some());
4611 }
4612
4273 #[gpui_kit::test]
4613 #[gpui_kit::test]
4274 fn deleting_the_last_column_deletes_the_table(cx: &mut TestAppContext) {
4614 fn deleting_the_last_column_deletes_the_table(cx: &mut TestAppContext) {
4275 let mut typist = typist(cx);
4615 let mut typist = typist(cx);
crates/app/src/views/composer/table.rs +187 −5
@@ -6,6 +6,7 @@ use gpui_kit::component::input::{
6 6 };
7 7 use gpui_kit::component::native_menu::NativeMenu;
8 8 use gpui_kit::component::{Sizable as _, h_flex, v_flex};
9 use gpui_kit::prelude::FluentBuilder as _;
9 10 use gpui_kit::*;
10 11 use teams_core::{Cell, PICKER_COLUMNS, PICKER_ROWS, TABLE_MARK, Table, TableEdit};
11 12
@@ -23,6 +24,11 @@ const PICKER_GAP: f32 = 3.;
23 24 const TABLE_MARGIN: f32 = 4.;
24 25 const CELL_PADDING_X: f32 = 2.;
25 26 const CELL_PADDING_Y: f32 = 3.;
27 const MENU_WIDTH: f32 = 200.;
28 const MENU_PADDING: f32 = 4.;
29 const MENU_ROW_PADDING_X: f32 = 10.;
30 const MENU_ROW_PADDING_Y: f32 = 6.;
31 pub(super) const SELECTED_TABLE_TAG: &str = "composer/table-selected";
26 32
27 33 pub(super) struct TableView {
28 34 cells: Vec<Vec<Entity<TextareaState>>>,
@@ -41,6 +47,55 @@ pub(super) struct TablePicker {
41 47 columns: usize,
42 48 }
43 49
50 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
51 pub(super) enum TableLineMenu {
52 Insert,
53 Remove,
54 }
55
56 impl TableLineMenu {
57 /// Teams' order: "Insert column or row" and "Delete column, row, or table".
58 pub(super) fn edits(self) -> &'static [TableEdit] {
59 match self {
60 TableLineMenu::Insert => &[
61 TableEdit::InsertColumnRight,
62 TableEdit::InsertColumnLeft,
63 TableEdit::InsertRowAbove,
64 TableEdit::InsertRowBelow,
65 ],
66 TableLineMenu::Remove => &[
67 TableEdit::DeleteColumn,
68 TableEdit::DeleteRow,
69 TableEdit::DeleteTable,
70 ],
71 }
72 }
73 }
74
75 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
76 pub(super) struct TableMenu {
77 pub(super) kind: TableLineMenu,
78 pub(super) highlighted: usize,
79 }
80
81 impl TableMenu {
82 pub(super) fn new(kind: TableLineMenu) -> Self {
83 TableMenu {
84 kind,
85 highlighted: 0,
86 }
87 }
88
89 pub(super) fn step(&mut self, delta: isize) {
90 let count = self.kind.edits().len() as isize;
91 self.highlighted = (self.highlighted as isize + delta).rem_euclid(count) as usize;
92 }
93
94 pub(super) fn chosen(&self) -> TableEdit {
95 self.kind.edits()[self.highlighted]
96 }
97 }
98
44 99 pub(super) fn token(number: usize) -> InlineToken {
45 100 InlineToken::new(format!("{TOKEN_PREFIX}{number}"), TABLE_MARK.to_string()).block()
46 101 }
@@ -480,6 +535,87 @@ impl Composer {
480 535 self.focus_cell(&token_id, Cell::new(row, 0), window, cx);
481 536 }
482 537
538 pub(super) fn in_table(&self, window: &Window, cx: &App) -> bool {
539 self.focused_cell(window, cx).is_some()
540 }
541
542 /// Ctrl+Alt+= and Ctrl+Alt+- or the format bar buttons; only inside a table, like Teams.
543 pub(super) fn open_table_menu(
544 &mut self,
545 kind: TableLineMenu,
546 window: &mut Window,
547 cx: &mut Context<Self>,
548 ) -> bool {
549 if !self.in_table(window, cx) {
550 return false;
551 }
552 self.table_picker = None;
553 self.table_menu = Some(TableMenu::new(kind));
554 if !self.format_bar_open() {
555 self.set_format_bar(true, cx);
556 }
557 cx.notify();
558 true
559 }
560
561 pub(super) fn accept_table_menu(
562 &mut self,
563 edit: Option<TableEdit>,
564 window: &mut Window,
565 cx: &mut Context<Self>,
566 ) {
567 let Some(menu) = self.table_menu.take() else {
568 return;
569 };
570 self.edit_table(edit.unwrap_or(menu.chosen()), window, cx);
571 }
572
573 pub(super) fn render_table_menu(
574 &self,
575 kind: TableLineMenu,
576 cx: &mut Context<Self>,
577 ) -> Option<AnyElement> {
578 let menu = self.table_menu.filter(|menu| menu.kind == kind)?;
579 let rows = kind.edits().iter().enumerate().map(|(index, edit)| {
580 let edit = *edit;
581 div()
582 .id(ElementId::Name(format!("table-menu-{edit:?}").into()))
583 .px(px(MENU_ROW_PADDING_X))
584 .py(px(MENU_ROW_PADDING_Y))
585 .rounded(px(6.))
586 .text_size(px(13.))
587 .text_color(theme::text())
588 .cursor_pointer()
589 .when(index == menu.highlighted, |row| {
590 row.bg(theme::popup_hover())
591 })
592 .hover(|row| row.bg(theme::popup_hover()))
593 .on_mouse_down(MouseButton::Left, |_, window, cx| {
594 window.prevent_default();
595 cx.stop_propagation();
596 })
597 .on_click(cx.listener(move |this, _, window, cx| {
598 this.accept_table_menu(Some(edit), window, cx)
599 }))
600 .child(edit.label())
601 });
602 Some(
603 v_flex()
604 .id("table-line-menu")
605 .min_w(px(MENU_WIDTH))
606 .p(px(MENU_PADDING))
607 .rounded(px(10.))
608 .flyout_surface()
609 .occlude()
610 .on_mouse_down_out(cx.listener(|this, _, _, cx| {
611 this.table_menu = None;
612 cx.notify();
613 }))
614 .children(rows)
615 .into_any_element(),
616 )
617 }
618
483 619 pub(super) fn open_table_picker(&mut self, cx: &mut Context<Self>) {
484 620 self.table_picker = Some(TablePicker {
485 621 rows: 1,
@@ -522,10 +658,6 @@ impl Composer {
522 658 Some(
523 659 v_flex()
524 660 .id("table-picker")
525 .absolute()
526 .bottom(relative(1.))
527 .left_0()
528 .mb(px(6.))
529 661 .p(px(10.))
530 662 .gap(px(8.))
531 663 .rounded(px(10.))
@@ -547,7 +679,14 @@ impl Composer {
547 679 }
548 680
549 681 /// The table token: a grid of cell inputs over the full width, solid lines like Teams.
550 pub(super) fn render_table_token(&self, token_id: &str, width: Pixels) -> Option<AnyElement> {
682 /// The editor leaves a selected token's highlight to the token, so a selection over the
683 /// table tints every cell in the text selection color.
684 pub(super) fn render_table_token(
685 &self,
686 token_id: &str,
687 width: Pixels,
688 selected: bool,
689 ) -> Option<AnyElement> {
551 690 let view = self.tables.get(token_id)?;
552 691 let rows = view.cells.iter().map(|inputs| {
553 692 h_flex()
@@ -562,6 +701,7 @@ impl Composer {
562 701 .border_r_1()
563 702 .border_b_1()
564 703 .border_color(theme::border_strong())
704 .when(selected, |cell| cell.bg(theme::selection()))
565 705 .child(
566 706 Textarea::new(input)
567 707 .appearance(false)
@@ -574,6 +714,9 @@ impl Composer {
574 714 Some(
575 715 v_flex()
576 716 .id(ElementId::Name(format!("composer-{token_id}").into()))
717 .when(selected, |table| {
718 table.debug_selector(|| SELECTED_TABLE_TAG.into())
719 })
577 720 .w(width)
578 721 .my(px(TABLE_MARGIN))
579 722 .border_t_1()
@@ -628,3 +771,42 @@ fn edit_action(edit: TableEdit) -> Box<dyn Action> {
628 771 }
629 772
630 773 pub(super) type TableViews = HashMap<String, TableView>;
774
775 #[cfg(test)]
776 mod tests {
777 use teams_core::TableEdit;
778
779 use super::{TableLineMenu, TableMenu};
780
781 #[test]
782 fn the_line_menus_list_teams_entries_in_teams_order() {
783 assert_eq!(
784 TableLineMenu::Insert.edits(),
785 &[
786 TableEdit::InsertColumnRight,
787 TableEdit::InsertColumnLeft,
788 TableEdit::InsertRowAbove,
789 TableEdit::InsertRowBelow
790 ]
791 );
792 assert_eq!(
793 TableLineMenu::Remove.edits(),
794 &[
795 TableEdit::DeleteColumn,
796 TableEdit::DeleteRow,
797 TableEdit::DeleteTable
798 ]
799 );
800 }
801
802 #[test]
803 fn the_menu_highlight_wraps_both_ways() {
804 let mut menu = TableMenu::new(TableLineMenu::Remove);
805 assert_eq!(menu.chosen(), TableEdit::DeleteColumn);
806 menu.step(-1);
807 assert_eq!(menu.chosen(), TableEdit::DeleteTable);
808 menu.step(1);
809 menu.step(1);
810 assert_eq!(menu.chosen(), TableEdit::DeleteRow);
811 }
812 }
@@ -6,6 +6,7 @@ use gpui_kit::component::input::{
6};
6};
7use gpui_kit::component::native_menu::NativeMenu;
7use gpui_kit::component::native_menu::NativeMenu;
8use gpui_kit::component::{Sizable as _, h_flex, v_flex};
8use gpui_kit::component::{Sizable as _, h_flex, v_flex};
9use gpui_kit::prelude::FluentBuilder as _;
9use gpui_kit::*;
10use gpui_kit::*;
10use teams_core::{Cell, PICKER_COLUMNS, PICKER_ROWS, TABLE_MARK, Table, TableEdit};
11use teams_core::{Cell, PICKER_COLUMNS, PICKER_ROWS, TABLE_MARK, Table, TableEdit};
11
12
@@ -23,6 +24,11 @@ const PICKER_GAP: f32 = 3.;
23const TABLE_MARGIN: f32 = 4.;
24const TABLE_MARGIN: f32 = 4.;
24const CELL_PADDING_X: f32 = 2.;
25const CELL_PADDING_X: f32 = 2.;
25const CELL_PADDING_Y: f32 = 3.;
26const CELL_PADDING_Y: f32 = 3.;
27const MENU_WIDTH: f32 = 200.;
28const MENU_PADDING: f32 = 4.;
29const MENU_ROW_PADDING_X: f32 = 10.;
30const MENU_ROW_PADDING_Y: f32 = 6.;
31pub(super) const SELECTED_TABLE_TAG: &str = "composer/table-selected";
26
32
27pub(super) struct TableView {
33pub(super) struct TableView {
28 cells: Vec<Vec<Entity<TextareaState>>>,
34 cells: Vec<Vec<Entity<TextareaState>>>,
@@ -41,6 +47,55 @@ pub(super) struct TablePicker {
41 columns: usize,
47 columns: usize,
42}
48}
43
49
50#[derive(Debug, Clone, Copy, PartialEq, Eq)]
51pub(super) enum TableLineMenu {
52 Insert,
53 Remove,
54}
55
56impl TableLineMenu {
57 /// Teams' order: "Insert column or row" and "Delete column, row, or table".
58 pub(super) fn edits(self) -> &'static [TableEdit] {
59 match self {
60 TableLineMenu::Insert => &[
61 TableEdit::InsertColumnRight,
62 TableEdit::InsertColumnLeft,
63 TableEdit::InsertRowAbove,
64 TableEdit::InsertRowBelow,
65 ],
66 TableLineMenu::Remove => &[
67 TableEdit::DeleteColumn,
68 TableEdit::DeleteRow,
69 TableEdit::DeleteTable,
70 ],
71 }
72 }
73}
74
75#[derive(Debug, Clone, Copy, PartialEq, Eq)]
76pub(super) struct TableMenu {
77 pub(super) kind: TableLineMenu,
78 pub(super) highlighted: usize,
79}
80
81impl TableMenu {
82 pub(super) fn new(kind: TableLineMenu) -> Self {
83 TableMenu {
84 kind,
85 highlighted: 0,
86 }
87 }
88
89 pub(super) fn step(&mut self, delta: isize) {
90 let count = self.kind.edits().len() as isize;
91 self.highlighted = (self.highlighted as isize + delta).rem_euclid(count) as usize;
92 }
93
94 pub(super) fn chosen(&self) -> TableEdit {
95 self.kind.edits()[self.highlighted]
96 }
97}
98
44pub(super) fn token(number: usize) -> InlineToken {
99pub(super) fn token(number: usize) -> InlineToken {
45 InlineToken::new(format!("{TOKEN_PREFIX}{number}"), TABLE_MARK.to_string()).block()
100 InlineToken::new(format!("{TOKEN_PREFIX}{number}"), TABLE_MARK.to_string()).block()
46}
101}
@@ -480,6 +535,87 @@ impl Composer {
480 self.focus_cell(&token_id, Cell::new(row, 0), window, cx);
535 self.focus_cell(&token_id, Cell::new(row, 0), window, cx);
481 }
536 }
482
537
538 pub(super) fn in_table(&self, window: &Window, cx: &App) -> bool {
539 self.focused_cell(window, cx).is_some()
540 }
541
542 /// Ctrl+Alt+= and Ctrl+Alt+- or the format bar buttons; only inside a table, like Teams.
543 pub(super) fn open_table_menu(
544 &mut self,
545 kind: TableLineMenu,
546 window: &mut Window,
547 cx: &mut Context<Self>,
548 ) -> bool {
549 if !self.in_table(window, cx) {
550 return false;
551 }
552 self.table_picker = None;
553 self.table_menu = Some(TableMenu::new(kind));
554 if !self.format_bar_open() {
555 self.set_format_bar(true, cx);
556 }
557 cx.notify();
558 true
559 }
560
561 pub(super) fn accept_table_menu(
562 &mut self,
563 edit: Option<TableEdit>,
564 window: &mut Window,
565 cx: &mut Context<Self>,
566 ) {
567 let Some(menu) = self.table_menu.take() else {
568 return;
569 };
570 self.edit_table(edit.unwrap_or(menu.chosen()), window, cx);
571 }
572
573 pub(super) fn render_table_menu(
574 &self,
575 kind: TableLineMenu,
576 cx: &mut Context<Self>,
577 ) -> Option<AnyElement> {
578 let menu = self.table_menu.filter(|menu| menu.kind == kind)?;
579 let rows = kind.edits().iter().enumerate().map(|(index, edit)| {
580 let edit = *edit;
581 div()
582 .id(ElementId::Name(format!("table-menu-{edit:?}").into()))
583 .px(px(MENU_ROW_PADDING_X))
584 .py(px(MENU_ROW_PADDING_Y))
585 .rounded(px(6.))
586 .text_size(px(13.))
587 .text_color(theme::text())
588 .cursor_pointer()
589 .when(index == menu.highlighted, |row| {
590 row.bg(theme::popup_hover())
591 })
592 .hover(|row| row.bg(theme::popup_hover()))
593 .on_mouse_down(MouseButton::Left, |_, window, cx| {
594 window.prevent_default();
595 cx.stop_propagation();
596 })
597 .on_click(cx.listener(move |this, _, window, cx| {
598 this.accept_table_menu(Some(edit), window, cx)
599 }))
600 .child(edit.label())
601 });
602 Some(
603 v_flex()
604 .id("table-line-menu")
605 .min_w(px(MENU_WIDTH))
606 .p(px(MENU_PADDING))
607 .rounded(px(10.))
608 .flyout_surface()
609 .occlude()
610 .on_mouse_down_out(cx.listener(|this, _, _, cx| {
611 this.table_menu = None;
612 cx.notify();
613 }))
614 .children(rows)
615 .into_any_element(),
616 )
617 }
618
483 pub(super) fn open_table_picker(&mut self, cx: &mut Context<Self>) {
619 pub(super) fn open_table_picker(&mut self, cx: &mut Context<Self>) {
484 self.table_picker = Some(TablePicker {
620 self.table_picker = Some(TablePicker {
485 rows: 1,
621 rows: 1,
@@ -522,10 +658,6 @@ impl Composer {
522 Some(
658 Some(
523 v_flex()
659 v_flex()
524 .id("table-picker")
660 .id("table-picker")
525 .absolute()
526 .bottom(relative(1.))
527 .left_0()
528 .mb(px(6.))
529 .p(px(10.))
661 .p(px(10.))
530 .gap(px(8.))
662 .gap(px(8.))
531 .rounded(px(10.))
663 .rounded(px(10.))
@@ -547,7 +679,14 @@ impl Composer {
547 }
679 }
548
680
549 /// The table token: a grid of cell inputs over the full width, solid lines like Teams.
681 /// The table token: a grid of cell inputs over the full width, solid lines like Teams.
550 pub(super) fn render_table_token(&self, token_id: &str, width: Pixels) -> Option<AnyElement> {
682 /// The editor leaves a selected token's highlight to the token, so a selection over the
683 /// table tints every cell in the text selection color.
684 pub(super) fn render_table_token(
685 &self,
686 token_id: &str,
687 width: Pixels,
688 selected: bool,
689 ) -> Option<AnyElement> {
551 let view = self.tables.get(token_id)?;
690 let view = self.tables.get(token_id)?;
552 let rows = view.cells.iter().map(|inputs| {
691 let rows = view.cells.iter().map(|inputs| {
553 h_flex()
692 h_flex()
@@ -562,6 +701,7 @@ impl Composer {
562 .border_r_1()
701 .border_r_1()
563 .border_b_1()
702 .border_b_1()
564 .border_color(theme::border_strong())
703 .border_color(theme::border_strong())
704 .when(selected, |cell| cell.bg(theme::selection()))
565 .child(
705 .child(
566 Textarea::new(input)
706 Textarea::new(input)
567 .appearance(false)
707 .appearance(false)
@@ -574,6 +714,9 @@ impl Composer {
574 Some(
714 Some(
575 v_flex()
715 v_flex()
576 .id(ElementId::Name(format!("composer-{token_id}").into()))
716 .id(ElementId::Name(format!("composer-{token_id}").into()))
717 .when(selected, |table| {
718 table.debug_selector(|| SELECTED_TABLE_TAG.into())
719 })
577 .w(width)
720 .w(width)
578 .my(px(TABLE_MARGIN))
721 .my(px(TABLE_MARGIN))
579 .border_t_1()
722 .border_t_1()
@@ -628,3 +771,42 @@ fn edit_action(edit: TableEdit) -> Box<dyn Action> {
628}
771}
629
772
630pub(super) type TableViews = HashMap<String, TableView>;
773pub(super) type TableViews = HashMap<String, TableView>;
774
775#[cfg(test)]
776mod tests {
777 use teams_core::TableEdit;
778
779 use super::{TableLineMenu, TableMenu};
780
781 #[test]
782 fn the_line_menus_list_teams_entries_in_teams_order() {
783 assert_eq!(
784 TableLineMenu::Insert.edits(),
785 &[
786 TableEdit::InsertColumnRight,
787 TableEdit::InsertColumnLeft,
788 TableEdit::InsertRowAbove,
789 TableEdit::InsertRowBelow
790 ]
791 );
792 assert_eq!(
793 TableLineMenu::Remove.edits(),
794 &[
795 TableEdit::DeleteColumn,
796 TableEdit::DeleteRow,
797 TableEdit::DeleteTable
798 ]
799 );
800 }
801
802 #[test]
803 fn the_menu_highlight_wraps_both_ways() {
804 let mut menu = TableMenu::new(TableLineMenu::Remove);
805 assert_eq!(menu.chosen(), TableEdit::DeleteColumn);
806 menu.step(-1);
807 assert_eq!(menu.chosen(), TableEdit::DeleteTable);
808 menu.step(1);
809 menu.step(1);
810 assert_eq!(menu.chosen(), TableEdit::DeleteRow);
811 }
812}
crates/app/src/views/format_toolbar.rs +213 −54
@@ -20,6 +20,7 @@ const ARROW_MARGIN: f32 = 12.;
20 20 const LINK_FIELD_WIDTH: f32 = 240.;
21 21 const APPLY_WIDTH: f32 = 64.;
22 22 const ACTIVE_ALPHA: f32 = 0.22;
23 const DISABLED_OPACITY: f32 = 0.4;
23 24 const SMALL_LETTER: f32 = 11.;
24 25 const NORMAL_LETTER: f32 = 15.;
25 26 const LARGE_LETTER: f32 = 19.;
@@ -43,29 +44,93 @@ pub enum FormatButton {
43 44 Numbered,
44 45 Quote,
45 46 Table,
47 TableInsertLine,
48 TableRemoveLine,
46 49 }
47 50
48 pub const GROUPS: [&[FormatButton]; 6] = [
49 &[
50 FormatButton::Bold,
51 FormatButton::Italic,
52 FormatButton::Underline,
53 FormatButton::Strike,
54 ],
55 &[FormatButton::Superscript, FormatButton::Subscript],
56 &[
57 FormatButton::SizeSmall,
58 FormatButton::SizeNormal,
59 FormatButton::SizeLarge,
60 ],
61 &[FormatButton::Code, FormatButton::Link],
62 &[
63 FormatButton::Bulleted,
64 FormatButton::Numbered,
65 FormatButton::Quote,
66 ],
67 &[FormatButton::Table],
51 const STYLE_GROUP: &[FormatButton] = &[
52 FormatButton::Bold,
53 FormatButton::Italic,
54 FormatButton::Underline,
55 FormatButton::Strike,
68 56 ];
57 const SCRIPT_GROUP: &[FormatButton] = &[FormatButton::Superscript, FormatButton::Subscript];
58 const SIZE_GROUP: &[FormatButton] = &[
59 FormatButton::SizeSmall,
60 FormatButton::SizeNormal,
61 FormatButton::SizeLarge,
62 ];
63 const INLINE_GROUP: &[FormatButton] = &[FormatButton::Code, FormatButton::Link];
64 const LINE_GROUP: &[FormatButton] = &[
65 FormatButton::Bulleted,
66 FormatButton::Numbered,
67 FormatButton::Quote,
68 ];
69 const TABLE_GROUP: &[FormatButton] = &[
70 FormatButton::Table,
71 FormatButton::TableInsertLine,
72 FormatButton::TableRemoveLine,
73 ];
74
75 /// The floating bar over a selection: text formats only, tables live in the format bar.
76 pub const SELECTION_GROUPS: [&[FormatButton]; 5] = [
77 STYLE_GROUP,
78 SCRIPT_GROUP,
79 SIZE_GROUP,
80 INLINE_GROUP,
81 LINE_GROUP,
82 ];
83
84 /// The format bar the Format button opens, table group second to last like Teams.
85 pub const BAR_GROUPS: [&[FormatButton]; 6] = [
86 STYLE_GROUP,
87 SCRIPT_GROUP,
88 SIZE_GROUP,
89 INLINE_GROUP,
90 TABLE_GROUP,
91 LINE_GROUP,
92 ];
93
94 pub const TOGGLE_SHORTCUT: &str = "Ctrl+Shift+X";
95
96 pub fn toggle_tooltip(open: bool) -> String {
97 let label = if open {
98 "Hide Formatting options"
99 } else {
100 "Show Formatting options"
101 };
102 format!("{label} ({TOGGLE_SHORTCUT})")
103 }
104
105 pub fn toggle_symbol(open: bool) -> &'static str {
106 if open {
107 "fluent_text_edit_style_filled"
108 } else {
109 "fluent_text_edit_style"
110 }
111 }
112
113 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
114 pub struct ButtonState {
115 pub format: FormatState,
116 pub enabled: bool,
117 }
118
119 impl ButtonState {
120 pub fn enabled(format: FormatState) -> Self {
121 ButtonState {
122 format,
123 enabled: true,
124 }
125 }
126
127 pub fn disabled() -> Self {
128 ButtonState {
129 format: FormatState::Off,
130 enabled: false,
131 }
132 }
133 }
69 134
70 135 impl FormatButton {
71 136 fn tooltip(self) -> &'static str {
@@ -73,7 +138,7 @@ impl FormatButton {
73 138 FormatButton::Bold => "Bold (Ctrl+B)",
74 139 FormatButton::Italic => "Italic (Ctrl+I)",
75 140 FormatButton::Underline => "Underline (Ctrl+U)",
76 FormatButton::Strike => "Strikethrough (Ctrl+Shift+X)",
141 FormatButton::Strike => "Strikethrough (Ctrl+Alt+X)",
77 142 FormatButton::Superscript => "Superscript (Ctrl+Shift+=)",
78 143 FormatButton::Subscript => "Subscript (Ctrl+=)",
79 144 FormatButton::SizeSmall => "Small text",
@@ -85,6 +150,8 @@ impl FormatButton {
85 150 FormatButton::Numbered => "Numbered list",
86 151 FormatButton::Quote => "Quote",
87 152 FormatButton::Table => "Insert table",
153 FormatButton::TableInsertLine => "Insert column or row (Ctrl+Alt+=)",
154 FormatButton::TableRemoveLine => "Delete column, row, or table (Ctrl+Alt+-)",
88 155 }
89 156 }
90 157
@@ -106,6 +173,12 @@ impl FormatButton {
106 173 FormatButton::Numbered => symbol("format_list_numbered", 18., color).into_any_element(),
107 174 FormatButton::Quote => symbol("format_quote", 18., color).into_any_element(),
108 175 FormatButton::Table => symbol("table", 18., color).into_any_element(),
176 FormatButton::TableInsertLine => {
177 symbol("fluent_table_add", 20., color).into_any_element()
178 }
179 FormatButton::TableRemoveLine => {
180 symbol("fluent_table_dismiss", 20., color).into_any_element()
181 }
109 182 }
110 183 }
111 184 }
@@ -134,9 +207,10 @@ fn sized_letter(size: f32, color: Hsla) -> AnyElement {
134 207 }
135 208
136 209 pub fn bar_width() -> f32 {
137 let buttons: usize = GROUPS.iter().map(|group| group.len()).sum();
138 let gaps: usize = GROUPS.iter().map(|group| group.len() - 1).sum();
139 let separators = (GROUPS.len() - 1) as f32 * (1. + 2. * SEPARATOR_MARGIN);
210 let groups = SELECTION_GROUPS;
211 let buttons: usize = groups.iter().map(|group| group.len()).sum();
212 let gaps: usize = groups.iter().map(|group| group.len() - 1).sum();
213 let separators = (groups.len() - 1) as f32 * (1. + 2. * SEPARATOR_MARGIN);
140 214 2. * PADDING + buttons as f32 * BUTTON + gaps as f32 * BUTTON_GAP + separators
141 215 }
142 216
@@ -172,7 +246,7 @@ pub type ApplyHandler = Rc<dyn Fn(&mut Window, &mut App)>;
172 246
173 247 pub enum Mode<'a> {
174 248 Bar {
175 state: &'a dyn Fn(FormatButton) -> FormatState,
249 state: &'a dyn Fn(FormatButton) -> ButtonState,
176 250 on_press: PressHandler,
177 251 },
178 252 Link {
@@ -182,8 +256,13 @@ pub enum Mode<'a> {
182 256 },
183 257 }
184 258
185 fn button(format: FormatButton, state: FormatState, on_press: PressHandler) -> impl IntoElement {
186 let on = state == FormatState::On;
259 fn button(
260 format: FormatButton,
261 state: ButtonState,
262 on_press: PressHandler,
263 overlay: Option<AnyElement>,
264 ) -> impl IntoElement {
265 let on = state.enabled && state.format == FormatState::On;
187 266 let color = if on {
188 267 theme::accent_soft()
189 268 } else {
@@ -198,30 +277,44 @@ fn button(format: FormatButton, state: FormatState, on_press: PressHandler) -> i
198 277 .items_center()
199 278 .justify_center()
200 279 .rounded(px(6.))
201 .cursor_pointer()
202 .when(on, |button| {
203 button.bg(theme::accent().opacity(ACTIVE_ALPHA))
204 })
205 .hover(|button| button.bg(theme::popup_hover()))
206 280 .tooltip(move |window, cx| Tooltip::new(format.tooltip()).build(window, cx))
207 281 .child(format.content(color))
208 .when(state == FormatState::Mixed, |button| {
209 button.child(
210 div()
211 .absolute()
212 .bottom(px(2.))
213 .left(px(7.))
214 .w(px(BUTTON - 16.))
215 .h(px(2.))
216 .rounded(px(1.))
217 .bg(theme::accent()),
218 )
219 })
220 282 .on_mouse_down(MouseButton::Left, |_, window, cx| {
221 283 window.prevent_default();
222 284 cx.stop_propagation();
223 285 })
224 .on_click(move |_, window, cx| on_press(format, window, cx))
286 .when(!state.enabled, |button| button.opacity(DISABLED_OPACITY))
287 .when(state.enabled, |button| {
288 button
289 .cursor_pointer()
290 .when(on, |button| {
291 button.bg(theme::accent().opacity(ACTIVE_ALPHA))
292 })
293 .hover(|button| button.bg(theme::popup_hover()))
294 .when(state.format == FormatState::Mixed, |button| {
295 button.child(
296 div()
297 .absolute()
298 .bottom(px(2.))
299 .left(px(7.))
300 .w(px(BUTTON - 16.))
301 .h(px(2.))
302 .rounded(px(1.))
303 .bg(theme::accent()),
304 )
305 })
306 .on_click(move |_, window, cx| on_press(format, window, cx))
307 })
308 .children(overlay.map(|overlay| {
309 div().absolute().top(px(BUTTON + GAP / 2.)).left_0().child(
310 deferred(
311 anchored()
312 .snap_to_window_with_margin(px(GAP))
313 .child(overlay),
314 )
315 .with_priority(2),
316 )
317 }))
225 318 }
226 319
227 320 fn separator() -> Div {
@@ -232,19 +325,42 @@ fn separator() -> Div {
232 325 .bg(theme::popup_hover())
233 326 }
234 327
235 fn bar(state: &dyn Fn(FormatButton) -> FormatState, on_press: PressHandler) -> Div {
236 let mut row = h_flex().gap(px(BUTTON_GAP)).items_center();
237 for (index, group) in GROUPS.iter().enumerate() {
328 fn groups_row(
329 groups: &[&[FormatButton]],
330 state: &dyn Fn(FormatButton) -> ButtonState,
331 on_press: PressHandler,
332 overlay: &mut dyn FnMut(FormatButton) -> Option<AnyElement>,
333 ) -> Div {
334 let mut row = h_flex().flex_wrap().gap(px(BUTTON_GAP)).items_center();
335 for (index, group) in groups.iter().enumerate() {
238 336 if index > 0 {
239 337 row = row.child(separator());
240 338 }
241 339 for format in group.iter().copied() {
242 row = row.child(button(format, state(format), on_press.clone()));
340 row = row.child(button(
341 format,
342 state(format),
343 on_press.clone(),
344 overlay(format),
345 ));
243 346 }
244 347 }
245 348 row
246 349 }
247 350
351 fn bar(state: &dyn Fn(FormatButton) -> ButtonState, on_press: PressHandler) -> Div {
352 groups_row(&SELECTION_GROUPS, state, on_press, &mut |_| None)
353 }
354
355 /// The format bar inside the composer box. `overlay` hangs a popup under a button.
356 pub fn render_format_bar(
357 state: &dyn Fn(FormatButton) -> ButtonState,
358 on_press: PressHandler,
359 overlay: &mut dyn FnMut(FormatButton) -> Option<AnyElement>,
360 ) -> Div {
361 groups_row(&BAR_GROUPS, state, on_press, overlay)
362 }
363
248 364 fn link_editor(field: &Entity<InputState>, refused: bool, on_apply: ApplyHandler) -> Div {
249 365 let row = h_flex()
250 366 .gap(px(BUTTON_GAP * 2.))
@@ -357,7 +473,10 @@ pub fn render(placement: Placement, mode: Mode<'_>) -> AnyElement {
357 473 mod tests {
358 474 use gpui_kit::px;
359 475
360 use super::{ARROW_MARGIN, BUTTON, FormatButton, GROUPS, bar_width, place};
476 use super::{
477 ARROW_MARGIN, BAR_GROUPS, BUTTON, FormatButton, SELECTION_GROUPS, bar_width, place,
478 toggle_symbol, toggle_tooltip,
479 };
361 480
362 481 #[test]
363 482 fn the_bar_centers_over_the_selection() {
@@ -385,9 +504,49 @@ mod tests {
385 504 }
386 505
387 506 #[test]
388 fn the_bar_holds_fifteen_buttons_in_six_groups_with_the_table_last() {
389 assert_eq!(GROUPS.iter().map(|group| group.len()).sum::<usize>(), 15);
390 assert_eq!(GROUPS.last().copied(), Some(&[FormatButton::Table][..]));
391 assert!(bar_width() > 15. * BUTTON);
507 fn the_selection_bar_holds_the_text_formats_without_the_table() {
508 assert_eq!(
509 SELECTION_GROUPS
510 .iter()
511 .map(|group| group.len())
512 .sum::<usize>(),
513 14
514 );
515 assert!(
516 SELECTION_GROUPS
517 .iter()
518 .all(|group| !group.contains(&FormatButton::Table))
519 );
520 assert!(bar_width() > 14. * BUTTON);
521 }
522
523 #[test]
524 fn the_format_bar_has_the_teams_table_group_second_to_last() {
525 let table_group = BAR_GROUPS[BAR_GROUPS.len() - 2];
526 assert_eq!(
527 table_group,
528 &[
529 FormatButton::Table,
530 FormatButton::TableInsertLine,
531 FormatButton::TableRemoveLine
532 ][..]
533 );
534 for group in SELECTION_GROUPS {
535 assert!(BAR_GROUPS.contains(&group));
536 }
537 }
538
539 #[test]
540 fn the_format_toggle_reads_like_teams() {
541 assert_eq!(
542 toggle_tooltip(false),
543 "Show Formatting options (Ctrl+Shift+X)"
544 );
545 assert_eq!(
546 toggle_tooltip(true),
547 "Hide Formatting options (Ctrl+Shift+X)"
548 );
549 assert_ne!(toggle_symbol(false), toggle_symbol(true));
550 assert_eq!(FormatButton::Strike.tooltip(), "Strikethrough (Ctrl+Alt+X)");
392 551 }
393 552 }
@@ -20,6 +20,7 @@ const ARROW_MARGIN: f32 = 12.;
20const LINK_FIELD_WIDTH: f32 = 240.;
20const LINK_FIELD_WIDTH: f32 = 240.;
21const APPLY_WIDTH: f32 = 64.;
21const APPLY_WIDTH: f32 = 64.;
22const ACTIVE_ALPHA: f32 = 0.22;
22const ACTIVE_ALPHA: f32 = 0.22;
23const DISABLED_OPACITY: f32 = 0.4;
23const SMALL_LETTER: f32 = 11.;
24const SMALL_LETTER: f32 = 11.;
24const NORMAL_LETTER: f32 = 15.;
25const NORMAL_LETTER: f32 = 15.;
25const LARGE_LETTER: f32 = 19.;
26const LARGE_LETTER: f32 = 19.;
@@ -43,29 +44,93 @@ pub enum FormatButton {
43 Numbered,
44 Numbered,
44 Quote,
45 Quote,
45 Table,
46 Table,
47 TableInsertLine,
48 TableRemoveLine,
46}
49}
47
50
48pub const GROUPS: [&[FormatButton]; 6] = [
51const STYLE_GROUP: &[FormatButton] = &[
49 &[
52 FormatButton::Bold,
50 FormatButton::Bold,
53 FormatButton::Italic,
51 FormatButton::Italic,
54 FormatButton::Underline,
52 FormatButton::Underline,
55 FormatButton::Strike,
53 FormatButton::Strike,
54 ],
55 &[FormatButton::Superscript, FormatButton::Subscript],
56 &[
57 FormatButton::SizeSmall,
58 FormatButton::SizeNormal,
59 FormatButton::SizeLarge,
60 ],
61 &[FormatButton::Code, FormatButton::Link],
62 &[
63 FormatButton::Bulleted,
64 FormatButton::Numbered,
65 FormatButton::Quote,
66 ],
67 &[FormatButton::Table],
68];
56];
57const SCRIPT_GROUP: &[FormatButton] = &[FormatButton::Superscript, FormatButton::Subscript];
58const SIZE_GROUP: &[FormatButton] = &[
59 FormatButton::SizeSmall,
60 FormatButton::SizeNormal,
61 FormatButton::SizeLarge,
62];
63const INLINE_GROUP: &[FormatButton] = &[FormatButton::Code, FormatButton::Link];
64const LINE_GROUP: &[FormatButton] = &[
65 FormatButton::Bulleted,
66 FormatButton::Numbered,
67 FormatButton::Quote,
68];
69const TABLE_GROUP: &[FormatButton] = &[
70 FormatButton::Table,
71 FormatButton::TableInsertLine,
72 FormatButton::TableRemoveLine,
73];
74
75/// The floating bar over a selection: text formats only, tables live in the format bar.
76pub const SELECTION_GROUPS: [&[FormatButton]; 5] = [
77 STYLE_GROUP,
78 SCRIPT_GROUP,
79 SIZE_GROUP,
80 INLINE_GROUP,
81 LINE_GROUP,
82];
83
84/// The format bar the Format button opens, table group second to last like Teams.
85pub const BAR_GROUPS: [&[FormatButton]; 6] = [
86 STYLE_GROUP,
87 SCRIPT_GROUP,
88 SIZE_GROUP,
89 INLINE_GROUP,
90 TABLE_GROUP,
91 LINE_GROUP,
92];
93
94pub const TOGGLE_SHORTCUT: &str = "Ctrl+Shift+X";
95
96pub fn toggle_tooltip(open: bool) -> String {
97 let label = if open {
98 "Hide Formatting options"
99 } else {
100 "Show Formatting options"
101 };
102 format!("{label} ({TOGGLE_SHORTCUT})")
103}
104
105pub fn toggle_symbol(open: bool) -> &'static str {
106 if open {
107 "fluent_text_edit_style_filled"
108 } else {
109 "fluent_text_edit_style"
110 }
111}
112
113#[derive(Debug, Clone, Copy, PartialEq, Eq)]
114pub struct ButtonState {
115 pub format: FormatState,
116 pub enabled: bool,
117}
118
119impl ButtonState {
120 pub fn enabled(format: FormatState) -> Self {
121 ButtonState {
122 format,
123 enabled: true,
124 }
125 }
126
127 pub fn disabled() -> Self {
128 ButtonState {
129 format: FormatState::Off,
130 enabled: false,
131 }
132 }
133}
69
134
70impl FormatButton {
135impl FormatButton {
71 fn tooltip(self) -> &'static str {
136 fn tooltip(self) -> &'static str {
@@ -73,7 +138,7 @@ impl FormatButton {
73 FormatButton::Bold => "Bold (Ctrl+B)",
138 FormatButton::Bold => "Bold (Ctrl+B)",
74 FormatButton::Italic => "Italic (Ctrl+I)",
139 FormatButton::Italic => "Italic (Ctrl+I)",
75 FormatButton::Underline => "Underline (Ctrl+U)",
140 FormatButton::Underline => "Underline (Ctrl+U)",
76 FormatButton::Strike => "Strikethrough (Ctrl+Shift+X)",
141 FormatButton::Strike => "Strikethrough (Ctrl+Alt+X)",
77 FormatButton::Superscript => "Superscript (Ctrl+Shift+=)",
142 FormatButton::Superscript => "Superscript (Ctrl+Shift+=)",
78 FormatButton::Subscript => "Subscript (Ctrl+=)",
143 FormatButton::Subscript => "Subscript (Ctrl+=)",
79 FormatButton::SizeSmall => "Small text",
144 FormatButton::SizeSmall => "Small text",
@@ -85,6 +150,8 @@ impl FormatButton {
85 FormatButton::Numbered => "Numbered list",
150 FormatButton::Numbered => "Numbered list",
86 FormatButton::Quote => "Quote",
151 FormatButton::Quote => "Quote",
87 FormatButton::Table => "Insert table",
152 FormatButton::Table => "Insert table",
153 FormatButton::TableInsertLine => "Insert column or row (Ctrl+Alt+=)",
154 FormatButton::TableRemoveLine => "Delete column, row, or table (Ctrl+Alt+-)",
88 }
155 }
89 }
156 }
90
157
@@ -106,6 +173,12 @@ impl FormatButton {
106 FormatButton::Numbered => symbol("format_list_numbered", 18., color).into_any_element(),
173 FormatButton::Numbered => symbol("format_list_numbered", 18., color).into_any_element(),
107 FormatButton::Quote => symbol("format_quote", 18., color).into_any_element(),
174 FormatButton::Quote => symbol("format_quote", 18., color).into_any_element(),
108 FormatButton::Table => symbol("table", 18., color).into_any_element(),
175 FormatButton::Table => symbol("table", 18., color).into_any_element(),
176 FormatButton::TableInsertLine => {
177 symbol("fluent_table_add", 20., color).into_any_element()
178 }
179 FormatButton::TableRemoveLine => {
180 symbol("fluent_table_dismiss", 20., color).into_any_element()
181 }
109 }
182 }
110 }
183 }
111}
184}
@@ -134,9 +207,10 @@ fn sized_letter(size: f32, color: Hsla) -> AnyElement {
134}
207}
135
208
136pub fn bar_width() -> f32 {
209pub fn bar_width() -> f32 {
137 let buttons: usize = GROUPS.iter().map(|group| group.len()).sum();
210 let groups = SELECTION_GROUPS;
138 let gaps: usize = GROUPS.iter().map(|group| group.len() - 1).sum();
211 let buttons: usize = groups.iter().map(|group| group.len()).sum();
139 let separators = (GROUPS.len() - 1) as f32 * (1. + 2. * SEPARATOR_MARGIN);
212 let gaps: usize = groups.iter().map(|group| group.len() - 1).sum();
213 let separators = (groups.len() - 1) as f32 * (1. + 2. * SEPARATOR_MARGIN);
140 2. * PADDING + buttons as f32 * BUTTON + gaps as f32 * BUTTON_GAP + separators
214 2. * PADDING + buttons as f32 * BUTTON + gaps as f32 * BUTTON_GAP + separators
141}
215}
142
216
@@ -172,7 +246,7 @@ pub type ApplyHandler = Rc<dyn Fn(&mut Window, &mut App)>;
172
246
173pub enum Mode<'a> {
247pub enum Mode<'a> {
174 Bar {
248 Bar {
175 state: &'a dyn Fn(FormatButton) -> FormatState,
249 state: &'a dyn Fn(FormatButton) -> ButtonState,
176 on_press: PressHandler,
250 on_press: PressHandler,
177 },
251 },
178 Link {
252 Link {
@@ -182,8 +256,13 @@ pub enum Mode<'a> {
182 },
256 },
183}
257}
184
258
185fn button(format: FormatButton, state: FormatState, on_press: PressHandler) -> impl IntoElement {
259fn button(
186 let on = state == FormatState::On;
260 format: FormatButton,
261 state: ButtonState,
262 on_press: PressHandler,
263 overlay: Option<AnyElement>,
264) -> impl IntoElement {
265 let on = state.enabled && state.format == FormatState::On;
187 let color = if on {
266 let color = if on {
188 theme::accent_soft()
267 theme::accent_soft()
189 } else {
268 } else {
@@ -198,30 +277,44 @@ fn button(format: FormatButton, state: FormatState, on_press: PressHandler) -> i
198 .items_center()
277 .items_center()
199 .justify_center()
278 .justify_center()
200 .rounded(px(6.))
279 .rounded(px(6.))
201 .cursor_pointer()
202 .when(on, |button| {
203 button.bg(theme::accent().opacity(ACTIVE_ALPHA))
204 })
205 .hover(|button| button.bg(theme::popup_hover()))
206 .tooltip(move |window, cx| Tooltip::new(format.tooltip()).build(window, cx))
280 .tooltip(move |window, cx| Tooltip::new(format.tooltip()).build(window, cx))
207 .child(format.content(color))
281 .child(format.content(color))
208 .when(state == FormatState::Mixed, |button| {
209 button.child(
210 div()
211 .absolute()
212 .bottom(px(2.))
213 .left(px(7.))
214 .w(px(BUTTON - 16.))
215 .h(px(2.))
216 .rounded(px(1.))
217 .bg(theme::accent()),
218 )
219 })
220 .on_mouse_down(MouseButton::Left, |_, window, cx| {
282 .on_mouse_down(MouseButton::Left, |_, window, cx| {
221 window.prevent_default();
283 window.prevent_default();
222 cx.stop_propagation();
284 cx.stop_propagation();
223 })
285 })
224 .on_click(move |_, window, cx| on_press(format, window, cx))
286 .when(!state.enabled, |button| button.opacity(DISABLED_OPACITY))
287 .when(state.enabled, |button| {
288 button
289 .cursor_pointer()
290 .when(on, |button| {
291 button.bg(theme::accent().opacity(ACTIVE_ALPHA))
292 })
293 .hover(|button| button.bg(theme::popup_hover()))
294 .when(state.format == FormatState::Mixed, |button| {
295 button.child(
296 div()
297 .absolute()
298 .bottom(px(2.))
299 .left(px(7.))
300 .w(px(BUTTON - 16.))
301 .h(px(2.))
302 .rounded(px(1.))
303 .bg(theme::accent()),
304 )
305 })
306 .on_click(move |_, window, cx| on_press(format, window, cx))
307 })
308 .children(overlay.map(|overlay| {
309 div().absolute().top(px(BUTTON + GAP / 2.)).left_0().child(
310 deferred(
311 anchored()
312 .snap_to_window_with_margin(px(GAP))
313 .child(overlay),
314 )
315 .with_priority(2),
316 )
317 }))
225}
318}
226
319
227fn separator() -> Div {
320fn separator() -> Div {
@@ -232,19 +325,42 @@ fn separator() -> Div {
232 .bg(theme::popup_hover())
325 .bg(theme::popup_hover())
233}
326}
234
327
235fn bar(state: &dyn Fn(FormatButton) -> FormatState, on_press: PressHandler) -> Div {
328fn groups_row(
236 let mut row = h_flex().gap(px(BUTTON_GAP)).items_center();
329 groups: &[&[FormatButton]],
237 for (index, group) in GROUPS.iter().enumerate() {
330 state: &dyn Fn(FormatButton) -> ButtonState,
331 on_press: PressHandler,
332 overlay: &mut dyn FnMut(FormatButton) -> Option<AnyElement>,
333) -> Div {
334 let mut row = h_flex().flex_wrap().gap(px(BUTTON_GAP)).items_center();
335 for (index, group) in groups.iter().enumerate() {
238 if index > 0 {
336 if index > 0 {
239 row = row.child(separator());
337 row = row.child(separator());
240 }
338 }
241 for format in group.iter().copied() {
339 for format in group.iter().copied() {
242 row = row.child(button(format, state(format), on_press.clone()));
340 row = row.child(button(
341 format,
342 state(format),
343 on_press.clone(),
344 overlay(format),
345 ));
243 }
346 }
244 }
347 }
245 row
348 row
246}
349}
247
350
351fn bar(state: &dyn Fn(FormatButton) -> ButtonState, on_press: PressHandler) -> Div {
352 groups_row(&SELECTION_GROUPS, state, on_press, &mut |_| None)
353}
354
355/// The format bar inside the composer box. `overlay` hangs a popup under a button.
356pub fn render_format_bar(
357 state: &dyn Fn(FormatButton) -> ButtonState,
358 on_press: PressHandler,
359 overlay: &mut dyn FnMut(FormatButton) -> Option<AnyElement>,
360) -> Div {
361 groups_row(&BAR_GROUPS, state, on_press, overlay)
362}
363
248fn link_editor(field: &Entity<InputState>, refused: bool, on_apply: ApplyHandler) -> Div {
364fn link_editor(field: &Entity<InputState>, refused: bool, on_apply: ApplyHandler) -> Div {
249 let row = h_flex()
365 let row = h_flex()
250 .gap(px(BUTTON_GAP * 2.))
366 .gap(px(BUTTON_GAP * 2.))
@@ -357,7 +473,10 @@ pub fn render(placement: Placement, mode: Mode<'_>) -> AnyElement {
357mod tests {
473mod tests {
358 use gpui_kit::px;
474 use gpui_kit::px;
359
475
360 use super::{ARROW_MARGIN, BUTTON, FormatButton, GROUPS, bar_width, place};
476 use super::{
477 ARROW_MARGIN, BAR_GROUPS, BUTTON, FormatButton, SELECTION_GROUPS, bar_width, place,
478 toggle_symbol, toggle_tooltip,
479 };
361
480
362 #[test]
481 #[test]
363 fn the_bar_centers_over_the_selection() {
482 fn the_bar_centers_over_the_selection() {
@@ -385,9 +504,49 @@ mod tests {
385 }
504 }
386
505
387 #[test]
506 #[test]
388 fn the_bar_holds_fifteen_buttons_in_six_groups_with_the_table_last() {
507 fn the_selection_bar_holds_the_text_formats_without_the_table() {
389 assert_eq!(GROUPS.iter().map(|group| group.len()).sum::<usize>(), 15);
508 assert_eq!(
390 assert_eq!(GROUPS.last().copied(), Some(&[FormatButton::Table][..]));
509 SELECTION_GROUPS
391 assert!(bar_width() > 15. * BUTTON);
510 .iter()
511 .map(|group| group.len())
512 .sum::<usize>(),
513 14
514 );
515 assert!(
516 SELECTION_GROUPS
517 .iter()
518 .all(|group| !group.contains(&FormatButton::Table))
519 );
520 assert!(bar_width() > 14. * BUTTON);
521 }
522
523 #[test]
524 fn the_format_bar_has_the_teams_table_group_second_to_last() {
525 let table_group = BAR_GROUPS[BAR_GROUPS.len() - 2];
526 assert_eq!(
527 table_group,
528 &[
529 FormatButton::Table,
530 FormatButton::TableInsertLine,
531 FormatButton::TableRemoveLine
532 ][..]
533 );
534 for group in SELECTION_GROUPS {
535 assert!(BAR_GROUPS.contains(&group));
536 }
537 }
538
539 #[test]
540 fn the_format_toggle_reads_like_teams() {
541 assert_eq!(
542 toggle_tooltip(false),
543 "Show Formatting options (Ctrl+Shift+X)"
544 );
545 assert_eq!(
546 toggle_tooltip(true),
547 "Hide Formatting options (Ctrl+Shift+X)"
548 );
549 assert_ne!(toggle_symbol(false), toggle_symbol(true));
550 assert_eq!(FormatButton::Strike.tooltip(), "Strikethrough (Ctrl+Alt+X)");
392 }
551 }
393}
552}
crates/app/src/views/shortcuts.rs +38 −5
@@ -1,15 +1,17 @@
1 1 use gpui_kit::*;
2 2
3 3 use super::actions::{
4 COMPOSER_KEY_CONTEXT as COMPOSER_CONTEXT, EditLink, NewChat, OpenSwitcher, PastePlainText,
5 ReplyToHovered, ScheduleSend, ToggleBold, ToggleCallMute, ToggleCode, ToggleItalic,
6 ToggleStrike, ToggleSubscript, ToggleSuperscript, ToggleUnderline,
4 COMPOSER_KEY_CONTEXT as COMPOSER_CONTEXT, EditLink, InsertTableLine, NewChat, OpenSwitcher,
5 PastePlainText, RemoveTableLine, ReplyToHovered, ScheduleSend, ToggleBold, ToggleCallMute,
6 ToggleCode, ToggleFormatBar, ToggleItalic, ToggleStrike, ToggleSubscript, ToggleSuperscript,
7 ToggleUnderline,
7 8 };
8 9
9 10 const TEXT_FIELD_CONTEXT: &str = "Input";
10 11 const OUTSIDE_COMPOSER_CONTEXT: &str = "!Composer";
11 12 const OUTSIDE_TEXT_FIELD_CONTEXT: &str = "!Input";
12 const ALTERNATE_KEYS: &[(&str, &str)] = &[("alt-shift-=", "alt-shift-+")];
13 const ALTERNATE_KEYS: &[(&str, &str)] =
14 &[("alt-shift-=", "alt-shift-+"), ("ctrl-alt-=", "ctrl-alt-+")];
13 15
14 16 actions!(
15 17 teams,
@@ -117,13 +119,16 @@ pub const SHORTCUTS: &[Shortcut] = &[
117 119 shortcut!(Composer, Composer, "Bold", "ctrl-b", Some("cmd-b"), ToggleBold),
118 120 shortcut!(Composer, Composer, "Italic", "ctrl-i", Some("cmd-i"), ToggleItalic),
119 121 shortcut!(Composer, Composer, "Underline", "ctrl-u", Some("cmd-u"), ToggleUnderline),
120 shortcut!(Composer, Composer, "Strikethrough", "ctrl-shift-x", None, ToggleStrike),
122 shortcut!(Composer, Composer, "Format", "ctrl-shift-x", Some("cmd-shift-x"), ToggleFormatBar),
123 shortcut!(Composer, Composer, "Strikethrough", "ctrl-alt-x", None, ToggleStrike),
121 124 shortcut!(Composer, Composer, "Superscript", "ctrl-shift-=", None, ToggleSuperscript),
122 125 shortcut!(Composer, Composer, "Subscript", "ctrl-=", None, ToggleSubscript),
123 126 shortcut!(Composer, Composer, "Code", "ctrl-shift-c", None, ToggleCode),
124 127 shortcut!(Composer, Composer, "Insert link", "ctrl-k", Some("cmd-k"), EditLink),
125 128 shortcut!(Composer, Composer, "Paste as plain text", "ctrl-shift-v", Some("cmd-shift-v"), PastePlainText),
126 129 shortcut!(Composer, Composer, "Schedule send", "ctrl-shift-enter", None, ScheduleSend),
130 shortcut!(Composer, Composer, "Insert column or row", "ctrl-alt-=", None, InsertTableLine),
131 shortcut!(Composer, Composer, "Delete column, row, or table", "ctrl-alt--", None, RemoveTableLine),
127 132 shortcut!(Calls, GlobalOverTextFields, "Accept incoming call", "ctrl-shift-a", Some("cmd-shift-a"), AcceptCall),
128 133 shortcut!(Calls, GlobalOverTextFields, "Decline incoming call", "ctrl-shift-d", Some("cmd-shift-d"), DeclineCall),
129 134 shortcut!(Calls, GlobalOverTextFields, "Join the meeting from its reminder", "ctrl-shift-j", Some("cmd-shift-j"), JoinMeetingFromToast),
@@ -348,5 +353,33 @@ mod tests {
348 353 assert_eq!(display_keys("alt-shift-="), "Alt+Shift+Plus");
349 354 assert_eq!(display_keys("alt-shift--"), "Alt+Shift+Minus");
350 355 assert_eq!(display_keys("alt-shift-0"), "Alt+Shift+0");
356 assert_eq!(display_keys("ctrl-alt--"), "Ctrl+Alt+Minus");
357 }
358
359 #[gpui_kit::test]
360 fn composer_format_and_table_keys_match_teams(cx: &mut TestAppContext) {
361 registered(cx);
362 let typing = ["AppShell", "Composer", "Input"];
363 let action_of = |label: &str| {
364 action_name(
365 SHORTCUTS
366 .iter()
367 .find(|shortcut| shortcut.label == label)
368 .unwrap(),
369 )
370 };
371 for (keys, label) in [
372 ("ctrl-shift-x", "Format"),
373 ("ctrl-alt-x", "Strikethrough"),
374 ("ctrl-alt-=", "Insert column or row"),
375 ("ctrl-alt-+", "Insert column or row"),
376 ("ctrl-alt--", "Delete column, row, or table"),
377 ] {
378 assert_eq!(
379 bound_action(cx, keys, &typing),
380 Some(action_of(label)),
381 "{keys}"
382 );
383 }
351 384 }
352 385 }
@@ -1,15 +1,17 @@
1use gpui_kit::*;
1use gpui_kit::*;
2
2
3use super::actions::{
3use super::actions::{
4 COMPOSER_KEY_CONTEXT as COMPOSER_CONTEXT, EditLink, NewChat, OpenSwitcher, PastePlainText,
4 COMPOSER_KEY_CONTEXT as COMPOSER_CONTEXT, EditLink, InsertTableLine, NewChat, OpenSwitcher,
5 ReplyToHovered, ScheduleSend, ToggleBold, ToggleCallMute, ToggleCode, ToggleItalic,
5 PastePlainText, RemoveTableLine, ReplyToHovered, ScheduleSend, ToggleBold, ToggleCallMute,
6 ToggleStrike, ToggleSubscript, ToggleSuperscript, ToggleUnderline,
6 ToggleCode, ToggleFormatBar, ToggleItalic, ToggleStrike, ToggleSubscript, ToggleSuperscript,
7 ToggleUnderline,
7};
8};
8
9
9const TEXT_FIELD_CONTEXT: &str = "Input";
10const TEXT_FIELD_CONTEXT: &str = "Input";
10const OUTSIDE_COMPOSER_CONTEXT: &str = "!Composer";
11const OUTSIDE_COMPOSER_CONTEXT: &str = "!Composer";
11const OUTSIDE_TEXT_FIELD_CONTEXT: &str = "!Input";
12const OUTSIDE_TEXT_FIELD_CONTEXT: &str = "!Input";
12const ALTERNATE_KEYS: &[(&str, &str)] = &[("alt-shift-=", "alt-shift-+")];
13const ALTERNATE_KEYS: &[(&str, &str)] =
14 &[("alt-shift-=", "alt-shift-+"), ("ctrl-alt-=", "ctrl-alt-+")];
13
15
14actions!(
16actions!(
15 teams,
17 teams,
@@ -117,13 +119,16 @@ pub const SHORTCUTS: &[Shortcut] = &[
117 shortcut!(Composer, Composer, "Bold", "ctrl-b", Some("cmd-b"), ToggleBold),
119 shortcut!(Composer, Composer, "Bold", "ctrl-b", Some("cmd-b"), ToggleBold),
118 shortcut!(Composer, Composer, "Italic", "ctrl-i", Some("cmd-i"), ToggleItalic),
120 shortcut!(Composer, Composer, "Italic", "ctrl-i", Some("cmd-i"), ToggleItalic),
119 shortcut!(Composer, Composer, "Underline", "ctrl-u", Some("cmd-u"), ToggleUnderline),
121 shortcut!(Composer, Composer, "Underline", "ctrl-u", Some("cmd-u"), ToggleUnderline),
120 shortcut!(Composer, Composer, "Strikethrough", "ctrl-shift-x", None, ToggleStrike),
122 shortcut!(Composer, Composer, "Format", "ctrl-shift-x", Some("cmd-shift-x"), ToggleFormatBar),
123 shortcut!(Composer, Composer, "Strikethrough", "ctrl-alt-x", None, ToggleStrike),
121 shortcut!(Composer, Composer, "Superscript", "ctrl-shift-=", None, ToggleSuperscript),
124 shortcut!(Composer, Composer, "Superscript", "ctrl-shift-=", None, ToggleSuperscript),
122 shortcut!(Composer, Composer, "Subscript", "ctrl-=", None, ToggleSubscript),
125 shortcut!(Composer, Composer, "Subscript", "ctrl-=", None, ToggleSubscript),
123 shortcut!(Composer, Composer, "Code", "ctrl-shift-c", None, ToggleCode),
126 shortcut!(Composer, Composer, "Code", "ctrl-shift-c", None, ToggleCode),
124 shortcut!(Composer, Composer, "Insert link", "ctrl-k", Some("cmd-k"), EditLink),
127 shortcut!(Composer, Composer, "Insert link", "ctrl-k", Some("cmd-k"), EditLink),
125 shortcut!(Composer, Composer, "Paste as plain text", "ctrl-shift-v", Some("cmd-shift-v"), PastePlainText),
128 shortcut!(Composer, Composer, "Paste as plain text", "ctrl-shift-v", Some("cmd-shift-v"), PastePlainText),
126 shortcut!(Composer, Composer, "Schedule send", "ctrl-shift-enter", None, ScheduleSend),
129 shortcut!(Composer, Composer, "Schedule send", "ctrl-shift-enter", None, ScheduleSend),
130 shortcut!(Composer, Composer, "Insert column or row", "ctrl-alt-=", None, InsertTableLine),
131 shortcut!(Composer, Composer, "Delete column, row, or table", "ctrl-alt--", None, RemoveTableLine),
127 shortcut!(Calls, GlobalOverTextFields, "Accept incoming call", "ctrl-shift-a", Some("cmd-shift-a"), AcceptCall),
132 shortcut!(Calls, GlobalOverTextFields, "Accept incoming call", "ctrl-shift-a", Some("cmd-shift-a"), AcceptCall),
128 shortcut!(Calls, GlobalOverTextFields, "Decline incoming call", "ctrl-shift-d", Some("cmd-shift-d"), DeclineCall),
133 shortcut!(Calls, GlobalOverTextFields, "Decline incoming call", "ctrl-shift-d", Some("cmd-shift-d"), DeclineCall),
129 shortcut!(Calls, GlobalOverTextFields, "Join the meeting from its reminder", "ctrl-shift-j", Some("cmd-shift-j"), JoinMeetingFromToast),
134 shortcut!(Calls, GlobalOverTextFields, "Join the meeting from its reminder", "ctrl-shift-j", Some("cmd-shift-j"), JoinMeetingFromToast),
@@ -348,5 +353,33 @@ mod tests {
348 assert_eq!(display_keys("alt-shift-="), "Alt+Shift+Plus");
353 assert_eq!(display_keys("alt-shift-="), "Alt+Shift+Plus");
349 assert_eq!(display_keys("alt-shift--"), "Alt+Shift+Minus");
354 assert_eq!(display_keys("alt-shift--"), "Alt+Shift+Minus");
350 assert_eq!(display_keys("alt-shift-0"), "Alt+Shift+0");
355 assert_eq!(display_keys("alt-shift-0"), "Alt+Shift+0");
356 assert_eq!(display_keys("ctrl-alt--"), "Ctrl+Alt+Minus");
357 }
358
359 #[gpui_kit::test]
360 fn composer_format_and_table_keys_match_teams(cx: &mut TestAppContext) {
361 registered(cx);
362 let typing = ["AppShell", "Composer", "Input"];
363 let action_of = |label: &str| {
364 action_name(
365 SHORTCUTS
366 .iter()
367 .find(|shortcut| shortcut.label == label)
368 .unwrap(),
369 )
370 };
371 for (keys, label) in [
372 ("ctrl-shift-x", "Format"),
373 ("ctrl-alt-x", "Strikethrough"),
374 ("ctrl-alt-=", "Insert column or row"),
375 ("ctrl-alt-+", "Insert column or row"),
376 ("ctrl-alt--", "Delete column, row, or table"),
377 ] {
378 assert_eq!(
379 bound_action(cx, keys, &typing),
380 Some(action_of(label)),
381 "{keys}"
382 );
383 }
351 }
384 }
352}
385}