Address suggestions

This commit is contained in:
Friz64 2019-12-02 18:51:34 +01:00
parent 9a733bb3c8
commit 6943041e0f
5 changed files with 105 additions and 132 deletions

View file

@ -145,7 +145,7 @@ impl Steps {
Step::Debugger, Step::Debugger,
Step::End, Step::End,
], ],
current: 0, current: 6,
} }
} }

View file

@ -1,7 +1,7 @@
use crate::{ use crate::{
button, checkbox, column, radio, row, scrollable, text, text_input, button, checkbox, column, radio, row, scrollable, text, text_input,
Background, Color, Element, Font, HorizontalAlignment, Layout, Point, Background, Color, Element, Font, HorizontalAlignment, Layout, Point,
Rectangle, Renderer, ScrollbarGrab, Size, VerticalAlignment, Rectangle, Renderer, Size, VerticalAlignment,
}; };
/// A renderer that does nothing. /// A renderer that does nothing.
@ -61,24 +61,17 @@ impl text::Renderer for Null {
} }
impl scrollable::Renderer for Null { impl scrollable::Renderer for Null {
fn scrollbar_bounds( fn scrollbar_bounds(_bounds: Rectangle) -> Rectangle {
&self,
_bounds: Rectangle,
_content_bounds: Rectangle,
_offset: u32,
) -> (Rectangle, Rectangle) {
Default::default() Default::default()
} }
fn scrollbar_grab( fn scroller_bounds(
&self,
_bounds: Rectangle, _bounds: Rectangle,
_content_bounds: Rectangle, _content_bounds: Rectangle,
_background_bounds: Rectangle, _scrollbar_bounds: Rectangle,
_scroller_bounds: Rectangle, _offset: u32,
_cursor_position: Point, ) -> Rectangle {
) -> Option<ScrollbarGrab> { Default::default()
None
} }
fn draw( fn draw(
@ -88,6 +81,8 @@ impl scrollable::Renderer for Null {
_content_bounds: Rectangle, _content_bounds: Rectangle,
_is_mouse_over: bool, _is_mouse_over: bool,
_is_mouse_over_scrollbar: bool, _is_mouse_over_scrollbar: bool,
_scrollbar_bounds: Rectangle,
_scroller_bounds: Rectangle,
_offset: u32, _offset: u32,
_content: Self::Output, _content: Self::Output,
) { ) {

View file

@ -47,7 +47,7 @@ pub use radio::Radio;
#[doc(no_inline)] #[doc(no_inline)]
pub use row::Row; pub use row::Row;
#[doc(no_inline)] #[doc(no_inline)]
pub use scrollable::{Scrollable, ScrollbarGrab}; pub use scrollable::Scrollable;
#[doc(no_inline)] #[doc(no_inline)]
pub use slider::Slider; pub use slider::Slider;
#[doc(no_inline)] #[doc(no_inline)]

View file

@ -162,12 +162,17 @@ where
let content_bounds = content.bounds(); let content_bounds = content.bounds();
let offset = self.state.offset(bounds, content_bounds); let offset = self.state.offset(bounds, content_bounds);
let (background_bounds, scroller_bounds) = let scrollbar_bounds = Renderer::scrollbar_bounds(bounds);
renderer.scrollbar_bounds(bounds, content_bounds, offset); let scroller_bounds = Renderer::scroller_bounds(
let scrollbar_grab = renderer.scrollbar_grab(
bounds, bounds,
content_bounds, content_bounds,
background_bounds, scrollbar_bounds,
offset,
);
let scrollbar_grab = ScrollbarItem::from_cursor_position(
bounds,
content_bounds,
scrollbar_bounds,
scroller_bounds, scroller_bounds,
cursor_position, cursor_position,
); );
@ -190,7 +195,7 @@ where
} }
} }
if self.state.currently_grabbed() || scrollbar_grab.is_some() { if self.state.is_scroller_grabbed() || scrollbar_grab.is_some() {
match event { match event {
Event::Mouse(mouse::Event::Input { Event::Mouse(mouse::Event::Input {
button: mouse::Button::Left, button: mouse::Button::Left,
@ -199,8 +204,8 @@ where
ButtonState::Pressed => { ButtonState::Pressed => {
let scroller_grabbed_at = match scrollbar_grab.unwrap() let scroller_grabbed_at = match scrollbar_grab.unwrap()
{ {
ScrollbarGrab::Background => 0.5, ScrollbarItem::Background => 0.5,
ScrollbarGrab::Scroller => { ScrollbarItem::Scroller => {
(cursor_position.y - scroller_bounds.y) (cursor_position.y - scroller_bounds.y)
/ scroller_bounds.height / scroller_bounds.height
} }
@ -245,7 +250,7 @@ where
} }
let cursor_position = if is_mouse_over let cursor_position = if is_mouse_over
&& !(scrollbar_grab.is_some() || self.state.currently_grabbed()) && !(scrollbar_grab.is_some() || self.state.is_scroller_grabbed())
{ {
Point::new( Point::new(
cursor_position.x, cursor_position.x,
@ -281,17 +286,21 @@ where
let offset = self.state.offset(bounds, content_bounds); let offset = self.state.offset(bounds, content_bounds);
let is_mouse_over = bounds.contains(cursor_position); let is_mouse_over = bounds.contains(cursor_position);
let (background_bounds, scroller_bounds) = let scrollbar_bounds = Renderer::scrollbar_bounds(bounds);
renderer.scrollbar_bounds(bounds, content_bounds, offset); let scroller_bounds = Renderer::scroller_bounds(
let is_mouse_over_scrollbar = renderer bounds,
.scrollbar_grab( content_bounds,
bounds, scrollbar_bounds,
content_bounds, offset,
background_bounds, );
scroller_bounds, let is_mouse_over_scrollbar = ScrollbarItem::from_cursor_position(
cursor_position, bounds,
) content_bounds,
.is_some(); scrollbar_bounds,
scroller_bounds,
cursor_position,
)
.is_some();
let content = { let content = {
let cursor_position = if is_mouse_over && !is_mouse_over_scrollbar { let cursor_position = if is_mouse_over && !is_mouse_over_scrollbar {
@ -310,6 +319,8 @@ where
content_layout.bounds(), content_layout.bounds(),
is_mouse_over, is_mouse_over,
is_mouse_over_scrollbar, is_mouse_over_scrollbar,
scrollbar_bounds,
scroller_bounds,
offset, offset,
content, content,
) )
@ -392,21 +403,41 @@ impl State {
self.offset.min(hidden_content as f32) as u32 self.offset.min(hidden_content as f32) as u32
} }
/// Returns whether the scrollbar is currently grabbed or not. /// Returns whether the scroller is currently grabbed or not.
pub fn currently_grabbed(&self) -> bool { pub fn is_scroller_grabbed(&self) -> bool {
self.scroller_grabbed_at.is_some() self.scroller_grabbed_at.is_some()
} }
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
/// What the mouse is grabbing on the scrollbar enum ScrollbarItem {
pub enum ScrollbarGrab {
/// The mouse is grabbing the background
Background, Background,
/// The mouse is grabbing the scroller
Scroller, Scroller,
} }
impl ScrollbarItem {
/// `None` means the cursor is not over any item
fn from_cursor_position(
bounds: Rectangle,
content_bounds: Rectangle,
scrollbar_bounds: Rectangle,
scroller_bounds: Rectangle,
cursor_position: Point,
) -> Option<ScrollbarItem> {
if content_bounds.height > bounds.height
&& scrollbar_bounds.contains(cursor_position)
{
Some(if scroller_bounds.contains(cursor_position) {
ScrollbarItem::Scroller
} else {
ScrollbarItem::Background
})
} else {
None
}
}
}
/// The renderer of a [`Scrollable`]. /// The renderer of a [`Scrollable`].
/// ///
/// Your [renderer] will need to implement this trait before being /// Your [renderer] will need to implement this trait before being
@ -416,27 +447,16 @@ pub enum ScrollbarGrab {
/// [renderer]: ../../renderer/index.html /// [renderer]: ../../renderer/index.html
pub trait Renderer: crate::Renderer + Sized { pub trait Renderer: crate::Renderer + Sized {
/// Returns the bounds of the scrollbar /// Returns the bounds of the scrollbar
/// - Background fn scrollbar_bounds(bounds: Rectangle) -> Rectangle;
/// - Movable Scroller
fn scrollbar_bounds(
&self,
bounds: Rectangle,
content_bounds: Rectangle,
offset: u32,
) -> (Rectangle, Rectangle);
/// Returns what part of the scrollbar is being grabbed by the mouse /// Returns the bounds of the scroller
/// given the bounds of the [`Scrollable`] and its contents. /// "The part that you can drag around with your mouse to scroll"
/// fn scroller_bounds(
/// [`Scrollable`]: struct.Scrollable.html
fn scrollbar_grab(
&self,
bounds: Rectangle, bounds: Rectangle,
content_bounds: Rectangle, content_bounds: Rectangle,
background_bounds: Rectangle, scrollbar_bounds: Rectangle,
scroller_bounds: Rectangle, offset: u32,
cursor_position: Point, ) -> Rectangle;
) -> Option<ScrollbarGrab>;
/// Draws the [`Scrollable`]. /// Draws the [`Scrollable`].
/// ///
@ -457,6 +477,8 @@ pub trait Renderer: crate::Renderer + Sized {
content_bounds: Rectangle, content_bounds: Rectangle,
is_mouse_over: bool, is_mouse_over: bool,
is_mouse_over_scrollbar: bool, is_mouse_over_scrollbar: bool,
scrollbar_bounds: Rectangle,
scroller_bounds: Rectangle,
offset: u32, offset: u32,
content: Self::Output, content: Self::Output,
) -> Self::Output; ) -> Self::Output;

View file

@ -1,72 +1,34 @@
use crate::{Primitive, Renderer}; use crate::{Primitive, Renderer};
use iced_native::{ use iced_native::{scrollable, Background, MouseCursor, Rectangle, Vector};
scrollable, Background, MouseCursor, Point, Rectangle, ScrollbarGrab,
Vector,
};
const SCROLLBAR_WIDTH: u16 = 10; const SCROLLBAR_WIDTH: u16 = 10;
const SCROLLBAR_MARGIN: u16 = 2; 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 { impl scrollable::Renderer for Renderer {
fn scrollbar_bounds( fn scrollbar_bounds(bounds: Rectangle) -> Rectangle {
&self, Rectangle {
bounds: Rectangle, x: bounds.x + bounds.width
content_bounds: Rectangle, - f32::from(SCROLLBAR_WIDTH + 2 * SCROLLBAR_MARGIN),
offset: u32, y: bounds.y,
) -> (Rectangle, Rectangle) { width: f32::from(SCROLLBAR_WIDTH + 2 * SCROLLBAR_MARGIN),
let background_bounds = background_bounds(bounds); height: bounds.height,
let scroller_bounds = }
scroller_bounds(bounds, content_bounds, background_bounds, offset);
(background_bounds, scroller_bounds)
} }
fn scrollbar_grab( fn scroller_bounds(
&self,
bounds: Rectangle, bounds: Rectangle,
content_bounds: Rectangle, content_bounds: Rectangle,
background_bounds: Rectangle, scrollbar_bounds: Rectangle,
scroller_bounds: Rectangle, offset: u32,
cursor_position: Point, ) -> Rectangle {
) -> Option<ScrollbarGrab> { let ratio = bounds.height / content_bounds.height;
if content_bounds.height > bounds.height let scrollbar_height = bounds.height * ratio;
&& background_bounds.contains(cursor_position) let y_offset = offset as f32 * ratio;
{ Rectangle {
Some(if scroller_bounds.contains(cursor_position) { x: scrollbar_bounds.x + f32::from(SCROLLBAR_MARGIN),
ScrollbarGrab::Scroller y: scrollbar_bounds.y + y_offset,
} else { width: scrollbar_bounds.width - f32::from(2 * SCROLLBAR_MARGIN),
ScrollbarGrab::Background height: scrollbar_height,
})
} else {
None
} }
} }
@ -77,11 +39,12 @@ impl scrollable::Renderer for Renderer {
content_bounds: Rectangle, content_bounds: Rectangle,
is_mouse_over: bool, is_mouse_over: bool,
is_mouse_over_scrollbar: bool, is_mouse_over_scrollbar: bool,
scrollbar_bounds: Rectangle,
scroller_bounds: Rectangle,
offset: u32, offset: u32,
(content, mouse_cursor): Self::Output, (content, mouse_cursor): Self::Output,
) -> Self::Output { ) -> Self::Output {
let is_content_overflowing = content_bounds.height > bounds.height; let is_content_overflowing = content_bounds.height > bounds.height;
let background_bounds = background_bounds(bounds);
let clip = Primitive::Clip { let clip = Primitive::Clip {
bounds, bounds,
@ -91,28 +54,21 @@ impl scrollable::Renderer for Renderer {
( (
if is_content_overflowing 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 { let scrollbar = Primitive::Quad {
bounds: scroller_bounds, bounds: scroller_bounds,
background: Background::Color([0.0, 0.0, 0.0, 0.7].into()), background: Background::Color([0.0, 0.0, 0.0, 0.7].into()),
border_radius: 5, 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 { let scrollbar_background = Primitive::Quad {
bounds: Rectangle { bounds: Rectangle {
x: background_bounds.x x: scrollbar_bounds.x + f32::from(SCROLLBAR_MARGIN),
+ f32::from(SCROLLBAR_MARGIN), width: scrollbar_bounds.width
width: background_bounds.width
- f32::from(2 * SCROLLBAR_MARGIN), - f32::from(2 * SCROLLBAR_MARGIN),
..background_bounds ..scrollbar_bounds
}, },
background: Background::Color( background: Background::Color(
[0.0, 0.0, 0.0, 0.3].into(), [0.0, 0.0, 0.0, 0.3].into(),
@ -131,7 +87,7 @@ impl scrollable::Renderer for Renderer {
} else { } else {
clip clip
}, },
if is_mouse_over_scrollbar || state.currently_grabbed() { if is_mouse_over_scrollbar || state.is_scroller_grabbed() {
MouseCursor::Idle MouseCursor::Idle
} else { } else {
mouse_cursor mouse_cursor