1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use {
crate::{
action::Action,
app_context::AppContext,
components::component_traits::{Component, HandleFocus},
},
ratatui::{
layout::{Alignment, Constraint, Direction, Layout, Rect},
text::{Line, Span},
widgets::{block::Block, Borders, Paragraph, Wrap},
},
ratatui_image::picker::Picker,
std::{io, sync::Arc},
tokio::sync::mpsc,
};
/// `TitleBar` is a struct that represents a title bar.
/// It is responsible for managing the layout and rendering of the title bar.
pub struct TitleBar {
/// The application configuration.
app_context: Arc<AppContext>,
/// The name of the `TitleBar`.
name: String,
/// An unbounded sender that send action for processing.
command_tx: Option<mpsc::UnboundedSender<Action>>,
/// Indicates whether the `TitleBar` is focused or not.
focused: bool,
// The image of the `TitleBar`.
// _image_state: Box<dyn Protocol>, // Box<dyn StatefulProtocol>,
}
/// Implementation of `TitleBar` struct.
impl TitleBar {
pub fn new(app_context: Arc<AppContext>) -> Self {
let command_tx = None;
let name = "".to_string();
let focused = false;
let mut picker = Picker::new((8, 12));
picker.guess_protocol();
// let dyn_img = image::io::Reader::open(
// tgt_dir()
// .unwrap()
// .join("imgs")
// .join("logo.png")
// .to_string_lossy()
// .to_string(),
// )
// .unwrap()
// .decode()
// .unwrap();
// let image = picker.new_resize_protocol(dyn_img);
// let image_state: Box<dyn StatefulProtocol> = image.into();
// let image_state = picker
// .new_protocol(dyn_img.clone(), Rect::new(0, 0, 30, 30), Resize::Fit(None))
// .unwrap();
TitleBar {
app_context,
command_tx,
name,
focused,
// _image_state: image_state,
}
}
/// Set the name of the `TitleBar`.
///
/// # Arguments
/// * `name` - The name of the `TitleBar`.
///
/// # Returns
/// * `Self` - The modified instance of the `TitleBar`.
pub fn with_name(mut self, name: impl AsRef<str>) -> Self {
self.name = name.as_ref().to_string();
self
}
}
/// Implement the `HandleFocus` trait for the `TitleBar` struct.
/// This trait allows the `TitleBar` to be focused or unfocused.
impl HandleFocus for TitleBar {
/// Set the `focused` flag for the `TitleBar`.
fn focus(&mut self) {
self.focused = true;
}
/// Set the `focused` flag for the `TitleBar`.
fn unfocus(&mut self) {
self.focused = false;
}
}
/// Implement the `Component` trait for the `TitleBar` struct.
impl Component for TitleBar {
fn register_action_handler(&mut self, tx: mpsc::UnboundedSender<Action>) -> io::Result<()> {
self.command_tx = Some(tx);
Ok(())
}
fn draw(&mut self, frame: &mut ratatui::Frame<'_>, area: Rect) -> io::Result<()> {
let chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(0), Constraint::Percentage(100)].as_ref())
.split(area);
let name: Vec<char> = self.name.chars().collect::<Vec<char>>();
// Span::raw(" - A TUI for Telegram"),
let text = vec![Line::from(vec![
Span::styled(
name[0].to_string(),
self.app_context.style_title_bar_title1(),
),
Span::styled(
name[1].to_string(),
self.app_context.style_title_bar_title2(),
),
Span::styled(
name[2].to_string(),
self.app_context.style_title_bar_title3(),
),
Span::styled(" - ", self.app_context.style_title_bar_title1()),
Span::styled("A", self.app_context.style_title_bar_title2()),
Span::styled(" T", self.app_context.style_title_bar_title3()),
Span::styled("U", self.app_context.style_title_bar_title1()),
Span::styled("I", self.app_context.style_title_bar_title2()),
Span::styled(" f", self.app_context.style_title_bar_title3()),
Span::styled("o", self.app_context.style_title_bar_title1()),
Span::styled("r", self.app_context.style_title_bar_title2()),
Span::styled(" T", self.app_context.style_title_bar_title3()),
Span::styled("e", self.app_context.style_title_bar_title1()),
Span::styled("l", self.app_context.style_title_bar_title2()),
Span::styled("e", self.app_context.style_title_bar_title3()),
Span::styled("g", self.app_context.style_title_bar_title1()),
Span::styled("r", self.app_context.style_title_bar_title2()),
Span::styled("a", self.app_context.style_title_bar_title3()),
Span::styled("m", self.app_context.style_title_bar_title1()),
])];
let block = Block::new().borders(Borders::ALL);
let paragraph = Paragraph::new(text)
.block(block.clone())
.style(self.app_context.style_title_bar())
.alignment(Alignment::Center)
.wrap(Wrap { trim: true });
// let statefull_image = StatefulImage::new(None);
// frame.render_stateful_widget(statefull_image, chunks[0], &mut self.image_state);
// let image = Image::new(self.image_state.as_ref());
// frame.render_widget(image, chunks[0]);
frame.render_widget(paragraph, chunks[1]);
Ok(())
}
}