mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-10 03:10:41 +00:00
Scolling fixes
This commit is contained in:
parent
94504ae91b
commit
5eddc1b89a
@ -1,10 +1,10 @@
|
||||
use align::HAlign;
|
||||
use direction::Direction;
|
||||
use event::*;
|
||||
use rect::Rect;
|
||||
use theme::ColorStyle;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
use vec::Vec2;
|
||||
use rect::Rect;
|
||||
use view::View;
|
||||
use {Cursive, Printer, With};
|
||||
|
||||
@ -22,6 +22,7 @@ pub struct Button {
|
||||
label: String,
|
||||
callback: Callback,
|
||||
enabled: bool,
|
||||
last_size: Vec2,
|
||||
}
|
||||
|
||||
impl Button {
|
||||
@ -44,6 +45,7 @@ impl Button {
|
||||
label: label.into(),
|
||||
callback: Callback::from_fn(cb),
|
||||
enabled: true,
|
||||
last_size: Vec2::zero(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -140,13 +142,18 @@ impl View for Button {
|
||||
ColorStyle::highlight()
|
||||
};
|
||||
|
||||
let offset = HAlign::Center.get_offset(self.label.width(), printer.size.x);
|
||||
let offset =
|
||||
HAlign::Center.get_offset(self.label.width(), printer.size.x);
|
||||
|
||||
printer.with_color(style, |printer| {
|
||||
printer.print((offset, 0), &self.label);
|
||||
});
|
||||
}
|
||||
|
||||
fn layout(&mut self, size: Vec2) {
|
||||
self.last_size = size;
|
||||
}
|
||||
|
||||
fn required_size(&mut self, _: Vec2) -> Vec2 {
|
||||
// Meh. Fixed size we are.
|
||||
self.req_size()
|
||||
@ -155,6 +162,8 @@ impl View for Button {
|
||||
fn on_event(&mut self, event: Event) -> EventResult {
|
||||
// eprintln!("{:?}", event);
|
||||
// eprintln!("{:?}", self.req_size());
|
||||
let width = self.label.width();
|
||||
let self_offset = HAlign::Center.get_offset(width, self.last_size.x);
|
||||
match event {
|
||||
// 10 is the ascii code for '\n', that is the return key
|
||||
Event::Key(Key::Enter) => {
|
||||
@ -164,7 +173,7 @@ impl View for Button {
|
||||
event: MouseEvent::Release(MouseButton::Left),
|
||||
position,
|
||||
offset,
|
||||
} if position.fits_in_rect(offset, self.req_size()) =>
|
||||
} if position.fits_in_rect(offset + (self_offset, 0), self.req_size()) =>
|
||||
{
|
||||
EventResult::Consumed(Some(self.callback.clone()))
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
use direction::Direction;
|
||||
use event::{AnyCb, Event, EventResult, Key};
|
||||
use event::{AnyCb, Event, EventResult, Key, MouseEvent};
|
||||
use rect::Rect;
|
||||
use vec::Vec2;
|
||||
use view::{Selector, View};
|
||||
@ -175,6 +175,7 @@ where
|
||||
fn on_event(&mut self, event: Event) -> EventResult {
|
||||
// Relativize event accorging to the offset
|
||||
let mut relative_event = event.clone();
|
||||
// eprintln!("Mouse = {:?}", relative_event);
|
||||
if let Some(pos) = relative_event.mouse_position_mut() {
|
||||
*pos = *pos + self.offset;
|
||||
}
|
||||
@ -184,6 +185,14 @@ 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 => {
|
||||
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 += 3;
|
||||
EventResult::Consumed(None)
|
||||
}
|
||||
Event::Ctrl(Key::Up) |
|
||||
Event::Key(Key::Up)
|
||||
if self.enabled.y && self.offset.y > 0 =>
|
||||
|
Loading…
Reference in New Issue
Block a user