Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace old documentation popups #1241

Merged
merged 1 commit into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5123,8 +5123,12 @@ fn hover(cx: &mut Context) {
// skip if contents empty

let contents = ui::Markdown::new(contents, editor.syn_loader.clone());
let popup = Popup::new(contents);
compositor.push(Box::new(popup));
let popup = Popup::new("documentation", contents);
if let Some(doc_popup) = compositor.find_id("documentation") {
*doc_popup = popup;
} else {
compositor.push(Box::new(popup));
}
}
},
);
Expand Down
12 changes: 12 additions & 0 deletions helix-term/src/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ pub trait Component: Any + AnyComponent {
fn type_name(&self) -> &'static str {
std::any::type_name::<Self>()
}

fn id(&self) -> Option<&'static str> {
None
}
}

use anyhow::Error;
Expand Down Expand Up @@ -184,6 +188,14 @@ impl Compositor {
.find(|component| component.type_name() == type_name)
.and_then(|component| component.as_any_mut().downcast_mut())
}

pub fn find_id<T: 'static>(&mut self, id: &'static str) -> Option<&mut T> {
let type_name = std::any::type_name::<T>();
self.layers
.iter_mut()
.find(|component| component.type_name() == type_name && component.id() == Some(id))
.and_then(|component| component.as_any_mut().downcast_mut())
}
}

// View casting, taken straight from Cursive
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/ui/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl Completion {
}
};
});
let popup = Popup::new(menu);
let popup = Popup::new("completion", menu);
let mut completion = Self {
popup,
start_offset,
Expand Down
8 changes: 7 additions & 1 deletion helix-term/src/ui/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ pub struct Popup<T: Component> {
position: Option<Position>,
size: (u16, u16),
scroll: usize,
id: &'static str,
}

impl<T: Component> Popup<T> {
pub fn new(contents: T) -> Self {
pub fn new(id: &'static str, contents: T) -> Self {
Self {
contents,
position: None,
size: (0, 0),
scroll: 0,
id,
}
}

Expand Down Expand Up @@ -143,4 +145,8 @@ impl<T: Component> Component for Popup<T> {

self.contents.render(area, surface, cx);
}

fn id(&self) -> Option<&'static str> {
Some(self.id)
}
}