From b3206e52873d030b8c9280da26be06c95abfd0d8 Mon Sep 17 00:00:00 2001 From: Junker Date: Fri, 20 Dec 2019 13:30:35 +0700 Subject: [PATCH] Notification class. some fixes --- meson.build | 1 + src/mictray.vala | 34 +++++----------------------------- src/notification.vala | 42 ++++++++++++++++++++++++++++++++++++++++++ src/pulse.vala | 2 ++ src/status-icon.vala | 2 +- 5 files changed, 51 insertions(+), 30 deletions(-) create mode 100644 src/notification.vala diff --git a/meson.build b/meson.build index adc0a70..ecf132b 100644 --- a/meson.build +++ b/meson.build @@ -13,6 +13,7 @@ add_project_arguments('-DGETTEXT_PACKAGE="mictray"', language: 'c') sources = [ 'src/pulse.vala', + 'src/notification.vala', 'src/config-file.vala', 'src/status-icon.vala', 'src/settings-window.vala' diff --git a/src/mictray.vala b/src/mictray.vala index a3a4758..b8d0bee 100644 --- a/src/mictray.vala +++ b/src/mictray.vala @@ -1,13 +1,12 @@ using Gtk; -using Notify; static MicStatusIcon status_icon; static Pulse pulse; static ConfigFile config; static MicTrayApp app; -static Notify.Notification vol_notification; +static Notification notification; extern const string GETTEXT_PACKAGE; @@ -16,32 +15,17 @@ class MicTrayApp : Gtk.Application protected override void activate () { status_icon = new MicStatusIcon(); - config = new ConfigFile(); - pulse = new Pulse(); - - Notify.init ("MicTray"); - vol_notification = new Notify.Notification ("Volume: %d%c".printf(pulse.volume, '%'), "", status_icon.icon_name); - vol_notification.set_timeout(2000); - vol_notification.set_hint("transient", new Variant.boolean(true)); + notification = new Notification(); pulse.change_callback = () => { status_icon.update(); - if (config.show_notifications && (pulse.old_volume != pulse.volume || pulse.old_muted != pulse.muted)) + if (config.show_notifications && !pulse.first_change && (pulse.old_volume != pulse.volume || pulse.old_muted != pulse.muted)) { - try - { - vol_notification.update("Volume: %d%c".printf(pulse.volume, '%'), "", status_icon.icon_name); - vol_notification.set_hint("value", new Variant.int32(pulse.volume)); - vol_notification.show(); - } - catch (Error e) - { - error ("Error: %s", e.message); - } + notification.update(); } }; @@ -49,15 +33,7 @@ class MicTrayApp : Gtk.Application { if (config.show_notifications) { - try - { - Notify.Notification notification = new Notify.Notification ("Input source changed", pulse.current_source_description, status_icon.icon_name); - notification.show(); - } - catch (Error e) - { - error ("Error: %s", e.message); - } + notification.source_changed(); } }; diff --git a/src/notification.vala b/src/notification.vala new file mode 100644 index 0000000..cdfaf9b --- /dev/null +++ b/src/notification.vala @@ -0,0 +1,42 @@ +using Notify; + +class Notification : Object +{ + private Notify.Notification vol_notification; + + construct + { + Notify.init ("MicTray"); + + vol_notification = new Notify.Notification ("Volume: %d%c".printf(pulse.volume, '%'), "", status_icon.icon_name); + vol_notification.set_timeout(2000); + vol_notification.set_hint("transient", new Variant.boolean(true)); + } + + public void update() + { + try + { + this.vol_notification.update("Volume: %d%c".printf(pulse.volume, '%'), "", status_icon.icon_name); + this.vol_notification.set_hint("value", new Variant.int32(pulse.volume)); + this.vol_notification.show(); + } + catch (Error e) + { + error ("Error: %s", e.message); + } + } + + public void source_changed() + { + try + { + Notify.Notification notification = new Notify.Notification ("Input source changed", pulse.current_source_description, status_icon.icon_name); + notification.show(); + } + catch (Error e) + { + error ("Error: %s", e.message); + } + } +} \ No newline at end of file diff --git a/src/pulse.vala b/src/pulse.vala index 103dbc3..9c98654 100644 --- a/src/pulse.vala +++ b/src/pulse.vala @@ -10,6 +10,7 @@ public class Pulse : Object public int old_volume; public bool muted; public bool old_muted; + public bool first_change = true; public Callback change_callback; public Callback source_change_callback; @@ -198,6 +199,7 @@ public class Pulse : Object this.source_change_callback(); } + this.first_change = false; }); } } \ No newline at end of file diff --git a/src/status-icon.vala b/src/status-icon.vala index 8b3d399..7cf44d0 100644 --- a/src/status-icon.vala +++ b/src/status-icon.vala @@ -72,7 +72,7 @@ public class MicStatusIcon : StatusIcon { var about = new Gtk.AboutDialog(); about.set_logo_icon_name("microphone-sensitivity-high"); - about.set_version("0.2.1"); + about.set_version("0.2.3"); about.set_program_name("MicTray"); about.set_comments("Microphone control application"); about.set_copyright("Dmitry Kosenkov");