feat: centered selection when scrolling

This commit is contained in:
2024-05-22 00:19:32 +02:00
parent 58955e056f
commit ad03cd4d36
2 changed files with 17 additions and 5 deletions

View File

@@ -1,4 +1,4 @@
pub const MAX_ITEM_DISPLAY_COUNT: u16 = 10; pub const MAX_ITEM_DISPLAY_COUNT: u16 = 9;
pub const PADDING: u16 = 8; pub const PADDING: u16 = 8;
pub const LINE_SPACING: u16 = 2; pub const LINE_SPACING: u16 = 2;
pub const FONT_POINT_SIZE: u16 = 16; pub const FONT_POINT_SIZE: u16 = 16;

View File

@@ -1,4 +1,4 @@
use std::time::Duration; use std::{ops::Add, time::Duration};
use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher}; use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher};
use sdl2::{ use sdl2::{
@@ -183,6 +183,7 @@ impl Runner {
&mut filtered_executables, &mut filtered_executables,
&matcher, &matcher,
); );
selection_index = 0; selection_index = 0;
} }
_ => {} _ => {}
@@ -209,9 +210,18 @@ impl Runner {
let _ = self.canvas.copy(&texture, None, Some(rect)); let _ = self.canvas.copy(&texture, None, Some(rect));
} }
let item_count = MAX_ITEM_DISPLAY_COUNT.min(filtered_executables.len() as u16); // try to keep the selection centered
for i in 0..item_count { let executables_len: u16 = filtered_executables.len() as u16;
let offset = PADDING + (font.height() as u16 + LINE_SPACING) * (i + 1); let start: u16 = selection_index
.saturating_sub(MAX_ITEM_DISPLAY_COUNT.div_euclid(2))
.min(executables_len.saturating_sub(MAX_ITEM_DISPLAY_COUNT));
let end: u16 = (start + MAX_ITEM_DISPLAY_COUNT).min(executables_len);
// ---
let mut display_count: u16 = 0;
for i in start..end {
let offset = PADDING + (font.height() as u16 + LINE_SPACING) * (display_count + 1);
let surface = font let surface = font
.render(&filtered_executables[i as usize]) .render(&filtered_executables[i as usize])
@@ -245,6 +255,8 @@ impl Runner {
.expect("Error creating texture"); .expect("Error creating texture");
let _ = self.canvas.copy(&texture, None, Some(rect)); let _ = self.canvas.copy(&texture, None, Some(rect));
display_count += 1;
} }
self.canvas.present(); self.canvas.present();