From f3528088cd4e9f37fe8e8fa691fdefc0fcb0b8d4 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Wed, 11 Oct 2017 15:07:25 -0700 Subject: [PATCH] Add mouse support to Dialog and Button --- src/views/button.rs | 23 +++- src/views/dialog.rs | 328 +++++++++++++++++++++++++++++--------------- 2 files changed, 238 insertions(+), 113 deletions(-) diff --git a/src/views/button.rs b/src/views/button.rs index 591d300..6232e70 100644 --- a/src/views/button.rs +++ b/src/views/button.rs @@ -28,7 +28,8 @@ pub struct Button { impl Button { /// Creates a new button with the given content and callback. pub fn new>(label: S, cb: F) -> Self - where F: Fn(&mut Cursive) + 'static + where + F: Fn(&mut Cursive) + 'static, { Button { label: label.into(), @@ -41,7 +42,8 @@ impl Button { /// /// Replaces the previous callback. pub fn set_callback(&mut self, cb: F) - where F: Fn(&mut Cursive) + 'static + where + F: Fn(&mut Cursive) + 'static, { self.callback = Callback::from_fn(cb); } @@ -74,11 +76,14 @@ impl Button { pub fn is_enabled(&self) -> bool { self.enabled } + + fn req_size(&self) -> Vec2 { + Vec2::new(2 + self.label.width(), 1) + } } impl View for Button { fn draw(&self, printer: &Printer) { - if printer.size.x == 0 { return; } @@ -101,15 +106,25 @@ impl View for Button { fn required_size(&mut self, _: Vec2) -> Vec2 { // Meh. Fixed size we are. - Vec2::new(2 + self.label.width(), 1) + self.req_size() } fn on_event(&mut self, event: Event) -> EventResult { + eprintln!("{:?}", event); + eprintln!("{:?}", self.req_size()); match event { // 10 is the ascii code for '\n', that is the return key Event::Key(Key::Enter) => { EventResult::Consumed(Some(self.callback.clone())) } + Event::Mouse { + event: MouseEvent::Release(MouseButton::Left), + position, + offset, + } if position.fits_in_rect(offset, self.req_size()) => + { + EventResult::Consumed(Some(self.callback.clone())) + } _ => EventResult::Ignored, } } diff --git a/src/views/dialog.rs b/src/views/dialog.rs index 1747171..8f53330 100644 --- a/src/views/dialog.rs +++ b/src/views/dialog.rs @@ -1,5 +1,3 @@ - - use Cursive; use Printer; use With; @@ -7,9 +5,9 @@ use align::*; use direction::Direction; use event::*; use std::any::Any; +use std::cell::Cell; use std::cmp::max; use theme::ColorStyle; - use unicode_width::UnicodeWidthStr; use vec::{Vec2, Vec4}; use view::{Selector, View}; @@ -21,6 +19,23 @@ enum Focus { Button(usize), } +struct ChildButton { + button: SizedView