diff options
Diffstat (limited to 'wgpu/src/renderer')
-rw-r--r-- | wgpu/src/renderer/scrollable.rs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/wgpu/src/renderer/scrollable.rs b/wgpu/src/renderer/scrollable.rs new file mode 100644 index 00000000..14ff9ff4 --- /dev/null +++ b/wgpu/src/renderer/scrollable.rs @@ -0,0 +1,70 @@ +use crate::{Primitive, Renderer}; +use iced_native::{ + scrollable, Background, Color, Layout, Point, Rectangle, Scrollable, Widget, +}; + +impl scrollable::Renderer for Renderer { + fn draw<Message>( + &mut self, + scrollable: &Scrollable<'_, Message, Self>, + layout: Layout<'_>, + cursor_position: Point, + ) -> Self::Output { + let bounds = layout.bounds(); + let is_mouse_over = bounds.contains(cursor_position); + + let content = layout.children().next().unwrap(); + let content_bounds = content.bounds(); + + let cursor_position = if bounds.contains(cursor_position) { + Point::new( + cursor_position.x, + cursor_position.y + scrollable.state.offset as f32, + ) + } else { + Point::new(cursor_position.x, -1.0) + }; + + let (content, mouse_cursor) = + scrollable.content.draw(self, content, cursor_position); + + let primitive = Primitive::Scrollable { + bounds, + offset: scrollable.state.offset, + content: Box::new(content), + }; + + ( + Primitive::Group { + primitives: if is_mouse_over + && content_bounds.height > bounds.height + { + let ratio = bounds.height / content_bounds.height; + let scrollbar_height = bounds.height * ratio; + let y_offset = scrollable.state.offset as f32 * ratio; + + let scrollbar = Primitive::Quad { + bounds: Rectangle { + x: bounds.x + bounds.width - 12.0, + y: bounds.y + y_offset, + width: 10.0, + height: scrollbar_height, + }, + background: Background::Color(Color { + r: 0.0, + g: 0.0, + b: 0.0, + a: 0.5, + }), + border_radius: 5, + }; + + vec![primitive, scrollbar] + } else { + vec![primitive] + }, + }, + mouse_cursor, + ) + } +} |