summaryrefslogtreecommitdiffstats
path: root/wgpu
diff options
context:
space:
mode:
authorLibravatar Friz64 <friz64@arch.localdomain>2019-11-25 20:27:15 +0100
committerLibravatar Friz64 <friz64mc@gmail.com>2019-12-02 19:09:17 +0100
commit5eec3a8867f4fbd54e2e28e5d984c2ca7ec0fea4 (patch)
tree2af1212ca051827f434845c6dc14a3b4b91f3bce /wgpu
parente404f5098a4e053f52e75175b6cd677e60d27517 (diff)
downloadiced-5eec3a8867f4fbd54e2e28e5d984c2ca7ec0fea4.tar.gz
iced-5eec3a8867f4fbd54e2e28e5d984c2ca7ec0fea4.tar.bz2
iced-5eec3a8867f4fbd54e2e28e5d984c2ca7ec0fea4.zip
First implementation
Diffstat (limited to 'wgpu')
-rw-r--r--wgpu/src/renderer/widget/scrollable.rs84
1 files changed, 60 insertions, 24 deletions
diff --git a/wgpu/src/renderer/widget/scrollable.rs b/wgpu/src/renderer/widget/scrollable.rs
index 58dc3df9..d069b799 100644
--- a/wgpu/src/renderer/widget/scrollable.rs
+++ b/wgpu/src/renderer/widget/scrollable.rs
@@ -1,12 +1,13 @@
use crate::{Primitive, Renderer};
use iced_native::{
- scrollable, Background, MouseCursor, Point, Rectangle, Vector,
+ scrollable, Background, MouseCursor, Point, Rectangle, ScrollbarGrab,
+ Vector,
};
const SCROLLBAR_WIDTH: u16 = 10;
const SCROLLBAR_MARGIN: u16 = 2;
-fn scrollbar_bounds(bounds: Rectangle) -> Rectangle {
+fn background_bounds(bounds: Rectangle) -> Rectangle {
Rectangle {
x: bounds.x + bounds.width
- f32::from(SCROLLBAR_WIDTH + 2 * SCROLLBAR_MARGIN),
@@ -16,15 +17,53 @@ fn scrollbar_bounds(bounds: Rectangle) -> Rectangle {
}
}
+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 is_mouse_over_scrollbar(
+ fn scrollbar_grab(
&self,
bounds: Rectangle,
content_bounds: Rectangle,
+ offset: u32,
cursor_position: Point,
- ) -> bool {
- content_bounds.height > bounds.height
- && scrollbar_bounds(bounds).contains(cursor_position)
+ ) -> Option<(ScrollbarGrab, Rectangle)> {
+ let background_bounds = background_bounds(bounds);
+ if content_bounds.height > bounds.height
+ && background_bounds.contains(cursor_position)
+ {
+ let scroller_bounds = scroller_bounds(
+ bounds,
+ content_bounds,
+ background_bounds,
+ offset,
+ );
+
+ let scrollbar_grab = if scroller_bounds.contains(cursor_position) {
+ ScrollbarGrab::Scroller
+ } else {
+ ScrollbarGrab::Background
+ };
+
+ Some((scrollbar_grab, scroller_bounds))
+ } else {
+ None
+ }
}
fn draw(
@@ -38,7 +77,7 @@ impl scrollable::Renderer for Renderer {
(content, mouse_cursor): Self::Output,
) -> Self::Output {
let is_content_overflowing = content_bounds.height > bounds.height;
- let scrollbar_bounds = scrollbar_bounds(bounds);
+ let background_bounds = background_bounds(bounds);
let clip = Primitive::Clip {
bounds,
@@ -48,31 +87,28 @@ impl scrollable::Renderer for Renderer {
(
if is_content_overflowing
- && (is_mouse_over || state.is_scrollbar_grabbed())
+ && (is_mouse_over || state.currently_grabbed())
{
- let ratio = bounds.height / content_bounds.height;
- let scrollbar_height = bounds.height * ratio;
- let y_offset = offset as f32 * ratio;
-
+ let scroller_bounds = scroller_bounds(
+ bounds,
+ content_bounds,
+ background_bounds,
+ offset,
+ );
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,
- },
+ bounds: scroller_bounds,
background: Background::Color([0.0, 0.0, 0.0, 0.7].into()),
border_radius: 5,
};
- if is_mouse_over_scrollbar || state.is_scrollbar_grabbed() {
+ if is_mouse_over_scrollbar || state.currently_grabbed() {
let scrollbar_background = Primitive::Quad {
bounds: Rectangle {
- x: scrollbar_bounds.x + f32::from(SCROLLBAR_MARGIN),
- width: scrollbar_bounds.width
+ x: background_bounds.x
+ + f32::from(SCROLLBAR_MARGIN),
+ width: background_bounds.width
- f32::from(2 * SCROLLBAR_MARGIN),
- ..scrollbar_bounds
+ ..background_bounds
},
background: Background::Color(
[0.0, 0.0, 0.0, 0.3].into(),
@@ -91,7 +127,7 @@ impl scrollable::Renderer for Renderer {
} else {
clip
},
- if is_mouse_over_scrollbar || state.is_scrollbar_grabbed() {
+ if is_mouse_over_scrollbar || state.currently_grabbed() {
MouseCursor::Idle
} else {
mouse_cursor