BlueGit

ict rusty-teams

fix(title-bar): center the search on the window

- search box is laid out absolutely over the full title bar width, centered on the window
- leading and trailing groups are measured each frame; the box shrinks centered (min 240 px), then shifts, keeping an 8 px gap; hidden only when under 36 px fit
- Ctrl K hint hidden below 200 px, search text truncates
- layout_guard: title_search_is_centered_on_the_window (1920x1080, 1280x800 with Demo pill + Restart to update, 640x480 no overlap)

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
e576db4568f7f48b80abd1e27ec452d660d1e692
Parent
cc61e5c4
3 files changed +258 −41
crates/app/src/layout_guard.rs +70 −0
@@ -753,6 +753,76 @@ mod frames {
753 753 .join("\n")
754 754 );
755 755 }
756 const TITLE_SEARCH: &str = "shell/title-bar/search";
757 const TITLE_NEIGHBOURS: &[&str] = &[
758 "shell/title-bar/leading",
759 "shell/title-bar/mode-pill",
760 "shell/title-bar/trailing",
761 "shell/title-bar/update",
762 OWN_STATUS,
763 ];
764 const TITLE_SEARCH_SIZES: [(f32, f32, bool); 3] = [
765 (1920., 1080., true),
766 (1280., 800., true),
767 (640., 480., false),
768 ];
769
770 #[gpui_kit::test]
771 fn title_search_is_centered_on_the_window(cx: &mut TestAppContext) {
772 let (handle, _) = mount(cx);
773 let mut visual = VisualTestContext::from_window(handle, cx);
774 let shell = visual.update(|window, cx| {
775 window
776 .root::<gpui_kit::base::Root>()
777 .flatten()
778 .and_then(|root| root.read(cx).view().clone().downcast::<AppShell>().ok())
779 .expect("app shell is the root view")
780 });
781 visual.update(|_, cx| {
782 shell.update(cx, |shell, cx| {
783 shell.set_update_status(crate::updater::UpdateStatus::Ready, cx)
784 })
785 });
786 let mut found = Vec::new();
787 for (width, height, centered) in TITLE_SEARCH_SIZES {
788 visual.simulate_resize(size(px(width), px(height)));
789 collect(&mut visual, Some(TITLE_SEARCH));
790 let search = visual
791 .debug_bounds(TITLE_SEARCH)
792 .expect("title search rendered");
793 let search_center = f32::from(search.center().x);
794 eprintln!(
795 "title search @{width}x{height}: {} center {search_center:.1}, window center {:.1}",
796 describe(search),
797 width / 2.
798 );
799 if centered && (search_center - width / 2.).abs() > 1. {
800 found.push(format!(
801 "@{width}x{height}: search center {search_center:.1} is not the window center {:.1}",
802 width / 2.
803 ));
804 }
805 if !contains(rectangle(0., 0., width, height), search) {
806 found.push(format!(
807 "@{width}x{height}: search {} leaves the window",
808 describe(search)
809 ));
810 }
811 for neighbour in TITLE_NEIGHBOURS {
812 match visual.debug_bounds(neighbour) {
813 None => found.push(format!("@{width}x{height}: {neighbour} was not rendered")),
814 Some(bounds) if overlaps(search, bounds) => found.push(format!(
815 "@{width}x{height}: search {} overlaps {neighbour} {}",
816 describe(search),
817 describe(bounds)
818 )),
819 Some(_) => {}
820 }
821 }
822 }
823 assert!(found.is_empty(), "{}", found.join("\n"));
824 }
825
756 826 #[gpui_kit::test]
757 827 fn call_chat_bubbles_stay_inside_the_panel(cx: &mut TestAppContext) {
758 828 let (handle, app) = mount(cx);
@@ -753,6 +753,76 @@ mod frames {
753 .join("\n")
753 .join("\n")
754 );
754 );
755 }
755 }
756 const TITLE_SEARCH: &str = "shell/title-bar/search";
757 const TITLE_NEIGHBOURS: &[&str] = &[
758 "shell/title-bar/leading",
759 "shell/title-bar/mode-pill",
760 "shell/title-bar/trailing",
761 "shell/title-bar/update",
762 OWN_STATUS,
763 ];
764 const TITLE_SEARCH_SIZES: [(f32, f32, bool); 3] = [
765 (1920., 1080., true),
766 (1280., 800., true),
767 (640., 480., false),
768 ];
769
770 #[gpui_kit::test]
771 fn title_search_is_centered_on_the_window(cx: &mut TestAppContext) {
772 let (handle, _) = mount(cx);
773 let mut visual = VisualTestContext::from_window(handle, cx);
774 let shell = visual.update(|window, cx| {
775 window
776 .root::<gpui_kit::base::Root>()
777 .flatten()
778 .and_then(|root| root.read(cx).view().clone().downcast::<AppShell>().ok())
779 .expect("app shell is the root view")
780 });
781 visual.update(|_, cx| {
782 shell.update(cx, |shell, cx| {
783 shell.set_update_status(crate::updater::UpdateStatus::Ready, cx)
784 })
785 });
786 let mut found = Vec::new();
787 for (width, height, centered) in TITLE_SEARCH_SIZES {
788 visual.simulate_resize(size(px(width), px(height)));
789 collect(&mut visual, Some(TITLE_SEARCH));
790 let search = visual
791 .debug_bounds(TITLE_SEARCH)
792 .expect("title search rendered");
793 let search_center = f32::from(search.center().x);
794 eprintln!(
795 "title search @{width}x{height}: {} center {search_center:.1}, window center {:.1}",
796 describe(search),
797 width / 2.
798 );
799 if centered && (search_center - width / 2.).abs() > 1. {
800 found.push(format!(
801 "@{width}x{height}: search center {search_center:.1} is not the window center {:.1}",
802 width / 2.
803 ));
804 }
805 if !contains(rectangle(0., 0., width, height), search) {
806 found.push(format!(
807 "@{width}x{height}: search {} leaves the window",
808 describe(search)
809 ));
810 }
811 for neighbour in TITLE_NEIGHBOURS {
812 match visual.debug_bounds(neighbour) {
813 None => found.push(format!("@{width}x{height}: {neighbour} was not rendered")),
814 Some(bounds) if overlaps(search, bounds) => found.push(format!(
815 "@{width}x{height}: search {} overlaps {neighbour} {}",
816 describe(search),
817 describe(bounds)
818 )),
819 Some(_) => {}
820 }
821 }
822 }
823 assert!(found.is_empty(), "{}", found.join("\n"));
824 }
825
756 #[gpui_kit::test]
826 #[gpui_kit::test]
757 fn call_chat_bubbles_stay_inside_the_panel(cx: &mut TestAppContext) {
827 fn call_chat_bubbles_stay_inside_the_panel(cx: &mut TestAppContext) {
758 let (handle, app) = mount(cx);
828 let (handle, app) = mount(cx);
crates/app/src/views/shell.rs +6 −0
@@ -143,6 +143,12 @@ pub struct AppShell {
143 143 }
144 144
145 145 impl AppShell {
146 #[cfg(test)]
147 pub(crate) fn set_update_status(&mut self, update: UpdateStatus, cx: &mut Context<Self>) {
148 self.update = update;
149 cx.notify();
150 }
151
146 152 fn sync_call_chat(
147 153 &mut self,
148 154 thread: Option<String>,
@@ -143,6 +143,12 @@ pub struct AppShell {
143}
143}
144
144
145impl AppShell {
145impl AppShell {
146 #[cfg(test)]
147 pub(crate) fn set_update_status(&mut self, update: UpdateStatus, cx: &mut Context<Self>) {
148 self.update = update;
149 cx.notify();
150 }
151
146 fn sync_call_chat(
152 fn sync_call_chat(
147 &mut self,
153 &mut self,
148 thread: Option<String>,
154 thread: Option<String>,
crates/app/src/views/title_bar.rs +182 −41
@@ -12,6 +12,11 @@ use crate::updater::UpdateStatus;
12 12
13 13 pub const TITLE_BAR_HEIGHT: f32 = 40.;
14 14 const SEARCH_WIDTH: f32 = 380.;
15 const SEARCH_MIN_WIDTH: f32 = 240.;
16 const SEARCH_MIN_VISIBLE_WIDTH: f32 = 36.;
17 const SEARCH_HEIGHT: f32 = 28.;
18 const SEARCH_GAP: f32 = 8.;
19 const SHORTCUT_MIN_WIDTH: f32 = 200.;
15 20 const SEARCH_HINT: &str = "Search chats, people and channels";
16 21 const SHORTCUT_HINT: &str = "Ctrl K";
17 22 const APP_ICON_SIZE: f32 = 18.;
@@ -90,65 +95,201 @@ pub fn update_button(
90 95 )
91 96 }
92 97
98 #[derive(Clone, Copy, Debug, PartialEq)]
99 struct SearchSpan {
100 left: f32,
101 width: f32,
102 }
103
104 fn search_span(window_width: f32, leading_end: f32, trailing_start: f32) -> Option<SearchSpan> {
105 let start = leading_end + SEARCH_GAP;
106 let end = trailing_start - SEARCH_GAP;
107 let room = end - start;
108 if room < SEARCH_MIN_VISIBLE_WIDTH {
109 return None;
110 }
111 let center = window_width / 2.;
112 let centered_width = (2. * (center - start).min(end - center)).min(SEARCH_WIDTH);
113 let width = centered_width.max(SEARCH_MIN_WIDTH).min(room);
114 let left = (center - width / 2.).clamp(start, end - width);
115 Some(SearchSpan { left, width })
116 }
117
118 #[derive(Clone, Copy, PartialEq)]
119 struct Reserved {
120 leading_end: f32,
121 trailing_from_right: f32,
122 }
123
124 #[derive(IntoElement)]
125 struct TitleBarRow {
126 mode_label: Option<&'static str>,
127 update: Option<AnyElement>,
128 own: Option<AnyElement>,
129 on_search: Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>,
130 }
131
93 132 pub fn render_title_bar(
94 133 mode_label: Option<&'static str>,
95 134 update: Option<AnyElement>,
96 135 own: Option<AnyElement>,
97 136 on_search: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
98 137 ) -> impl IntoElement {
99 let search = h_flex()
138 TitleBarRow {
139 mode_label,
140 update,
141 own,
142 on_search: Box::new(on_search),
143 }
144 }
145
146 fn search_box(
147 span: SearchSpan,
148 on_search: Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>,
149 ) -> impl IntoElement {
150 h_flex()
100 151 .id("title-search")
101 152 .debug_selector(|| "shell/title-bar/search".into())
102 153 .occlude()
103 .w(px(SEARCH_WIDTH))
104 .h(px(28.))
154 .absolute()
155 .top(px((TITLE_BAR_HEIGHT - SEARCH_HEIGHT) / 2.))
156 .left(px(span.left))
157 .w(px(span.width))
158 .h(px(SEARCH_HEIGHT))
105 159 .px(px(10.))
106 160 .gap(px(8.))
107 161 .items_center()
162 .overflow_hidden()
108 163 .rounded(px(6.))
109 164 .bg(theme::surface())
110 165 .cursor_pointer()
111 166 .text_size(px(12.))
112 167 .text_color(theme::text_muted())
113 .child(icon(IconName::Search, 14., theme::text_muted()))
114 .child(div().flex_1().child(SEARCH_HINT))
115 168 .child(
116 169 div()
117 .px(px(6.))
118 .py(px(1.))
119 .rounded(px(4.))
120 .bg(theme::surface_raised())
121 .text_size(px(11.))
122 .child(SHORTCUT_HINT),
170 .flex_none()
171 .child(icon(IconName::Search, 14., theme::text_muted())),
123 172 )
124 .on_click(on_search);
125 let bar = TitleBar::new()
126 .h(px(TITLE_BAR_HEIGHT))
127 .border_b_0()
128 .bg(theme::background())
129 .child(
130 h_flex()
131 .flex_1()
132 .h_full()
133 .gap(px(12.))
134 .items_center()
135 .child(img(app_icon()).size(px(APP_ICON_SIZE)).flex_none())
136 .child(
137 div()
138 .text_size(px(13.))
139 .font_weight(FontWeight::SEMIBOLD)
140 .text_color(theme::text())
141 .child(app_base::APP_NAME),
142 )
143 .children(mode_label.map(mode_pill))
144 .child(div().flex_1().flex().justify_center().child(search))
145 .children(update)
146 .children(own)
147 .child(div().w(px(4.))),
148 );
149 div()
150 .debug_selector(|| "shell/title-bar".into())
151 .flex_none()
152 .w_full()
153 .child(bar)
173 .child(div().flex_1().min_w_0().truncate().child(SEARCH_HINT))
174 .when(span.width >= SHORTCUT_MIN_WIDTH, |search| {
175 search.child(
176 div()
177 .flex_none()
178 .px(px(6.))
179 .py(px(1.))
180 .rounded(px(4.))
181 .bg(theme::surface_raised())
182 .text_size(px(11.))
183 .child(SHORTCUT_HINT),
184 )
185 })
186 .on_click(move |event, window, cx| on_search(event, window, cx))
187 }
188
189 impl RenderOnce for TitleBarRow {
190 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
191 let reserved = window.use_keyed_state("title-bar-reserved", cx, |_, _| None::<Reserved>);
192 let window_width = f32::from(window.viewport_size().width);
193 let span = (*reserved.read(cx)).and_then(|reserved| {
194 search_span(
195 window_width,
196 reserved.leading_end,
197 window_width - reserved.trailing_from_right,
198 )
199 });
200 let leading = h_flex()
201 .debug_selector(|| "shell/title-bar/leading".into())
202 .flex_none()
203 .h_full()
204 .gap(px(12.))
205 .items_center()
206 .child(img(app_icon()).size(px(APP_ICON_SIZE)).flex_none())
207 .child(
208 div()
209 .text_size(px(13.))
210 .font_weight(FontWeight::SEMIBOLD)
211 .text_color(theme::text())
212 .child(app_base::APP_NAME),
213 )
214 .children(self.mode_label.map(mode_pill));
215 let trailing = h_flex()
216 .debug_selector(|| "shell/title-bar/trailing".into())
217 .flex_none()
218 .h_full()
219 .gap(px(12.))
220 .items_center()
221 .children(self.update)
222 .children(self.own)
223 .child(div().w(px(4.)));
224 let row = h_flex()
225 .flex_1()
226 .h_full()
227 .gap(px(12.))
228 .items_center()
229 .child(leading)
230 .child(div().flex_1())
231 .child(trailing)
232 .on_children_prepainted(move |children, window, cx| {
233 let [leading, _, trailing] = children[..] else {
234 return;
235 };
236 let measured = Reserved {
237 leading_end: f32::from(leading.right()),
238 trailing_from_right: f32::from(window.viewport_size().width - trailing.left()),
239 };
240 reserved.update(cx, |current, cx| {
241 let unchanged = current.is_some_and(|current| {
242 (current.leading_end - measured.leading_end).abs() < 0.5
243 && (current.trailing_from_right - measured.trailing_from_right).abs()
244 < 0.5
245 });
246 if !unchanged {
247 *current = Some(measured);
248 cx.notify();
249 }
250 });
251 });
252 let bar = TitleBar::new()
253 .h(px(TITLE_BAR_HEIGHT))
254 .border_b_0()
255 .bg(theme::background())
256 .child(row);
257 let on_search = self.on_search;
258 div()
259 .debug_selector(|| "shell/title-bar".into())
260 .relative()
261 .flex_none()
262 .w_full()
263 .child(bar)
264 .children(span.map(|span| search_box(span, on_search)))
265 }
266 }
267
268 #[cfg(test)]
269 mod tests {
270 use super::{SEARCH_MIN_WIDTH, SEARCH_WIDTH, search_span};
271
272 #[test]
273 fn search_stays_centered_while_it_fits() {
274 let span = search_span(1280., 200., 1080.).expect("visible");
275 assert_eq!(span.width, SEARCH_WIDTH);
276 assert_eq!(span.left + span.width / 2., 640.);
277 }
278
279 #[test]
280 fn search_shrinks_centered_before_it_shifts() {
281 let span = search_span(800., 200., 580.).expect("visible");
282 assert_eq!(span.left + span.width / 2., 400.);
283 assert_eq!(span.width, 344.);
284 let shifted = search_span(800., 200., 500.).expect("visible");
285 assert_eq!(shifted.width, SEARCH_MIN_WIDTH);
286 assert_eq!(shifted.left + shifted.width, 492.);
287 }
288
289 #[test]
290 fn search_takes_the_room_left_and_hides_without_any() {
291 let span = search_span(640., 200., 400.).expect("visible");
292 assert_eq!((span.left, span.width), (208., 184.));
293 assert_eq!(search_span(640., 300., 340.), None);
294 }
154 295 }
@@ -12,6 +12,11 @@ use crate::updater::UpdateStatus;
12
12
13pub const TITLE_BAR_HEIGHT: f32 = 40.;
13pub const TITLE_BAR_HEIGHT: f32 = 40.;
14const SEARCH_WIDTH: f32 = 380.;
14const SEARCH_WIDTH: f32 = 380.;
15const SEARCH_MIN_WIDTH: f32 = 240.;
16const SEARCH_MIN_VISIBLE_WIDTH: f32 = 36.;
17const SEARCH_HEIGHT: f32 = 28.;
18const SEARCH_GAP: f32 = 8.;
19const SHORTCUT_MIN_WIDTH: f32 = 200.;
15const SEARCH_HINT: &str = "Search chats, people and channels";
20const SEARCH_HINT: &str = "Search chats, people and channels";
16const SHORTCUT_HINT: &str = "Ctrl K";
21const SHORTCUT_HINT: &str = "Ctrl K";
17const APP_ICON_SIZE: f32 = 18.;
22const APP_ICON_SIZE: f32 = 18.;
@@ -90,65 +95,201 @@ pub fn update_button(
90 )
95 )
91}
96}
92
97
98#[derive(Clone, Copy, Debug, PartialEq)]
99struct SearchSpan {
100 left: f32,
101 width: f32,
102}
103
104fn search_span(window_width: f32, leading_end: f32, trailing_start: f32) -> Option<SearchSpan> {
105 let start = leading_end + SEARCH_GAP;
106 let end = trailing_start - SEARCH_GAP;
107 let room = end - start;
108 if room < SEARCH_MIN_VISIBLE_WIDTH {
109 return None;
110 }
111 let center = window_width / 2.;
112 let centered_width = (2. * (center - start).min(end - center)).min(SEARCH_WIDTH);
113 let width = centered_width.max(SEARCH_MIN_WIDTH).min(room);
114 let left = (center - width / 2.).clamp(start, end - width);
115 Some(SearchSpan { left, width })
116}
117
118#[derive(Clone, Copy, PartialEq)]
119struct Reserved {
120 leading_end: f32,
121 trailing_from_right: f32,
122}
123
124#[derive(IntoElement)]
125struct TitleBarRow {
126 mode_label: Option<&'static str>,
127 update: Option<AnyElement>,
128 own: Option<AnyElement>,
129 on_search: Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>,
130}
131
93pub fn render_title_bar(
132pub fn render_title_bar(
94 mode_label: Option<&'static str>,
133 mode_label: Option<&'static str>,
95 update: Option<AnyElement>,
134 update: Option<AnyElement>,
96 own: Option<AnyElement>,
135 own: Option<AnyElement>,
97 on_search: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
136 on_search: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
98) -> impl IntoElement {
137) -> impl IntoElement {
99 let search = h_flex()
138 TitleBarRow {
139 mode_label,
140 update,
141 own,
142 on_search: Box::new(on_search),
143 }
144}
145
146fn search_box(
147 span: SearchSpan,
148 on_search: Box<dyn Fn(&ClickEvent, &mut Window, &mut App)>,
149) -> impl IntoElement {
150 h_flex()
100 .id("title-search")
151 .id("title-search")
101 .debug_selector(|| "shell/title-bar/search".into())
152 .debug_selector(|| "shell/title-bar/search".into())
102 .occlude()
153 .occlude()
103 .w(px(SEARCH_WIDTH))
154 .absolute()
104 .h(px(28.))
155 .top(px((TITLE_BAR_HEIGHT - SEARCH_HEIGHT) / 2.))
156 .left(px(span.left))
157 .w(px(span.width))
158 .h(px(SEARCH_HEIGHT))
105 .px(px(10.))
159 .px(px(10.))
106 .gap(px(8.))
160 .gap(px(8.))
107 .items_center()
161 .items_center()
162 .overflow_hidden()
108 .rounded(px(6.))
163 .rounded(px(6.))
109 .bg(theme::surface())
164 .bg(theme::surface())
110 .cursor_pointer()
165 .cursor_pointer()
111 .text_size(px(12.))
166 .text_size(px(12.))
112 .text_color(theme::text_muted())
167 .text_color(theme::text_muted())
113 .child(icon(IconName::Search, 14., theme::text_muted()))
114 .child(div().flex_1().child(SEARCH_HINT))
115 .child(
168 .child(
116 div()
169 div()
117 .px(px(6.))
170 .flex_none()
118 .py(px(1.))
171 .child(icon(IconName::Search, 14., theme::text_muted())),
119 .rounded(px(4.))
120 .bg(theme::surface_raised())
121 .text_size(px(11.))
122 .child(SHORTCUT_HINT),
123 )
172 )
124 .on_click(on_search);
173 .child(div().flex_1().min_w_0().truncate().child(SEARCH_HINT))
125 let bar = TitleBar::new()
174 .when(span.width >= SHORTCUT_MIN_WIDTH, |search| {
126 .h(px(TITLE_BAR_HEIGHT))
175 search.child(
127 .border_b_0()
176 div()
128 .bg(theme::background())
177 .flex_none()
129 .child(
178 .px(px(6.))
130 h_flex()
179 .py(px(1.))
131 .flex_1()
180 .rounded(px(4.))
132 .h_full()
181 .bg(theme::surface_raised())
133 .gap(px(12.))
182 .text_size(px(11.))
134 .items_center()
183 .child(SHORTCUT_HINT),
135 .child(img(app_icon()).size(px(APP_ICON_SIZE)).flex_none())
184 )
136 .child(
185 })
137 div()
186 .on_click(move |event, window, cx| on_search(event, window, cx))
138 .text_size(px(13.))
187}
139 .font_weight(FontWeight::SEMIBOLD)
188
140 .text_color(theme::text())
189impl RenderOnce for TitleBarRow {
141 .child(app_base::APP_NAME),
190 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
142 )
191 let reserved = window.use_keyed_state("title-bar-reserved", cx, |_, _| None::<Reserved>);
143 .children(mode_label.map(mode_pill))
192 let window_width = f32::from(window.viewport_size().width);
144 .child(div().flex_1().flex().justify_center().child(search))
193 let span = (*reserved.read(cx)).and_then(|reserved| {
145 .children(update)
194 search_span(
146 .children(own)
195 window_width,
147 .child(div().w(px(4.))),
196 reserved.leading_end,
148 );
197 window_width - reserved.trailing_from_right,
149 div()
198 )
150 .debug_selector(|| "shell/title-bar".into())
199 });
151 .flex_none()
200 let leading = h_flex()
152 .w_full()
201 .debug_selector(|| "shell/title-bar/leading".into())
153 .child(bar)
202 .flex_none()
203 .h_full()
204 .gap(px(12.))
205 .items_center()
206 .child(img(app_icon()).size(px(APP_ICON_SIZE)).flex_none())
207 .child(
208 div()
209 .text_size(px(13.))
210 .font_weight(FontWeight::SEMIBOLD)
211 .text_color(theme::text())
212 .child(app_base::APP_NAME),
213 )
214 .children(self.mode_label.map(mode_pill));
215 let trailing = h_flex()
216 .debug_selector(|| "shell/title-bar/trailing".into())
217 .flex_none()
218 .h_full()
219 .gap(px(12.))
220 .items_center()
221 .children(self.update)
222 .children(self.own)
223 .child(div().w(px(4.)));
224 let row = h_flex()
225 .flex_1()
226 .h_full()
227 .gap(px(12.))
228 .items_center()
229 .child(leading)
230 .child(div().flex_1())
231 .child(trailing)
232 .on_children_prepainted(move |children, window, cx| {
233 let [leading, _, trailing] = children[..] else {
234 return;
235 };
236 let measured = Reserved {
237 leading_end: f32::from(leading.right()),
238 trailing_from_right: f32::from(window.viewport_size().width - trailing.left()),
239 };
240 reserved.update(cx, |current, cx| {
241 let unchanged = current.is_some_and(|current| {
242 (current.leading_end - measured.leading_end).abs() < 0.5
243 && (current.trailing_from_right - measured.trailing_from_right).abs()
244 < 0.5
245 });
246 if !unchanged {
247 *current = Some(measured);
248 cx.notify();
249 }
250 });
251 });
252 let bar = TitleBar::new()
253 .h(px(TITLE_BAR_HEIGHT))
254 .border_b_0()
255 .bg(theme::background())
256 .child(row);
257 let on_search = self.on_search;
258 div()
259 .debug_selector(|| "shell/title-bar".into())
260 .relative()
261 .flex_none()
262 .w_full()
263 .child(bar)
264 .children(span.map(|span| search_box(span, on_search)))
265 }
266}
267
268#[cfg(test)]
269mod tests {
270 use super::{SEARCH_MIN_WIDTH, SEARCH_WIDTH, search_span};
271
272 #[test]
273 fn search_stays_centered_while_it_fits() {
274 let span = search_span(1280., 200., 1080.).expect("visible");
275 assert_eq!(span.width, SEARCH_WIDTH);
276 assert_eq!(span.left + span.width / 2., 640.);
277 }
278
279 #[test]
280 fn search_shrinks_centered_before_it_shifts() {
281 let span = search_span(800., 200., 580.).expect("visible");
282 assert_eq!(span.left + span.width / 2., 400.);
283 assert_eq!(span.width, 344.);
284 let shifted = search_span(800., 200., 500.).expect("visible");
285 assert_eq!(shifted.width, SEARCH_MIN_WIDTH);
286 assert_eq!(shifted.left + shifted.width, 492.);
287 }
288
289 #[test]
290 fn search_takes_the_room_left_and_hides_without_any() {
291 let span = search_span(640., 200., 400.).expect("visible");
292 assert_eq!((span.left, span.width), (208., 184.));
293 assert_eq!(search_span(640., 300., 340.), None);
294 }
154}
295}