| @@ -0,0 +1,197 @@ |
| 1 | + //! Find in thread (Ctrl+F), like T3 Code's: a search field over the |
| 2 | + //! conversation's top right corner with the match count, previous and next, |
| 3 | + //! which scrolls to each message that matches. |
| 4 | + |
| 5 | + use gpui_kit::component::{ |
| 6 | + Icon, IconName, h_flex, |
| 7 | + input::{Input, InputEvent, InputState}, |
| 8 | + }; |
| 9 | + use gpui_kit::*; |
| 10 | + |
| 11 | + use super::ChatView; |
| 12 | + use crate::session::ChatItem; |
| 13 | + use crate::ui::metal::{self, MetalButton}; |
| 14 | + |
| 15 | + pub(super) struct Find { |
| 16 | + input: Entity<InputState>, |
| 17 | + /// The transcript rows that match, in order. |
| 18 | + pub matches: Vec<usize>, |
| 19 | + pub current: usize, |
| 20 | + _subscription: Subscription, |
| 21 | + } |
| 22 | + |
| 23 | + impl ChatView { |
| 24 | + /// Opens the find bar, or with it open, selects what's typed in it. |
| 25 | + pub(super) fn open_find(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 26 | + if self.find.is_none() { |
| 27 | + let input = cx.new(|cx| InputState::new(window, cx).placeholder("Find in thread")); |
| 28 | + let subscription = cx.subscribe_in(&input, window, |this, _, event: &InputEvent, _, cx| match event { |
| 29 | + InputEvent::Change => { |
| 30 | + this.update_find(cx); |
| 31 | + this.find_step(true, true, cx); |
| 32 | + } |
| 33 | + InputEvent::PressEnter { shift, .. } => this.find_step(!shift, false, cx), |
| 34 | + _ => {} |
| 35 | + }); |
| 36 | + self.find = Some(Find { input, matches: Vec::new(), current: 0, _subscription: subscription }); |
| 37 | + } |
| 38 | + if let Some(find) = &self.find { |
| 39 | + find.input.update(cx, |input, cx| { |
| 40 | + input.focus(window, cx); |
| 41 | + input.select_all(window, cx); |
| 42 | + }); |
| 43 | + } |
| 44 | + cx.notify(); |
| 45 | + } |
| 46 | + |
| 47 | + pub(super) fn close_find(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 48 | + if self.find.take().is_some() { |
| 49 | + self.focus_composer(window, cx); |
| 50 | + cx.notify(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /// The rows whose messages contain what's typed, case aside. |
| 55 | + fn update_find(&mut self, cx: &mut Context<Self>) { |
| 56 | + let Some(find) = self.find.as_mut() else { return }; |
| 57 | + let query = find.input.read(cx).value().trim().to_lowercase(); |
| 58 | + find.matches = if query.is_empty() { |
| 59 | + Vec::new() |
| 60 | + } else { |
| 61 | + self.session |
| 62 | + .read(cx) |
| 63 | + .items |
| 64 | + .iter() |
| 65 | + .enumerate() |
| 66 | + .filter(|(_, item)| match item { |
| 67 | + ChatItem::User { text, .. } => text.to_lowercase().contains(&query), |
| 68 | + ChatItem::Assistant(answer) => answer.text.to_lowercase().contains(&query), |
| 69 | + ChatItem::Tool(tool) if tool.name == "ExitPlanMode" => { |
| 70 | + tool.input.get("plan").and_then(|plan| plan.as_str()).is_some_and(|plan| plan.to_lowercase().contains(&query)) |
| 71 | + } |
| 72 | + _ => false, |
| 73 | + }) |
| 74 | + .map(|(ix, _)| ix) |
| 75 | + .collect() |
| 76 | + }; |
| 77 | + find.current = find.current.min(find.matches.len().saturating_sub(1)); |
| 78 | + } |
| 79 | + |
| 80 | + /// Moves to the next (or previous) match, unfolding the turn it's in |
| 81 | + /// and scrolling to it. `stay` keeps the current one, for a new query. |
| 82 | + fn find_step(&mut self, forward: bool, stay: bool, cx: &mut Context<Self>) { |
| 83 | + let Some(find) = self.find.as_mut() else { return }; |
| 84 | + let count = find.matches.len(); |
| 85 | + if count == 0 { |
| 86 | + cx.notify(); |
| 87 | + return; |
| 88 | + } |
| 89 | + if !stay { |
| 90 | + find.current = if forward { (find.current + 1) % count } else { (find.current + count - 1) % count }; |
| 91 | + } |
| 92 | + let ix = find.matches[find.current]; |
| 93 | + let session = self.session.read(cx); |
| 94 | + let fold = crate::settings::get(|settings| settings.fold_turns); |
| 95 | + let hidden = session |
| 96 | + .finished_turn(ix) |
| 97 | + .filter(|turn| turn.answer != Some(ix) && session.turn_expanded(turn.end) != fold) |
| 98 | + .map(|turn| turn.end); |
| 99 | + if let Some(end) = hidden { |
| 100 | + self.session.update(cx, |session, cx| session.toggle_turn(end, cx)); |
| 101 | + } |
| 102 | + self.scroller.update(cx, |scroller, cx| { |
| 103 | + if hidden.is_some() { |
| 104 | + scroller.remeasure(cx); |
| 105 | + } |
| 106 | + scroller.scroll_to_item(ix, cx); |
| 107 | + }); |
| 108 | + cx.notify(); |
| 109 | + } |
| 110 | + |
| 111 | + /// The bar, floating over the conversation's top right corner. |
| 112 | + pub(super) fn render_find_bar(&self, cx: &mut Context<Self>) -> Option<AnyElement> { |
| 113 | + let find = self.find.as_ref()?; |
| 114 | + let p = metal::palette(cx); |
| 115 | + let count = find.matches.len(); |
| 116 | + let searching = !find.input.read(cx).value().trim().is_empty(); |
| 117 | + let label = if count > 0 { format!("{}/{count}", find.current + 1) } else { "0/0".to_string() }; |
| 118 | + Some( |
| 119 | + h_flex() |
| 120 | + .id("find-bar") |
| 121 | + .absolute() |
| 122 | + .top(px(12.)) |
| 123 | + .right(px(12.)) |
| 124 | + .w(px(384.)) |
| 125 | + .h(px(36.)) |
| 126 | + .pl(px(12.)) |
| 127 | + .pr(px(4.)) |
| 128 | + .gap(px(4.)) |
| 129 | + .rounded(px(12.)) |
| 130 | + .bg(p.menu_bg) |
| 131 | + .shadow(p.raise.clone()) |
| 132 | + .on_key_down(cx.listener(|this, event: &KeyDownEvent, window, cx| { |
| 133 | + if event.keystroke.key == "escape" { |
| 134 | + cx.stop_propagation(); |
| 135 | + this.close_find(window, cx); |
| 136 | + } |
| 137 | + })) |
| 138 | + .child(Icon::new(IconName::Search).size(px(14.)).text_color(p.ink3)) |
| 139 | + .child(div().flex_1().min_w_0().text_size(px(13.)).child(Input::new(&find.input).appearance(false))) |
| 140 | + .child( |
| 141 | + div() |
| 142 | + .min_w(px(40.)) |
| 143 | + .text_center() |
| 144 | + .text_size(px(12.)) |
| 145 | + .text_color(if searching && count == 0 { p.red } else { p.ink3 }) |
| 146 | + .child(label), |
| 147 | + ) |
| 148 | + .child( |
| 149 | + MetalButton::new("find-previous") |
| 150 | + .ghost() |
| 151 | + .mini() |
| 152 | + .size(px(24.)) |
| 153 | + .icon(IconName::ChevronUp) |
| 154 | + .tooltip("Previous match") |
| 155 | + .disabled(count == 0) |
| 156 | + .on_click(cx.listener(|this, _, _, cx| this.find_step(false, false, cx))), |
| 157 | + ) |
| 158 | + .child( |
| 159 | + MetalButton::new("find-next") |
| 160 | + .ghost() |
| 161 | + .mini() |
| 162 | + .size(px(24.)) |
| 163 | + .icon(IconName::ChevronDown) |
| 164 | + .tooltip("Next match") |
| 165 | + .disabled(count == 0) |
| 166 | + .on_click(cx.listener(|this, _, _, cx| this.find_step(true, false, cx))), |
| 167 | + ) |
| 168 | + .child( |
| 169 | + MetalButton::new("find-close") |
| 170 | + .ghost() |
| 171 | + .mini() |
| 172 | + .size(px(24.)) |
| 173 | + .icon(IconName::Close) |
| 174 | + .tooltip("Close find") |
| 175 | + .on_click(cx.listener(|this, _, window, cx| this.close_find(window, cx))), |
| 176 | + ) |
| 177 | + .into_any_element(), |
| 178 | + ) |
| 179 | + } |
| 180 | + |
| 181 | + /// The match the find bar is on, and the others, for the timeline to |
| 182 | + /// mark. |
| 183 | + pub(super) fn find_marks(&self) -> Option<(Vec<usize>, Option<usize>)> { |
| 184 | + let find = self.find.as_ref()?; |
| 185 | + Some((find.matches.clone(), find.matches.get(find.current).copied())) |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + /// How a matching message is marked: an amber outline, stronger on the |
| 190 | + /// current one. |
| 191 | + pub(super) fn mark(element: Div, current: bool, p: &metal::Palette) -> Div { |
| 192 | + element |
| 193 | + .rounded(px(12.)) |
| 194 | + .border_1() |
| 195 | + .border_color(p.amber.opacity(if current { 0.9 } else { 0.35 })) |
| 196 | + .bg(p.amber.opacity(if current { 0.08 } else { 0.03 })) |
| 197 | + } |