on master help sign in
Only use a single documentation popup (#1241)
29c053eKirawi
helix-term/src/commands.rs+6−2
@@ -5130,8 +5130,12 @@ fn hover(cx: &mut Context) {
5130 // skip if contents empty
5130 // skip if contents empty
5131
5131
5132 let contents = ui::Markdown::new(contents, editor.syn_loader.clone());
5132 let contents = ui::Markdown::new(contents, editor.syn_loader.clone());
5133− let popup = Popup::new(contents);
5133+ let popup = Popup::new("documentation", contents);
5134+ if let Some(doc_popup) = compositor.find_id("documentation") {
5135+ *doc_popup = popup;
5136+ } else {
5134− compositor.push(Box::new(popup));
5137+ compositor.push(Box::new(popup));
5138+ }
5135 }
5139 }
5136 },
5140 },
5137 );
5141 );
helix-term/src/compositor.rs+12
@@ -64,6 +64,10 @@ pub trait Component: Any + AnyComponent {
64 fn type_name(&self) -> &'static str {
64 fn type_name(&self) -> &'static str {
65 std::any::type_name::<Self>()
65 std::any::type_name::<Self>()
66 }
66 }
67+
68+ fn id(&self) -> Option<&'static str> {
69+ None
70+ }
67}
71}
68
72
69use anyhow::Error;
73use anyhow::Error;
@@ -184,6 +188,14 @@ impl Compositor {
184 .find(|component| component.type_name() == type_name)
188 .find(|component| component.type_name() == type_name)
185 .and_then(|component| component.as_any_mut().downcast_mut())
189 .and_then(|component| component.as_any_mut().downcast_mut())
186 }
190 }
191+
192+ pub fn find_id<T: 'static>(&mut self, id: &'static str) -> Option<&mut T> {
193+ let type_name = std::any::type_name::<T>();
194+ self.layers
195+ .iter_mut()
196+ .find(|component| component.type_name() == type_name && component.id() == Some(id))
197+ .and_then(|component| component.as_any_mut().downcast_mut())
198+ }
187}
199}
188
200
189// View casting, taken straight from Cursive
201// View casting, taken straight from Cursive
helix-term/src/ui/completion.rs+1−1
@@ -168,7 +168,7 @@ impl Completion {
168 }
168 }
169 };
169 };
170 });
170 });
171− let popup = Popup::new(menu);
171+ let popup = Popup::new("completion", menu);
172 let mut completion = Self {
172 let mut completion = Self {
173 popup,
173 popup,
174 start_offset,
174 start_offset,
helix-term/src/ui/popup.rs+7−1
@@ -16,15 +16,17 @@ pub struct Popup<T: Component> {
16 position: Option<Position>,
16 position: Option<Position>,
17 size: (u16, u16),
17 size: (u16, u16),
18 scroll: usize,
18 scroll: usize,
19+ id: &'static str,
19}
20}
20
21
21impl<T: Component> Popup<T> {
22impl<T: Component> Popup<T> {
22− pub fn new(contents: T) -> Self {
23+ pub fn new(id: &'static str, contents: T) -> Self {
23 Self {
24 Self {
24 contents,
25 contents,
25 position: None,
26 position: None,
26 size: (0, 0),
27 size: (0, 0),
27 scroll: 0,
28 scroll: 0,
29+ id,
28 }
30 }
29 }
31 }
30
32
@@ -143,4 +145,8 @@ impl<T: Component> Component for Popup<T> {
143
145
144 self.contents.render(area, surface, cx);
146 self.contents.render(area, surface, cx);
145 }
147 }
148+
149+ fn id(&self) -> Option<&'static str> {
150+ Some(self.id)
151+ }
146}
152}