meli/ui/src/components.rs

452 lines
11 KiB
Rust
Raw Normal View History

2018-07-11 17:07:51 +03:00
/*
2018-08-07 15:01:15 +03:00
* meli - ui crate.
2018-07-11 17:07:51 +03:00
*
* Copyright 2017-2018 Manos Pitsidianakis
*
* This file is part of meli.
*
* meli is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* meli is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with meli. If not, see <http://www.gnu.org/licenses/>.
*/
2018-07-18 10:42:52 +03:00
/*!
2019-03-14 12:19:25 +02:00
Components are ways to handle application data. They can draw on the terminal and receive events, but also do other stuff as well. (For example, see the `notifications` module.)
2018-07-18 10:42:52 +03:00
2019-03-14 12:19:25 +02:00
See the `Component` Trait for more details.
*/
2018-07-18 10:42:52 +03:00
use super::*;
2018-08-06 16:53:23 +03:00
2018-07-11 17:07:51 +03:00
pub mod mail;
2018-08-06 16:53:23 +03:00
pub use mail::*;
pub mod notifications;
2018-07-11 17:07:51 +03:00
2018-10-14 19:49:16 +03:00
pub mod indexer;
pub use self::indexer::*;
2018-08-06 16:53:23 +03:00
pub mod utilities;
2018-08-03 13:46:08 +03:00
pub use self::utilities::*;
2018-07-11 17:07:51 +03:00
2019-02-15 09:06:42 +02:00
pub mod contacts;
pub use contacts::*;
use std::fmt;
2018-08-23 15:36:52 +03:00
use std::fmt::{Debug, Display};
use std::ops::{Deref, DerefMut};
2019-03-02 19:25:11 +02:00
use fnv::FnvHashMap;
use uuid::Uuid;
2018-08-26 19:29:12 +03:00
use super::{Key, StatusEvent, UIEvent, UIEventType};
2018-07-11 17:07:51 +03:00
/// The upper and lower boundary char.
const HORZ_BOUNDARY: char = '─';
/// The left and right boundary char.
const VERT_BOUNDARY: char = '│';
/// The top-left corner
2018-07-24 20:20:32 +03:00
const _TOP_LEFT_CORNER: char = '┌';
2018-07-11 17:07:51 +03:00
/// The top-right corner
2018-07-24 20:20:32 +03:00
const _TOP_RIGHT_CORNER: char = '┐';
2018-07-11 17:07:51 +03:00
/// The bottom-left corner
2018-07-24 20:20:32 +03:00
const _BOTTOM_LEFT_CORNER: char = '└';
2018-07-11 17:07:51 +03:00
/// The bottom-right corner
2018-07-24 20:20:32 +03:00
const _BOTTOM_RIGHT_CORNER: char = '┘';
2018-07-11 17:07:51 +03:00
const LIGHT_VERTICAL_AND_RIGHT: char = '├';
2018-07-24 20:20:32 +03:00
const _LIGHT_VERTICAL_AND_LEFT: char = '┤';
2018-07-11 17:07:51 +03:00
const LIGHT_DOWN_AND_HORIZONTAL: char = '┬';
const LIGHT_UP_AND_HORIZONTAL: char = '┴';
2019-02-17 12:08:35 +02:00
const _DOUBLE_DOWN_AND_RIGHT: char = '╔';
const _DOUBLE_DOWN_AND_LEFT: char = '╗';
const _DOUBLE_UP_AND_LEFT: char = '╝';
const _DOUBLE_UP_AND_RIGHT: char = '╚';
2019-02-15 09:06:42 +02:00
2019-02-21 15:44:26 +02:00
type EntityId = Uuid;
/// `Entity` is a container for Components.
#[derive(Debug)]
2018-07-11 17:07:51 +03:00
pub struct Entity {
2019-02-21 15:44:26 +02:00
id: EntityId,
2018-07-11 17:07:51 +03:00
pub component: Box<Component>, // more than one?
}
impl From<Box<Component>> for Entity {
2019-02-21 15:29:26 +02:00
fn from(mut kind: Box<Component>) -> Entity {
let id = Uuid::new_v4();
kind.set_id(id);
Entity {
id,
component: kind,
}
}
}
impl<C: 'static> From<Box<C>> for Entity
where
C: Component,
{
2019-02-21 15:29:26 +02:00
fn from(mut kind: Box<C>) -> Entity {
let id = Uuid::new_v4();
kind.set_id(id);
Entity {
id,
component: kind,
}
}
}
impl Display for Entity {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.component, f)
}
}
impl DerefMut for Entity {
fn deref_mut(&mut self) -> &mut Box<Component> {
&mut self.component
}
}
impl Deref for Entity {
type Target = Box<Component>;
fn deref(&self) -> &Box<Component> {
&self.component
}
}
2018-07-11 17:07:51 +03:00
impl Entity {
2019-02-21 15:44:26 +02:00
pub fn id(&self) -> &EntityId {
&self.id
}
2018-07-11 18:58:57 +03:00
/// Pass events to child component.
2019-02-26 17:50:47 +02:00
pub fn rcv_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool {
self.component.process_event(event, context)
2018-07-11 17:07:51 +03:00
}
}
pub type ShortcutMap = FnvHashMap<&'static str, Key>;
2019-03-02 19:25:11 +02:00
2018-07-11 18:58:57 +03:00
/// Types implementing this Trait can draw on the terminal and receive events.
/// If a type wants to skip drawing if it has not changed anything, it can hold some flag in its
/// fields (eg self.dirty = false) and act upon that in their `draw` implementation.
2019-02-26 10:55:22 +02:00
pub trait Component: Display + Debug + Send {
2018-07-14 15:04:42 +03:00
fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context);
2019-02-26 17:50:47 +02:00
fn process_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool;
2018-07-14 15:04:42 +03:00
fn is_dirty(&self) -> bool {
true
}
fn set_dirty(&mut self);
fn kill(&mut self, _id: EntityId) {}
fn set_id(&mut self, _id: EntityId) {}
2019-03-02 19:25:11 +02:00
2019-03-14 12:19:25 +02:00
fn get_shortcuts(&self, context: &Context) -> ShortcutMap {
Default::default()
}
2018-07-11 17:07:51 +03:00
}
/*
2018-08-26 19:29:12 +03:00
pub(crate) fn is_box_char(ch: char) -> bool {
match ch {
HORZ_BOUNDARY | VERT_BOUNDARY => true,
_ => false,
}
}
* pub(crate) fn is_box_char(ch: char) -> bool {
* match ch {
* '└' | '─' | '┘' | '┴' | '┌' | '│' | '├' | '┐' | '┬' | '┤' | '┼' | '╷' | '╵' | '╴' | '╶' => true,
* _ => false,
* }
* }
*/
fn bin_to_ch(b: u32) -> char {
match b {
0b0001 => '╶',
0b0010 => '╵',
0b0011 => '└',
0b0100 => '╴',
0b0101 => '─',
0b0110 => '┘',
0b0111 => '┴',
0b1000 => '╷',
0b1001 => '┌',
0b1010 => '│',
0b1011 => '├',
0b1100 => '┐',
0b1101 => '┬',
0b1110 => '┤',
0b1111 => '┼',
x => unreachable!(format!("unreachable bin_to_ch(x), x = {:b}", x)),
}
}
fn ch_to_bin(ch: char) -> Option<u32> {
match ch {
'└' => Some(0b0011),
'─' => Some(0b0101),
'┘' => Some(0b0110),
'┴' => Some(0b0111),
'┌' => Some(0b1001),
'│' => Some(0b1010),
'├' => Some(0b1011),
'┐' => Some(0b1100),
'┬' => Some(0b1101),
'┤' => Some(0b1110),
'┼' => Some(0b1111),
'╷' => Some(0b1000),
'╵' => Some(0b0010),
'╴' => Some(0b0100),
'╶' => Some(0b0001),
_ => None,
}
}
#[allow(clippy::never_loop)]
fn set_and_join_vert(grid: &mut CellBuffer, idx: Pos) -> u32 {
let (x, y) = idx;
let mut bin_set = 0b1010;
/* Check left side
*
* 1
* -> 2 0
* 3
*/
loop {
if x > 0 {
if let Some(cell) = grid.get_mut(x - 1, y) {
if let Some(adj) = ch_to_bin(cell.ch()) {
if (adj & 0b0001) > 0 {
bin_set |= 0b0100;
break;
} else if adj == 0b0100 {
cell.set_ch(bin_to_ch(0b0101));
bin_set |= 0b0100;
break;
}
}
}
}
bin_set &= 0b1011;
break;
}
/* Check right side
*
* 1
* 2 0 <-
* 3
*/
loop {
if let Some(cell) = grid.get_mut(x + 1, y) {
if let Some(adj) = ch_to_bin(cell.ch()) {
if (adj & 0b0100) > 0 {
bin_set |= 0b0001;
break;
}
}
}
bin_set &= 0b1110;
break;
2018-07-15 15:35:57 +03:00
}
2018-08-21 19:51:48 +03:00
/* Set upper side
*
* 1 <-
* 2 0
* 3
*/
loop {
if y > 0 {
if let Some(cell) = grid.get_mut(x, y - 1) {
if let Some(adj) = ch_to_bin(cell.ch()) {
cell.set_ch(bin_to_ch(adj | 0b1000));
break;
}
}
}
bin_set &= 0b1101;
break;
2018-08-03 13:46:08 +03:00
}
2018-07-15 15:35:57 +03:00
/* Set bottom side
*
* 1
* 2 0
* 3 <-
*/
loop {
if let Some(cell) = grid.get_mut(x, y + 1) {
if let Some(adj) = ch_to_bin(cell.ch()) {
cell.set_ch(bin_to_ch(adj | 0b0010));
break;
2018-07-15 15:35:57 +03:00
}
}
bin_set &= 0b0111;
break;
2018-07-15 15:35:57 +03:00
}
if bin_set == 0 {
bin_set = 0b1010;
2018-07-15 15:35:57 +03:00
}
bin_set
2018-07-15 15:35:57 +03:00
}
#[allow(clippy::never_loop)]
fn set_and_join_horz(grid: &mut CellBuffer, idx: Pos) -> u32 {
let (x, y) = idx;
let mut bin_set = 0b0101;
/* Check upper side
*
* 1 <-
* 2 0
* 3
*/
loop {
if y > 0 {
if let Some(cell) = grid.get_mut(x, y - 1) {
if let Some(adj) = ch_to_bin(cell.ch()) {
if (adj & 0b1000) > 0 {
bin_set |= 0b0010;
break;
} else if adj == 0b0010 {
bin_set |= 0b0010;
cell.set_ch(bin_to_ch(0b1010));
break;
}
}
2018-08-23 15:36:52 +03:00
}
}
bin_set &= 0b1101;
break;
}
/* Check bottom side
*
* 1
* 2 0
* 3 <-
*/
loop {
if let Some(cell) = grid.get_mut(x, y + 1) {
if let Some(adj) = ch_to_bin(cell.ch()) {
if (adj & 0b0010) > 0 {
bin_set |= 0b1000;
break;
} else if adj == 0b1000 {
cell.set_ch(bin_to_ch(0b1010));
break;
}
2018-07-11 17:07:51 +03:00
}
2018-08-23 15:36:52 +03:00
}
bin_set &= 0b0111;
break;
2018-07-11 17:07:51 +03:00
}
/* Set left side
*
* 1
* -> 2 0
* 3
*/
loop {
if x > 0 {
if let Some(cell) = grid.get_mut(x - 1, y) {
if let Some(adj) = ch_to_bin(cell.ch()) {
cell.set_ch(bin_to_ch(adj | 0b0001));
break;
}
}
}
bin_set &= 0b1011;
break;
}
/* Set right side
*
* 1
* 2 0 <-
* 3
*/
loop {
if let Some(cell) = grid.get_mut(x + 1, y) {
if let Some(adj) = ch_to_bin(cell.ch()) {
cell.set_ch(bin_to_ch(adj | 0b0100));
break;
}
2018-07-11 17:07:51 +03:00
}
bin_set &= 0b1110;
break;
}
if bin_set == 0 {
bin_set = 0b0101;
2018-07-11 17:07:51 +03:00
}
bin_set
2018-07-11 17:07:51 +03:00
}
2018-08-26 19:29:12 +03:00
pub(crate) fn set_and_join_box(grid: &mut CellBuffer, idx: Pos, ch: char) {
/* Connected sides:
*
* 1
* 2 c 0
* 3
*
* #3210
* 0b____
*/
let bin_set = match ch {
'│' => set_and_join_vert(grid, idx),
'─' => set_and_join_horz(grid, idx),
_ => unreachable!(),
};
grid[idx].set_ch(bin_to_ch(bin_set));
}
2019-02-15 09:06:42 +02:00
pub fn create_box(grid: &mut CellBuffer, area: Area) {
2019-03-14 12:19:25 +02:00
if !is_valid_area!(area) {
return;
}
let upper_left = upper_left!(area);
let bottom_right = bottom_right!(area);
2019-02-15 09:06:42 +02:00
2019-03-14 12:19:25 +02:00
for x in get_x(upper_left)..get_x(bottom_right) {
grid[(x, get_y(upper_left))].set_ch(HORZ_BOUNDARY);
grid[(x, get_y(bottom_right))].set_ch(HORZ_BOUNDARY);
}
2019-02-15 09:06:42 +02:00
2019-03-14 12:19:25 +02:00
for y in get_y(upper_left)..get_y(bottom_right) {
grid[(get_x(upper_left), y)].set_ch(VERT_BOUNDARY);
grid[(get_x(bottom_right), y)].set_ch(VERT_BOUNDARY);
}
set_and_join_box(grid, upper_left, HORZ_BOUNDARY);
set_and_join_box(grid, set_x(upper_left, get_x(bottom_right)), HORZ_BOUNDARY);
set_and_join_box(grid, set_y(upper_left, get_y(bottom_right)), VERT_BOUNDARY);
set_and_join_box(grid, bottom_right, VERT_BOUNDARY);
2019-02-15 09:06:42 +02:00
}