Skip to content

Commit

Permalink
Merge pull request #536 from squidowl/hugegulp-main
Browse files Browse the repository at this point in the history
notifications for direct messages
  • Loading branch information
tarkah authored Sep 4, 2024
2 parents cddeb42 + f05ff88 commit 38f6727
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Added:
- Overwrite nicklist `width` in channels. See [configuration](https://halloy.squidowl.org/configuration/buffer.html#bufferchannelnicklist-section).
- Show/hide user access levels in buffer and nicklist. See [configuration](https://halloy.squidowl.org/configuration/buffer.html#bufferchannelnicklist-section)
- `buffer_focused_action` added to `sidebar` to enable actions a focused buffer. See [configuration](https://halloy.squidowl.org/configuration/sidebar.html#sidebar-section).
- Notification for direct messages. See [notification configuration](https://halloy.squidowl.org/configuration/notifications.html).

Fixed:

Expand Down
1 change: 1 addition & 0 deletions book/src/configuration/notifications.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Following notifications are available:
- `connected`
- `disconnected`
- `reconnected`
- `direct_message`
- `highlight`
- `file_transfer_request`

Expand Down
10 changes: 10 additions & 0 deletions data/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub enum State {

#[derive(Debug)]
pub enum Notification {
DirectMessage(User),
Highlight(User, String),
}

Expand Down Expand Up @@ -636,6 +637,15 @@ impl Client {
// If we sent (echo) & context exists (we sent from this client), ignore
return None;
}

// use `channel` to confirm the direct message, then send notification
if channel == &self.nickname().to_string() {
return Some(vec![Event::Notification(
message.clone(),
self.nickname().to_owned(),
Notification::DirectMessage(user),
)]);
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions data/src/config/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub struct Notifications<T = String> {
#[serde(default)]
pub reconnected: Notification<T>,
#[serde(default)]
pub direct_message: Notification<T>,
#[serde(default)]
pub highlight: Notification<T>,
#[serde(default)]
pub file_transfer_request: Notification<T>,
Expand All @@ -40,6 +42,7 @@ impl<T> Default for Notifications<T> {
connected: Notification::default(),
disconnected: Notification::default(),
reconnected: Notification::default(),
direct_message: Notification::default(),
highlight: Notification::default(),
file_transfer_request: Notification::default(),
}
Expand All @@ -59,6 +62,7 @@ impl Notifications {
connected: load(&self.connected)?,
disconnected: load(&self.disconnected)?,
reconnected: load(&self.reconnected)?,
direct_message: load(&self.direct_message)?,
highlight: load(&self.highlight)?,
file_transfer_request: load(&self.file_transfer_request)?,
})
Expand Down
30 changes: 24 additions & 6 deletions data/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,33 @@ pub mod size;
pub struct Window {
pub position: Option<Position>,
pub size: Size,
#[serde(skip)]
focused: Option<bool>,
}

impl Window {
pub fn update(self, event: Event) -> Self {
pub fn update(&mut self, event: Event) -> Self {
match event {
Event::Moved(position) => Self {
position: Some(position),
..self
},
Event::Resized(size) => Self { size, ..self },
Event::Moved(position) => {
self.position = Some(position);
}
Event::Resized(size) => {
self.size = size;
}
Event::Focused => {
self.focused = Some(true);
}
Event::Unfocused => {
self.focused = Some(false);
}
}
*self
}

pub fn focused(&self) -> bool {
// Assume window is initially focused until an
// `Event` is received to set this field
self.focused.unwrap_or(true)
}

pub fn load() -> Result<Self, Error> {
Expand Down Expand Up @@ -68,4 +84,6 @@ pub enum Error {
pub enum Event {
Moved(Position),
Resized(Size),
Focused,
Unfocused,
}
4 changes: 4 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pub enum Event {
Escape,
Home,
End,
Focused,
Unfocused,
}

pub fn events() -> Subscription<Event> {
Expand Down Expand Up @@ -39,6 +41,8 @@ fn filtered_events(
..
}) if ignored(status) => Some(Event::End),
iced::Event::Window(window::Event::CloseRequested) => Some(Event::CloseRequested(window)),
iced::Event::Window(window::Event::Focused) => Some(Event::Focused),
iced::Event::Window(window::Event::Unfocused) => Some(Event::Unfocused),
_ => None,
}
}
21 changes: 19 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use chrono::Utc;
use data::config::{self, Config};
use data::version::Version;
use data::window::Window;
use data::{environment, server, version, User};
use data::{environment, history, server, version, User};
use iced::widget::{column, container};
use iced::{Length, Subscription, Task};
use screen::{dashboard, help, migration, welcome};
Expand Down Expand Up @@ -539,6 +539,22 @@ impl Halloy {
}

match notification {
data::client::Notification::DirectMessage(user) => {
// only send notification if query has unread
// or if window is not focused
if dashboard.history().has_unread(
&server,
&history::Kind::Query(
user.nickname().to_owned(),
),
) || !self.window.focused()
{
notification::direct_message(
&self.config.notifications,
user.nickname(),
);
}
}
data::client::Notification::Highlight(
user,
channel,
Expand Down Expand Up @@ -607,6 +623,7 @@ impl Halloy {
&self.version,
&self.config,
&mut self.theme,
&mut self.window,
)
.map(Message::Dashboard)
} else if let event::Event::CloseRequested(window) = event {
Expand Down Expand Up @@ -668,7 +685,7 @@ impl Halloy {
Task::none()
}
Message::Window(event) => {
self.window = self.window.update(event);
self.window.update(event);

Task::perform(self.window.save(), Message::WindowSettingsSaved)
}
Expand Down
8 changes: 8 additions & 0 deletions src/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ pub fn disconnected(config: &config::Notifications<Sound>, server: impl ToString
show_notification(&config.disconnected, "Disconnected", server);
}

pub fn direct_message(config: &config::Notifications<Sound>, nick: NickRef) {
show_notification(
&config.direct_message,
"Direct message",
format!("{} sent you a direct message", nick),
);
}

pub fn highlight(config: &config::Notifications<Sound>, nick: NickRef, channel: String) {
show_notification(
&config.highlight,
Expand Down
15 changes: 14 additions & 1 deletion src/screen/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::time::{Duration, Instant};
use data::file_transfer;
use data::history::manager::Broadcast;
use data::user::Nick;
use data::{client, environment, history, Config, Server, User, Version};
use data::{client, environment, history, window::Window, Config, Server, User, Version};
use iced::widget::pane_grid::{self, PaneGrid};
use iced::widget::{column, container, row, Space};
use iced::{clipboard, padding, window, Length, Task};
Expand Down Expand Up @@ -719,6 +719,7 @@ impl Dashboard {
version: &Version,
config: &Config,
theme: &mut Theme,
window: &mut Window,
) -> Task<Message> {
use event::Event::*;

Expand Down Expand Up @@ -776,6 +777,14 @@ impl Dashboard {

Task::perform(task, move |_| Message::Close(window))
}
Focused => {
window.update(data::window::Event::Focused);
Task::none()
}
Unfocused => {
window.update(data::window::Event::Unfocused);
Task::none()
}
}
}

Expand Down Expand Up @@ -1356,6 +1365,10 @@ impl Dashboard {
file_transfers: file_transfer::Manager::new(config.file_transfer.clone()),
}
}

pub fn history(&self) -> &history::Manager {
&self.history
}
}

impl<'a> From<&'a Dashboard> for data::Dashboard {
Expand Down
15 changes: 7 additions & 8 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ impl subscription::Recipe for Events {
iced::window::Event::Resized(Size { width, height }) => {
Some(Event::Resized(window::Size::new(width, height)))
}
iced::window::Event::Focused => Some(Event::Focused),
iced::window::Event::Unfocused => Some(Event::Unfocused),
_ => None,
},
_ => None,
Expand All @@ -130,14 +132,11 @@ impl subscription::Recipe for Events {
},
move |state| async move {
match state {
State::Idle { mut stream } => stream.next().await.map(|event| {
(
vec![],
match event {
Event::Moved(position) => State::Moving { stream, position },
Event::Resized(size) => State::Resizing { stream, size },
},
)
State::Idle { mut stream } => stream.next().await.map(|event| match event {
Event::Moved(position) => (vec![], State::Moving { stream, position }),
Event::Resized(size) => (vec![], State::Resizing { stream, size }),
Event::Focused => (vec![Event::Focused], State::Idle { stream }),
Event::Unfocused => (vec![Event::Unfocused], State::Idle { stream }),
}),
State::Moving {
mut stream,
Expand Down

0 comments on commit 38f6727

Please sign in to comment.