improve hover popup ui appearance

pull/10122/head
kyfanc 3 months ago
parent 28029dcd07
commit 3313c60c07

@ -97,13 +97,15 @@ impl Hover {
} }
} }
const PADDING: u16 = 2; const PADDING_HORIZONTAL: u16 = 2;
const PADDING_TOP: u16 = 1;
const PADDING_BOTTOM: u16 = 1;
const HEADER_HEIGHT: u16 = 1; const HEADER_HEIGHT: u16 = 1;
const SEPARATOR_HEIGHT: u16 = 1; const SEPARATOR_HEIGHT: u16 = 1;
impl Component for Hover { impl Component for Hover {
fn render(&mut self, area: Rect, surface: &mut Buffer, cx: &mut Context) { fn render(&mut self, area: Rect, surface: &mut Buffer, cx: &mut Context) {
let margin = Margin::horizontal(1); let margin = Margin::all(1);
let area = area.inner(margin); let area = area.inner(margin);
let (Some(header), Some(contents)) = (self.header.as_ref(), self.contents.as_ref()) else { let (Some(header), Some(contents)) = (self.header.as_ref(), self.contents.as_ref()) else {
@ -111,6 +113,8 @@ impl Component for Hover {
return; return;
}; };
// show header and border only when more than one results
if self.hovers.len() > 1 {
// header LSP Name // header LSP Name
let header = header.parse(Some(&cx.editor.theme)); let header = header.parse(Some(&cx.editor.theme));
let header = Paragraph::new(&header); let header = Paragraph::new(&header);
@ -124,11 +128,16 @@ impl Component for Hover {
cell.set_symbol(borders.horizontal).set_style(sep_style); cell.set_symbol(borders.horizontal).set_style(sep_style);
} }
} }
}
// hover content // hover content
let contents = contents.parse(Some(&cx.editor.theme)); let contents = contents.parse(Some(&cx.editor.theme));
let contents_area = area let contents_area = area
.clip_top(2) .clip_top(if self.hovers.len() > 1 {
HEADER_HEIGHT + SEPARATOR_HEIGHT
} else {
0
})
.clip_bottom(u16::from(cx.editor.popup_border())); .clip_bottom(u16::from(cx.editor.popup_border()));
let contents_para = Paragraph::new(&contents) let contents_para = Paragraph::new(&contents)
.wrap(Wrap { trim: false }) .wrap(Wrap { trim: false })
@ -137,7 +146,7 @@ impl Component for Hover {
} }
fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> { fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
let max_text_width = viewport.0.saturating_sub(PADDING).clamp(10, 120); let max_text_width = viewport.0.saturating_sub(PADDING_HORIZONTAL).clamp(10, 120);
let (Some(header), Some(contents)) = (self.header.as_ref(), self.contents.as_ref()) else { let (Some(header), Some(contents)) = (self.header.as_ref(), self.contents.as_ref()) else {
log::info!("markdown not ready"); log::info!("markdown not ready");
@ -152,12 +161,14 @@ impl Component for Hover {
let (content_width, content_height) = let (content_width, content_height) =
crate::ui::text::required_size(&contents, max_text_width); crate::ui::text::required_size(&contents, max_text_width);
let (width, height) = ( let width = PADDING_HORIZONTAL + header_width.max(content_width);
header_width.max(content_width), let height = if self.hovers.len() > 1 {
HEADER_HEIGHT + SEPARATOR_HEIGHT + content_height, PADDING_TOP + HEADER_HEIGHT + SEPARATOR_HEIGHT + content_height + PADDING_BOTTOM
); } else {
PADDING_TOP + content_height + PADDING_BOTTOM
};
Some((width + PADDING, height + PADDING)) Some((width, height))
} }
fn handle_event(&mut self, event: &Event, _ctx: &mut Context) -> EventResult { fn handle_event(&mut self, event: &Event, _ctx: &mut Context) -> EventResult {

Loading…
Cancel
Save