use tuirealm::command::{Cmd, CmdResult, Direction};
use tuirealm::props::{
Alignment, AttrValue, Attribute, Borders, Color, PropPayload, PropValue, Props, Style,
};
use tuirealm::ratatui::text::Line as Spans;
use tuirealm::ratatui::{layout::Rect, text::Span, widgets::Tabs};
use tuirealm::{Frame, MockComponent, State, StateValue};
#[derive(Default)]
pub struct CheckboxStates {
pub choice: usize, pub choices: Vec<String>, pub selection: Vec<usize>, }
impl CheckboxStates {
pub fn next_choice(&mut self, rewind: bool) {
if rewind && self.choice + 1 >= self.choices.len() {
self.choice = 0;
} else if self.choice + 1 < self.choices.len() {
self.choice += 1;
}
}
pub fn prev_choice(&mut self, rewind: bool) {
if rewind && self.choice == 0 && !self.choices.is_empty() {
self.choice = self.choices.len() - 1;
} else if self.choice > 0 {
self.choice -= 1;
}
}
pub fn toggle(&mut self) {
let option = self.choice;
if self.selection.contains(&option) {
let target_index = self.selection.iter().position(|x| *x == option).unwrap();
self.selection.remove(target_index);
} else {
self.selection.push(option);
}
}
pub fn select(&mut self, i: usize) {
if i < self.choices.len() && !self.selection.contains(&i) {
self.selection.push(i);
}
}
pub fn has(&self, option: usize) -> bool {
self.selection.contains(&option)
}
pub fn set_choices(&mut self, choices: &[String]) {
self.choices = choices.to_vec();
self.selection.clear();
if self.choice >= self.choices.len() {
self.choice = match self.choices.len() {
0 => 0,
l => l - 1,
};
}
}
}
#[derive(Default)]
pub struct Checkbox {
props: Props,
pub states: CheckboxStates,
}
impl Checkbox {
pub fn foreground(mut self, fg: Color) -> Self {
self.attr(Attribute::Foreground, AttrValue::Color(fg));
self
}
pub fn background(mut self, bg: Color) -> Self {
self.attr(Attribute::Background, AttrValue::Color(bg));
self
}
pub fn borders(mut self, b: Borders) -> Self {
self.attr(Attribute::Borders, AttrValue::Borders(b));
self
}
pub fn title<S: AsRef<str>>(mut self, t: S, a: Alignment) -> Self {
self.attr(
Attribute::Title,
AttrValue::Title((t.as_ref().to_string(), a)),
);
self
}
pub fn inactive(mut self, s: Style) -> Self {
self.attr(Attribute::FocusStyle, AttrValue::Style(s));
self
}
pub fn rewind(mut self, r: bool) -> Self {
self.attr(Attribute::Rewind, AttrValue::Flag(r));
self
}
pub fn choices<S: AsRef<str>>(mut self, choices: &[S]) -> Self {
self.attr(
Attribute::Content,
AttrValue::Payload(PropPayload::Vec(
choices
.iter()
.map(|x| PropValue::Str(x.as_ref().to_string()))
.collect(),
)),
);
self
}
pub fn values(mut self, selected: &[usize]) -> Self {
self.attr(
Attribute::Value,
AttrValue::Payload(PropPayload::Vec(
selected.iter().map(|x| PropValue::Usize(*x)).collect(),
)),
);
self
}
fn rewindable(&self) -> bool {
self.props
.get_or(Attribute::Rewind, AttrValue::Flag(false))
.unwrap_flag()
}
}
impl MockComponent for Checkbox {
fn view(&mut self, render: &mut Frame, area: Rect) {
if self.props.get_or(Attribute::Display, AttrValue::Flag(true)) == AttrValue::Flag(true) {
let foreground = self
.props
.get_or(Attribute::Foreground, AttrValue::Color(Color::Reset))
.unwrap_color();
let background = self
.props
.get_or(Attribute::Background, AttrValue::Color(Color::Reset))
.unwrap_color();
let borders = self
.props
.get_or(Attribute::Borders, AttrValue::Borders(Borders::default()))
.unwrap_borders();
let title = self.props.get(Attribute::Title).map(|x| x.unwrap_title());
let focus = self
.props
.get_or(Attribute::Focus, AttrValue::Flag(false))
.unwrap_flag();
let inactive_style = self
.props
.get(Attribute::FocusStyle)
.map(|x| x.unwrap_style());
let div = crate::utils::get_block(borders, title, focus, inactive_style);
let (bg, fg, block_color): (Color, Color, Color) = match &focus {
true => (foreground, background, foreground),
false => (Color::Reset, foreground, Color::Reset),
};
let choices: Vec<Spans> = self
.states
.choices
.iter()
.enumerate()
.map(|(idx, x)| {
let checkbox: &str = match self.states.has(idx) {
true => "☑ ",
false => "☐ ",
};
let (fg, bg) = match focus {
true => match self.states.choice == idx {
true => (fg, bg),
false => (bg, fg),
},
false => (fg, bg),
};
Spans::from(vec![
Span::styled(checkbox, Style::default().fg(fg).bg(bg)),
Span::styled(x.to_string(), Style::default().fg(fg).bg(bg)),
])
})
.collect();
let checkbox: Tabs = Tabs::new(choices)
.block(div)
.select(self.states.choice)
.style(Style::default().fg(block_color));
render.render_widget(checkbox, area);
}
}
fn query(&self, attr: Attribute) -> Option<AttrValue> {
self.props.get(attr)
}
fn attr(&mut self, attr: Attribute, value: AttrValue) {
match attr {
Attribute::Content => {
let current_selection = self.states.selection.clone();
let choices: Vec<String> = value
.unwrap_payload()
.unwrap_vec()
.iter()
.cloned()
.map(|x| x.unwrap_str())
.collect();
self.states.set_choices(&choices);
for c in current_selection.into_iter() {
self.states.select(c);
}
}
Attribute::Value => {
self.states.selection.clear();
for c in value.unwrap_payload().unwrap_vec() {
self.states.select(c.unwrap_usize());
}
}
attr => {
self.props.set(attr, value);
}
}
}
fn state(&self) -> State {
State::Vec(
self.states
.selection
.iter()
.map(|x| StateValue::Usize(*x))
.collect(),
)
}
fn perform(&mut self, cmd: Cmd) -> CmdResult {
match cmd {
Cmd::Move(Direction::Right) => {
self.states.next_choice(self.rewindable());
CmdResult::None
}
Cmd::Move(Direction::Left) => {
self.states.prev_choice(self.rewindable());
CmdResult::None
}
Cmd::Toggle => {
self.states.toggle();
CmdResult::Changed(self.state())
}
Cmd::Submit => {
CmdResult::Submit(self.state())
}
_ => CmdResult::None,
}
}
}
#[cfg(test)]
mod test {
use super::*;
use pretty_assertions::{assert_eq, assert_ne};
use tuirealm::props::{PropPayload, PropValue};
#[test]
fn test_components_checkbox_states() {
let mut states: CheckboxStates = CheckboxStates::default();
assert_eq!(states.choice, 0);
assert_eq!(states.choices.len(), 0);
assert_eq!(states.selection.len(), 0);
let choices: &[String] = &[
"lemon".to_string(),
"strawberry".to_string(),
"vanilla".to_string(),
"chocolate".to_string(),
];
states.set_choices(choices);
assert_eq!(states.choice, 0);
assert_eq!(states.choices.len(), 4);
assert_eq!(states.selection.len(), 0);
states.toggle();
assert_eq!(states.selection, vec![0]);
states.prev_choice(false);
assert_eq!(states.choice, 0);
states.next_choice(false);
assert_eq!(states.choice, 1);
states.next_choice(false);
assert_eq!(states.choice, 2);
states.toggle();
assert_eq!(states.selection, vec![0, 2]);
states.next_choice(false);
states.next_choice(false);
assert_eq!(states.choice, 3);
states.prev_choice(false);
assert_eq!(states.choice, 2);
states.toggle();
assert_eq!(states.selection, vec![0]);
assert_eq!(states.has(0), true);
assert_ne!(states.has(2), true);
let choices: &[String] = &["lemon".to_string(), "strawberry".to_string()];
states.set_choices(choices);
assert_eq!(states.choice, 1); assert_eq!(states.choices.len(), 2);
assert_eq!(states.selection.len(), 0);
let choices: &[String] = &[];
states.set_choices(choices);
assert_eq!(states.choice, 0); assert_eq!(states.choices.len(), 0);
assert_eq!(states.selection.len(), 0);
let choices: &[String] = &[
"lemon".to_string(),
"strawberry".to_string(),
"vanilla".to_string(),
"chocolate".to_string(),
];
states.set_choices(choices);
assert_eq!(states.choice, 0);
states.prev_choice(true);
assert_eq!(states.choice, 3);
states.next_choice(true);
assert_eq!(states.choice, 0);
states.next_choice(true);
assert_eq!(states.choice, 1);
states.prev_choice(true);
assert_eq!(states.choice, 0);
}
#[test]
fn test_components_checkbox() {
let mut component = Checkbox::default()
.background(Color::Blue)
.foreground(Color::Red)
.borders(Borders::default())
.title("Which food do you prefer?", Alignment::Center)
.choices(&["Pizza", "Hummus", "Ramen", "Gyoza", "Pasta"])
.values(&[1, 4])
.rewind(false);
assert_eq!(component.states.selection, vec![1, 4]);
assert_eq!(component.states.choice, 0);
assert_eq!(component.states.choices.len(), 5);
component.attr(
Attribute::Content,
AttrValue::Payload(PropPayload::Vec(vec![
PropValue::Str(String::from("Pizza")),
PropValue::Str(String::from("Hummus")),
PropValue::Str(String::from("Ramen")),
PropValue::Str(String::from("Gyoza")),
PropValue::Str(String::from("Pasta")),
PropValue::Str(String::from("Falafel")),
])),
);
assert_eq!(component.states.selection, vec![1, 4]);
assert_eq!(component.states.choices.len(), 6);
component.attr(
Attribute::Value,
AttrValue::Payload(PropPayload::Vec(vec![PropValue::Usize(1)])),
);
assert_eq!(component.states.selection, vec![1]);
assert_eq!(component.states.choices.len(), 6);
assert_eq!(component.state(), State::Vec(vec![StateValue::Usize(1)]));
assert_eq!(
component.perform(Cmd::Move(Direction::Left)),
CmdResult::None,
);
assert_eq!(component.state(), State::Vec(vec![StateValue::Usize(1)]));
assert_eq!(
component.perform(Cmd::Toggle),
CmdResult::Changed(State::Vec(vec![StateValue::Usize(1), StateValue::Usize(0)]))
);
assert_eq!(
component.perform(Cmd::Move(Direction::Left)),
CmdResult::None,
);
assert_eq!(component.states.choice, 0);
assert_eq!(
component.perform(Cmd::Move(Direction::Right)),
CmdResult::None,
);
assert_eq!(
component.perform(Cmd::Toggle),
CmdResult::Changed(State::Vec(vec![StateValue::Usize(0)]))
);
assert_eq!(
component.perform(Cmd::Move(Direction::Right)),
CmdResult::None,
);
assert_eq!(component.states.choice, 2);
assert_eq!(
component.perform(Cmd::Move(Direction::Right)),
CmdResult::None,
);
assert_eq!(component.states.choice, 3);
assert_eq!(
component.perform(Cmd::Move(Direction::Right)),
CmdResult::None,
);
assert_eq!(component.states.choice, 4);
assert_eq!(
component.perform(Cmd::Move(Direction::Right)),
CmdResult::None,
);
assert_eq!(component.states.choice, 5);
assert_eq!(
component.perform(Cmd::Move(Direction::Right)),
CmdResult::None,
);
assert_eq!(component.states.choice, 5);
assert_eq!(
component.perform(Cmd::Submit),
CmdResult::Submit(State::Vec(vec![StateValue::Usize(0)])),
);
}
}