Add mouse support to radio view

This commit is contained in:
Alexandre Bury 2017-10-12 17:29:12 -07:00
parent 1b67c3eceb
commit 13059dae0b

View File

@ -1,6 +1,6 @@
use {Printer, With}; use {Printer, With};
use direction::Direction; use direction::Direction;
use event::{Event, EventResult, Key}; use event::{Event, EventResult, Key, MouseButton, MouseEvent};
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc; use std::rc::Rc;
use theme::ColorStyle; use theme::ColorStyle;
@ -144,16 +144,20 @@ impl<T> RadioButton<T> {
printer.print((4, 0), &self.label); printer.print((4, 0), &self.label);
} }
} }
}
impl<T> View for RadioButton<T> { fn req_size(&self) -> Vec2 {
fn required_size(&mut self, _: Vec2) -> Vec2 {
if self.label.is_empty() { if self.label.is_empty() {
Vec2::new(3, 1) Vec2::new(3, 1)
} else { } else {
Vec2::new(3 + 1 + self.label.len(), 1) Vec2::new(3 + 1 + self.label.len(), 1)
} }
} }
}
impl<T> View for RadioButton<T> {
fn required_size(&mut self, _: Vec2) -> Vec2 {
self.req_size()
}
fn take_focus(&mut self, _: Direction) -> bool { fn take_focus(&mut self, _: Direction) -> bool {
self.enabled self.enabled
@ -179,6 +183,15 @@ impl<T> View for RadioButton<T> {
self.select(); self.select();
EventResult::Consumed(None) EventResult::Consumed(None)
} }
Event::Mouse {
event: MouseEvent::Release(MouseButton::Left),
position,
offset,
} if position.fits_in_rect(offset, self.req_size()) =>
{
self.select();
EventResult::Consumed(None)
}
_ => EventResult::Ignored, _ => EventResult::Ignored,
} }
} }