feat(core): wire up show_icon and show_notification options

This commit is contained in:
Federico Terzi 2021-08-20 19:36:52 +02:00
parent fa149471f2
commit e8db703014
5 changed files with 78 additions and 8 deletions

View File

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

View File

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

View File

@ -17,5 +17,6 @@
* along with espanso. If not, see <https://www.gnu.org/licenses/>.
*/
pub mod notification;
pub mod util;
pub mod welcome;

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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!");
}
}

View File

@ -82,6 +82,18 @@ macro_rules! generate_patchable_config {
fn search_shortcut(&self) -> Option<String> {
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()
}
}
};
}