BlueGit

ict rusty-teams

Files

rusty-teams / vendor / gpui-component / src / input / popovers / hover_popover.rs

hover_popover.rs

hover_popover.rs 7.9 KiB Download Raw
1use std::{ops::Range, rc::Rc};
2
3use gpui::{
4 AnyElement, App, AppContext as _, AvailableSpace, Bounds, Element, ElementId, Entity,
5 InteractiveElement, IntoElement, MouseDownEvent, MouseMoveEvent, ParentElement as _, Pixels,
6 Render, StatefulInteractiveElement as _, StyleRefinement, Styled, Window, deferred, div, point,
7 px,
8};
9
10use crate::{
11 StyledExt, ThemeStyled as _,
12 input::{EditorState, popovers::render_markdown},
13};
14
15pub struct HoverPopover {
16 editor: Entity<EditorState>,
17 /// The symbol range byte of the hover trigger.
18 pub(crate) symbol_range: Range<usize>,
19 pub(crate) hover: Rc<lsp_types::Hover>,
20}
21
22impl HoverPopover {
23 pub fn new(
24 editor: Entity<EditorState>,
25 symbol_range: Range<usize>,
26 hover: &lsp_types::Hover,
27 cx: &mut App,
28 ) -> Entity<Self> {
29 let hover = Rc::new(hover.clone());
30
31 cx.new(|_| Self {
32 editor,
33 symbol_range,
34 hover,
35 })
36 }
37}
38
39impl Render for HoverPopover {
40 fn render(&mut self, _: &mut Window, _: &mut gpui::Context<Self>) -> impl IntoElement {
41 let contents = match self.hover.contents.clone() {
42 lsp_types::HoverContents::Scalar(scalar) => match scalar {
43 lsp_types::MarkedString::String(s) => s,
44 lsp_types::MarkedString::LanguageString(ls) => ls.value,
45 },
46 lsp_types::HoverContents::Array(arr) => arr
47 .into_iter()
48 .map(|item| match item {
49 lsp_types::MarkedString::String(s) => s,
50 lsp_types::MarkedString::LanguageString(ls) => ls.value,
51 })
52 .collect::<Vec<_>>()
53 .join("\n\n"),
54 lsp_types::HoverContents::Markup(markup) => markup.value,
55 };
56
57 Popover::new(
58 "hover-popover",
59 self.editor.clone(),
60 self.symbol_range.clone(),
61 move |window, cx| render_markdown("message", contents.clone(), window, cx),
62 )
63 .into_any_element()
64 }
65}
66
67pub(crate) struct Popover {
68 id: ElementId,
69 style: StyleRefinement,
70 editor: Entity<EditorState>,
71 range: Range<usize>,
72 width_limit: Range<Pixels>,
73 content_builder: Box<dyn Fn(&mut Window, &mut App) -> AnyElement>,
74}
75
76impl Styled for Popover {
77 fn style(&mut self) -> &mut StyleRefinement {
78 &mut self.style
79 }
80}
81
82impl Popover {
83 pub fn new<F, E>(
84 id: impl Into<ElementId>,
85 editor: Entity<EditorState>,
86 range: Range<usize>,
87 f: F,
88 ) -> Self
89 where
90 F: Fn(&mut Window, &mut App) -> E + 'static,
91 E: IntoElement,
92 {
93 Self {
94 id: id.into(),
95 editor,
96 range,
97 style: StyleRefinement::default(),
98 width_limit: px(200.)..px(500.),
99 content_builder: Box::new(move |window, cx| (f)(window, cx).into_any_element()),
100 }
101 }
102
103 /// Get the bounds of the range in the editor, if it is visible.
104 fn trigger_bounds(&self, cx: &App) -> Option<Bounds<Pixels>> {
105 let editor = self.editor.read(cx);
106 editor.range_to_bounds(&self.range)
107 }
108}
109
110impl IntoElement for Popover {
111 type Element = Self;
112
113 fn into_element(self) -> Self::Element {
114 self
115 }
116}
117
118pub(crate) struct PopoverLayoutState {
119 bounds: Bounds<Pixels>,
120 element: Option<AnyElement>,
121}
122
123impl Element for Popover {
124 type RequestLayoutState = PopoverLayoutState;
125 type PrepaintState = ();
126
127 fn id(&self) -> Option<ElementId> {
128 Some(self.id.clone())
129 }
130
131 fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
132 None
133 }
134
135 fn request_layout(
136 &mut self,
137 _: Option<&gpui::GlobalElementId>,
138 _: Option<&gpui::InspectorElementId>,
139 window: &mut Window,
140 cx: &mut App,
141 ) -> (gpui::LayoutId, Self::RequestLayoutState) {
142 let trigger_bounds = match self.trigger_bounds(cx) {
143 Some(bounds) => bounds,
144 None => {
145 return (
146 div().into_any_element().request_layout(window, cx),
147 PopoverLayoutState {
148 bounds: Bounds::default(),
149 element: None,
150 },
151 );
152 }
153 };
154
155 let max_width = self
156 .width_limit
157 .end
158 .min(window.bounds().size.width - SNAP_TO_EDGE * 2)
159 .max(px(200.));
160 let max_height = (window.bounds().size.height - SNAP_TO_EDGE * 2).min(px(320.));
161
162 let mut popover = deferred(
163 div()
164 .id("hover-popover-content")
165 .flex_none()
166 .occlude()
167 .p_1()
168 .text_xs()
169 .popover_style(cx)
170 .shadow_md()
171 .max_w(max_width)
172 .max_h(max_height)
173 .overflow_y_scroll()
174 .refine_style(&self.style)
175 .child((self.content_builder)(window, cx)),
176 )
177 .into_any_element();
178
179 let popover_size = popover.layout_as_root(AvailableSpace::min_size(), window, cx);
180 const SNAP_TO_EDGE: Pixels = px(8.);
181 let top_space = trigger_bounds.top() - SNAP_TO_EDGE;
182 let right_space = window.bounds().size.width - trigger_bounds.left() - SNAP_TO_EDGE;
183
184 let mut pos = point(
185 trigger_bounds.left(),
186 trigger_bounds.top() - popover_size.height,
187 );
188 if popover_size.height > top_space {
189 pos.y = trigger_bounds.bottom();
190 }
191 if popover_size.width > right_space {
192 pos.x = trigger_bounds.right() - popover_size.width;
193 }
194
195 let mut empty = div().into_any_element();
196 let layout_id = empty.request_layout(window, cx);
197 (
198 layout_id,
199 PopoverLayoutState {
200 bounds: Bounds {
201 origin: pos,
202 size: popover_size,
203 },
204 element: Some(popover),
205 },
206 )
207 }
208
209 fn prepaint(
210 &mut self,
211 _: Option<&gpui::GlobalElementId>,
212 _: Option<&gpui::InspectorElementId>,
213 _: Bounds<Pixels>,
214 request_layout: &mut Self::RequestLayoutState,
215 window: &mut Window,
216 cx: &mut App,
217 ) -> Self::PrepaintState {
218 let bounds = request_layout.bounds;
219 let Some(popover) = request_layout.element.as_mut() else {
220 return;
221 };
222
223 window.with_absolute_element_offset(bounds.origin, |window| {
224 popover.prepaint(window, cx);
225 })
226 }
227
228 fn paint(
229 &mut self,
230 _: Option<&gpui::GlobalElementId>,
231 _: Option<&gpui::InspectorElementId>,
232 _: Bounds<Pixels>,
233 request_layout: &mut Self::RequestLayoutState,
234 _: &mut Self::PrepaintState,
235 window: &mut Window,
236 cx: &mut App,
237 ) {
238 let bounds = request_layout.bounds;
239 let Some(popover) = request_layout.element.as_mut() else {
240 return;
241 };
242
243 popover.paint(window, cx);
244
245 let editor = self.editor.clone();
246 // Mouse down out to hide.
247 window.on_mouse_event(move |event: &MouseDownEvent, _, _, cx| {
248 if !bounds.contains(&event.position) {
249 let _ = editor.update(cx, |editor, cx| {
250 editor.clear_hover_state(cx);
251 });
252 }
253 });
254
255 // Mouse out of trigger + popover bounds
256 let editor = self.editor.clone();
257 let trigger_bounds = self.trigger_bounds(cx).unwrap_or(bounds);
258 let keep_open_region = trigger_bounds.union(&bounds);
259 window.on_mouse_event(move |event: &MouseMoveEvent, _, _, cx| {
260 if !keep_open_region.contains(&event.position) {
261 let _ = editor.update(cx, |editor, cx| {
262 editor.clear_hover_state(cx);
263 });
264 }
265 })
266 }
267}