BlueGit

ict rusty-teams

fix(notify): size toasts from the measured preview so actions are not clipped

The height guessed 40 chars per line from the raw preview, without the
'Name: ' prefix shown in group and channel toasts.

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

metis Daniel Däschle · pushed by metis

Authored 2026-10-08 · 7 weeks ago

Hash
698feaaeaf934ec09dba916ce055d9e3ace8cd8c
Parent
656a19d7
3 files changed +57 −28
crates/app/src/notify/center.rs +31 −2
@@ -18,6 +18,7 @@ use super::settings_view::SettingsView;
18 18 use super::stack::{
19 19 FADE_IN, FAST_FADE, ReplyState, SENT_DURATION, SLOW_FADE, ToastModel, ToastStack,
20 20 };
21 use super::text::describe;
21 22 use super::toast::{PillView, ToastView};
22 23 use crate::app_state::{AppEvent, AppState, Selection};
23 24 use crate::data::{self, Directory, PresenceKind};
@@ -308,7 +309,10 @@ impl NotificationCenter {
308 309 .stack
309 310 .visible()
310 311 .iter()
311 .map(|toast| layout::toast_height(toast, self.settings.preview))
312 .map(|toast| {
313 let lines = preview_lines(&describe(toast, self.settings.preview).preview, cx);
314 layout::toast_height(toast, lines)
315 })
312 316 .collect();
313 317 let with_hide_all = visible.len() > 1 || self.stack.queued_count() > 0;
314 318 let slots = layout::stack_slots(area, self.settings.corner, &heights, with_hide_all);
@@ -781,6 +785,21 @@ fn open_popup<V: Render>(
781 785 });
782 786 }
783 787
788 fn preview_lines(text: &str, cx: &App) -> usize {
789 let mut wrapper = cx
790 .text_system()
791 .line_wrapper(font(crate::theme::font_family()), px(layout::PREVIEW_SIZE));
792 text.split('\n')
793 .map(|line| {
794 let fragments = [LineFragment::text(line)];
795 wrapper
796 .wrap_line(&fragments, px(layout::PREVIEW_WIDTH), IndentAdjustment::NoIndent)
797 .count()
798 + 1
799 })
800 .sum()
801 }
802
784 803 fn popup_options(slot: Slot, area: WorkArea) -> WindowOptions {
785 804 let scale = area.scale;
786 805 let bounds = Bounds::new(
@@ -898,7 +917,7 @@ fn demo_incoming() -> Vec<Incoming> {
898 917 mod tests {
899 918 use store::{ChannelRecord, ChatRecord, SidebarTeam, TeamRecord};
900 919
901 use super::{selection_for, unread_summary};
920 use super::{preview_lines, selection_for, unread_summary};
902 921 use crate::app_state::Selection;
903 922 use store::Sidebar;
904 923
@@ -954,4 +973,14 @@ mod tests {
954 973 );
955 974 assert_eq!(selection_for(&sidebar(), "x"), None);
956 975 }
976
977 #[gpui_kit::test]
978 fn preview_lines_follow_the_wrapped_text(cx: &mut gpui_kit::TestAppContext) {
979 cx.update(|cx| {
980 assert_eq!(preview_lines("ok", cx), 1);
981 assert_eq!(preview_lines("a\nb", cx), 2);
982 let long = "Cristina: ok, thanks for the info! I think not now ".repeat(3);
983 assert!(preview_lines(&long, cx) >= 2);
984 });
985 }
957 986 }
@@ -18,6 +18,7 @@ use super::settings_view::SettingsView;
18use super::stack::{
18use super::stack::{
19 FADE_IN, FAST_FADE, ReplyState, SENT_DURATION, SLOW_FADE, ToastModel, ToastStack,
19 FADE_IN, FAST_FADE, ReplyState, SENT_DURATION, SLOW_FADE, ToastModel, ToastStack,
20};
20};
21use super::text::describe;
21use super::toast::{PillView, ToastView};
22use super::toast::{PillView, ToastView};
22use crate::app_state::{AppEvent, AppState, Selection};
23use crate::app_state::{AppEvent, AppState, Selection};
23use crate::data::{self, Directory, PresenceKind};
24use crate::data::{self, Directory, PresenceKind};
@@ -308,7 +309,10 @@ impl NotificationCenter {
308 .stack
309 .stack
309 .visible()
310 .visible()
310 .iter()
311 .iter()
311 .map(|toast| layout::toast_height(toast, self.settings.preview))
312 .map(|toast| {
313 let lines = preview_lines(&describe(toast, self.settings.preview).preview, cx);
314 layout::toast_height(toast, lines)
315 })
312 .collect();
316 .collect();
313 let with_hide_all = visible.len() > 1 || self.stack.queued_count() > 0;
317 let with_hide_all = visible.len() > 1 || self.stack.queued_count() > 0;
314 let slots = layout::stack_slots(area, self.settings.corner, &heights, with_hide_all);
318 let slots = layout::stack_slots(area, self.settings.corner, &heights, with_hide_all);
@@ -781,6 +785,21 @@ fn open_popup<V: Render>(
781 });
785 });
782}
786}
783
787
788fn preview_lines(text: &str, cx: &App) -> usize {
789 let mut wrapper = cx
790 .text_system()
791 .line_wrapper(font(crate::theme::font_family()), px(layout::PREVIEW_SIZE));
792 text.split('\n')
793 .map(|line| {
794 let fragments = [LineFragment::text(line)];
795 wrapper
796 .wrap_line(&fragments, px(layout::PREVIEW_WIDTH), IndentAdjustment::NoIndent)
797 .count()
798 + 1
799 })
800 .sum()
801}
802
784fn popup_options(slot: Slot, area: WorkArea) -> WindowOptions {
803fn popup_options(slot: Slot, area: WorkArea) -> WindowOptions {
785 let scale = area.scale;
804 let scale = area.scale;
786 let bounds = Bounds::new(
805 let bounds = Bounds::new(
@@ -898,7 +917,7 @@ fn demo_incoming() -> Vec<Incoming> {
898mod tests {
917mod tests {
899 use store::{ChannelRecord, ChatRecord, SidebarTeam, TeamRecord};
918 use store::{ChannelRecord, ChatRecord, SidebarTeam, TeamRecord};
900
919
901 use super::{selection_for, unread_summary};
920 use super::{preview_lines, selection_for, unread_summary};
902 use crate::app_state::Selection;
921 use crate::app_state::Selection;
903 use store::Sidebar;
922 use store::Sidebar;
904
923
@@ -954,4 +973,14 @@ mod tests {
954 );
973 );
955 assert_eq!(selection_for(&sidebar(), "x"), None);
974 assert_eq!(selection_for(&sidebar(), "x"), None);
956 }
975 }
976
977 #[gpui_kit::test]
978 fn preview_lines_follow_the_wrapped_text(cx: &mut gpui_kit::TestAppContext) {
979 cx.update(|cx| {
980 assert_eq!(preview_lines("ok", cx), 1);
981 assert_eq!(preview_lines("a\nb", cx), 2);
982 let long = "Cristina: ok, thanks for the info! I think not now ".repeat(3);
983 assert!(preview_lines(&long, cx) >= 2);
984 });
985 }
957}
986}
crates/app/src/notify/layout.rs +16 −17
@@ -1,5 +1,5 @@
1 1 use super::platform::WorkArea;
2 use super::rules::{Corner, Preview};
2 use super::rules::Corner;
3 3 use super::stack::{ReplyState, ToastModel};
4 4
5 5 pub const TOAST_WIDTH: f32 = 360.;
@@ -11,7 +11,14 @@ pub const ACTION_BUTTON: f32 = 28.;
11 11 pub const SEND_BUTTON: f32 = 32.;
12 12 const BASE_HEIGHT: f32 = 62.;
13 13 const PREVIEW_LINE: f32 = 18.;
14 const CHARS_PER_LINE: usize = 40;
14 pub const PREVIEW_SIZE: f32 = 13.;
15 pub const PREVIEW_MAX_LINES: usize = 2;
16 pub const TOAST_PADDING: f32 = 12.;
17 pub const AVATAR_SIZE: f32 = 40.;
18 pub const AVATAR_GAP: f32 = 12.;
19 pub const MENTION_BORDER: f32 = 2.;
20 pub const PREVIEW_WIDTH: f32 =
21 TOAST_WIDTH - 2. * (TOAST_PADDING + MENTION_BORDER) - AVATAR_SIZE - AVATAR_GAP;
15 22 const ACTION_ROW_SPACING: f32 = 4.;
16 23 const REPLY_FIELD_FRAME: f32 = 14.;
17 24 const REPLY_HINT_ROW: f32 = 20.;
@@ -26,15 +33,8 @@ pub struct Slot {
26 33 pub height: i32,
27 34 }
28 35
29 fn preview_lines(model: &ToastModel, preview_on: bool) -> usize {
30 match (&model.preview, preview_on) {
31 (Preview::Text(text), true) => text.chars().count().div_ceil(CHARS_PER_LINE).clamp(1, 2),
32 _ => 1,
33 }
34 }
35
36 pub fn toast_height(model: &ToastModel, preview_on: bool) -> f32 {
37 let base = BASE_HEIGHT + preview_lines(model, preview_on) as f32 * PREVIEW_LINE;
36 pub fn toast_height(model: &ToastModel, preview_lines: usize) -> f32 {
37 let base = BASE_HEIGHT + preview_lines.clamp(1, PREVIEW_MAX_LINES) as f32 * PREVIEW_LINE;
38 38 match model.reply {
39 39 ReplyState::Sent | ReplyState::Failed => CONFIRM_HEIGHT,
40 40 ReplyState::Open => {
@@ -153,7 +153,7 @@ mod tests {
153 153
154 154 #[test]
155 155 fn heights_stay_in_board_range() {
156 use super::super::rules::ChatKind;
156 use super::super::rules::{ChatKind, Preview};
157 157 use super::super::stack::{ReplyState, ToastTimer};
158 158 let model = |text: &str| ToastModel {
159 159 id: 1,
@@ -175,10 +175,9 @@ mod tests {
175 175 dismissed: false,
176 176 fade: super::super::stack::Fade::shown(std::time::Instant::now()),
177 177 };
178 let short = toast_height(&model("Hi"), true);
179 let long = toast_height(&model(&"x".repeat(200)), true);
180 assert_eq!(short, 112.);
181 assert_eq!(long, 130.);
182 assert_eq!(toast_height(&model(&"x".repeat(200)), false), short);
178 assert_eq!(toast_height(&model("Hi"), 1), 112.);
179 assert_eq!(toast_height(&model("Hi"), 2), 130.);
180 assert_eq!(toast_height(&model("Hi"), 5), 130.);
181 assert_eq!(toast_height(&model("Hi"), 0), 112.);
183 182 }
184 183 }
@@ -1,5 +1,5 @@
1use super::platform::WorkArea;
1use super::platform::WorkArea;
2use super::rules::{Corner, Preview};
2use super::rules::Corner;
3use super::stack::{ReplyState, ToastModel};
3use super::stack::{ReplyState, ToastModel};
4
4
5pub const TOAST_WIDTH: f32 = 360.;
5pub const TOAST_WIDTH: f32 = 360.;
@@ -11,7 +11,14 @@ pub const ACTION_BUTTON: f32 = 28.;
11pub const SEND_BUTTON: f32 = 32.;
11pub const SEND_BUTTON: f32 = 32.;
12const BASE_HEIGHT: f32 = 62.;
12const BASE_HEIGHT: f32 = 62.;
13const PREVIEW_LINE: f32 = 18.;
13const PREVIEW_LINE: f32 = 18.;
14const CHARS_PER_LINE: usize = 40;
14pub const PREVIEW_SIZE: f32 = 13.;
15pub const PREVIEW_MAX_LINES: usize = 2;
16pub const TOAST_PADDING: f32 = 12.;
17pub const AVATAR_SIZE: f32 = 40.;
18pub const AVATAR_GAP: f32 = 12.;
19pub const MENTION_BORDER: f32 = 2.;
20pub const PREVIEW_WIDTH: f32 =
21 TOAST_WIDTH - 2. * (TOAST_PADDING + MENTION_BORDER) - AVATAR_SIZE - AVATAR_GAP;
15const ACTION_ROW_SPACING: f32 = 4.;
22const ACTION_ROW_SPACING: f32 = 4.;
16const REPLY_FIELD_FRAME: f32 = 14.;
23const REPLY_FIELD_FRAME: f32 = 14.;
17const REPLY_HINT_ROW: f32 = 20.;
24const REPLY_HINT_ROW: f32 = 20.;
@@ -26,15 +33,8 @@ pub struct Slot {
26 pub height: i32,
33 pub height: i32,
27}
34}
28
35
29fn preview_lines(model: &ToastModel, preview_on: bool) -> usize {
36pub fn toast_height(model: &ToastModel, preview_lines: usize) -> f32 {
30 match (&model.preview, preview_on) {
37 let base = BASE_HEIGHT + preview_lines.clamp(1, PREVIEW_MAX_LINES) as f32 * PREVIEW_LINE;
31 (Preview::Text(text), true) => text.chars().count().div_ceil(CHARS_PER_LINE).clamp(1, 2),
32 _ => 1,
33 }
34}
35
36pub fn toast_height(model: &ToastModel, preview_on: bool) -> f32 {
37 let base = BASE_HEIGHT + preview_lines(model, preview_on) as f32 * PREVIEW_LINE;
38 match model.reply {
38 match model.reply {
39 ReplyState::Sent | ReplyState::Failed => CONFIRM_HEIGHT,
39 ReplyState::Sent | ReplyState::Failed => CONFIRM_HEIGHT,
40 ReplyState::Open => {
40 ReplyState::Open => {
@@ -153,7 +153,7 @@ mod tests {
153
153
154 #[test]
154 #[test]
155 fn heights_stay_in_board_range() {
155 fn heights_stay_in_board_range() {
156 use super::super::rules::ChatKind;
156 use super::super::rules::{ChatKind, Preview};
157 use super::super::stack::{ReplyState, ToastTimer};
157 use super::super::stack::{ReplyState, ToastTimer};
158 let model = |text: &str| ToastModel {
158 let model = |text: &str| ToastModel {
159 id: 1,
159 id: 1,
@@ -175,10 +175,9 @@ mod tests {
175 dismissed: false,
175 dismissed: false,
176 fade: super::super::stack::Fade::shown(std::time::Instant::now()),
176 fade: super::super::stack::Fade::shown(std::time::Instant::now()),
177 };
177 };
178 let short = toast_height(&model("Hi"), true);
178 assert_eq!(toast_height(&model("Hi"), 1), 112.);
179 let long = toast_height(&model(&"x".repeat(200)), true);
179 assert_eq!(toast_height(&model("Hi"), 2), 130.);
180 assert_eq!(short, 112.);
180 assert_eq!(toast_height(&model("Hi"), 5), 130.);
181 assert_eq!(long, 130.);
181 assert_eq!(toast_height(&model("Hi"), 0), 112.);
182 assert_eq!(toast_height(&model(&"x".repeat(200)), false), short);
183 }
182 }
184}
183}
crates/app/src/notify/toast.rs +10 −9
@@ -9,7 +9,10 @@ use gpui_kit::prelude::FluentBuilder as _;
9 9 use gpui_kit::*;
10 10
11 11 use super::center::NotificationCenter;
12 use super::layout::{ACTION_BUTTON, SEND_BUTTON};
12 use super::layout::{
13 ACTION_BUTTON, AVATAR_GAP, AVATAR_SIZE, MENTION_BORDER, PREVIEW_MAX_LINES, PREVIEW_SIZE,
14 SEND_BUTTON, TOAST_PADDING,
15 };
13 16 use super::platform;
14 17 use super::rules::ChatKind;
15 18 use super::stack::{ReplyState, ToastModel};
@@ -20,9 +23,7 @@ use crate::views::composer::submitted_text;
20 23 use crate::views::widgets::symbol;
21 24 use crate::theme;
22 25
23 const AVATAR_SIZE: f32 = 40.;
24 26 const CLOSE_SIZE: f32 = 24.;
25 const TOAST_BORDER_MENTION: f32 = 2.;
26 27 const TOAST_RADIUS: f32 = 12.;
27 28
28 29 pub struct ToastView {
@@ -163,10 +164,10 @@ fn highlighted_preview(text: &str, mention: bool) -> AnyElement {
163 164 styled = styled.with_highlights(highlights);
164 165 }
165 166 div()
166 .text_size(px(13.))
167 .text_size(px(PREVIEW_SIZE))
167 168 .line_height(px(18.))
168 169 .text_color(theme::text())
169 .line_clamp(2)
170 .line_clamp(PREVIEW_MAX_LINES)
170 171 .child(styled)
171 172 .into_any_element()
172 173 }
@@ -492,7 +493,7 @@ impl Render for ToastView {
492 493 } else {
493 494 theme::border_strong()
494 495 };
495 let border_width = if mention { TOAST_BORDER_MENTION } else { 1. };
496 let border_width = if mention { MENTION_BORDER } else { 1. };
496 497 let hovered = model.hovered;
497 498 let content = match model.reply {
498 499 ReplyState::Sent | ReplyState::Failed => self.confirmation(&model, cx),
@@ -501,7 +502,7 @@ impl Render for ToastView {
501 502 .child(
502 503 h_flex()
503 504 .w_full()
504 .gap(px(12.))
505 .gap(px(AVATAR_GAP))
505 506 .items_start()
506 507 .child(self.avatar(&model, fill, cx))
507 508 .child(self.body(&model, &text, true)),
@@ -512,7 +513,7 @@ impl Render for ToastView {
512 513 .child(
513 514 h_flex()
514 515 .w_full()
515 .gap(px(12.))
516 .gap(px(AVATAR_GAP))
516 517 .items_start()
517 518 .child(self.avatar(&model, fill, cx))
518 519 .child(self.body(&model, &text, hovered)),
@@ -549,7 +550,7 @@ impl Render for ToastView {
549 550 this.center.update(cx, |center, cx| center.dismiss(id, cx));
550 551 }
551 552 }))
552 .child(div().size_full().p(px(12.)).child(content))
553 .child(div().size_full().p(px(TOAST_PADDING)).child(content))
553 554 .into_any_element()
554 555 }
555 556 }
@@ -9,7 +9,10 @@ use gpui_kit::prelude::FluentBuilder as _;
9use gpui_kit::*;
9use gpui_kit::*;
10
10
11use super::center::NotificationCenter;
11use super::center::NotificationCenter;
12use super::layout::{ACTION_BUTTON, SEND_BUTTON};
12use super::layout::{
13 ACTION_BUTTON, AVATAR_GAP, AVATAR_SIZE, MENTION_BORDER, PREVIEW_MAX_LINES, PREVIEW_SIZE,
14 SEND_BUTTON, TOAST_PADDING,
15};
13use super::platform;
16use super::platform;
14use super::rules::ChatKind;
17use super::rules::ChatKind;
15use super::stack::{ReplyState, ToastModel};
18use super::stack::{ReplyState, ToastModel};
@@ -20,9 +23,7 @@ use crate::views::composer::submitted_text;
20use crate::views::widgets::symbol;
23use crate::views::widgets::symbol;
21use crate::theme;
24use crate::theme;
22
25
23const AVATAR_SIZE: f32 = 40.;
24const CLOSE_SIZE: f32 = 24.;
26const CLOSE_SIZE: f32 = 24.;
25const TOAST_BORDER_MENTION: f32 = 2.;
26const TOAST_RADIUS: f32 = 12.;
27const TOAST_RADIUS: f32 = 12.;
27
28
28pub struct ToastView {
29pub struct ToastView {
@@ -163,10 +164,10 @@ fn highlighted_preview(text: &str, mention: bool) -> AnyElement {
163 styled = styled.with_highlights(highlights);
164 styled = styled.with_highlights(highlights);
164 }
165 }
165 div()
166 div()
166 .text_size(px(13.))
167 .text_size(px(PREVIEW_SIZE))
167 .line_height(px(18.))
168 .line_height(px(18.))
168 .text_color(theme::text())
169 .text_color(theme::text())
169 .line_clamp(2)
170 .line_clamp(PREVIEW_MAX_LINES)
170 .child(styled)
171 .child(styled)
171 .into_any_element()
172 .into_any_element()
172}
173}
@@ -492,7 +493,7 @@ impl Render for ToastView {
492 } else {
493 } else {
493 theme::border_strong()
494 theme::border_strong()
494 };
495 };
495 let border_width = if mention { TOAST_BORDER_MENTION } else { 1. };
496 let border_width = if mention { MENTION_BORDER } else { 1. };
496 let hovered = model.hovered;
497 let hovered = model.hovered;
497 let content = match model.reply {
498 let content = match model.reply {
498 ReplyState::Sent | ReplyState::Failed => self.confirmation(&model, cx),
499 ReplyState::Sent | ReplyState::Failed => self.confirmation(&model, cx),
@@ -501,7 +502,7 @@ impl Render for ToastView {
501 .child(
502 .child(
502 h_flex()
503 h_flex()
503 .w_full()
504 .w_full()
504 .gap(px(12.))
505 .gap(px(AVATAR_GAP))
505 .items_start()
506 .items_start()
506 .child(self.avatar(&model, fill, cx))
507 .child(self.avatar(&model, fill, cx))
507 .child(self.body(&model, &text, true)),
508 .child(self.body(&model, &text, true)),
@@ -512,7 +513,7 @@ impl Render for ToastView {
512 .child(
513 .child(
513 h_flex()
514 h_flex()
514 .w_full()
515 .w_full()
515 .gap(px(12.))
516 .gap(px(AVATAR_GAP))
516 .items_start()
517 .items_start()
517 .child(self.avatar(&model, fill, cx))
518 .child(self.avatar(&model, fill, cx))
518 .child(self.body(&model, &text, hovered)),
519 .child(self.body(&model, &text, hovered)),
@@ -549,7 +550,7 @@ impl Render for ToastView {
549 this.center.update(cx, |center, cx| center.dismiss(id, cx));
550 this.center.update(cx, |center, cx| center.dismiss(id, cx));
550 }
551 }
551 }))
552 }))
552 .child(div().size_full().p(px(12.)).child(content))
553 .child(div().size_full().p(px(TOAST_PADDING)).child(content))
553 .into_any_element()
554 .into_any_element()
554 }
555 }
555}
556}