BlueGit

ict rusty-teams

Merge remote-tracking branch 'origin/main' into feat/composer-format

metis Daniel Däschle · pushed by metis

Authored 2026-10-17 · 6 weeks ago

Hash
ae32100e326211bd8ee2723db3cda8d6eae8b0cd
4 files changed +204 −35
crates/app/src/views/conversation.rs +7 −7
@@ -42,8 +42,8 @@ use super::composer::{Composer, ComposerEvent, EditPreview, Outgoing, ReplyPrevi
42 42 use super::message_actions::{Action, DelegateEntry, MessageMenu, QUICK_REACTION_COUNT};
43 43 use super::message_row::{AVATAR_GAP, AVATAR_SIZE, render_message_row, render_skeleton_row};
44 44 use super::new_chat::{NewChatDraft, NewChatEvent, composer_placeholder};
45 use super::post_card::render_post;
46 45 use super::pin_banner::{pin_banner_inset, pin_banner_overlay};
46 use super::post_card::render_post;
47 47 use super::profile_card::opens_profile;
48 48 use super::reaction_picker::{PickHandler, ReactionPicker};
49 49 use super::reaction_pills::{ReactionControls, ReactionPopover};
@@ -69,10 +69,9 @@ use crate::read_state::{ReadTrigger, plan_read};
69 69 use crate::rows::{
70 70 Delivery, MessageRow, PendingReactions, Receipt, Row, RowContext, StartInfo, api_reaction,
71 71 apply_pending_reactions, assign_series, changed_indices, diff_keys, flat_rows,
72 forward_sender_ids,
73 has_own_reaction, message_draft, message_text, own_reaction_glyph, place_local_rows,
74 placeholder_rows,
75 reaction_glyph, reply_excerpt, set_own_reaction, thread_list_rows, thread_rows,
72 forward_sender_ids, has_own_reaction, message_draft, message_text, own_reaction_glyph,
73 place_local_rows, placeholder_rows, reaction_glyph, reply_excerpt, set_own_reaction,
74 thread_list_rows, thread_rows,
76 75 };
77 76 use crate::runtime;
78 77 use crate::scheduled_rows::SCHEDULED_KEY_PREFIX;
@@ -3427,6 +3426,7 @@ impl Render for ConversationView {
3427 3426 let pin_banner = self
3428 3427 .render_pin_banner(cx)
3429 3428 .filter(|_| !drafting && !self.shared_active() && !self.recap_active());
3429 let timeline_top_inset = pin_banner_inset(pin_banner.is_some());
3430 3430 let scroller = MessageScroller::new(
3431 3431 "messages",
3432 3432 self.scroller.clone(),
@@ -3530,7 +3530,7 @@ impl Render for ConversationView {
3530 3530 })
3531 3531 })
3532 3532 .with_bottom_fade(background)
3533 .with_top_inset(pin_banner_inset(pin_banner.is_some()));
3533 .with_top_inset(timeline_top_inset);
3534 3534
3535 3535 root = match self.current.as_ref().filter(|_| !drafting) {
3536 3536 Some(current) => root.child(self.render_header(current, cx)),
@@ -3601,7 +3601,7 @@ impl Render for ConversationView {
3601 3601 .relative()
3602 3602 .flex_1()
3603 3603 .min_h_0()
3604 .child(record_bounds(&self.timeline_bounds))
3604 .child(record_bounds(&self.timeline_bounds, timeline_top_inset))
3605 3605 .on_scroll_wheel(
3606 3606 cx.listener(|this, _, _, cx| this.close_reaction_details(cx)),
3607 3607 )
@@ -42,8 +42,8 @@ use super::composer::{Composer, ComposerEvent, EditPreview, Outgoing, ReplyPrevi
42use super::message_actions::{Action, DelegateEntry, MessageMenu, QUICK_REACTION_COUNT};
42use super::message_actions::{Action, DelegateEntry, MessageMenu, QUICK_REACTION_COUNT};
43use super::message_row::{AVATAR_GAP, AVATAR_SIZE, render_message_row, render_skeleton_row};
43use super::message_row::{AVATAR_GAP, AVATAR_SIZE, render_message_row, render_skeleton_row};
44use super::new_chat::{NewChatDraft, NewChatEvent, composer_placeholder};
44use super::new_chat::{NewChatDraft, NewChatEvent, composer_placeholder};
45use super::post_card::render_post;
46use super::pin_banner::{pin_banner_inset, pin_banner_overlay};
45use super::pin_banner::{pin_banner_inset, pin_banner_overlay};
46use super::post_card::render_post;
47use super::profile_card::opens_profile;
47use super::profile_card::opens_profile;
48use super::reaction_picker::{PickHandler, ReactionPicker};
48use super::reaction_picker::{PickHandler, ReactionPicker};
49use super::reaction_pills::{ReactionControls, ReactionPopover};
49use super::reaction_pills::{ReactionControls, ReactionPopover};
@@ -69,10 +69,9 @@ use crate::read_state::{ReadTrigger, plan_read};
69use crate::rows::{
69use crate::rows::{
70 Delivery, MessageRow, PendingReactions, Receipt, Row, RowContext, StartInfo, api_reaction,
70 Delivery, MessageRow, PendingReactions, Receipt, Row, RowContext, StartInfo, api_reaction,
71 apply_pending_reactions, assign_series, changed_indices, diff_keys, flat_rows,
71 apply_pending_reactions, assign_series, changed_indices, diff_keys, flat_rows,
72 forward_sender_ids,
72 forward_sender_ids, has_own_reaction, message_draft, message_text, own_reaction_glyph,
73 has_own_reaction, message_draft, message_text, own_reaction_glyph, place_local_rows,
73 place_local_rows, placeholder_rows, reaction_glyph, reply_excerpt, set_own_reaction,
74 placeholder_rows,
74 thread_list_rows, thread_rows,
75 reaction_glyph, reply_excerpt, set_own_reaction, thread_list_rows, thread_rows,
76};
75};
77use crate::runtime;
76use crate::runtime;
78use crate::scheduled_rows::SCHEDULED_KEY_PREFIX;
77use crate::scheduled_rows::SCHEDULED_KEY_PREFIX;
@@ -3427,6 +3426,7 @@ impl Render for ConversationView {
3427 let pin_banner = self
3426 let pin_banner = self
3428 .render_pin_banner(cx)
3427 .render_pin_banner(cx)
3429 .filter(|_| !drafting && !self.shared_active() && !self.recap_active());
3428 .filter(|_| !drafting && !self.shared_active() && !self.recap_active());
3429 let timeline_top_inset = pin_banner_inset(pin_banner.is_some());
3430 let scroller = MessageScroller::new(
3430 let scroller = MessageScroller::new(
3431 "messages",
3431 "messages",
3432 self.scroller.clone(),
3432 self.scroller.clone(),
@@ -3530,7 +3530,7 @@ impl Render for ConversationView {
3530 })
3530 })
3531 })
3531 })
3532 .with_bottom_fade(background)
3532 .with_bottom_fade(background)
3533 .with_top_inset(pin_banner_inset(pin_banner.is_some()));
3533 .with_top_inset(timeline_top_inset);
3534
3534
3535 root = match self.current.as_ref().filter(|_| !drafting) {
3535 root = match self.current.as_ref().filter(|_| !drafting) {
3536 Some(current) => root.child(self.render_header(current, cx)),
3536 Some(current) => root.child(self.render_header(current, cx)),
@@ -3601,7 +3601,7 @@ impl Render for ConversationView {
3601 .relative()
3601 .relative()
3602 .flex_1()
3602 .flex_1()
3603 .min_h_0()
3603 .min_h_0()
3604 .child(record_bounds(&self.timeline_bounds))
3604 .child(record_bounds(&self.timeline_bounds, timeline_top_inset))
3605 .on_scroll_wheel(
3605 .on_scroll_wheel(
3606 cx.listener(|this, _, _, cx| this.close_reaction_details(cx)),
3606 cx.listener(|this, _, _, cx| this.close_reaction_details(cx)),
3607 )
3607 )
crates/app/src/views/shell.rs +9 −5
@@ -893,11 +893,15 @@ impl Render for AppShell {
893 893 let title_bar = render_title_bar(
894 894 crate::active_appearance::current(cx).app_icon,
895 895 mode_label(state.mode),
896 update_button(
897 &self.update,
898 cx.listener(|this, _, _, cx| this.restart_into_update(cx)),
899 ),
900 own_status_button(&self.state, state),
896 [
897 update_button(
898 &self.update,
899 cx.listener(|this, _, _, cx| this.restart_into_update(cx)),
900 ),
901 own_status_button(&self.state, state),
902 ]
903 .into_iter()
904 .flatten(),
901 905 cx.listener(|this, _, window, cx| this.toggle_switcher("", window, cx)),
902 906 );
903 907 div()
@@ -893,11 +893,15 @@ impl Render for AppShell {
893 let title_bar = render_title_bar(
893 let title_bar = render_title_bar(
894 crate::active_appearance::current(cx).app_icon,
894 crate::active_appearance::current(cx).app_icon,
895 mode_label(state.mode),
895 mode_label(state.mode),
896 update_button(
896 [
897 &self.update,
897 update_button(
898 cx.listener(|this, _, _, cx| this.restart_into_update(cx)),
898 &self.update,
899 ),
899 cx.listener(|this, _, _, cx| this.restart_into_update(cx)),
900 own_status_button(&self.state, state),
900 ),
901 own_status_button(&self.state, state),
902 ]
903 .into_iter()
904 .flatten(),
901 cx.listener(|this, _, window, cx| this.toggle_switcher("", window, cx)),
905 cx.listener(|this, _, window, cx| this.toggle_switcher("", window, cx)),
902 );
906 );
903 div()
907 div()
crates/app/src/views/title_bar.rs +107 −9
@@ -129,27 +129,29 @@ struct Reserved {
129 129 struct TitleBarRow {
130 130 app_icon: AppIcon,
131 131 mode_label: Option<&'static str>,
132 update: Option<AnyElement>,
133 own: Option<AnyElement>,
132 controls: Vec<AnyElement>,
134 133 on_search: Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>,
135 134 }
136 135
137 136 pub fn render_title_bar(
138 137 app_icon: AppIcon,
139 138 mode_label: Option<&'static str>,
140 update: Option<AnyElement>,
141 own: Option<AnyElement>,
139 controls: impl IntoIterator<Item = AnyElement>,
142 140 on_search: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
143 141 ) -> impl IntoElement {
144 142 TitleBarRow {
145 143 app_icon,
146 144 mode_label,
147 update,
148 own,
145 controls: controls.into_iter().collect(),
149 146 on_search: Box::new(on_search),
150 147 }
151 148 }
152 149
150 // The Windows caption hit-test takes every point the Drag area reaches, so a control without occlusion never gets its click.
151 fn outside_caption(control: AnyElement) -> Div {
152 div().flex_none().occlude().child(control)
153 }
154
153 155 fn search_box(
154 156 span: SearchSpan,
155 157 on_search: Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>,
@@ -229,8 +231,7 @@ impl RenderOnce for TitleBarRow {
229 231 .h_full()
230 232 .gap(px(12.))
231 233 .items_center()
232 .children(self.update)
233 .children(self.own)
234 .children(self.controls.into_iter().map(outside_caption))
234 235 .child(div().w(px(4.)));
235 236 let row = h_flex()
236 237 .flex_1()
@@ -278,7 +279,104 @@ impl RenderOnce for TitleBarRow {
278 279
279 280 #[cfg(test)]
280 281 mod tests {
281 use super::{SEARCH_MIN_WIDTH, SEARCH_WIDTH, search_span};
282 use std::cell::{Cell, RefCell};
283 use std::rc::Rc;
284
285 use gpui_kit::component::button::{Button, ButtonVariants as _};
286 use gpui_kit::{
287 Context, Hitbox, HitboxBehavior, InteractiveElement as _, IntoElement, Modifiers,
288 ParentElement, Render, Styled, TestAppContext, Window, canvas, div, px, size,
289 };
290
291 use super::{
292 AppIcon, SEARCH_MIN_WIDTH, SEARCH_WIDTH, UpdateStatus, render_title_bar, search_span,
293 update_button,
294 };
295
296 struct CaptionHarness {
297 caption: Rc<RefCell<Option<Hitbox>>>,
298 restarts: Rc<Cell<usize>>,
299 }
300
301 impl Render for CaptionHarness {
302 fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
303 let caption = self.caption.clone();
304 let restarts = self.restarts.clone();
305 let own = div()
306 .debug_selector(|| "shell/title-bar/own-status".into())
307 .child(Button::new("own").ghost().size(px(32.)).child("O"))
308 .into_any_element();
309 div()
310 .relative()
311 .size_full()
312 .child(
313 canvas(
314 |bounds, window, _| window.insert_hitbox(bounds, HitboxBehavior::Normal),
315 move |_, hitbox, _, _| *caption.borrow_mut() = Some(hitbox),
316 )
317 .absolute()
318 .size_full(),
319 )
320 .child(render_title_bar(
321 AppIcon::ALL[0],
322 Some("Demo"),
323 [
324 update_button(&UpdateStatus::Ready, move |_, _, _| {
325 restarts.set(restarts.get() + 1)
326 }),
327 Some(own),
328 ]
329 .into_iter()
330 .flatten(),
331 |_, _, _| {},
332 ))
333 }
334 }
335
336 #[gpui_kit::test]
337 fn title_bar_controls_stay_out_of_the_caption(cx: &mut TestAppContext) {
338 cx.update(gpui_kit::init);
339 cx.update(crate::theme::apply);
340 let caption = Rc::new(RefCell::new(None));
341 let restarts = Rc::new(Cell::new(0));
342 let (_, window_cx) = cx.add_window_view({
343 let caption = caption.clone();
344 let restarts = restarts.clone();
345 move |_, _| CaptionHarness { caption, restarts }
346 });
347 window_cx.simulate_resize(size(px(1280.), px(800.)));
348 for _ in 0..2 {
349 window_cx.update(|window, cx| window.draw(cx).clear(cx));
350 }
351 let mut found = Vec::new();
352 for (selector, caption_expected) in [
353 ("shell/title-bar/update", false),
354 ("shell/title-bar/own-status", false),
355 ("shell/title-bar/search", false),
356 ("shell/title-bar/leading", true),
357 ] {
358 let Some(bounds) = window_cx.debug_bounds(selector) else {
359 found.push(format!("{selector} was not rendered"));
360 continue;
361 };
362 let center = bounds.center();
363 let in_caption = window_cx.update(|window, _| {
364 caption
365 .borrow()
366 .as_ref()
367 .is_some_and(|hitbox| hitbox.is_hovered_at(center, window))
368 });
369 if in_caption != caption_expected {
370 found.push(format!("{selector} in caption: {in_caption}"));
371 }
372 }
373 assert!(found.is_empty(), "{}", found.join("\n"));
374 let update = window_cx
375 .debug_bounds("shell/title-bar/update")
376 .expect("update button rendered");
377 window_cx.simulate_click(update.center(), Modifiers::default());
378 assert_eq!(restarts.get(), 1);
379 }
282 380
283 381 #[test]
284 382 fn search_stays_centered_while_it_fits() {
@@ -129,27 +129,29 @@ struct Reserved {
129struct TitleBarRow {
129struct TitleBarRow {
130 app_icon: AppIcon,
130 app_icon: AppIcon,
131 mode_label: Option<&'static str>,
131 mode_label: Option<&'static str>,
132 update: Option<AnyElement>,
132 controls: Vec<AnyElement>,
133 own: Option<AnyElement>,
134 on_search: Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>,
133 on_search: Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>,
135}
134}
136
135
137pub fn render_title_bar(
136pub fn render_title_bar(
138 app_icon: AppIcon,
137 app_icon: AppIcon,
139 mode_label: Option<&'static str>,
138 mode_label: Option<&'static str>,
140 update: Option<AnyElement>,
139 controls: impl IntoIterator<Item = AnyElement>,
141 own: Option<AnyElement>,
142 on_search: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
140 on_search: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
143) -> impl IntoElement {
141) -> impl IntoElement {
144 TitleBarRow {
142 TitleBarRow {
145 app_icon,
143 app_icon,
146 mode_label,
144 mode_label,
147 update,
145 controls: controls.into_iter().collect(),
148 own,
149 on_search: Box::new(on_search),
146 on_search: Box::new(on_search),
150 }
147 }
151}
148}
152
149
150// The Windows caption hit-test takes every point the Drag area reaches, so a control without occlusion never gets its click.
151fn outside_caption(control: AnyElement) -> Div {
152 div().flex_none().occlude().child(control)
153}
154
153fn search_box(
155fn search_box(
154 span: SearchSpan,
156 span: SearchSpan,
155 on_search: Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>,
157 on_search: Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>,
@@ -229,8 +231,7 @@ impl RenderOnce for TitleBarRow {
229 .h_full()
231 .h_full()
230 .gap(px(12.))
232 .gap(px(12.))
231 .items_center()
233 .items_center()
232 .children(self.update)
234 .children(self.controls.into_iter().map(outside_caption))
233 .children(self.own)
234 .child(div().w(px(4.)));
235 .child(div().w(px(4.)));
235 let row = h_flex()
236 let row = h_flex()
236 .flex_1()
237 .flex_1()
@@ -278,7 +279,104 @@ impl RenderOnce for TitleBarRow {
278
279
279#[cfg(test)]
280#[cfg(test)]
280mod tests {
281mod tests {
281 use super::{SEARCH_MIN_WIDTH, SEARCH_WIDTH, search_span};
282 use std::cell::{Cell, RefCell};
283 use std::rc::Rc;
284
285 use gpui_kit::component::button::{Button, ButtonVariants as _};
286 use gpui_kit::{
287 Context, Hitbox, HitboxBehavior, InteractiveElement as _, IntoElement, Modifiers,
288 ParentElement, Render, Styled, TestAppContext, Window, canvas, div, px, size,
289 };
290
291 use super::{
292 AppIcon, SEARCH_MIN_WIDTH, SEARCH_WIDTH, UpdateStatus, render_title_bar, search_span,
293 update_button,
294 };
295
296 struct CaptionHarness {
297 caption: Rc<RefCell<Option<Hitbox>>>,
298 restarts: Rc<Cell<usize>>,
299 }
300
301 impl Render for CaptionHarness {
302 fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
303 let caption = self.caption.clone();
304 let restarts = self.restarts.clone();
305 let own = div()
306 .debug_selector(|| "shell/title-bar/own-status".into())
307 .child(Button::new("own").ghost().size(px(32.)).child("O"))
308 .into_any_element();
309 div()
310 .relative()
311 .size_full()
312 .child(
313 canvas(
314 |bounds, window, _| window.insert_hitbox(bounds, HitboxBehavior::Normal),
315 move |_, hitbox, _, _| *caption.borrow_mut() = Some(hitbox),
316 )
317 .absolute()
318 .size_full(),
319 )
320 .child(render_title_bar(
321 AppIcon::ALL[0],
322 Some("Demo"),
323 [
324 update_button(&UpdateStatus::Ready, move |_, _, _| {
325 restarts.set(restarts.get() + 1)
326 }),
327 Some(own),
328 ]
329 .into_iter()
330 .flatten(),
331 |_, _, _| {},
332 ))
333 }
334 }
335
336 #[gpui_kit::test]
337 fn title_bar_controls_stay_out_of_the_caption(cx: &mut TestAppContext) {
338 cx.update(gpui_kit::init);
339 cx.update(crate::theme::apply);
340 let caption = Rc::new(RefCell::new(None));
341 let restarts = Rc::new(Cell::new(0));
342 let (_, window_cx) = cx.add_window_view({
343 let caption = caption.clone();
344 let restarts = restarts.clone();
345 move |_, _| CaptionHarness { caption, restarts }
346 });
347 window_cx.simulate_resize(size(px(1280.), px(800.)));
348 for _ in 0..2 {
349 window_cx.update(|window, cx| window.draw(cx).clear(cx));
350 }
351 let mut found = Vec::new();
352 for (selector, caption_expected) in [
353 ("shell/title-bar/update", false),
354 ("shell/title-bar/own-status", false),
355 ("shell/title-bar/search", false),
356 ("shell/title-bar/leading", true),
357 ] {
358 let Some(bounds) = window_cx.debug_bounds(selector) else {
359 found.push(format!("{selector} was not rendered"));
360 continue;
361 };
362 let center = bounds.center();
363 let in_caption = window_cx.update(|window, _| {
364 caption
365 .borrow()
366 .as_ref()
367 .is_some_and(|hitbox| hitbox.is_hovered_at(center, window))
368 });
369 if in_caption != caption_expected {
370 found.push(format!("{selector} in caption: {in_caption}"));
371 }
372 }
373 assert!(found.is_empty(), "{}", found.join("\n"));
374 let update = window_cx
375 .debug_bounds("shell/title-bar/update")
376 .expect("update button rendered");
377 window_cx.simulate_click(update.center(), Modifiers::default());
378 assert_eq!(restarts.get(), 1);
379 }
282
380
283 #[test]
381 #[test]
284 fn search_stays_centered_while_it_fits() {
382 fn search_stays_centered_while_it_fits() {
crates/app/src/views/toolbar_placement.rs +81 −14
@@ -19,16 +19,47 @@ pub fn toolbar_slot(id: ElementId, hover: HoverChange, bar: impl IntoElement) ->
19 19 .child(bar)
20 20 }
21 21
22 pub fn record_bounds(timeline: &TimelineBounds) -> impl IntoElement {
22 pub fn record_bounds(timeline: &TimelineBounds, top_inset: Pixels) -> impl IntoElement {
23 23 let timeline = timeline.clone();
24 24 canvas(
25 25 move |bounds, _, _| timeline.set(Some(bounds)),
26 26 |_, _, _, _| {},
27 27 )
28 28 .absolute()
29 .top_0()
29 .top(top_inset)
30 30 .left_0()
31 .size_full()
31 .right_0()
32 .bottom_0()
33 }
34
35 #[derive(Clone, Copy, Debug, PartialEq)]
36 pub enum ToolbarSide {
37 Above,
38 Below,
39 ViewportTop,
40 }
41
42 pub fn toolbar_side(anchor: Bounds<Pixels>, viewport: Bounds<Pixels>) -> ToolbarSide {
43 let above_top = anchor.top() - px(SLOT_HEIGHT - TOOLBAR_GAP);
44 let below_bottom = anchor.bottom() + px(SLOT_HEIGHT - TOOLBAR_GAP);
45 if above_top >= viewport.top() {
46 ToolbarSide::Above
47 } else if below_bottom <= viewport.bottom() {
48 ToolbarSide::Below
49 } else {
50 ToolbarSide::ViewportTop
51 }
52 }
53
54 fn toolbar_offset(anchor: Bounds<Pixels>, viewport: Option<Bounds<Pixels>>) -> Pixels {
55 let Some(viewport) = viewport else {
56 return px(0.);
57 };
58 match toolbar_side(anchor, viewport) {
59 ToolbarSide::Above => px(0.),
60 ToolbarSide::Below => anchor.size.height + px(SLOT_HEIGHT),
61 ToolbarSide::ViewportTop => viewport.top() - anchor.top() + px(SLOT_HEIGHT),
62 }
32 63 }
33 64
34 65 pub fn placed_toolbar(
@@ -59,16 +90,6 @@ struct PlacedToolbar {
59 90 overlay: AnyElement,
60 91 }
61 92
62 impl PlacedToolbar {
63 fn flip_offset(&self, anchor: Bounds<Pixels>) -> Pixels {
64 let bar_top = anchor.top() - px(SLOT_HEIGHT - TOOLBAR_GAP);
65 match self.timeline.get() {
66 Some(timeline) if bar_top < timeline.top() => anchor.size.height + px(SLOT_HEIGHT),
67 _ => px(0.),
68 }
69 }
70 }
71
72 93 impl Element for PlacedToolbar {
73 94 type RequestLayoutState = ();
74 95 type PrepaintState = ();
@@ -100,7 +121,7 @@ impl Element for PlacedToolbar {
100 121 window: &mut Window,
101 122 cx: &mut App,
102 123 ) {
103 let offset = point(px(0.), self.flip_offset(bounds));
124 let offset = point(px(0.), toolbar_offset(bounds, self.timeline.get()));
104 125 window.with_element_offset(offset, |window| self.overlay.prepaint(window, cx));
105 126 }
106 127
@@ -125,3 +146,49 @@ impl IntoElement for PlacedToolbar {
125 146 self
126 147 }
127 148 }
149
150 #[cfg(test)]
151 mod tests {
152 use super::{SLOT_HEIGHT, TOOLBAR_GAP, ToolbarSide, toolbar_offset, toolbar_side};
153 use gpui_kit::{Bounds, point, px, size};
154
155 fn span(top: f32, bottom: f32) -> Bounds<gpui_kit::Pixels> {
156 Bounds::new(point(px(0.), px(top)), size(px(400.), px(bottom - top)))
157 }
158
159 const VIEWPORT: (f32, f32) = (100., 600.);
160 const BAR_ROOM: f32 = SLOT_HEIGHT - TOOLBAR_GAP;
161
162 #[test]
163 fn stays_above_with_room_below_the_viewport_top() {
164 let viewport = span(VIEWPORT.0, VIEWPORT.1);
165 let message = span(VIEWPORT.0 + BAR_ROOM, 300.);
166 assert_eq!(toolbar_side(message, viewport), ToolbarSide::Above);
167 assert_eq!(toolbar_offset(message, Some(viewport)), px(0.));
168 }
169
170 #[test]
171 fn flips_below_when_the_bar_would_cross_the_viewport_top() {
172 let viewport = span(VIEWPORT.0, VIEWPORT.1);
173 let message = span(VIEWPORT.0 + BAR_ROOM - 1., 300.);
174 assert_eq!(toolbar_side(message, viewport), ToolbarSide::Below);
175 assert_eq!(
176 toolbar_offset(message, Some(viewport)),
177 message.size.height + px(SLOT_HEIGHT)
178 );
179 }
180
181 #[test]
182 fn sticks_to_the_viewport_top_when_neither_side_fits() {
183 let viewport = span(VIEWPORT.0, VIEWPORT.1);
184 let message = span(VIEWPORT.0 - 50., VIEWPORT.1 - BAR_ROOM + 1.);
185 assert_eq!(toolbar_side(message, viewport), ToolbarSide::ViewportTop);
186 let bar_top = message.top() - px(SLOT_HEIGHT) + toolbar_offset(message, Some(viewport));
187 assert_eq!(bar_top, viewport.top());
188 }
189
190 #[test]
191 fn stays_above_without_measured_viewport() {
192 assert_eq!(toolbar_offset(span(0., 100.), None), px(0.));
193 }
194 }
@@ -19,16 +19,47 @@ pub fn toolbar_slot(id: ElementId, hover: HoverChange, bar: impl IntoElement) ->
19 .child(bar)
19 .child(bar)
20}
20}
21
21
22pub fn record_bounds(timeline: &TimelineBounds) -> impl IntoElement {
22pub fn record_bounds(timeline: &TimelineBounds, top_inset: Pixels) -> impl IntoElement {
23 let timeline = timeline.clone();
23 let timeline = timeline.clone();
24 canvas(
24 canvas(
25 move |bounds, _, _| timeline.set(Some(bounds)),
25 move |bounds, _, _| timeline.set(Some(bounds)),
26 |_, _, _, _| {},
26 |_, _, _, _| {},
27 )
27 )
28 .absolute()
28 .absolute()
29 .top_0()
29 .top(top_inset)
30 .left_0()
30 .left_0()
31 .size_full()
31 .right_0()
32 .bottom_0()
33}
34
35#[derive(Clone, Copy, Debug, PartialEq)]
36pub enum ToolbarSide {
37 Above,
38 Below,
39 ViewportTop,
40}
41
42pub fn toolbar_side(anchor: Bounds<Pixels>, viewport: Bounds<Pixels>) -> ToolbarSide {
43 let above_top = anchor.top() - px(SLOT_HEIGHT - TOOLBAR_GAP);
44 let below_bottom = anchor.bottom() + px(SLOT_HEIGHT - TOOLBAR_GAP);
45 if above_top >= viewport.top() {
46 ToolbarSide::Above
47 } else if below_bottom <= viewport.bottom() {
48 ToolbarSide::Below
49 } else {
50 ToolbarSide::ViewportTop
51 }
52}
53
54fn toolbar_offset(anchor: Bounds<Pixels>, viewport: Option<Bounds<Pixels>>) -> Pixels {
55 let Some(viewport) = viewport else {
56 return px(0.);
57 };
58 match toolbar_side(anchor, viewport) {
59 ToolbarSide::Above => px(0.),
60 ToolbarSide::Below => anchor.size.height + px(SLOT_HEIGHT),
61 ToolbarSide::ViewportTop => viewport.top() - anchor.top() + px(SLOT_HEIGHT),
62 }
32}
63}
33
64
34pub fn placed_toolbar(
65pub fn placed_toolbar(
@@ -59,16 +90,6 @@ struct PlacedToolbar {
59 overlay: AnyElement,
90 overlay: AnyElement,
60}
91}
61
92
62impl PlacedToolbar {
63 fn flip_offset(&self, anchor: Bounds<Pixels>) -> Pixels {
64 let bar_top = anchor.top() - px(SLOT_HEIGHT - TOOLBAR_GAP);
65 match self.timeline.get() {
66 Some(timeline) if bar_top < timeline.top() => anchor.size.height + px(SLOT_HEIGHT),
67 _ => px(0.),
68 }
69 }
70}
71
72impl Element for PlacedToolbar {
93impl Element for PlacedToolbar {
73 type RequestLayoutState = ();
94 type RequestLayoutState = ();
74 type PrepaintState = ();
95 type PrepaintState = ();
@@ -100,7 +121,7 @@ impl Element for PlacedToolbar {
100 window: &mut Window,
121 window: &mut Window,
101 cx: &mut App,
122 cx: &mut App,
102 ) {
123 ) {
103 let offset = point(px(0.), self.flip_offset(bounds));
124 let offset = point(px(0.), toolbar_offset(bounds, self.timeline.get()));
104 window.with_element_offset(offset, |window| self.overlay.prepaint(window, cx));
125 window.with_element_offset(offset, |window| self.overlay.prepaint(window, cx));
105 }
126 }
106
127
@@ -125,3 +146,49 @@ impl IntoElement for PlacedToolbar {
125 self
146 self
126 }
147 }
127}
148}
149
150#[cfg(test)]
151mod tests {
152 use super::{SLOT_HEIGHT, TOOLBAR_GAP, ToolbarSide, toolbar_offset, toolbar_side};
153 use gpui_kit::{Bounds, point, px, size};
154
155 fn span(top: f32, bottom: f32) -> Bounds<gpui_kit::Pixels> {
156 Bounds::new(point(px(0.), px(top)), size(px(400.), px(bottom - top)))
157 }
158
159 const VIEWPORT: (f32, f32) = (100., 600.);
160 const BAR_ROOM: f32 = SLOT_HEIGHT - TOOLBAR_GAP;
161
162 #[test]
163 fn stays_above_with_room_below_the_viewport_top() {
164 let viewport = span(VIEWPORT.0, VIEWPORT.1);
165 let message = span(VIEWPORT.0 + BAR_ROOM, 300.);
166 assert_eq!(toolbar_side(message, viewport), ToolbarSide::Above);
167 assert_eq!(toolbar_offset(message, Some(viewport)), px(0.));
168 }
169
170 #[test]
171 fn flips_below_when_the_bar_would_cross_the_viewport_top() {
172 let viewport = span(VIEWPORT.0, VIEWPORT.1);
173 let message = span(VIEWPORT.0 + BAR_ROOM - 1., 300.);
174 assert_eq!(toolbar_side(message, viewport), ToolbarSide::Below);
175 assert_eq!(
176 toolbar_offset(message, Some(viewport)),
177 message.size.height + px(SLOT_HEIGHT)
178 );
179 }
180
181 #[test]
182 fn sticks_to_the_viewport_top_when_neither_side_fits() {
183 let viewport = span(VIEWPORT.0, VIEWPORT.1);
184 let message = span(VIEWPORT.0 - 50., VIEWPORT.1 - BAR_ROOM + 1.);
185 assert_eq!(toolbar_side(message, viewport), ToolbarSide::ViewportTop);
186 let bar_top = message.top() - px(SLOT_HEIGHT) + toolbar_offset(message, Some(viewport));
187 assert_eq!(bar_top, viewport.top());
188 }
189
190 #[test]
191 fn stays_above_without_measured_viewport() {
192 assert_eq!(toolbar_offset(span(0., 100.), None), px(0.));
193 }
194}