1use gpui::{Action, Context, Div, Entity, InteractiveElement, Window, div};
2use workspace::Workspace;
3
4use crate::BufferSearchBar;
5
6/// Registrar inverts the dependency between search and its downstream user, allowing said downstream user to register search action without knowing exactly what those actions are.
7pub trait SearchActionsRegistrar {
8 fn register_handler<A: Action>(&mut self, callback: impl ActionExecutor<A>);
9}
10
11type SearchBarActionCallback<A> =
12 fn(&mut BufferSearchBar, &A, &mut Window, &mut Context<BufferSearchBar>);
13
14type GetSearchBar<T> =
15 for<'a, 'b> fn(&'a T, &'a mut Window, &mut Context<'b, T>) -> Option<Entity<BufferSearchBar>>;
16
17/// Registers search actions on a div that can be taken out.
18pub struct DivRegistrar<'a, 'b, T: 'static> {
19 div: Option<Div>,
20 cx: &'a mut Context<'b, T>,
21 search_getter: GetSearchBar<T>,
22}
23
24impl<'a, 'b, T: 'static> DivRegistrar<'a, 'b, T> {
25 pub fn new(search_getter: GetSearchBar<T>, cx: &'a mut Context<'b, T>) -> Self {
26 Self {
27 div: Some(div()),
28 cx,
29 search_getter,
30 }
31 }
32 pub fn into_div(self) -> Div {
33 // This option is always Some; it's an option in the first place because we want to call methods
34 // on div that require ownership.
35 self.div.unwrap()
36 }
37}
38
39impl<T: 'static> SearchActionsRegistrar for DivRegistrar<'_, '_, T> {
40 fn register_handler<A: Action>(&mut self, callback: impl ActionExecutor<A>) {
41 let getter = self.search_getter;
42 self.div = self.div.take().map(|div| {
43 div.on_action(self.cx.listener(move |this, action, window, cx| {
44 let should_notify = (getter)(this, window, cx)
45 .map(|search_bar| {
46 search_bar.update(cx, |search_bar, cx| {
47 callback.execute(search_bar, action, window, cx)
48 })
49 })
50 .unwrap_or(false);
51 if should_notify {
52 cx.notify();
53 } else {
54 cx.propagate();
55 }
56 }))
57 });
58 }
59}
60
61/// Register actions for an active pane.
62impl SearchActionsRegistrar for Workspace {
63 fn register_handler<A: Action>(&mut self, callback: impl ActionExecutor<A>) {
64 self.register_action(move |workspace, action: &A, window, cx| {
65 if workspace.has_active_modal(window, cx) && !workspace.hide_modal(window, cx) {
66 cx.propagate();
67 return;
68 }
69
70 let pane = workspace.active_pane();
71 let callback = callback.clone();
72 pane.update(cx, |this, cx| {
73 this.toolbar().update(cx, move |this, cx| {
74 if let Some(search_bar) = this.item_of_type::<BufferSearchBar>() {
75 let should_notify = search_bar.update(cx, move |search_bar, cx| {
76 callback.execute(search_bar, action, window, cx)
77 });
78 if should_notify {
79 cx.notify();
80 } else {
81 cx.propagate();
82 }
83 }
84 })
85 });
86 });
87 }
88}
89
90type DidHandleAction = bool;
91/// Potentially executes the underlying action if some preconditions are met (e.g. buffer search bar is visible)
92pub trait ActionExecutor<A: Action>: 'static + Clone {
93 fn execute(
94 &self,
95 search_bar: &mut BufferSearchBar,
96 action: &A,
97 window: &mut Window,
98 cx: &mut Context<BufferSearchBar>,
99 ) -> DidHandleAction;
100}
101
102/// Run an action when the search bar has been dismissed from the panel.
103pub struct ForDismissed<A>(pub(super) SearchBarActionCallback<A>);
104impl<A> Clone for ForDismissed<A> {
105 fn clone(&self) -> Self {
106 Self(self.0)
107 }
108}
109
110impl<A: Action> ActionExecutor<A> for ForDismissed<A> {
111 fn execute(
112 &self,
113 search_bar: &mut BufferSearchBar,
114 action: &A,
115 window: &mut Window,
116 cx: &mut Context<BufferSearchBar>,
117 ) -> DidHandleAction {
118 if search_bar.is_dismissed() {
119 self.0(search_bar, action, window, cx);
120 true
121 } else {
122 false
123 }
124 }
125}
126
127/// Run an action when the search bar is deployed.
128pub struct ForDeployed<A>(pub(super) SearchBarActionCallback<A>);
129impl<A> Clone for ForDeployed<A> {
130 fn clone(&self) -> Self {
131 Self(self.0)
132 }
133}
134
135impl<A: Action> ActionExecutor<A> for ForDeployed<A> {
136 fn execute(
137 &self,
138 search_bar: &mut BufferSearchBar,
139 action: &A,
140 window: &mut Window,
141 cx: &mut Context<BufferSearchBar>,
142 ) -> DidHandleAction {
143 if search_bar.is_dismissed() || search_bar.active_searchable_item.is_none() {
144 false
145 } else {
146 self.0(search_bar, action, window, cx);
147 true
148 }
149 }
150}
151
152/// Run an action when the search bar has any matches, regardless of whether it
153/// is visible or not.
154pub struct WithResults<A>(pub(super) SearchBarActionCallback<A>);
155impl<A> Clone for WithResults<A> {
156 fn clone(&self) -> Self {
157 Self(self.0)
158 }
159}
160
161impl<A: Action> ActionExecutor<A> for WithResults<A> {
162 fn execute(
163 &self,
164 search_bar: &mut BufferSearchBar,
165 action: &A,
166 window: &mut Window,
167 cx: &mut Context<BufferSearchBar>,
168 ) -> DidHandleAction {
169 if search_bar.active_match_index.is_some() {
170 self.0(search_bar, action, window, cx);
171 true
172 } else {
173 false
174 }
175 }
176}