| @@ -22,7 +22,15 @@ use matrix_sdk::ruma::OwnedUserId; |
| 22 | use modalkit::crossterm::{ |
22 | use modalkit::crossterm::{ |
| 23 | self, |
23 | self, |
| 24 | cursor::Show as CursorShow, |
24 | cursor::Show as CursorShow, |
| 25 | event::{poll, read, DisableBracketedPaste, EnableBracketedPaste, Event}, |
25 | event::{ |
| |
26 | poll, |
| |
27 | read, |
| |
28 | DisableBracketedPaste, |
| |
29 | DisableFocusChange, |
| |
30 | EnableBracketedPaste, |
| |
31 | EnableFocusChange, |
| |
32 | Event, |
| |
33 | }, |
| 26 | execute, |
34 | execute, |
| 27 | terminal::{EnterAlternateScreen, LeaveAlternateScreen, SetTitle}, |
35 | terminal::{EnterAlternateScreen, LeaveAlternateScreen, SetTitle}, |
| 28 | }; |
36 | }; |
| @@ -109,6 +117,7 @@ struct Application { |
| 109 | bindings: KeyManager<TerminalKey, ProgramAction, RepeatType, ProgramContext>, |
117 | bindings: KeyManager<TerminalKey, ProgramAction, RepeatType, ProgramContext>, |
| 110 | actstack: VecDeque<(ProgramAction, ProgramContext)>, |
118 | actstack: VecDeque<(ProgramAction, ProgramContext)>, |
| 111 | screen: ScreenState<IambWindow, IambInfo>, |
119 | screen: ScreenState<IambWindow, IambInfo>, |
| |
120 | focused: bool, |
| 112 | } |
121 | } |
| 113 | |
122 | |
| 114 | impl Application { |
123 | impl Application { |
| @@ -120,6 +129,7 @@ impl Application { |
| 120 | crossterm::terminal::enable_raw_mode()?; |
129 | crossterm::terminal::enable_raw_mode()?; |
| 121 | crossterm::execute!(stdout, EnterAlternateScreen)?; |
130 | crossterm::execute!(stdout, EnterAlternateScreen)?; |
| 122 | crossterm::execute!(stdout, EnableBracketedPaste)?; |
131 | crossterm::execute!(stdout, EnableBracketedPaste)?; |
| |
132 | crossterm::execute!(stdout, EnableFocusChange)?; |
| 123 | |
133 | |
| 124 | let title = format!("iamb ({})", settings.profile.user_id); |
134 | let title = format!("iamb ({})", settings.profile.user_id); |
| 125 | crossterm::execute!(stdout, SetTitle(title))?; |
135 | crossterm::execute!(stdout, SetTitle(title))?; |
| @@ -154,11 +164,13 @@ impl Application { |
| 154 | bindings, |
164 | bindings, |
| 155 | actstack, |
165 | actstack, |
| 156 | screen, |
166 | screen, |
| |
167 | focused: true, |
| 157 | }) |
168 | }) |
| 158 | } |
169 | } |
| 159 | |
170 | |
| 160 | fn redraw(&mut self, full: bool, store: &mut ProgramStore) -> Result<(), std::io::Error> { |
171 | fn redraw(&mut self, full: bool, store: &mut ProgramStore) -> Result<(), std::io::Error> { |
| 161 | let bindings = &mut self.bindings; |
172 | let bindings = &mut self.bindings; |
| |
173 | let focused = self.focused; |
| 162 | let sstate = &mut self.screen; |
174 | let sstate = &mut self.screen; |
| 163 | let term = &mut self.terminal; |
175 | let term = &mut self.terminal; |
| 164 | |
176 | |
| @@ -176,7 +188,11 @@ impl Application { |
| 176 | // Don't show terminal cursor when we show a dialog. |
188 | // Don't show terminal cursor when we show a dialog. |
| 177 | let hide_cursor = !dialogstr.is_empty(); |
189 | let hide_cursor = !dialogstr.is_empty(); |
| 178 | |
190 | |
| 179 | let screen = Screen::new(store).show_dialog(dialogstr).show_mode(modestr).borders(true); |
191 | let screen = Screen::new(store) |
| |
192 | .show_dialog(dialogstr) |
| |
193 | .show_mode(modestr) |
| |
194 | .borders(true) |
| |
195 | .focus(focused); |
| 180 | f.render_stateful_widget(screen, area, sstate); |
196 | f.render_stateful_widget(screen, area, sstate); |
| 181 | |
197 | |
| 182 | if hide_cursor { |
198 | if hide_cursor { |
| @@ -212,8 +228,11 @@ impl Application { |
| 212 | Event::Mouse(_) => { |
228 | Event::Mouse(_) => { |
| 213 | // Do nothing for now. |
229 | // Do nothing for now. |
| 214 | }, |
230 | }, |
| 215 | Event::FocusGained | Event::FocusLost => { |
231 | Event::FocusGained => { |
| 216 | // Do nothing for now. |
232 | self.focused = true; |
| |
233 | }, |
| |
234 | Event::FocusLost => { |
| |
235 | self.focused = false; |
| 217 | }, |
236 | }, |
| 218 | Event::Resize(_, _) => { |
237 | Event::Resize(_, _) => { |
| 219 | // We'll redraw for the new size next time step() is called. |
238 | // We'll redraw for the new size next time step() is called. |
| @@ -532,13 +551,18 @@ async fn run(settings: ApplicationSettings) -> IambResult<()> { |
| 532 | |
551 | |
| 533 | login(worker, &settings).await.unwrap_or_else(print_exit); |
552 | login(worker, &settings).await.unwrap_or_else(print_exit); |
| 534 | |
553 | |
| 535 | // Make sure panics clean up the terminal properly. |
554 | fn restore_tty() { |
| 536 | let orig_hook = std::panic::take_hook(); |
| |
| 537 | std::panic::set_hook(Box::new(move |panic_info| { |
| |
| 538 | let _ = crossterm::terminal::disable_raw_mode(); |
555 | let _ = crossterm::terminal::disable_raw_mode(); |
| 539 | let _ = crossterm::execute!(stdout(), DisableBracketedPaste); |
556 | let _ = crossterm::execute!(stdout(), DisableBracketedPaste); |
| |
557 | let _ = crossterm::execute!(stdout(), DisableFocusChange); |
| 540 | let _ = crossterm::execute!(stdout(), LeaveAlternateScreen); |
558 | let _ = crossterm::execute!(stdout(), LeaveAlternateScreen); |
| 541 | let _ = crossterm::execute!(stdout(), CursorShow); |
559 | let _ = crossterm::execute!(stdout(), CursorShow); |
| |
560 | } |
| |
561 | |
| |
562 | // Make sure panics clean up the terminal properly. |
| |
563 | let orig_hook = std::panic::take_hook(); |
| |
564 | std::panic::set_hook(Box::new(move |panic_info| { |
| |
565 | restore_tty(); |
| 542 | orig_hook(panic_info); |
566 | orig_hook(panic_info); |
| 543 | process::exit(1); |
567 | process::exit(1); |
| 544 | })); |
568 | })); |
| @@ -547,6 +571,7 @@ async fn run(settings: ApplicationSettings) -> IambResult<()> { |
| 547 | |
571 | |
| 548 | // We can now run the application. |
572 | // We can now run the application. |
| 549 | application.run().await?; |
573 | application.run().await?; |
| |
574 | restore_tty(); |
| 550 | |
575 | |
| 551 | Ok(()) |
576 | Ok(()) |
| 552 | } |
577 | } |