summaryrefslogtreecommitdiffstats
path: root/src/popups/msg.rs
blob: 6da957ce886bfecc3152b6b364100549e03ff006 (plain)
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
use crate::components::{
	visibility_blocking, CommandBlocking, CommandInfo, Component,
	DrawableComponent, EventState,
};
use crate::{
	app::Environment,
	keys::{key_match, SharedKeyConfig},
	strings, ui,
};
use crossterm::event::Event;
use ratatui::{
	layout::{Alignment, Rect},
	text::Span,
	widgets::{Block, BorderType, Borders, Clear, Paragraph, Wrap},
	Frame,
};
use ui::style::SharedTheme;

pub struct MsgPopup {
	title: String,
	msg: String,
	visible: bool,
	theme: SharedTheme,
	key_config: SharedKeyConfig,
}

use anyhow::Result;

impl DrawableComponent for MsgPopup {
	fn draw(&self, f: &mut Frame, _rect: Rect) -> Result<()> {
		if !self.visible {
			return Ok(());
		}

		// determine the maximum width of text block
		let lens = self
			.msg
			.split('\n')
			.map(str::len)
			.collect::<Vec<usize>>();
		let mut max = lens.iter().max().expect("max") + 2;
		if max > std::u16::MAX as usize {
			max = std::u16::MAX as usize;
		}
		let mut width = u16::try_from(max)
			.expect("can't fail due to check above");
		// dont overflow screen, and dont get too narrow
		if width > f.size().width {
			width = f.size().width;
		} else if width < 60 {
			width = 60;
		}

		let area = ui::centered_rect_absolute(width, 25, f.size());
		f.render_widget(Clear, area);
		f.render_widget(
			Paragraph::new(self.msg.clone())
				.block(
					Block::default()
						.title(Span::styled(
							self.title.as_str(),
							self.theme.text_danger(),
						))
						.borders(Borders::ALL)
						.border_type(BorderType::Thick),
				)
				.alignment(Alignment::Left)
				.wrap(Wrap { trim: true }),
			area,
		);

		Ok(())
	}
}

impl Component for MsgPopup {
	fn commands(
		&self,
		out: &mut Vec<CommandInfo>,
		_force_all: bool,
	) -> CommandBlocking {
		out.push(CommandInfo::new(
			strings::commands::close_msg(&self.key_config),
			true,
			self.visible,
		));

		visibility_blocking(self)
	}

	fn event(&mut self, ev: &Event) -> Result<EventState> {
		if self.visible {
			if let Event::Key(e) = ev {
				if key_match(e, self.key_config.keys.enter) {
					self.hide();
				}
			}
			Ok(EventState::Consumed)
		} else {
			Ok(EventState::NotConsumed)
		}
	}

	fn is_visible(&self) -> bool {
		self.visible
	}

	fn hide(&mut self) {
		self.visible = false;
	}

	fn show(&mut self) -> Result<()> {
		self.visible = true;

		Ok(())
	}
}

impl MsgPopup {
	pub fn new(env: &Environment) -> Self {
		Self {
			title: String::new(),
			msg: String::new(),
			visible: false,
			theme: env.theme.clone(),
			key_config: env.key_config.clone(),
		}
	}

	///
	pub fn show_error(&mut self, msg: &str) -> Result<()> {
		self.title = strings::msg_title_error(&self.key_config);
		self.msg = msg.to_string();
		self.show()?;

		Ok(())
	}

	///
	pub fn show_info(&mut self, msg: &str) -> Result<()> {
		self.title = strings::msg_title_info(&self.key_config);
		self.msg = msg.to_string();
		self.show()?;

		Ok(())
	}
}