1
1
mirror of https://github.com/wez/wezterm.git synced 2024-09-19 18:57:59 +03:00

term: plumb Bell through toast notification channel

Per my comment: https://github.com/wez/wezterm/issues/3#issuecomment-780750798

this routes the bell through to the GUI layer, where it currently
does nothing about it.
This commit is contained in:
Wez Furlong 2021-02-18 22:34:05 -08:00
parent f0c163e212
commit 651e536d87
6 changed files with 54 additions and 39 deletions

View File

@ -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,
},
}

View File

@ -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 {
@ -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,
});
}
}

View File

@ -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.

View File

@ -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>,
@ -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(),
@ -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.
@ -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),
}
@ -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,
@ -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,

View File

@ -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;
@ -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 {

View File

@ -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);
}