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 = [
21372137[[package]]
21382138name = "modalkit"
21392139version = "0.0.15"
2140source = "git+https://github.com/ulyssa/modalkit?rev=7979680#797968002c53287d907ca3ae823e3482265c94bb"
2140source = "git+https://github.com/ulyssa/modalkit?rev=f641162#f6411625caa1ddb764c91a3fc47e052cfdc405b5"
21412141dependencies = [
21422142 "anymap2",
21432143 "arboard",
Cargo.toml +1 −1
@@ -52,7 +52,7 @@ url = {version = "^2.2.2", features = ["serde"]}
5252
5353[dependencies.modalkit]
5454git = "https://github.com/ulyssa/modalkit"
55rev = "7979680"
55rev = "f641162"
5656
5757[dependencies.matrix-sdk]
5858version = "0.6"
src/main.rs +32 −7
@@ -22,7 +22,15 @@ use matrix_sdk::ruma::OwnedUserId;
2222use modalkit::crossterm::{
2323 self,
2424 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 },
2634 execute,
2735 terminal::{EnterAlternateScreen, LeaveAlternateScreen, SetTitle},
2836};
@@ -109,6 +117,7 @@ struct Application {
109117 bindings: KeyManager<TerminalKey, ProgramAction, RepeatType, ProgramContext>,
110118 actstack: VecDeque<(ProgramAction, ProgramContext)>,
111119 screen: ScreenState<IambWindow, IambInfo>,
120 focused: bool,
112121}
113122
114123impl Application {
@@ -120,6 +129,7 @@ impl Application {
120129 crossterm::terminal::enable_raw_mode()?;
121130 crossterm::execute!(stdout, EnterAlternateScreen)?;
122131 crossterm::execute!(stdout, EnableBracketedPaste)?;
132 crossterm::execute!(stdout, EnableFocusChange)?;
123133
124134 let title = format!("iamb ({})", settings.profile.user_id);
125135 crossterm::execute!(stdout, SetTitle(title))?;
@@ -154,11 +164,13 @@ impl Application {
154164 bindings,
155165 actstack,
156166 screen,
167 focused: true,
157168 })
158169 }
159170
160171 fn redraw(&mut self, full: bool, store: &mut ProgramStore) -> Result<(), std::io::Error> {
161172 let bindings = &mut self.bindings;
173 let focused = self.focused;
162174 let sstate = &mut self.screen;
163175 let term = &mut self.terminal;
164176
@@ -176,7 +188,11 @@ impl Application {
176188 // Don't show terminal cursor when we show a dialog.
177189 let hide_cursor = !dialogstr.is_empty();
178190
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);
180196 f.render_stateful_widget(screen, area, sstate);
181197
182198 if hide_cursor {
@@ -212,8 +228,11 @@ impl Application {
212228 Event::Mouse(_) => {
213229 // Do nothing for now.
214230 },
215 Event::FocusGained | Event::FocusLost => {
216 // Do nothing for now.
231 Event::FocusGained => {
232 self.focused = true;
233 },
234 Event::FocusLost => {
235 self.focused = false;
217236 },
218237 Event::Resize(_, _) => {
219238 // We'll redraw for the new size next time step() is called.
@@ -532,13 +551,18 @@ async fn run(settings: ApplicationSettings) -> IambResult<()> {
532551
533552 login(worker, &settings).await.unwrap_or_else(print_exit);
534553
535 // Make sure panics clean up the terminal properly.
536 let orig_hook = std::panic::take_hook();
537 std::panic::set_hook(Box::new(move |panic_info| {
554 fn restore_tty() {
538555 let _ = crossterm::terminal::disable_raw_mode();
539556 let _ = crossterm::execute!(stdout(), DisableBracketedPaste);
557 let _ = crossterm::execute!(stdout(), DisableFocusChange);
540558 let _ = crossterm::execute!(stdout(), LeaveAlternateScreen);
541559 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();
542566 orig_hook(panic_info);
543567 process::exit(1);
544568 }));
@@ -547,6 +571,7 @@ async fn run(settings: ApplicationSettings) -> IambResult<()> {
547571
548572 // We can now run the application.
549573 application.run().await?;
574 restore_tty();
550575
551576 Ok(())
552577}
src/windows/room/chat.rs +3 −1
@@ -848,7 +848,9 @@ impl<'a> StatefulWidget for Chat<'a> {
848848
849849 // Render the message scrollback.
850850 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);
852854 scrollback.render(scrollarea, buf, &mut state.scrollback);
853855 }
854856}
src/windows/room/scrollback.rs +13 −2
@@ -1206,13 +1206,21 @@ impl TerminalCursor for ScrollbackState {
12061206}
12071207
12081208pub struct Scrollback<'a> {
1209 room_focused: bool,
12091210 focused: bool,
12101211 store: &'a mut ProgramStore,
12111212}
12121213
12131214impl<'a> Scrollback<'a> {
12141215 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
12161224 }
12171225
12181226 /// Indicate whether the scrollback is currently focused.
@@ -1307,7 +1315,10 @@ impl<'a> StatefulWidget for Scrollback<'a> {
13071315 y += 1;
13081316 }
13091317
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 {
13111322 // If the cursor is at the last message, then update the read marker.
13121323 info.read_till = info.messages.last_key_value().map(|(k, _)| k.1.clone());
13131324 }