Skip to content

Commit

Permalink
term: plumb Bell through toast notification channel
Browse files Browse the repository at this point in the history
Per my comment: #3 (comment)

this routes the bell through to the GUI layer, where it currently
does nothing about it.
  • Loading branch information
wez committed Feb 19, 2021
1 parent f0c163e commit 651e536
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 39 deletions.
4 changes: 2 additions & 2 deletions mux/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ use crate::activity::Activity;
pub enum MuxNotification {
PaneOutput(PaneId),
WindowCreated(WindowId),
ToastNotification {
Alert {
pane_id: PaneId,
notification: wezterm_term::ToastNotification,
alert: wezterm_term::Alert,
},
}

Expand Down
12 changes: 6 additions & 6 deletions mux/src/localpane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use termwiz::surface::Line;
use url::Url;
use wezterm_term::color::ColorPalette;
use wezterm_term::{
CellAttributes, Clipboard, KeyCode, KeyModifiers, MouseEvent, SemanticZone, StableRowIndex,
Terminal, ToastNotification, ToastNotificationHandler,
Alert, AlertHandler, CellAttributes, Clipboard, KeyCode, KeyModifiers, MouseEvent,
SemanticZone, StableRowIndex, Terminal,
};

pub struct LocalPane {
Expand Down Expand Up @@ -371,12 +371,12 @@ struct LocalPaneNotifHandler {
pane_id: PaneId,
}

impl ToastNotificationHandler for LocalPaneNotifHandler {
fn show_notification(&mut self, notification: ToastNotification) {
impl AlertHandler for LocalPaneNotifHandler {
fn alert(&mut self, alert: Alert) {
if let Some(mux) = Mux::get() {
mux.notify(MuxNotification::ToastNotification {
mux.notify(MuxNotification::Alert {
pane_id: self.pane_id,
notification,
alert,
});
}
}
Expand Down
23 changes: 13 additions & 10 deletions term/src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,21 @@ pub trait DeviceControlHandler {
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ToastNotification {
/// The title text for the notification.
pub title: Option<String>,
/// The message body
pub body: String,
/// Whether clicking on the notification should focus the
/// window/tab/pane that generated it
pub focus: bool,
pub enum Alert {
Bell,
ToastNotification {
/// The title text for the notification.
title: Option<String>,
/// The message body
body: String,
/// Whether clicking on the notification should focus the
/// window/tab/pane that generated it
focus: bool,
},
}

pub trait ToastNotificationHandler {
fn show_notification(&mut self, notif: ToastNotification);
pub trait AlertHandler {
fn alert(&mut self, alert: Alert);
}

/// Represents an instance of a terminal emulator.
Expand Down
24 changes: 15 additions & 9 deletions term/src/terminalstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ pub struct TerminalState {

clipboard: Option<Arc<dyn Clipboard>>,
device_control_handler: Option<Box<dyn DeviceControlHandler>>,
notification_handler: Option<Box<dyn ToastNotificationHandler>>,
alert_handler: Option<Box<dyn AlertHandler>>,

current_dir: Option<Url>,

Expand Down Expand Up @@ -434,7 +434,7 @@ impl TerminalState {
pixel_width: size.pixel_width,
clipboard: None,
device_control_handler: None,
notification_handler: None,
alert_handler: None,
current_dir: None,
term_program: term_program.to_string(),
term_version: term_version.to_string(),
Expand All @@ -451,8 +451,8 @@ impl TerminalState {
self.device_control_handler.replace(handler);
}

pub fn set_notification_handler(&mut self, handler: Box<dyn ToastNotificationHandler>) {
self.notification_handler.replace(handler);
pub fn set_notification_handler(&mut self, handler: Box<dyn AlertHandler>) {
self.alert_handler.replace(handler);
}

/// Returns the title text associated with the terminal session.
Expand Down Expand Up @@ -2982,7 +2982,13 @@ impl<'a> Performer<'a> {
ControlCode::HTS => self.c1_hts(),
ControlCode::IND => self.c1_index(),
ControlCode::NEL => self.c1_nel(),
ControlCode::Bell => log::info!("Ding! (this is the bell)"),
ControlCode::Bell => {
if let Some(handler) = self.alert_handler.as_mut() {
handler.alert(Alert::Bell);
} else {
log::info!("Ding! (this is the bell)");
}
}
ControlCode::RI => self.c1_reverse_index(),
_ => error!("unhandled ControlCode {:?}", control),
}
Expand Down Expand Up @@ -3182,8 +3188,8 @@ impl<'a> Performer<'a> {
}

OperatingSystemCommand::SystemNotification(message) => {
if let Some(handler) = self.notification_handler.as_mut() {
handler.show_notification(ToastNotification {
if let Some(handler) = self.alert_handler.as_mut() {
handler.alert(Alert::ToastNotification {
title: None,
body: message,
focus: true,
Expand All @@ -3204,8 +3210,8 @@ impl<'a> Performer<'a> {
return;
}
};
if let Some(handler) = self.notification_handler.as_mut() {
handler.show_notification(ToastNotification {
if let Some(handler) = self.alert_handler.as_mut() {
handler.alert(Alert::ToastNotification {
title,
body,
focus: true,
Expand Down
25 changes: 17 additions & 8 deletions wezterm-gui/src/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub use config::FrontEndSelection;
use mux::{Mux, MuxNotification};
use std::cell::RefCell;
use std::rc::Rc;
use wezterm_term::Alert;
use wezterm_toast_notification::*;

mod glyphcache;
Expand Down Expand Up @@ -64,21 +65,29 @@ impl GuiFrontEnd {
}
}
MuxNotification::PaneOutput(_) => {}
MuxNotification::ToastNotification {
MuxNotification::Alert {
pane_id: _,
notification,
alert:
Alert::ToastNotification {
title,
body,
focus: _,
},
} => {
let title = notification.title.as_ref().unwrap_or(&notification.body);
let message = if notification.title.is_none() {
""
} else {
&notification.body
};
let message = if title.is_none() { "" } else { &body };
let title = title.as_ref().unwrap_or(&body);
// FIXME: if notification.focus is true, we should do
// something here to arrange to focus pane_id when the
// notification is clicked
persistent_toast_notification(title, message);
}
MuxNotification::Alert {
pane_id: _,
alert: Alert::Bell,
} => {
// persistent_toast_notification("Ding!", "This is the bell");
log::info!("Ding! (this is the bell)");
}
}
true
} else {
Expand Down
5 changes: 1 addition & 4 deletions wezterm-mux-server-impl/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ where
Ok(Item::Notif(MuxNotification::PaneOutput(pane_id))) => {
handler.schedule_pane_push(pane_id);
}
Ok(Item::Notif(MuxNotification::ToastNotification {
pane_id,
notification: _,
})) => {
Ok(Item::Notif(MuxNotification::Alert { pane_id, alert: _ })) => {
// FIXME: queue notification to send to client!
handler.schedule_pane_push(pane_id);
}
Expand Down

0 comments on commit 651e536

Please sign in to comment.