| 1 |
use crate::{ |
| 2 |
compositor::{Callback, Component, Compositor, Context, Event, EventResult}, |
| 3 |
ctrl, key, shift, |
| 4 |
}; |
| 5 |
use tui::{buffer::Buffer as Surface, widgets::Table}; |
| 6 |
|
| 7 |
pub use tui::widgets::{Cell, Row}; |
| 8 |
|
| 9 |
use helix_view::{editor::SmartTabConfig, graphics::Rect, Editor}; |
| 10 |
use tui::layout::Constraint; |
| 11 |
|
| 12 |
pub trait Item: Sync + Send + 'static { |
| 13 |
|
| 14 |
type Data: Sync + Send + 'static; |
| 15 |
|
| 16 |
fn format(&self, data: &Self::Data) -> Row<'_>; |
| 17 |
} |
| 18 |
|
| 19 |
pub type MenuCallback<T> = Box<dyn Fn(&mut Editor, Option<&T>, MenuEvent)>; |
| 20 |
|
| 21 |
pub struct Menu<T: Item> { |
| 22 |
options: Vec<T>, |
| 23 |
editor_data: T::Data, |
| 24 |
|
| 25 |
cursor: Option<usize>, |
| 26 |
|
| 27 |
|
| 28 |
matches: Vec<(u32, u32)>, |
| 29 |
|
| 30 |
widths: Vec<Constraint>, |
| 31 |
|
| 32 |
callback_fn: MenuCallback<T>, |
| 33 |
|
| 34 |
scroll: usize, |
| 35 |
size: (u16, u16), |
| 36 |
viewport: (u16, u16), |
| 37 |
recalculate: bool, |
| 38 |
auto_close: bool, |
| 39 |
} |
| 40 |
|
| 41 |
impl<T: Item> Menu<T> { |
| 42 |
const LEFT_PADDING: usize = 1; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
pub fn new( |
| 47 |
options: Vec<T>, |
| 48 |
editor_data: <T as Item>::Data, |
| 49 |
callback_fn: impl Fn(&mut Editor, Option<&T>, MenuEvent) + 'static, |
| 50 |
) -> Self { |
| 51 |
let matches = (0..options.len() as u32).map(|i| (i, 0)).collect(); |
| 52 |
Self { |
| 53 |
options, |
| 54 |
editor_data, |
| 55 |
matches, |
| 56 |
cursor: None, |
| 57 |
widths: Vec::new(), |
| 58 |
callback_fn: Box::new(callback_fn), |
| 59 |
scroll: 0, |
| 60 |
size: (0, 0), |
| 61 |
viewport: (0, 0), |
| 62 |
recalculate: true, |
| 63 |
auto_close: false, |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
pub fn reset_cursor(&mut self) { |
| 68 |
self.cursor = None; |
| 69 |
self.scroll = 0; |
| 70 |
self.recalculate = true; |
| 71 |
} |
| 72 |
|
| 73 |
pub fn update_options(&mut self) -> (&mut Vec<(u32, u32)>, &mut Vec<T>) { |
| 74 |
self.recalculate = true; |
| 75 |
(&mut self.matches, &mut self.options) |
| 76 |
} |
| 77 |
|
| 78 |
pub fn ensure_cursor_in_bounds(&mut self) { |
| 79 |
if self.matches.is_empty() { |
| 80 |
self.cursor = None; |
| 81 |
self.scroll = 0; |
| 82 |
} else { |
| 83 |
self.scroll = 0; |
| 84 |
self.recalculate = true; |
| 85 |
if let Some(cursor) = &mut self.cursor { |
| 86 |
*cursor = (*cursor).min(self.matches.len() - 1) |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
pub fn clear(&mut self) { |
| 92 |
self.matches.clear(); |
| 93 |
|
| 94 |
|
| 95 |
self.cursor = None; |
| 96 |
self.scroll = 0; |
| 97 |
} |
| 98 |
|
| 99 |
pub fn move_up(&mut self) { |
| 100 |
let len = self.matches.len(); |
| 101 |
let max_index = len.saturating_sub(1); |
| 102 |
let pos = self.cursor.map_or(max_index, |i| (i + max_index) % len) % len; |
| 103 |
self.cursor = Some(pos); |
| 104 |
self.adjust_scroll(); |
| 105 |
} |
| 106 |
|
| 107 |
pub fn move_half_page_up(&mut self) { |
| 108 |
let len = self.matches.len(); |
| 109 |
let max_index = len.saturating_sub((self.size.1 as usize / 2).max(1)); |
| 110 |
let pos = self.cursor.map_or(max_index, |i| (i + max_index) % len) % len; |
| 111 |
self.cursor = Some(pos); |
| 112 |
self.adjust_scroll(); |
| 113 |
} |
| 114 |
|
| 115 |
pub fn move_down(&mut self) { |
| 116 |
let len = self.matches.len(); |
| 117 |
let pos = self.cursor.map_or(0, |i| i + 1) % len; |
| 118 |
self.cursor = Some(pos); |
| 119 |
self.adjust_scroll(); |
| 120 |
} |
| 121 |
|
| 122 |
pub fn move_half_page_down(&mut self) { |
| 123 |
let len = self.matches.len(); |
| 124 |
let pos = self |
| 125 |
.cursor |
| 126 |
.map_or(0, |i| i + (self.size.1 as usize / 2).max(1)) |
| 127 |
% len; |
| 128 |
self.cursor = Some(pos); |
| 129 |
self.adjust_scroll(); |
| 130 |
} |
| 131 |
|
| 132 |
pub fn auto_close(mut self, auto_close: bool) -> Self { |
| 133 |
self.auto_close = auto_close; |
| 134 |
self |
| 135 |
} |
| 136 |
|
| 137 |
fn recalculate_size(&mut self, viewport: (u16, u16)) { |
| 138 |
let n = self |
| 139 |
.options |
| 140 |
.first() |
| 141 |
.map(|option| option.format(&self.editor_data).cells.len()) |
| 142 |
.unwrap_or_default(); |
| 143 |
let max_lens = self.options.iter().fold(vec![0; n], |mut acc, option| { |
| 144 |
let row = option.format(&self.editor_data); |
| 145 |
|
| 146 |
for (acc, cell) in acc.iter_mut().zip(row.cells.iter()) { |
| 147 |
let width = cell.content.width(); |
| 148 |
if width > *acc { |
| 149 |
*acc = width; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
acc |
| 154 |
}); |
| 155 |
|
| 156 |
let height = self.matches.len().min(10).min(viewport.1 as usize); |
| 157 |
|
| 158 |
let fits = self.matches.len() <= height; |
| 159 |
|
| 160 |
let mut len = max_lens.iter().sum::<usize>() + n; |
| 161 |
|
| 162 |
if !fits { |
| 163 |
len += 1; |
| 164 |
} |
| 165 |
|
| 166 |
len += Self::LEFT_PADDING; |
| 167 |
let width = len.min(viewport.0 as usize); |
| 168 |
|
| 169 |
self.widths = max_lens |
| 170 |
.into_iter() |
| 171 |
.map(|len| Constraint::Length(len as u16)) |
| 172 |
.collect(); |
| 173 |
|
| 174 |
self.size = (width as u16, height as u16); |
| 175 |
|
| 176 |
|
| 177 |
self.adjust_scroll(); |
| 178 |
self.recalculate = false; |
| 179 |
} |
| 180 |
|
| 181 |
fn adjust_scroll(&mut self) { |
| 182 |
let win_height = self.size.1 as usize; |
| 183 |
if let Some(cursor) = self.cursor { |
| 184 |
let mut scroll = self.scroll; |
| 185 |
if cursor > (win_height + scroll).saturating_sub(1) { |
| 186 |
|
| 187 |
scroll += cursor - (win_height + scroll).saturating_sub(1) |
| 188 |
} else if cursor < scroll { |
| 189 |
|
| 190 |
scroll = cursor |
| 191 |
} |
| 192 |
self.scroll = scroll; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
pub fn selection(&self) -> Option<&T> { |
| 197 |
self.cursor.and_then(|cursor| { |
| 198 |
self.matches |
| 199 |
.get(cursor) |
| 200 |
.map(|(index, _score)| &self.options[*index as usize]) |
| 201 |
}) |
| 202 |
} |
| 203 |
|
| 204 |
pub fn selection_mut(&mut self) -> Option<&mut T> { |
| 205 |
self.cursor.and_then(|cursor| { |
| 206 |
self.matches |
| 207 |
.get(cursor) |
| 208 |
.map(|(index, _score)| &mut self.options[*index as usize]) |
| 209 |
}) |
| 210 |
} |
| 211 |
|
| 212 |
pub fn is_empty(&self) -> bool { |
| 213 |
self.matches.is_empty() |
| 214 |
} |
| 215 |
|
| 216 |
pub fn len(&self) -> usize { |
| 217 |
self.matches.len() |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
impl<T: Item + PartialEq> Menu<T> { |
| 222 |
pub fn replace_option(&mut self, old_option: &impl PartialEq<T>, new_option: T) { |
| 223 |
for option in &mut self.options { |
| 224 |
if old_option == option { |
| 225 |
*option = new_option; |
| 226 |
break; |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
use super::PromptEvent as MenuEvent; |
| 233 |
|
| 234 |
impl<T: Item + 'static> Component for Menu<T> { |
| 235 |
fn handle_event(&mut self, event: &Event, cx: &mut Context) -> EventResult { |
| 236 |
let event = match event { |
| 237 |
Event::Key(event) => *event, |
| 238 |
|
| 239 |
|
| 240 |
Event::Mouse(_) => return EventResult::Consumed(None), |
| 241 |
_ => return EventResult::Ignored(None), |
| 242 |
}; |
| 243 |
|
| 244 |
let close_fn: Option<Callback> = Some(Box::new(|compositor: &mut Compositor, _| { |
| 245 |
|
| 246 |
compositor.pop(); |
| 247 |
})); |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
if (event == key!(Tab) || event == shift!(Tab)) |
| 252 |
&& cx.editor.config().auto_completion |
| 253 |
&& matches!( |
| 254 |
cx.editor.config().smart_tab, |
| 255 |
Some(SmartTabConfig { |
| 256 |
enable: true, |
| 257 |
supersede_menu: true, |
| 258 |
}) |
| 259 |
) |
| 260 |
{ |
| 261 |
return EventResult::Ignored(None); |
| 262 |
} |
| 263 |
|
| 264 |
match event { |
| 265 |
|
| 266 |
key!(Esc) | ctrl!('c') => { |
| 267 |
(self.callback_fn)(cx.editor, self.selection(), MenuEvent::Abort); |
| 268 |
return EventResult::Consumed(close_fn); |
| 269 |
} |
| 270 |
|
| 271 |
shift!(Tab) | key!(Up) | ctrl!('p') => { |
| 272 |
self.move_up(); |
| 273 |
(self.callback_fn)(cx.editor, self.selection(), MenuEvent::Update); |
| 274 |
return EventResult::Consumed(None); |
| 275 |
} |
| 276 |
key!(Tab) | key!(Down) | ctrl!('n') => { |
| 277 |
|
| 278 |
self.move_down(); |
| 279 |
(self.callback_fn)(cx.editor, self.selection(), MenuEvent::Update); |
| 280 |
return EventResult::Consumed(None); |
| 281 |
} |
| 282 |
key!(PageUp) | ctrl!('u') => { |
| 283 |
|
| 284 |
self.move_half_page_up(); |
| 285 |
(self.callback_fn)(cx.editor, self.selection(), MenuEvent::Update); |
| 286 |
return EventResult::Consumed(None); |
| 287 |
} |
| 288 |
key!(PageDown) | ctrl!('d') => { |
| 289 |
|
| 290 |
self.move_half_page_down(); |
| 291 |
(self.callback_fn)(cx.editor, self.selection(), MenuEvent::Update); |
| 292 |
return EventResult::Consumed(None); |
| 293 |
} |
| 294 |
key!(Enter) => { |
| 295 |
if let Some(selection) = self.selection() { |
| 296 |
(self.callback_fn)(cx.editor, Some(selection), MenuEvent::Validate); |
| 297 |
return EventResult::Consumed(close_fn); |
| 298 |
} else { |
| 299 |
return EventResult::Ignored(close_fn); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
_ if self.auto_close => { |
| 316 |
(self.callback_fn)(cx.editor, self.selection(), MenuEvent::Abort); |
| 317 |
return EventResult::Ignored(close_fn); |
| 318 |
} |
| 319 |
_ => (), |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
EventResult::Ignored(None) |
| 325 |
} |
| 326 |
|
| 327 |
fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> { |
| 328 |
if viewport != self.viewport || self.recalculate { |
| 329 |
self.recalculate_size(viewport); |
| 330 |
} |
| 331 |
|
| 332 |
Some(self.size) |
| 333 |
} |
| 334 |
|
| 335 |
fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) { |
| 336 |
let theme = &cx.editor.theme; |
| 337 |
let style = theme |
| 338 |
.try_get("ui.menu") |
| 339 |
.unwrap_or_else(|| theme.get("ui.text")); |
| 340 |
let selected = theme.get("ui.menu.selected"); |
| 341 |
|
| 342 |
surface.clear_with(area, style); |
| 343 |
|
| 344 |
let scroll = self.scroll; |
| 345 |
|
| 346 |
let options: Vec<_> = self |
| 347 |
.matches |
| 348 |
.iter() |
| 349 |
.map(|(index, _score)| { |
| 350 |
|
| 351 |
&self.options[*index as usize] |
| 352 |
}) |
| 353 |
.collect(); |
| 354 |
|
| 355 |
let len = options.len(); |
| 356 |
|
| 357 |
let win_height = area.height as usize; |
| 358 |
|
| 359 |
let rows = options |
| 360 |
.iter() |
| 361 |
.map(|option| option.format(&self.editor_data)); |
| 362 |
let table = Table::new(rows) |
| 363 |
.style(style) |
| 364 |
.highlight_style(selected) |
| 365 |
.column_spacing(1) |
| 366 |
.widths(&self.widths); |
| 367 |
|
| 368 |
use tui::widgets::TableState; |
| 369 |
|
| 370 |
table.render_table( |
| 371 |
area.clip_left(Self::LEFT_PADDING as u16).clip_right(1), |
| 372 |
surface, |
| 373 |
&mut TableState { |
| 374 |
offset: scroll, |
| 375 |
selected: self.cursor, |
| 376 |
}, |
| 377 |
false, |
| 378 |
); |
| 379 |
|
| 380 |
let render_borders = cx.editor.menu_border(); |
| 381 |
|
| 382 |
if !render_borders { |
| 383 |
if let Some(cursor) = self.cursor { |
| 384 |
let offset_from_top = cursor - scroll; |
| 385 |
let left = &mut surface[(area.left(), area.y + offset_from_top as u16)]; |
| 386 |
left.set_style(selected); |
| 387 |
let right = &mut surface[( |
| 388 |
area.right().saturating_sub(1), |
| 389 |
area.y + offset_from_top as u16, |
| 390 |
)]; |
| 391 |
right.set_style(selected); |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
let fits = len <= win_height; |
| 396 |
|
| 397 |
let scroll_style = theme.get("ui.menu.scroll"); |
| 398 |
if !fits { |
| 399 |
let scroll_height = win_height.pow(2).div_ceil(len).min(win_height); |
| 400 |
let scroll_line = (win_height - scroll_height) * scroll |
| 401 |
/ std::cmp::max(1, len.saturating_sub(win_height)); |
| 402 |
|
| 403 |
let mut cell; |
| 404 |
for i in 0..win_height { |
| 405 |
cell = &mut surface[(area.right() - 1, area.top() + i as u16)]; |
| 406 |
|
| 407 |
let half_block = if render_borders { "▌" } else { "▐" }; |
| 408 |
|
| 409 |
if scroll_line <= i && i < scroll_line + scroll_height { |
| 410 |
|
| 411 |
cell.set_symbol(half_block); |
| 412 |
cell.set_fg(scroll_style.fg.unwrap_or(helix_view::theme::Color::Reset)); |
| 413 |
} else if !render_borders { |
| 414 |
|
| 415 |
cell.set_symbol(half_block); |
| 416 |
cell.set_fg(scroll_style.bg.unwrap_or(helix_view::theme::Color::Reset)); |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
} |