2016-10-02 21:55:19 +00:00
|
|
|
use direction::Direction;
|
2017-10-13 00:29:12 +00:00
|
|
|
use event::{Event, EventResult, Key, MouseButton, MouseEvent};
|
2016-10-02 21:55:19 +00:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
2016-10-02 22:22:29 +00:00
|
|
|
use theme::ColorStyle;
|
|
|
|
use vec::Vec2;
|
|
|
|
use view::View;
|
2018-06-11 06:29:10 +00:00
|
|
|
use {Printer, With};
|
2016-10-02 21:55:19 +00:00
|
|
|
|
|
|
|
struct SharedState<T> {
|
|
|
|
selection: usize,
|
|
|
|
values: Vec<Rc<T>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> SharedState<T> {
|
|
|
|
pub fn selection(&self) -> Rc<T> {
|
2017-10-13 18:22:02 +00:00
|
|
|
Rc::clone(&self.values[self.selection])
|
2016-10-02 21:55:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Group to coordinate multiple radio buttons.
|
|
|
|
///
|
|
|
|
/// A `RadioGroup` is used to create and manage [`RadioButton`]s.
|
|
|
|
///
|
2016-10-02 22:03:03 +00:00
|
|
|
/// A `RadioGroup` can be cloned; it will keep pointing to the same group.
|
2016-10-02 21:55:19 +00:00
|
|
|
///
|
2016-10-02 22:03:03 +00:00
|
|
|
/// [`RadioButton`]: struct.RadioButton.html
|
|
|
|
#[derive(Clone)]
|
2016-10-02 21:55:19 +00:00
|
|
|
pub struct RadioGroup<T> {
|
|
|
|
// Given to every child button
|
|
|
|
state: Rc<RefCell<SharedState<T>>>,
|
|
|
|
}
|
|
|
|
|
2017-10-12 23:38:55 +00:00
|
|
|
impl<T> Default for RadioGroup<T> {
|
2017-07-17 23:43:50 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-02 21:55:19 +00:00
|
|
|
impl<T> RadioGroup<T> {
|
|
|
|
/// Creates an empty group for radio buttons.
|
|
|
|
pub fn new() -> Self {
|
|
|
|
RadioGroup {
|
|
|
|
state: Rc::new(RefCell::new(SharedState {
|
|
|
|
selection: 0,
|
|
|
|
values: Vec::new(),
|
|
|
|
})),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a new button to the group.
|
|
|
|
///
|
|
|
|
/// The button will display `label` next to it, and will embed `value`.
|
2017-10-12 23:38:55 +00:00
|
|
|
pub fn button<S: Into<String>>(
|
2018-06-11 06:29:10 +00:00
|
|
|
&mut self, value: T, label: S,
|
2017-10-12 23:38:55 +00:00
|
|
|
) -> RadioButton<T> {
|
2016-10-02 22:03:03 +00:00
|
|
|
let count = self.state.borrow().values.len();
|
2016-10-02 21:55:19 +00:00
|
|
|
self.state.borrow_mut().values.push(Rc::new(value));
|
2017-10-13 18:22:02 +00:00
|
|
|
RadioButton::new(Rc::clone(&self.state), count, label.into())
|
2016-10-02 21:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the id of the selected button.
|
|
|
|
///
|
|
|
|
/// Buttons are indexed in the order they are created, starting from 0.
|
|
|
|
pub fn selected_id(&self) -> usize {
|
|
|
|
self.state.borrow().selection
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the value associated with the selected button.
|
|
|
|
pub fn selection(&self) -> Rc<T> {
|
|
|
|
self.state.borrow().selection()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RadioGroup<String> {
|
|
|
|
/// Adds a button, using the label itself as value.
|
2017-10-12 23:38:55 +00:00
|
|
|
pub fn button_str<S: Into<String>>(
|
2018-06-11 06:29:10 +00:00
|
|
|
&mut self, text: S,
|
2017-10-12 23:38:55 +00:00
|
|
|
) -> RadioButton<String> {
|
2016-10-02 21:55:19 +00:00
|
|
|
let text = text.into();
|
|
|
|
self.button(text.clone(), text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Variant of `Checkbox` arranged in group.
|
|
|
|
///
|
2016-10-02 23:03:31 +00:00
|
|
|
/// `RadioButton`s are managed by a [`RadioGroup`]. A single group can contain
|
2016-10-02 21:55:19 +00:00
|
|
|
/// several radio buttons, but only one button per group can be active at a
|
|
|
|
/// time.
|
|
|
|
///
|
|
|
|
/// `RadioButton`s are not created directly, but through
|
|
|
|
/// [`RadioGroup::button()`].
|
|
|
|
///
|
|
|
|
/// [`RadioGroup`]: struct.RadioGroup.html
|
|
|
|
/// [`RadioGroup::button()`]: struct.RadioGroup.html#method.button
|
|
|
|
pub struct RadioButton<T> {
|
|
|
|
state: Rc<RefCell<SharedState<T>>>,
|
|
|
|
id: usize,
|
|
|
|
enabled: bool,
|
|
|
|
label: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> RadioButton<T> {
|
|
|
|
impl_enabled!(self.enabled);
|
|
|
|
|
2017-10-12 23:38:55 +00:00
|
|
|
fn new(
|
2018-06-11 06:29:10 +00:00
|
|
|
state: Rc<RefCell<SharedState<T>>>, id: usize, label: String,
|
2017-10-12 23:38:55 +00:00
|
|
|
) -> Self {
|
2016-10-02 21:55:19 +00:00
|
|
|
RadioButton {
|
2018-04-10 18:53:25 +00:00
|
|
|
state,
|
|
|
|
id,
|
2016-10-02 21:55:19 +00:00
|
|
|
enabled: true,
|
2018-04-10 18:53:25 +00:00
|
|
|
label,
|
2016-10-02 21:55:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if this button is selected.
|
|
|
|
pub fn is_selected(&self) -> bool {
|
|
|
|
self.state.borrow().selection == self.id
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Selects this button, un-selecting any other in the same group.
|
|
|
|
pub fn select(&mut self) {
|
|
|
|
self.state.borrow_mut().selection = self.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Selects this button, un-selecting any other in the same group.
|
|
|
|
///
|
|
|
|
/// Chainable variant.
|
|
|
|
pub fn selected(self) -> Self {
|
|
|
|
self.with(Self::select)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn draw_internal(&self, printer: &Printer) {
|
|
|
|
printer.print((0, 0), "( )");
|
|
|
|
if self.is_selected() {
|
|
|
|
printer.print((1, 0), "X");
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.label.is_empty() {
|
|
|
|
// We want the space to be highlighted if focused
|
|
|
|
printer.print((3, 0), " ");
|
|
|
|
printer.print((4, 0), &self.label);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-13 00:29:12 +00:00
|
|
|
fn req_size(&self) -> Vec2 {
|
2016-10-02 21:55:19 +00:00
|
|
|
if self.label.is_empty() {
|
|
|
|
Vec2::new(3, 1)
|
|
|
|
} else {
|
|
|
|
Vec2::new(3 + 1 + self.label.len(), 1)
|
|
|
|
}
|
|
|
|
}
|
2017-10-13 00:29:12 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 02:54:40 +00:00
|
|
|
impl<T: 'static> View for RadioButton<T> {
|
2017-10-13 00:29:12 +00:00
|
|
|
fn required_size(&mut self, _: Vec2) -> Vec2 {
|
|
|
|
self.req_size()
|
|
|
|
}
|
2016-10-02 21:55:19 +00:00
|
|
|
|
|
|
|
fn take_focus(&mut self, _: Direction) -> bool {
|
|
|
|
self.enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
fn draw(&self, printer: &Printer) {
|
|
|
|
if self.enabled {
|
2017-12-30 22:03:42 +00:00
|
|
|
printer.with_selection(printer.focused, |printer| {
|
|
|
|
self.draw_internal(printer)
|
|
|
|
});
|
2016-10-02 21:55:19 +00:00
|
|
|
} else {
|
2018-01-17 17:35:57 +00:00
|
|
|
printer.with_color(ColorStyle::secondary(), |printer| {
|
2017-12-30 22:03:42 +00:00
|
|
|
self.draw_internal(printer)
|
|
|
|
});
|
2016-10-02 21:55:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_event(&mut self, event: Event) -> EventResult {
|
|
|
|
match event {
|
2017-10-12 23:38:55 +00:00
|
|
|
Event::Key(Key::Enter) | Event::Char(' ') => {
|
2016-10-02 21:55:19 +00:00
|
|
|
self.select();
|
|
|
|
EventResult::Consumed(None)
|
|
|
|
}
|
2017-10-13 00:29:12 +00:00
|
|
|
Event::Mouse {
|
|
|
|
event: MouseEvent::Release(MouseButton::Left),
|
|
|
|
position,
|
|
|
|
offset,
|
2018-07-20 02:44:59 +00:00
|
|
|
}
|
|
|
|
if position.fits_in_rect(offset, self.req_size()) =>
|
2017-10-13 00:29:12 +00:00
|
|
|
{
|
|
|
|
self.select();
|
|
|
|
EventResult::Consumed(None)
|
|
|
|
}
|
2016-10-02 21:55:19 +00:00
|
|
|
_ => EventResult::Ignored,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|