diff options
author | 2019-12-02 18:51:34 +0100 | |
---|---|---|
committer | 2019-12-02 19:12:05 +0100 | |
commit | 6943041e0fad65927a741e316509bf07bdaa2f61 (patch) | |
tree | df73b7f8e5d2a51a437d70984192393909e39dfd /wgpu | |
parent | 9a733bb3c89e15ada05fc90efb71eeae12b8c9c1 (diff) | |
download | iced-6943041e0fad65927a741e316509bf07bdaa2f61.tar.gz iced-6943041e0fad65927a741e316509bf07bdaa2f61.tar.bz2 iced-6943041e0fad65927a741e316509bf07bdaa2f61.zip |
Address suggestions
Diffstat (limited to 'wgpu')
-rw-r--r-- | wgpu/src/renderer/widget/scrollable.rs | 102 |
1 files changed, 29 insertions, 73 deletions
diff --git a/wgpu/src/renderer/widget/scrollable.rs b/wgpu/src/renderer/widget/scrollable.rs index 175fac11..b83cee1b 100644 --- a/wgpu/src/renderer/widget/scrollable.rs +++ b/wgpu/src/renderer/widget/scrollable.rs @@ -1,72 +1,34 @@ use crate::{Primitive, Renderer}; -use iced_native::{ - scrollable, Background, MouseCursor, Point, Rectangle, ScrollbarGrab, - Vector, -}; +use iced_native::{scrollable, Background, MouseCursor, Rectangle, Vector}; const SCROLLBAR_WIDTH: u16 = 10; const SCROLLBAR_MARGIN: u16 = 2; -fn background_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, - } -} - -fn scroller_bounds( - bounds: Rectangle, - content_bounds: Rectangle, - background_bounds: Rectangle, - offset: u32, -) -> Rectangle { - let ratio = bounds.height / content_bounds.height; - let scrollbar_height = bounds.height * ratio; - let y_offset = offset as f32 * ratio; - - Rectangle { - x: background_bounds.x + f32::from(SCROLLBAR_MARGIN), - y: background_bounds.y + y_offset, - width: background_bounds.width - f32::from(2 * SCROLLBAR_MARGIN), - height: scrollbar_height, - } -} - impl scrollable::Renderer for Renderer { - fn scrollbar_bounds( - &self, - bounds: Rectangle, - content_bounds: Rectangle, - offset: u32, - ) -> (Rectangle, Rectangle) { - let background_bounds = background_bounds(bounds); - let scroller_bounds = - scroller_bounds(bounds, content_bounds, background_bounds, offset); - - (background_bounds, scroller_bounds) + 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, + } } - fn scrollbar_grab( - &self, + fn scroller_bounds( bounds: Rectangle, content_bounds: Rectangle, - background_bounds: Rectangle, - scroller_bounds: Rectangle, - cursor_position: Point, - ) -> Option<ScrollbarGrab> { - if content_bounds.height > bounds.height - && background_bounds.contains(cursor_position) - { - Some(if scroller_bounds.contains(cursor_position) { - ScrollbarGrab::Scroller - } else { - ScrollbarGrab::Background - }) - } else { - None + scrollbar_bounds: Rectangle, + offset: u32, + ) -> Rectangle { + let ratio = bounds.height / content_bounds.height; + let scrollbar_height = bounds.height * ratio; + let y_offset = offset as f32 * ratio; + 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, } } @@ -77,11 +39,12 @@ impl scrollable::Renderer for Renderer { content_bounds: Rectangle, is_mouse_over: bool, is_mouse_over_scrollbar: bool, + scrollbar_bounds: Rectangle, + scroller_bounds: Rectangle, offset: u32, (content, mouse_cursor): Self::Output, ) -> Self::Output { let is_content_overflowing = content_bounds.height > bounds.height; - let background_bounds = background_bounds(bounds); let clip = Primitive::Clip { bounds, @@ -91,28 +54,21 @@ impl scrollable::Renderer for Renderer { ( if is_content_overflowing - && (is_mouse_over || state.currently_grabbed()) + && (is_mouse_over || state.is_scroller_grabbed()) { - let scroller_bounds = scroller_bounds( - bounds, - content_bounds, - background_bounds, - offset, - ); let scrollbar = Primitive::Quad { bounds: scroller_bounds, background: Background::Color([0.0, 0.0, 0.0, 0.7].into()), border_radius: 5, }; - if is_mouse_over_scrollbar || state.currently_grabbed() { + if is_mouse_over_scrollbar || state.is_scroller_grabbed() { let scrollbar_background = Primitive::Quad { bounds: Rectangle { - x: background_bounds.x - + f32::from(SCROLLBAR_MARGIN), - width: background_bounds.width + x: scrollbar_bounds.x + f32::from(SCROLLBAR_MARGIN), + width: scrollbar_bounds.width - f32::from(2 * SCROLLBAR_MARGIN), - ..background_bounds + ..scrollbar_bounds }, background: Background::Color( [0.0, 0.0, 0.0, 0.3].into(), @@ -131,7 +87,7 @@ impl scrollable::Renderer for Renderer { } else { clip }, - if is_mouse_over_scrollbar || state.currently_grabbed() { + if is_mouse_over_scrollbar || state.is_scroller_grabbed() { MouseCursor::Idle } else { mouse_cursor |