summaryrefslogtreecommitdiffstats
path: root/graphics/src/overlay/menu.rs
diff options
context:
space:
mode:
Diffstat (limited to 'graphics/src/overlay/menu.rs')
-rw-r--r--graphics/src/overlay/menu.rs31
1 files changed, 20 insertions, 11 deletions
diff --git a/graphics/src/overlay/menu.rs b/graphics/src/overlay/menu.rs
index a952f065..9e91a0ef 100644
--- a/graphics/src/overlay/menu.rs
+++ b/graphics/src/overlay/menu.rs
@@ -2,8 +2,8 @@
use crate::backend::{self, Backend};
use crate::{Primitive, Renderer};
use iced_native::{
- mouse, overlay, Color, Font, HorizontalAlignment, Point, Rectangle,
- VerticalAlignment,
+ mouse, overlay, Color, Font, HorizontalAlignment, Padding, Point,
+ Rectangle, VerticalAlignment,
};
pub use iced_style::menu::Style;
@@ -29,7 +29,7 @@ where
background: style.background,
border_color: style.border_color,
border_width: style.border_width,
- border_radius: 0,
+ border_radius: 0.0,
},
primitives,
],
@@ -42,9 +42,10 @@ where
&mut self,
bounds: Rectangle,
cursor_position: Point,
+ viewport: &Rectangle,
options: &[T],
hovered_option: Option<usize>,
- padding: u16,
+ padding: Padding,
text_size: u16,
font: Font,
style: &Style,
@@ -52,18 +53,26 @@ where
use std::f32;
let is_mouse_over = bounds.contains(cursor_position);
+ let option_height = (text_size + padding.vertical()) as usize;
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 bounds = Rectangle {
x: bounds.x,
- y: bounds.y
- + ((text_size as usize + padding as usize * 2) * i) as f32,
+ y: bounds.y + (option_height * i) as f32,
width: bounds.width,
- height: f32::from(text_size + padding * 2),
+ height: f32::from(text_size + padding.vertical()),
};
if is_selected {
@@ -71,15 +80,15 @@ where
bounds,
background: style.selected_background,
border_color: Color::TRANSPARENT,
- border_width: 0,
- border_radius: 0,
+ border_width: 0.0,
+ border_radius: 0.0,
});
}
primitives.push(Primitive::Text {
content: option.to_string(),
bounds: Rectangle {
- x: bounds.x + f32::from(padding),
+ x: bounds.x + padding.left as f32,
y: bounds.center_y(),
width: f32::INFINITY,
..bounds