2018-07-11 17:07:51 +03:00
|
|
|
/*
|
2020-02-04 15:52:12 +02:00
|
|
|
* meli
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2023-07-01 16:20:59 +03:00
|
|
|
//! Components visual and logical separations of application interfaces.
|
|
|
|
///
|
|
|
|
/// They can draw on the terminal and receive events, but also do other stuff
|
|
|
|
/// as well.
|
|
|
|
/// For an example, see the [`notifications`] module.
|
|
|
|
/// See also the [`Component`] trait for more details.
|
2023-06-16 20:20:12 +03:00
|
|
|
use smallvec::SmallVec;
|
|
|
|
|
2018-07-20 12:15:06 +03:00
|
|
|
use super::*;
|
2023-04-30 19:39:41 +03:00
|
|
|
use crate::{
|
|
|
|
melib::text_processing::{TextProcessing, Truncate},
|
|
|
|
terminal::boundaries::*,
|
|
|
|
};
|
2018-08-06 16:53:23 +03:00
|
|
|
|
2018-07-11 17:07:51 +03:00
|
|
|
pub mod mail;
|
2019-06-18 21:13:58 +03:00
|
|
|
pub use crate::mail::*;
|
2018-08-06 16:53:23 +03:00
|
|
|
|
2018-07-17 17:16:16 +03:00
|
|
|
pub mod notifications;
|
2018-07-11 17:07:51 +03:00
|
|
|
|
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;
|
2019-06-18 21:13:58 +03:00
|
|
|
pub use crate::contacts::*;
|
2019-02-15 09:06:42 +02:00
|
|
|
|
2022-11-25 15:15:02 +02:00
|
|
|
pub mod mailbox_management;
|
|
|
|
pub use self::mailbox_management::*;
|
|
|
|
|
2020-06-12 01:37:57 +03:00
|
|
|
#[cfg(feature = "svgscreenshot")]
|
|
|
|
pub mod svg;
|
|
|
|
|
2023-04-30 19:39:41 +03:00
|
|
|
use std::{
|
|
|
|
fmt,
|
|
|
|
fmt::{Debug, Display},
|
|
|
|
};
|
2018-09-04 01:49:29 +03:00
|
|
|
|
2020-08-17 15:53:59 +03:00
|
|
|
use indexmap::IndexMap;
|
2018-09-04 01:49:29 +03:00
|
|
|
use uuid::Uuid;
|
2018-08-11 18:00:21 +03:00
|
|
|
|
2023-06-13 17:45:48 +03:00
|
|
|
#[derive(Clone, Copy, Eq, Deserialize, Hash, Ord, PartialOrd, PartialEq, Serialize)]
|
|
|
|
#[repr(transparent)]
|
|
|
|
pub struct ComponentId(Uuid);
|
|
|
|
|
|
|
|
impl AsRef<Uuid> for ComponentId {
|
|
|
|
fn as_ref(&self) -> &Uuid {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for ComponentId {
|
|
|
|
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
std::fmt::Display::fmt(&self.0.as_simple(), fmt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for ComponentId {
|
|
|
|
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
std::fmt::Display::fmt(&self.0.as_simple(), fmt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ComponentId {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self(Uuid::new_v4())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::LowerHex for ComponentId {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
std::fmt::LowerHex::fmt(self.0.as_hyphenated(), f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::UpperHex for ComponentId {
|
|
|
|
#[inline]
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
std::fmt::UpperHex::fmt(self.0.as_hyphenated(), f)
|
|
|
|
}
|
|
|
|
}
|
2018-07-11 17:07:51 +03:00
|
|
|
|
2020-08-17 15:53:59 +03:00
|
|
|
pub type ShortcutMap = IndexMap<&'static str, Key>;
|
|
|
|
pub type ShortcutMaps = IndexMap<&'static str, ShortcutMap>;
|
2019-03-02 19:25:11 +02:00
|
|
|
|
2020-10-21 13:13:46 +03:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub enum PageMovement {
|
|
|
|
Up(usize),
|
|
|
|
Right(usize),
|
|
|
|
Left(usize),
|
|
|
|
Down(usize),
|
|
|
|
PageUp(usize),
|
|
|
|
PageDown(usize),
|
|
|
|
Home,
|
|
|
|
End,
|
|
|
|
}
|
|
|
|
|
2022-09-11 15:19:40 +03:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
2021-01-07 20:26:17 +02:00
|
|
|
pub struct ScrollContext {
|
|
|
|
shown_lines: usize,
|
|
|
|
total_lines: usize,
|
|
|
|
has_more_lines: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub enum ScrollUpdate {
|
|
|
|
End(ComponentId),
|
|
|
|
Update {
|
|
|
|
id: ComponentId,
|
|
|
|
context: ScrollContext,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-07-11 18:58:57 +03:00
|
|
|
/// Types implementing this Trait can draw on the terminal and receive events.
|
2023-04-30 19:39:41 +03:00
|
|
|
/// If a type wants to skip drawing if it has not changed anything, it can hold
|
2023-06-13 17:45:48 +03:00
|
|
|
/// some flag in its fields (eg `self.dirty = false`) and act upon that in their
|
|
|
|
/// [`draw`](Component::draw) implementation.
|
2020-02-19 16:57:37 +02:00
|
|
|
pub trait Component: Display + Debug + Send + Sync {
|
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;
|
2019-09-25 23:14:24 +03:00
|
|
|
fn is_dirty(&self) -> bool;
|
2023-06-13 17:45:48 +03:00
|
|
|
/// If the component is meant to be currently visible to the user.
|
2019-03-26 16:59:35 +02:00
|
|
|
fn is_visible(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2023-06-13 17:45:48 +03:00
|
|
|
|
|
|
|
/// If the component can quit right away without any unsaved or ongoing
|
|
|
|
/// operations.
|
2019-10-06 11:28:12 +03:00
|
|
|
fn can_quit_cleanly(&mut self, _context: &Context) -> bool {
|
2019-09-27 13:14:16 +03:00
|
|
|
true
|
|
|
|
}
|
2023-06-13 17:45:48 +03:00
|
|
|
|
2019-12-14 18:50:05 +02:00
|
|
|
fn set_dirty(&mut self, value: bool);
|
2023-06-13 17:45:48 +03:00
|
|
|
|
2019-09-07 22:07:13 +03:00
|
|
|
fn kill(&mut self, _id: ComponentId, _context: &mut Context) {}
|
2023-06-13 17:45:48 +03:00
|
|
|
|
2019-04-10 22:01:02 +03:00
|
|
|
fn id(&self) -> ComponentId;
|
2019-03-02 19:25:11 +02:00
|
|
|
|
2023-06-13 17:45:48 +03:00
|
|
|
fn shortcuts(&self, _context: &Context) -> ShortcutMaps {
|
2019-03-14 12:19:25 +02:00
|
|
|
Default::default()
|
|
|
|
}
|
2019-09-15 09:41:52 +03:00
|
|
|
|
2023-06-13 17:45:48 +03:00
|
|
|
/// Get status message for the status line.
|
|
|
|
fn status(&self, _context: &Context) -> String {
|
2020-02-08 23:18:02 +02:00
|
|
|
String::new()
|
2019-09-15 09:41:52 +03:00
|
|
|
}
|
2023-06-14 12:24:20 +03:00
|
|
|
|
|
|
|
fn attributes(&self) -> &'static ComponentAttr {
|
|
|
|
&ComponentAttr::DEFAULT
|
|
|
|
}
|
|
|
|
|
|
|
|
fn children(&self) -> IndexMap<ComponentId, &dyn Component> {
|
|
|
|
IndexMap::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn children_mut(&mut self) -> IndexMap<ComponentId, &mut dyn Component> {
|
|
|
|
IndexMap::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn realize(&self, parent: Option<ComponentId>, context: &mut Context) {
|
2023-06-16 20:20:12 +03:00
|
|
|
// log::trace!("Realizing id {} w/ parent {:?}", self.id(), &parent);
|
2023-06-14 12:24:20 +03:00
|
|
|
context.realized.insert(self.id(), parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unrealize(&self, context: &mut Context) {
|
2023-06-16 20:20:12 +03:00
|
|
|
// log::trace!("Unrealizing id {}", self.id());
|
2023-06-14 12:24:20 +03:00
|
|
|
context.unrealized.insert(self.id());
|
|
|
|
context
|
|
|
|
.replies
|
|
|
|
.push_back(UIEvent::ComponentUnrealize(self.id()));
|
|
|
|
}
|
2018-07-11 17:07:51 +03:00
|
|
|
}
|
2023-06-13 19:11:59 +03:00
|
|
|
|
|
|
|
impl Component for Box<dyn Component> {
|
|
|
|
fn draw(&mut self, grid: &mut CellBuffer, area: Area, context: &mut Context) {
|
|
|
|
(**self).draw(grid, area, context)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process_event(&mut self, event: &mut UIEvent, context: &mut Context) -> bool {
|
|
|
|
(**self).process_event(event, context)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_dirty(&self) -> bool {
|
|
|
|
(**self).is_dirty()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_visible(&self) -> bool {
|
|
|
|
(**self).is_visible()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn can_quit_cleanly(&mut self, context: &Context) -> bool {
|
|
|
|
(**self).can_quit_cleanly(context)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_dirty(&mut self, value: bool) {
|
|
|
|
(**self).set_dirty(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn kill(&mut self, id: ComponentId, context: &mut Context) {
|
|
|
|
(**self).kill(id, context)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn id(&self) -> ComponentId {
|
|
|
|
(**self).id()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn shortcuts(&self, context: &Context) -> ShortcutMaps {
|
|
|
|
(**self).shortcuts(context)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn status(&self, context: &Context) -> String {
|
|
|
|
(**self).status(context)
|
|
|
|
}
|
2023-06-14 12:24:20 +03:00
|
|
|
|
|
|
|
fn attributes(&self) -> &'static ComponentAttr {
|
|
|
|
(**self).attributes()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn children(&self) -> IndexMap<ComponentId, &dyn Component> {
|
|
|
|
(**self).children()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn children_mut(&mut self) -> IndexMap<ComponentId, &mut dyn Component> {
|
|
|
|
(**self).children_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn realize(&self, parent: Option<ComponentId>, context: &mut Context) {
|
|
|
|
(**self).realize(parent, context)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unrealize(&self, context: &mut Context) {
|
|
|
|
(**self).unrealize(context)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bitflags::bitflags! {
|
|
|
|
/// Attributes of a [`Component`] widget.
|
|
|
|
///
|
|
|
|
/// `ComponentAttr::DEFAULT` represents no attribute.
|
|
|
|
pub struct ComponentAttr: u8 {
|
|
|
|
/// Nothing special going on.
|
|
|
|
const DEFAULT = 0;
|
|
|
|
const HAS_ANIMATIONS = 1;
|
|
|
|
const CONTAINER = 1 << 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ComponentAttr {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::DEFAULT
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Eq, PartialEq, Debug, Clone)]
|
|
|
|
pub struct ComponentPath {
|
|
|
|
id: ComponentId,
|
|
|
|
tail: SmallVec<[ComponentId; 8]>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ComponentPath {
|
|
|
|
pub fn new(id: ComponentId) -> Self {
|
|
|
|
Self {
|
|
|
|
id,
|
|
|
|
tail: SmallVec::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push_front(&mut self, id: ComponentId) {
|
|
|
|
self.tail.insert(0, self.id);
|
|
|
|
self.id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push_back(&mut self, id: ComponentId) {
|
|
|
|
self.tail.push(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resolve<'c>(&self, root: &'c dyn Component) -> Option<&'c dyn Component> {
|
|
|
|
let mut cursor = root;
|
|
|
|
for id in self.tail.iter().rev().chain(std::iter::once(&self.id)) {
|
2023-06-16 20:20:12 +03:00
|
|
|
// log::trace!("resolve cursor = {} next id is {}", cursor.id(), &id);
|
2023-06-14 12:24:20 +03:00
|
|
|
if *id == cursor.id() {
|
2023-06-16 20:20:12 +03:00
|
|
|
// log::trace!("continue;");
|
2023-06-14 12:24:20 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cursor = cursor.children().remove(id)?;
|
|
|
|
}
|
|
|
|
Some(cursor)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn parent(&self) -> Option<&ComponentId> {
|
|
|
|
self.tail.first()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn root(&self) -> Option<&ComponentId> {
|
|
|
|
self.tail.last()
|
|
|
|
}
|
2023-06-13 19:11:59 +03:00
|
|
|
}
|