diff --git a/examples/scroll.rs b/examples/scroll.rs index 0f5e517..37b4fa2 100644 --- a/examples/scroll.rs +++ b/examples/scroll.rs @@ -1,20 +1,28 @@ extern crate cursive; use cursive::traits::Boxable; -use cursive::views::{Canvas, Dialog, ScrollView, LinearLayout, Button}; +use cursive::views::{Button, Canvas, Dialog, LinearLayout, ScrollView}; use cursive::Printer; fn main() { let mut siv = cursive::Cursive::default(); - siv.add_layer(Dialog::around( + siv.add_layer( + Dialog::around( ScrollView::new( LinearLayout::vertical() - .child(Button::new("Foo", |s| s.add_layer(Dialog::info("Ah")))) - .child(Canvas::new(()).with_draw(draw).fixed_size((120, 40))) - .child(Button::new("Bar", |s| s.add_layer(Dialog::info("Uh")))) - ).scroll_x(true), - ).fixed_size((60, 30))); + .child(Button::new("Foo", |s| { + s.add_layer(Dialog::info("Ah")) + })) + .child( + Canvas::new(()).with_draw(draw).fixed_size((120, 40)), + ) + .child(Button::new("Bar", |s| { + s.add_layer(Dialog::info("Uh")) + })), + ).scroll_x(true), + ).fixed_size((60, 30)), + ); siv.add_global_callback('q', |s| s.quit()); @@ -24,7 +32,7 @@ fn main() { fn draw(_: &(), p: &Printer) { for x in 0..p.size.x { for y in 0..p.size.y { - let c = (x + 6*y) % 10; + let c = (x + 6 * y) % 10; p.print((x, y), &format!("{}", c)); } } diff --git a/src/views/button.rs b/src/views/button.rs index c76b391..3066195 100644 --- a/src/views/button.rs +++ b/src/views/button.rs @@ -173,7 +173,8 @@ impl View for Button { event: MouseEvent::Release(MouseButton::Left), position, offset, - } if position.fits_in_rect(offset + (self_offset, 0), self.req_size()) => + } if position + .fits_in_rect(offset + (self_offset, 0), self.req_size()) => { EventResult::Consumed(Some(self.callback.clone())) } diff --git a/src/views/scroll_view.rs b/src/views/scroll_view.rs index 2dcc4fb..42e9f2e 100644 --- a/src/views/scroll_view.rs +++ b/src/views/scroll_view.rs @@ -187,23 +187,34 @@ where // If it's a mouse scroll, try to scroll as well. // Also allow Ctrl+arrow to move the view without moving selection. match event { - Event::Mouse { event: MouseEvent::WheelUp, .. } if self.enabled.y && self.offset.y > 0 => { + Event::Mouse { + event: MouseEvent::WheelUp, + .. + } if self.enabled.y && self.offset.y > 0 => + { self.offset.y = self.offset.y.saturating_sub(3); EventResult::Consumed(None) } - Event::Mouse { event: MouseEvent::WheelDown, .. } if self.enabled.y && (self.offset.y + self.last_size.y < self.inner_size.y) => { - self.offset.y = min(self.inner_size.y.saturating_sub(self.last_size.y), self.offset.y + 3); + Event::Mouse { + event: MouseEvent::WheelDown, + .. + } if self.enabled.y + && (self.offset.y + self.last_size.y + < self.inner_size.y) => + { + self.offset.y = min( + self.inner_size.y.saturating_sub(self.last_size.y), + self.offset.y + 3, + ); EventResult::Consumed(None) } - Event::Ctrl(Key::Up) | - Event::Key(Key::Up) + Event::Ctrl(Key::Up) | Event::Key(Key::Up) if self.enabled.y && self.offset.y > 0 => { self.offset.y -= 1; EventResult::Consumed(None) } - Event::Ctrl(Key::Down) | - Event::Key(Key::Down) + Event::Ctrl(Key::Down) | Event::Key(Key::Down) if self.enabled.y && (self.offset.y + self.last_size.y < self.inner_size.y) => @@ -211,15 +222,13 @@ where self.offset.y += 1; EventResult::Consumed(None) } - Event::Ctrl(Key::Left) | - Event::Key(Key::Left) + Event::Ctrl(Key::Left) | Event::Key(Key::Left) if self.enabled.x && self.offset.x > 0 => { self.offset.x -= 1; EventResult::Consumed(None) } - Event::Ctrl(Key::Right) | - Event::Key(Key::Right) + Event::Ctrl(Key::Right) | Event::Key(Key::Right) if self.enabled.x && (self.offset.x + self.last_size.x < self.inner_size.x) => @@ -235,7 +244,8 @@ where let important = self.inner.important_area(self.inner_size); // The furthest top-left we can go - let top_left = (important.bottom_right() + (1,1)).saturating_sub(self.last_size); + let top_left = (important.bottom_right() + (1, 1)) + .saturating_sub(self.last_size); // The furthest bottom-right we can go let bottom_right = important.top_left(); @@ -244,10 +254,11 @@ where let offset_min = Vec2::min(top_left, bottom_right); let offset_max = Vec2::max(top_left, bottom_right); - self.offset = self.offset.or_max(offset_min).or_min(offset_max); + self.offset = + self.offset.or_max(offset_min).or_min(offset_max); other - }, + } } }