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