Finalize work

This commit is contained in:
Friz64 2019-11-30 17:30:42 +01:00
parent 5eec3a8867
commit f8fac432c6
3 changed files with 60 additions and 43 deletions

View file

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

View file

@ -151,10 +151,13 @@ 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) =
renderer.scrollbar_bounds(bounds, content_bounds, offset);
let scrollbar_grab = renderer.scrollbar_grab( let scrollbar_grab = renderer.scrollbar_grab(
bounds, bounds,
content_bounds, content_bounds,
offset, background_bounds,
scroller_bounds,
cursor_position, cursor_position,
); );
@ -183,10 +186,8 @@ where
state, state,
}) => match state { }) => match state {
ButtonState::Pressed => { ButtonState::Pressed => {
let (scrollbar_grab, scroller_bounds) = let scroller_grabbed_at = match scrollbar_grab.unwrap()
scrollbar_grab.unwrap(); {
let scroller_grabbed_at = match scrollbar_grab {
ScrollbarGrab::Background => 0.5, ScrollbarGrab::Background => 0.5,
ScrollbarGrab::Scroller => { ScrollbarGrab::Scroller => {
(cursor_position.y - scroller_bounds.y) (cursor_position.y - scroller_bounds.y)
@ -194,18 +195,15 @@ where
} }
}; };
let scroll_percentage = (cursor_position.y let scroll_percentage = (cursor_position.y + bounds.y
- (scroller_bounds.height * scroller_grabbed_at)) - scroller_bounds.height * scroller_grabbed_at)
/ (bounds.height / (bounds.height - scroller_bounds.height);
- (scroller_bounds.height
* scroller_grabbed_at));
dbg!((scroll_percentage, scroller_grabbed_at)); self.state.scroll_to(
/*self.state.scroll_to(
scroll_percentage, scroll_percentage,
bounds, bounds,
content_bounds, content_bounds,
);*/ );
self.state.scroller_grabbed_at = self.state.scroller_grabbed_at =
Some(scroller_grabbed_at); Some(scroller_grabbed_at);
@ -214,24 +212,21 @@ where
self.state.scroller_grabbed_at = None; self.state.scroller_grabbed_at = None;
} }
}, },
/* TODO: Implement dragging to scroll
Event::Mouse(mouse::Event::CursorMoved { .. }) => { Event::Mouse(mouse::Event::CursorMoved { .. }) => {
if let Some(scrollbar_grabbed_at) = if let Some(scroller_grabbed_at) =
self.state.scrollbar_grabbed_at self.state.scroller_grabbed_at
{ {
let ratio = content_bounds.height / bounds.height; let scroll_percentage = (cursor_position.y + bounds.y
let delta = scrollbar_grabbed_at.y - cursor_position.y; - scroller_bounds.height * scroller_grabbed_at)
/ (bounds.height - scroller_bounds.height);
self.state.scroll( self.state.scroll_to(
delta * ratio, scroll_percentage,
bounds, bounds,
content_bounds, content_bounds,
); );
self.state.scrollbar_grabbed_at = Some(cursor_position);
} }
} }
*/
_ => {} _ => {}
} }
} }
@ -273,8 +268,16 @@ 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) =
renderer.scrollbar_bounds(bounds, content_bounds, offset);
let is_mouse_over_scrollbar = renderer let is_mouse_over_scrollbar = renderer
.scrollbar_grab(bounds, content_bounds, offset, cursor_position) .scrollbar_grab(
bounds,
content_bounds,
background_bounds,
scroller_bounds,
cursor_position,
)
.is_some(); .is_some();
let content = { let content = {
@ -399,18 +402,28 @@ pub enum ScrollbarGrab {
/// [`Scrollable`]: struct.Scrollable.html /// [`Scrollable`]: struct.Scrollable.html
/// [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
/// - Background
/// - 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 what part of the scrollbar is being grabbed by the mouse
/// given the bounds of the [`Scrollable`] and its contents together /// given the bounds of the [`Scrollable`] and its contents.
/// with the current scroller bounds.
/// ///
/// [`Scrollable`]: struct.Scrollable.html /// [`Scrollable`]: struct.Scrollable.html
fn scrollbar_grab( fn scrollbar_grab(
&self, &self,
bounds: Rectangle, bounds: Rectangle,
content_bounds: Rectangle, content_bounds: Rectangle,
offset: u32, background_bounds: Rectangle,
scroller_bounds: Rectangle,
cursor_position: Point, cursor_position: Point,
) -> Option<(ScrollbarGrab, Rectangle)>; ) -> Option<ScrollbarGrab>;
/// Draws the [`Scrollable`]. /// Draws the [`Scrollable`].
/// ///

View file

@ -36,31 +36,35 @@ fn scroller_bounds(
} }
impl scrollable::Renderer for Renderer { impl scrollable::Renderer for Renderer {
fn scrollbar_grab( fn scrollbar_bounds(
&self, &self,
bounds: Rectangle, bounds: Rectangle,
content_bounds: Rectangle, content_bounds: Rectangle,
offset: u32, offset: u32,
cursor_position: Point, ) -> (Rectangle, Rectangle) {
) -> Option<(ScrollbarGrab, Rectangle)> {
let background_bounds = background_bounds(bounds); let background_bounds = background_bounds(bounds);
let scroller_bounds =
scroller_bounds(bounds, content_bounds, background_bounds, offset);
(background_bounds, scroller_bounds)
}
fn scrollbar_grab(
&self,
bounds: Rectangle,
content_bounds: Rectangle,
background_bounds: Rectangle,
scroller_bounds: Rectangle,
cursor_position: Point,
) -> Option<ScrollbarGrab> {
if content_bounds.height > bounds.height if content_bounds.height > bounds.height
&& background_bounds.contains(cursor_position) && background_bounds.contains(cursor_position)
{ {
let scroller_bounds = scroller_bounds( Some(if scroller_bounds.contains(cursor_position) {
bounds,
content_bounds,
background_bounds,
offset,
);
let scrollbar_grab = if scroller_bounds.contains(cursor_position) {
ScrollbarGrab::Scroller ScrollbarGrab::Scroller
} else { } else {
ScrollbarGrab::Background ScrollbarGrab::Background
}; })
Some((scrollbar_grab, scroller_bounds))
} else { } else {
None None
} }