cacao/examples/todos_list/todos/list/row.rs
Ryan McGrath 724b40e5a8
Add a (useful) example: a todo list.
- Adds a Todo list example.
- Updates ListView to support ending the row actions visible state.
- Support setting Label text color.
2021-02-07 23:37:25 -08:00

61 lines
2.1 KiB
Rust

use cacao::layout::{Layout, LayoutConstraint};
use cacao::text::{Font, Label, LineBreakMode};
use cacao::view::{View, ViewDelegate};
use cacao::color::rgb;
use crate::storage::{Todo, TodoStatus};
/// This view is used as a row in our `TodosListView`. It displays information/status for a provided
/// `Todo`, and gets cached and reused as necessary. It should not store anything long-term.
#[derive(Default, Debug)]
pub struct TodoViewRow {
pub title: Label,
pub status: Label
}
impl TodoViewRow {
/// Called when this view is being presented, and configures itself with the given todo.
pub fn configure_with(&mut self, todo: &Todo) {
self.title.set_text(&todo.title);
match todo.status {
TodoStatus::Incomplete => {
self.status.set_color(rgb(219, 66, 66));
self.status.set_text("Incomplete");
},
TodoStatus::Complete => {
self.status.set_color(rgb(11, 121, 254));
self.status.set_text("Complete");
}
}
}
}
impl ViewDelegate for TodoViewRow {
const NAME: &'static str = "TodoViewRow";
/// Called when the view is first created; handles setup of layout and associated styling that
/// doesn't change.
fn did_load(&mut self, view: View) {
view.add_subview(&self.title);
view.add_subview(&self.status);
self.title.set_line_break_mode(LineBreakMode::TruncateMiddle);
let font = Font::system(10.);
self.status.set_font(&font);
LayoutConstraint::activate(&[
self.title.top.constraint_equal_to(&view.top).offset(16.),
self.title.leading.constraint_equal_to(&view.leading).offset(16.),
self.title.trailing.constraint_equal_to(&view.trailing).offset(-16.),
self.status.top.constraint_equal_to(&self.title.bottom).offset(8.),
self.status.leading.constraint_equal_to(&view.leading).offset(16.),
self.status.trailing.constraint_equal_to(&view.trailing).offset(-16.),
self.status.bottom.constraint_equal_to(&view.bottom).offset(-16.)
]);
}
}