diff options
author | 2019-12-04 01:14:13 +0100 | |
---|---|---|
committer | 2019-12-04 01:14:13 +0100 | |
commit | 6c145bbb239e87569bf4aa797ea7f8d34e25cf62 (patch) | |
tree | 87861279f19235f2a2c8306513c6ac618b0231ef /wgpu | |
parent | 0b5409c53d2563825d5f36bcae9700ec8721c34e (diff) | |
parent | 2cd517c09998f2dd4c8b15ba3b014d9904f957fd (diff) | |
download | iced-6c145bbb239e87569bf4aa797ea7f8d34e25cf62.tar.gz iced-6c145bbb239e87569bf4aa797ea7f8d34e25cf62.tar.bz2 iced-6c145bbb239e87569bf4aa797ea7f8d34e25cf62.zip |
Merge pull request #95 from Friz64/scrolling-behaviour
Make scrolling behave like you'd expect it to
Diffstat (limited to 'wgpu')
-rw-r--r-- | wgpu/src/renderer/widget/scrollable.rs | 120 |
1 files changed, 64 insertions, 56 deletions
diff --git a/wgpu/src/renderer/widget/scrollable.rs b/wgpu/src/renderer/widget/scrollable.rs index 58dc3df9..6ef57185 100644 --- a/wgpu/src/renderer/widget/scrollable.rs +++ b/wgpu/src/renderer/widget/scrollable.rs @@ -1,45 +1,58 @@ use crate::{Primitive, Renderer}; -use iced_native::{ - scrollable, Background, MouseCursor, Point, Rectangle, Vector, -}; +use iced_native::{scrollable, Background, MouseCursor, Rectangle, Vector}; const SCROLLBAR_WIDTH: u16 = 10; const SCROLLBAR_MARGIN: u16 = 2; -fn scrollbar_bounds(bounds: Rectangle) -> Rectangle { - Rectangle { - x: bounds.x + bounds.width - - f32::from(SCROLLBAR_WIDTH + 2 * SCROLLBAR_MARGIN), - y: bounds.y, - width: f32::from(SCROLLBAR_WIDTH + 2 * SCROLLBAR_MARGIN), - height: bounds.height, - } -} - impl scrollable::Renderer for Renderer { - fn is_mouse_over_scrollbar( + fn scrollbar( &self, bounds: Rectangle, content_bounds: Rectangle, - cursor_position: Point, - ) -> bool { - content_bounds.height > bounds.height - && scrollbar_bounds(bounds).contains(cursor_position) + offset: u32, + ) -> Option<scrollable::Scrollbar> { + if content_bounds.height > bounds.height { + let scrollbar_bounds = Rectangle { + x: bounds.x + bounds.width + - f32::from(SCROLLBAR_WIDTH + 2 * SCROLLBAR_MARGIN), + y: bounds.y, + width: f32::from(SCROLLBAR_WIDTH + 2 * SCROLLBAR_MARGIN), + height: bounds.height, + }; + + let ratio = bounds.height / content_bounds.height; + let scrollbar_height = bounds.height * ratio; + let y_offset = offset as f32 * ratio; + + let scroller_bounds = Rectangle { + x: scrollbar_bounds.x + f32::from(SCROLLBAR_MARGIN), + y: scrollbar_bounds.y + y_offset, + width: scrollbar_bounds.width - f32::from(2 * SCROLLBAR_MARGIN), + height: scrollbar_height, + }; + + Some(scrollable::Scrollbar { + bounds: scrollbar_bounds, + scroller: scrollable::Scroller { + bounds: scroller_bounds, + }, + }) + } else { + None + } } fn draw( &mut self, state: &scrollable::State, bounds: Rectangle, - content_bounds: Rectangle, + _content_bounds: Rectangle, is_mouse_over: bool, is_mouse_over_scrollbar: bool, + scrollbar: Option<scrollable::Scrollbar>, offset: u32, (content, mouse_cursor): Self::Output, ) -> Self::Output { - let is_content_overflowing = content_bounds.height > bounds.height; - let scrollbar_bounds = scrollbar_bounds(bounds); - let clip = Primitive::Clip { bounds, offset: Vector::new(0, offset), @@ -47,51 +60,46 @@ impl scrollable::Renderer for Renderer { }; ( - if is_content_overflowing - && (is_mouse_over || state.is_scrollbar_grabbed()) - { - let ratio = bounds.height / content_bounds.height; - let scrollbar_height = bounds.height * ratio; - let y_offset = offset as f32 * ratio; - - let scrollbar = Primitive::Quad { - bounds: Rectangle { - x: scrollbar_bounds.x + f32::from(SCROLLBAR_MARGIN), - y: scrollbar_bounds.y + y_offset, - width: scrollbar_bounds.width - - f32::from(2 * SCROLLBAR_MARGIN), - height: scrollbar_height, - }, - background: Background::Color([0.0, 0.0, 0.0, 0.7].into()), - border_radius: 5, - }; - - if is_mouse_over_scrollbar || state.is_scrollbar_grabbed() { - let scrollbar_background = Primitive::Quad { - bounds: Rectangle { - x: scrollbar_bounds.x + f32::from(SCROLLBAR_MARGIN), - width: scrollbar_bounds.width - - f32::from(2 * SCROLLBAR_MARGIN), - ..scrollbar_bounds - }, + if let Some(scrollbar) = scrollbar { + if is_mouse_over || state.is_scroller_grabbed() { + let scroller = Primitive::Quad { + bounds: scrollbar.scroller.bounds, background: Background::Color( - [0.0, 0.0, 0.0, 0.3].into(), + [0.0, 0.0, 0.0, 0.7].into(), ), border_radius: 5, }; - Primitive::Group { - primitives: vec![clip, scrollbar_background, scrollbar], + if is_mouse_over_scrollbar || state.is_scroller_grabbed() { + let scrollbar = Primitive::Quad { + bounds: Rectangle { + x: scrollbar.bounds.x + + f32::from(SCROLLBAR_MARGIN), + width: scrollbar.bounds.width + - f32::from(2 * SCROLLBAR_MARGIN), + ..scrollbar.bounds + }, + background: Background::Color( + [0.0, 0.0, 0.0, 0.3].into(), + ), + border_radius: 5, + }; + + Primitive::Group { + primitives: vec![clip, scrollbar, scroller], + } + } else { + Primitive::Group { + primitives: vec![clip, scroller], + } } } else { - Primitive::Group { - primitives: vec![clip, scrollbar], - } + clip } } else { clip }, - if is_mouse_over_scrollbar || state.is_scrollbar_grabbed() { + if is_mouse_over_scrollbar || state.is_scroller_grabbed() { MouseCursor::Idle } else { mouse_cursor |