Draw only visible options in `overlay::Menu`

This commit is contained in:
Héctor Ramón Jiménez 2020-08-18 04:12:23 +02:00
parent 4bd44e4ed2
commit 9cab88b986
2 changed files with 15 additions and 4 deletions

View File

@ -42,6 +42,7 @@ where
&mut self, &mut self,
bounds: Rectangle, bounds: Rectangle,
cursor_position: Point, cursor_position: Point,
viewport: &Rectangle,
options: &[T], options: &[T],
hovered_option: Option<usize>, hovered_option: Option<usize>,
padding: u16, padding: u16,
@ -52,16 +53,24 @@ where
use std::f32; use std::f32;
let is_mouse_over = bounds.contains(cursor_position); let is_mouse_over = bounds.contains(cursor_position);
let option_height = text_size as usize + padding as usize * 2;
let mut primitives = Vec::new(); let mut primitives = Vec::new();
for (i, option) in options.iter().enumerate() { let offset = viewport.y - bounds.y;
let start = (offset / option_height as f32) as usize;
let end =
((offset + viewport.height) / option_height as f32).ceil() as usize;
let visible_options = &options[start..end.min(options.len())];
for (i, option) in visible_options.iter().enumerate() {
let i = start + i;
let is_selected = hovered_option == Some(i); let is_selected = hovered_option == Some(i);
let bounds = Rectangle { let bounds = Rectangle {
x: bounds.x, x: bounds.x,
y: bounds.y y: bounds.y + (option_height * i) as f32,
+ ((text_size as usize + padding as usize * 2) * i) as f32,
width: bounds.width, width: bounds.width,
height: f32::from(text_size + padding * 2), height: f32::from(text_size + padding * 2),
}; };

View File

@ -372,12 +372,13 @@ where
_defaults: &Renderer::Defaults, _defaults: &Renderer::Defaults,
layout: Layout<'_>, layout: Layout<'_>,
cursor_position: Point, cursor_position: Point,
_viewport: &Rectangle, viewport: &Rectangle,
) -> Renderer::Output { ) -> Renderer::Output {
self::Renderer::draw( self::Renderer::draw(
renderer, renderer,
layout.bounds(), layout.bounds(),
cursor_position, cursor_position,
viewport,
self.options, self.options,
*self.hovered_option, *self.hovered_option,
self.padding, self.padding,
@ -423,6 +424,7 @@ pub trait Renderer:
&mut self, &mut self,
bounds: Rectangle, bounds: Rectangle,
cursor_position: Point, cursor_position: Point,
viewport: &Rectangle,
options: &[T], options: &[T],
hovered_option: Option<usize>, hovered_option: Option<usize>,
padding: u16, padding: u16,