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(window, cx)
170 .max_w(max_width)
171 .max_h(max_height)
172 .overflow_y_scroll()
173 .refine_style(&self.style)
174 .child((self.content_builder)(window, cx)),
175 )
176 .into_any_element();
177
178 let popover_size = popover.layout_as_root(AvailableSpace::min_size(), window, cx);
179 const SNAP_TO_EDGE: Pixels = px(8.);
180 let top_space = trigger_bounds.top() - SNAP_TO_EDGE;
181 let right_space = window.bounds().size.width - trigger_bounds.left() - SNAP_TO_EDGE;
182
183 let mut pos = point(
184 trigger_bounds.left(),
185 trigger_bounds.top() - popover_size.height,
186 );
187 if popover_size.height > top_space {
188 pos.y = trigger_bounds.bottom();
189 }
190 if popover_size.width > right_space {
191 pos.x = trigger_bounds.right() - popover_size.width;
192 }
193
194 let mut empty = div().into_any_element();
195 let layout_id = empty.request_layout(window, cx);
196 (
197 layout_id,
198 PopoverLayoutState {
199 bounds: Bounds {
200 origin: pos,
201 size: popover_size,
202 },
203 element: Some(popover),
204 },
205 )
206 }
207
208 fn prepaint(
209 &mut self,
210 _: Option<&gpui::GlobalElementId>,
211 _: Option<&gpui::InspectorElementId>,
212 _: Bounds<Pixels>,
213 request_layout: &mut Self::RequestLayoutState,
214 window: &mut Window,
215 cx: &mut App,
216 ) -> Self::PrepaintState {
217 let bounds = request_layout.bounds;
218 let Some(popover) = request_layout.element.as_mut() else {
219 return;
220 };
221
222 window.with_absolute_element_offset(bounds.origin, |window| {
223 popover.prepaint(window, cx);
224 })
225 }
226
227 fn paint(
228 &mut self,
229 _: Option<&gpui::GlobalElementId>,
230 _: Option<&gpui::InspectorElementId>,
231 _: Bounds<Pixels>,
232 request_layout: &mut Self::RequestLayoutState,
233 _: &mut Self::PrepaintState,
234 window: &mut Window,
235 cx: &mut App,
236 ) {
237 let bounds = request_layout.bounds;
238 let Some(popover) = request_layout.element.as_mut() else {
239 return;
240 };
241
242 popover.paint(window, cx);
243
244 let editor = self.editor.clone();
245 // Mouse down out to hide.
246 window.on_mouse_event(move |event: &MouseDownEvent, _, _, cx| {
247 if !bounds.contains(&event.position) {
248 let _ = editor.update(cx, |editor, cx| {
249 editor.clear_hover_state(cx);
250 });
251 }
252 });
253
254 // Mouse out of trigger + popover bounds
255 let editor = self.editor.clone();
256 let trigger_bounds = self.trigger_bounds(cx).unwrap_or(bounds);
257 let keep_open_region = trigger_bounds.union(&bounds);
258 window.on_mouse_event(move |event: &MouseMoveEvent, _, _, cx| {
259 if !keep_open_region.contains(&event.position) {
260 let _ = editor.update(cx, |editor, cx| {
261 editor.clear_hover_state(cx);
262 });
263 }
264 })
265 }
266}