diff --git a/espanso/src/cli/worker/engine/mod.rs b/espanso/src/cli/worker/engine/mod.rs index bc33561..c860233 100644 --- a/espanso/src/cli/worker/engine/mod.rs +++ b/espanso/src/cli/worker/engine/mod.rs @@ -42,7 +42,7 @@ use crate::{cli::worker::{context::Context, engine::{dispatch::executor::{clipbo extension::{clipboard::ClipboardAdapter, form::FormProviderAdapter}, RendererAdapter, }, - }}, match_cache::{CombinedMatchCache, MatchCache}}, common_flags::{WORKER_START_REASON_CONFIG_CHANGED, WORKER_START_REASON_KEYBOARD_LAYOUT_CHANGED, WORKER_START_REASON_MANUAL}, preferences::Preferences}; + }}, match_cache::{CombinedMatchCache, MatchCache}, ui::notification::NotificationManager}, common_flags::{WORKER_START_REASON_CONFIG_CHANGED, WORKER_START_REASON_KEYBOARD_LAYOUT_CHANGED, WORKER_START_REASON_MANUAL}, preferences::Preferences}; use super::secure_input::SecureInputEvent; @@ -217,19 +217,21 @@ pub fn initialize_and_spawn( } } - // TODO: check config + let default_config = &*config_manager.default(); + let notification_manager = NotificationManager::new(&*ui_remote, default_config); + match start_reason.as_deref() { Some(flag) if flag == WORKER_START_REASON_CONFIG_CHANGED => { - ui_remote.show_notification("Configuration reloaded! Espanso automatically loads new changes as soon as you save them."); + notification_manager.notify_config_reloaded(false); } Some(flag) if flag == WORKER_START_REASON_MANUAL => { - ui_remote.show_notification("Configuration reloaded!"); + notification_manager.notify_config_reloaded(true); } Some(flag) if flag == WORKER_START_REASON_KEYBOARD_LAYOUT_CHANGED => { - ui_remote.show_notification("Updated keyboard layout!"); + notification_manager.notify_keyboard_layout_reloaded() } _ => { - ui_remote.show_notification("Espanso is running!"); + notification_manager.notify_start(); if !preferences.has_displayed_welcome() { super::ui::welcome::show_welcome_screen(); diff --git a/espanso/src/cli/worker/mod.rs b/espanso/src/cli/worker/mod.rs index 120ffd0..57978ed 100644 --- a/espanso/src/cli/worker/mod.rs +++ b/espanso/src/cli/worker/mod.rs @@ -98,13 +98,12 @@ fn worker_main(args: CliModuleArgs) -> i32 { crate::icon::load_icon_paths(&paths.runtime).expect("unable to initialize icons"); let (remote, mut eventloop) = espanso_ui::create_ui(espanso_ui::UIOptions { - // TODO: handle show icon + show_icon: config_store.default().show_icon(), icon_paths: convert_icon_paths_to_tray_vec(&icon_paths), notification_icon_path: icon_paths .logo .as_ref() .map(|path| path.to_string_lossy().to_string()), - ..Default::default() }) .expect("unable to create tray icon UI module"); diff --git a/espanso/src/cli/worker/ui/mod.rs b/espanso/src/cli/worker/ui/mod.rs index f08a156..68e2482 100644 --- a/espanso/src/cli/worker/ui/mod.rs +++ b/espanso/src/cli/worker/ui/mod.rs @@ -17,5 +17,6 @@ * along with espanso. If not, see . */ +pub mod notification; pub mod util; pub mod welcome; \ No newline at end of file diff --git a/espanso/src/cli/worker/ui/notification.rs b/espanso/src/cli/worker/ui/notification.rs new file mode 100644 index 0000000..c08f139 --- /dev/null +++ b/espanso/src/cli/worker/ui/notification.rs @@ -0,0 +1,56 @@ +/* + * This file is part of espanso. + * + * Copyright (C) 2019-2021 Federico Terzi + * + * espanso is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * espanso is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with espanso. If not, see . + */ + +use espanso_config::config::Config; +use espanso_ui::UIRemote; + +pub struct NotificationManager<'a> { + ui_remote: &'a dyn UIRemote, + config: &'a dyn Config, +} + +impl<'a> NotificationManager<'a> { + pub fn new(ui_remote: &'a dyn UIRemote, config: &'a dyn Config) -> Self { + NotificationManager { ui_remote, config } + } + + fn notify(&self, text: &str) { + if self.config.show_notifications() { + self.ui_remote.show_notification(text); + } + } + + pub fn notify_start(&self) { + self.notify("Espanso is running!"); + } + + pub fn notify_config_reloaded(&self, is_manual_restart: bool) { + if is_manual_restart { + self.notify("Configuration reloaded!"); + } else { + self.notify( + "Configuration reloaded! Espanso automatically loads new changes as soon as you save them.", + ); + } + } + + pub fn notify_keyboard_layout_reloaded(&self) { + self.notify("Updated keyboard layout!"); + } +} diff --git a/espanso/src/patch/patches/macros.rs b/espanso/src/patch/patches/macros.rs index 05d0943..21d8d46 100644 --- a/espanso/src/patch/patches/macros.rs +++ b/espanso/src/patch/patches/macros.rs @@ -82,6 +82,18 @@ macro_rules! generate_patchable_config { fn search_shortcut(&self) -> Option { self.base.search_shortcut() } + + fn show_icon(&self) -> bool { + self.base.show_icon() + } + + fn show_notifications(&self) -> bool { + self.base.show_notifications() + } + + fn secure_input_notification(&self) -> bool { + self.base.secure_input_notification() + } } }; } \ No newline at end of file