1use crate::color::Color;
17use crate::cursor::Cursor;
18use crate::keyboard::KeyboardEnhancements;
19use crate::model::Cmd;
20use crate::mouse::MouseMsg;
21use std::sync::Arc;
22
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
25pub enum MouseMode {
26 #[default]
28 MouseModeNone = 0,
29 MouseModeCellMotion = 1,
31 MouseModeAllMotion = 2,
33}
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
37pub enum ProgressBarState {
38 ProgressBarNone = 0,
40 ProgressBarDefault,
42 ProgressBarError,
44 ProgressBarIndeterminate,
46 ProgressBarWarning,
48}
49
50impl ProgressBarState {
51 pub fn to_string(&self) -> &'static str {
53 match self {
54 ProgressBarState::ProgressBarNone => "None",
55 ProgressBarState::ProgressBarDefault => "Default",
56 ProgressBarState::ProgressBarError => "Error",
57 ProgressBarState::ProgressBarIndeterminate => "Indeterminate",
58 ProgressBarState::ProgressBarWarning => "Warning",
59 }
60 }
61}
62
63#[derive(Debug, Clone, Copy, PartialEq, Eq)]
69pub struct ProgressBar {
70 pub state: ProgressBarState,
75 pub value: usize,
78}
79
80pub fn new_progress_bar(state: ProgressBarState, value: usize) -> ProgressBar {
84 ProgressBar {
85 state,
86 value: value.clamp(0, 100),
87 }
88}
89
90pub type OnMouseFn = Arc<dyn Fn(MouseMsg) -> Cmd + Send + Sync>;
96
97pub struct View {
99 pub content: String,
101 pub on_mouse: Option<OnMouseFn>,
103 pub cursor: Option<Cursor>,
105 pub background_color: Option<Color>,
107 pub foreground_color: Option<Color>,
109 pub window_title: String,
111 pub alt_screen: bool,
113 pub report_focus: bool,
115 pub disable_bracketed_paste_mode: bool,
117 pub mouse_mode: MouseMode,
119 pub keyboard_enhancements: KeyboardEnhancements,
121 pub progress_bar: Option<ProgressBar>,
123}
124
125impl Clone for View {
126 fn clone(&self) -> View {
127 View {
128 content: self.content.clone(),
129 on_mouse: self.on_mouse.clone(),
130 cursor: self.cursor.clone(),
131 background_color: self.background_color,
132 foreground_color: self.foreground_color,
133 window_title: self.window_title.clone(),
134 alt_screen: self.alt_screen,
135 report_focus: self.report_focus,
136 disable_bracketed_paste_mode: self.disable_bracketed_paste_mode,
137 mouse_mode: self.mouse_mode,
138 keyboard_enhancements: self.keyboard_enhancements,
139 progress_bar: self.progress_bar,
140 }
141 }
142}
143
144impl PartialEq for View {
145 fn eq(&self, other: &View) -> bool {
146 self.content == other.content
147 && self.cursor == other.cursor
148 && self.background_color == other.background_color
149 && self.foreground_color == other.foreground_color
150 && self.window_title == other.window_title
151 && self.alt_screen == other.alt_screen
152 && self.report_focus == other.report_focus
153 && self.disable_bracketed_paste_mode == other.disable_bracketed_paste_mode
154 && self.mouse_mode == other.mouse_mode
155 && self.keyboard_enhancements == other.keyboard_enhancements
156 && self.progress_bar == other.progress_bar
157 }
158}
159
160impl Default for View {
161 fn default() -> Self {
162 Self {
163 content: String::new(),
164 on_mouse: None,
165 cursor: None,
166 background_color: None,
167 foreground_color: None,
168 window_title: String::new(),
169 alt_screen: false,
170 report_focus: false,
171 disable_bracketed_paste_mode: false,
172 mouse_mode: MouseMode::MouseModeNone,
173 keyboard_enhancements: KeyboardEnhancements::default(),
174 progress_bar: None,
175 }
176 }
177}
178
179impl View {
180 pub fn new(content: &str) -> Self {
188 let mut v = Self::default();
189 v.set_content(content);
190 v
191 }
192
193 pub fn set_content(&mut self, s: &str) {
195 self.content = s.to_string();
196 }
197}
198
199impl View {
200 pub fn equals(&self, o: &View) -> bool {
203 self.content == o.content
204 && self.alt_screen == o.alt_screen
205 && self.report_focus == o.report_focus
206 && self.disable_bracketed_paste_mode == o.disable_bracketed_paste_mode
207 && self.window_title == o.window_title
208 && self.mouse_mode == o.mouse_mode
209 && self.background_color == o.background_color
210 && self.foreground_color == o.foreground_color
211 && self.keyboard_enhancements == o.keyboard_enhancements
212 && self.cursor == o.cursor
213 && self.progress_bar == o.progress_bar
214 }
215}