Commit 885b56038f

885b56038f5dbb8b1ef71bf3e77db5d5a5f028c1

parent: 430c601ff2

Unregistered key

Ulyssa <git@ulyssa.dev> · 2023-06-15 05:42 UTC

Use terminal window focus to determine when a message has actually been seen (#94)

Layout: unified · split

Cargo.lock +1 −1
@@ -2137,7 +2137,7 @@ dependencies = [
2137[[package]] 2137[[package]]
2138name = "modalkit" 2138name = "modalkit"
2139version = "0.0.15" 2139version = "0.0.15"
2140source = "git+https://github.com/ulyssa/modalkit?rev=7979680#797968002c53287d907ca3ae823e3482265c94bb" 2140source = "git+https://github.com/ulyssa/modalkit?rev=f641162#f6411625caa1ddb764c91a3fc47e052cfdc405b5"
2141dependencies = [ 2141dependencies = [
2142 "anymap2", 2142 "anymap2",
2143 "arboard", 2143 "arboard",
Cargo.toml +1 −1
@@ -52,7 +52,7 @@ url = {version = "^2.2.2", features = ["serde"]}
52 52
53[dependencies.modalkit] 53[dependencies.modalkit]
54git = "https://github.com/ulyssa/modalkit" 54git = "https://github.com/ulyssa/modalkit"
55rev = "7979680" 55rev = "f641162"
56 56
57[dependencies.matrix-sdk] 57[dependencies.matrix-sdk]
58version = "0.6" 58version = "0.6"
src/main.rs +32 −7
@@ -22,7 +22,15 @@ use matrix_sdk::ruma::OwnedUserId;
22use modalkit::crossterm::{ 22use 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
114impl Application { 123impl 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}
src/windows/room/chat.rs +3 −1
@@ -848,7 +848,9 @@ impl<'a> StatefulWidget for Chat<'a> {
848 848
849 // Render the message scrollback. 849 // Render the message scrollback.
850 let scrollback_focused = state.focus.is_scrollback() && self.focused; 850 let scrollback_focused = state.focus.is_scrollback() && self.focused;
851 let scrollback = Scrollback::new(self.store).focus(scrollback_focused); 851 let scrollback = Scrollback::new(self.store)
852 .focus(scrollback_focused)
853 .room_focus(self.focused);
852 scrollback.render(scrollarea, buf, &mut state.scrollback); 854 scrollback.render(scrollarea, buf, &mut state.scrollback);
853 } 855 }
854} 856}
src/windows/room/scrollback.rs +13 −2
@@ -1206,13 +1206,21 @@ impl TerminalCursor for ScrollbackState {
1206} 1206}
1207 1207
1208pub struct Scrollback<'a> { 1208pub struct Scrollback<'a> {
1209 room_focused: bool,
1209 focused: bool, 1210 focused: bool,
1210 store: &'a mut ProgramStore, 1211 store: &'a mut ProgramStore,
1211} 1212}
1212 1213
1213impl<'a> Scrollback<'a> { 1214impl<'a> Scrollback<'a> {
1214 pub fn new(store: &'a mut ProgramStore) -> Self { 1215 pub fn new(store: &'a mut ProgramStore) -> Self {
1215 Scrollback { focused: false, store } 1216 Scrollback { room_focused: false, focused: false, store }
1217 }
1218
1219 /// Indicate whether the room window is currently focused, regardless of whether the scrollback
1220 /// also is.
1221 pub fn room_focus(mut self, focused: bool) -> Self {
1222 self.room_focused = focused;
1223 self
1216 } 1224 }
1217 1225
1218 /// Indicate whether the scrollback is currently focused. 1226 /// Indicate whether the scrollback is currently focused.
@@ -1307,7 +1315,10 @@ impl<'a> StatefulWidget for Scrollback<'a> {
1307 y += 1; 1315 y += 1;
1308 } 1316 }
1309 1317
1310 if settings.tunables.read_receipt_send && state.cursor.timestamp.is_none() { 1318 if self.room_focused &&
1319 settings.tunables.read_receipt_send &&
1320 state.cursor.timestamp.is_none()
1321 {
1311 // If the cursor is at the last message, then update the read marker. 1322 // If the cursor is at the last message, then update the read marker.
1312 info.read_till = info.messages.last_key_value().map(|(k, _)| k.1.clone()); 1323 info.read_till = info.messages.last_key_value().map(|(k, _)| k.1.clone());
1313 } 1324 }