on master help sign in
Only use a single documentation popup (#1241)
commit29c053e84e2624feb786f520ebae4c752bc23279 authorKirawi <67773714+kirawi@users.noreply.github.com> dateWed 8 Dec 2021, 02:11 (5 years ago) parentd08bdfa
4 files changed+26−4
helix-term/src/commands.rs+6−2 helix-term/src/compositor.rs+12 helix-term/src/ui/completion.rs+1−1 helix-term/src/ui/popup.rs+7−1
helix-term/src/commands.rs+6−2
@@ -5130,8 +5130,12 @@ fn hover(cx: &mut Context) {
51305130 // skip if contents empty
51315131
51325132 let contents = ui::Markdown::new(contents, editor.syn_loader.clone());
5133− let popup = Popup::new(contents);
5134− compositor.push(Box::new(popup));
5133+ let popup = Popup::new("documentation", contents);
5134+ if let Some(doc_popup) = compositor.find_id("documentation") {
5135+ *doc_popup = popup;
5136+ } else {
5137+ compositor.push(Box::new(popup));
5138+ }
51355139 }
51365140 },
51375141 );
helix-term/src/compositor.rs+12
@@ -64,6 +64,10 @@ pub trait Component: Any + AnyComponent {
6464 fn type_name(&self) -> &'static str {
6565 std::any::type_name::<Self>()
6666 }
67+
68+ fn id(&self) -> Option<&'static str> {
69+ None
70+ }
6771}
6872
6973use anyhow::Error;
@@ -184,6 +188,14 @@ impl Compositor {
184188 .find(|component| component.type_name() == type_name)
185189 .and_then(|component| component.as_any_mut().downcast_mut())
186190 }
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+ }
187199}
188200
189201// View casting, taken straight from Cursive
helix-term/src/ui/completion.rs+1−1
@@ -168,7 +168,7 @@ impl Completion {
168168 }
169169 };
170170 });
171− let popup = Popup::new(menu);
171+ let popup = Popup::new("completion", menu);
172172 let mut completion = Self {
173173 popup,
174174 start_offset,
helix-term/src/ui/popup.rs+7−1
@@ -16,15 +16,17 @@ pub struct Popup<T: Component> {
1616 position: Option<Position>,
1717 size: (u16, u16),
1818 scroll: usize,
19+ id: &'static str,
1920}
2021
2122impl<T: Component> Popup<T> {
22− pub fn new(contents: T) -> Self {
23+ pub fn new(id: &'static str, contents: T) -> Self {
2324 Self {
2425 contents,
2526 position: None,
2627 size: (0, 0),
2728 scroll: 0,
29+ id,
2830 }
2931 }
3032
@@ -143,4 +145,8 @@ impl<T: Component> Component for Popup<T> {
143145
144146 self.contents.render(area, surface, cx);
145147 }
148+
149+ fn id(&self) -> Option<&'static str> {
150+ Some(self.id)
151+ }
146152}