Skip to content

Commit

Permalink
Better mouse scroll drag
Browse files Browse the repository at this point in the history
Now saturates when to the top or left.
  • Loading branch information
gyscos committed Oct 15, 2017
1 parent f906218 commit 420454c
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 26 deletions.
6 changes: 3 additions & 3 deletions examples/list_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ fn main() {
)
.with(|list| for i in 0..50 {
list.add_child(
&format!("Item {}", i),
EditView::new(),
);
&format!("Item {}", i),
EditView::new(),
);
}),
),
);
Expand Down
15 changes: 6 additions & 9 deletions src/views/list_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,8 @@ impl View for ListView {
offset,
} if self.scrollbase.is_dragging() =>
{
position
.checked_sub(offset)
.map(|position| self.scrollbase.drag(position));
let position = position.saturating_sub(offset);
self.scrollbase.drag(position);
return EventResult::Consumed(None);
}
Event::Mouse {
Expand Down Expand Up @@ -397,12 +396,10 @@ impl View for ListView {
direction::Direction::back(),
)
}
Event::Key(Key::End) | Event::Ctrl(Key::End) => {
self.move_focus(
usize::max_value(),
direction::Direction::front(),
)
}
Event::Key(Key::End) | Event::Ctrl(Key::End) => self.move_focus(
usize::max_value(),
direction::Direction::front(),
),
Event::Key(Key::Tab) => {
self.move_focus(1, direction::Direction::front())
}
Expand Down
9 changes: 4 additions & 5 deletions src/views/menu_popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ impl View for MenuPopup {
}
printer.print_hline((1, 0), printer.size.x - 2, " ");
printer.print((2, 0), label);
printer.print((printer.size.x.saturating_sub(4), 0),
">>");
let x = printer.size.x.saturating_sub(4);
printer.print((x, 0), ">>");
}
MenuItem::Leaf(ref label, _) => {
if printer.size.x < 2 {
Expand Down Expand Up @@ -333,9 +333,8 @@ impl View for MenuPopup {
} => {
// If the mouse is dragged, we always consume the event.
fix_scroll = false;
position
.checked_sub(offset + (0, 1))
.map(|position| self.scrollbase.drag(position));
let position = position.saturating_sub(offset);
self.scrollbase.drag(position);
}
Event::Mouse {
event: MouseEvent::Press(_),
Expand Down
5 changes: 2 additions & 3 deletions src/views/select_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,8 @@ impl<T: 'static> SelectView<T> {
} => {
// If the mouse is dragged, we always consume the event.
fix_scroll = false;
position
.checked_sub(offset)
.map(|position| self.scrollbase.drag(position));
let position = position.saturating_sub(offset);
self.scrollbase.drag(position);
}
Event::Mouse {
event: MouseEvent::Press(_),
Expand Down
5 changes: 2 additions & 3 deletions src/views/text_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,8 @@ impl View for TextArea {
offset,
} => {
fix_scroll = false;
position
.checked_sub(offset)
.map(|position| self.scrollbase.drag(position));
let position = position.saturating_sub(offset);
self.scrollbase.drag(position);
}
Event::Mouse {
event: MouseEvent::Press(_),
Expand Down
5 changes: 2 additions & 3 deletions src/views/text_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,8 @@ impl View for TextView {
offset,
} => {
// If the mouse is dragged, we always consume the event.
position
.checked_sub(offset)
.map(|position| self.scrollbase.drag(position));
let position = position.saturating_sub(offset);
self.scrollbase.drag(position);
}
Event::Mouse {
event: MouseEvent::Release(MouseButton::Left),
Expand Down

0 comments on commit 420454c

Please sign in to comment.