Skip to content

Commit 9a1adfb

Browse files
Allow notifications on open room if terminal not focused (#281)
1 parent cb44556 commit 9a1adfb

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

src/base.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,9 @@ pub struct ChatStore {
13191319

13201320
/// Whether to ring the terminal bell on the next redraw.
13211321
pub ring_bell: bool,
1322+
1323+
/// Whether the application is currently focused
1324+
pub focused: bool,
13221325
}
13231326

13241327
impl ChatStore {
@@ -1341,6 +1344,7 @@ impl ChatStore {
13411344
sync_info: Default::default(),
13421345
draw_curr: None,
13431346
ring_bell: false,
1347+
focused: true,
13441348
}
13451349
}
13461350

src/main.rs

+4
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,13 @@ impl Application {
355355
// Do nothing for now.
356356
},
357357
Event::FocusGained => {
358+
let mut store = self.store.lock().await;
359+
store.application.focused = true;
358360
self.focused = true;
359361
},
360362
Event::FocusLost => {
363+
let mut store = self.store.lock().await;
364+
store.application.focused = false;
361365
self.focused = false;
362366
},
363367
Event::Resize(_, _) => {

src/notifications.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use matrix_sdk::{
1414
use unicode_segmentation::UnicodeSegmentation;
1515

1616
use crate::{
17-
base::{AsyncProgramStore, IambError, IambResult},
17+
base::{AsyncProgramStore, IambError, IambResult, ProgramStore},
1818
config::{ApplicationSettings, NotifyVia},
1919
};
2020

@@ -44,7 +44,7 @@ pub async fn register_notifications(
4444
return;
4545
}
4646

47-
if is_open(&store, room.room_id()).await {
47+
if is_visible_room(&store, room.room_id()).await {
4848
return;
4949
}
5050

@@ -127,8 +127,7 @@ fn is_missing_mention(body: &Option<String>, mode: RoomNotificationMode, client:
127127
false
128128
}
129129

130-
async fn is_open(store: &AsyncProgramStore, room_id: &RoomId) -> bool {
131-
let mut locked = store.lock().await;
130+
fn is_open(locked: &mut ProgramStore, room_id: &RoomId) -> bool {
132131
if let Some(draw_curr) = locked.application.draw_curr {
133132
let info = locked.application.get_room_info(room_id.to_owned());
134133
if let Some(draw_last) = info.draw_last {
@@ -138,6 +137,16 @@ async fn is_open(store: &AsyncProgramStore, room_id: &RoomId) -> bool {
138137
false
139138
}
140139

140+
fn is_focused(locked: &ProgramStore) -> bool {
141+
locked.application.focused
142+
}
143+
144+
async fn is_visible_room(store: &AsyncProgramStore, room_id: &RoomId) -> bool {
145+
let mut locked = store.lock().await;
146+
147+
is_focused(&locked) && is_open(&mut locked, room_id)
148+
}
149+
141150
pub async fn parse_notification(
142151
notification: Notification,
143152
room: MatrixRoom,

0 commit comments

Comments
 (0)