From 9a1adfb287ecc5ff6b97b2760d4ab5e480835dc5 Mon Sep 17 00:00:00 2001 From: Andrew Collins Date: Thu, 1 Aug 2024 13:37:21 +1000 Subject: [PATCH] Allow notifications on open room if terminal not focused (#281) --- src/base.rs | 4 ++++ src/main.rs | 4 ++++ src/notifications.rs | 17 +++++++++++++---- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/base.rs b/src/base.rs index d334867..8be9a4e 100644 --- a/src/base.rs +++ b/src/base.rs @@ -1319,6 +1319,9 @@ pub struct ChatStore { /// Whether to ring the terminal bell on the next redraw. pub ring_bell: bool, + + /// Whether the application is currently focused + pub focused: bool, } impl ChatStore { @@ -1341,6 +1344,7 @@ impl ChatStore { sync_info: Default::default(), draw_curr: None, ring_bell: false, + focused: true, } } diff --git a/src/main.rs b/src/main.rs index 2311a3c..4105236 100644 --- a/src/main.rs +++ b/src/main.rs @@ -355,9 +355,13 @@ impl Application { // Do nothing for now. }, Event::FocusGained => { + let mut store = self.store.lock().await; + store.application.focused = true; self.focused = true; }, Event::FocusLost => { + let mut store = self.store.lock().await; + store.application.focused = false; self.focused = false; }, Event::Resize(_, _) => { diff --git a/src/notifications.rs b/src/notifications.rs index 3f08dcb..5ca905e 100644 --- a/src/notifications.rs +++ b/src/notifications.rs @@ -14,7 +14,7 @@ use matrix_sdk::{ use unicode_segmentation::UnicodeSegmentation; use crate::{ - base::{AsyncProgramStore, IambError, IambResult}, + base::{AsyncProgramStore, IambError, IambResult, ProgramStore}, config::{ApplicationSettings, NotifyVia}, }; @@ -44,7 +44,7 @@ pub async fn register_notifications( return; } - if is_open(&store, room.room_id()).await { + if is_visible_room(&store, room.room_id()).await { return; } @@ -127,8 +127,7 @@ fn is_missing_mention(body: &Option, mode: RoomNotificationMode, client: false } -async fn is_open(store: &AsyncProgramStore, room_id: &RoomId) -> bool { - let mut locked = store.lock().await; +fn is_open(locked: &mut ProgramStore, room_id: &RoomId) -> bool { if let Some(draw_curr) = locked.application.draw_curr { let info = locked.application.get_room_info(room_id.to_owned()); if let Some(draw_last) = info.draw_last { @@ -138,6 +137,16 @@ async fn is_open(store: &AsyncProgramStore, room_id: &RoomId) -> bool { false } +fn is_focused(locked: &ProgramStore) -> bool { + locked.application.focused +} + +async fn is_visible_room(store: &AsyncProgramStore, room_id: &RoomId) -> bool { + let mut locked = store.lock().await; + + is_focused(&locked) && is_open(&mut locked, room_id) +} + pub async fn parse_notification( notification: Notification, room: MatrixRoom,