Skip to content

Commit

Permalink
use libnotify for notifications. show volume notification
Browse files Browse the repository at this point in the history
  • Loading branch information
Junker committed Nov 17, 2019
1 parent 00e0c88 commit c918f22
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
3 changes: 2 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ project('mictray', 'vala', 'c',
gtk_dep = dependency('gtk+-3.0', version : '>=3.22')
gee_dep = dependency('gee-0.8')
pulse_dep = dependency('libpulse')
notify_dep = dependency('libnotify')
pulse_loop_dep = dependency('libpulse-mainloop-glib')


Expand All @@ -23,6 +24,6 @@ install_data('mictray.desktop', install_dir: install_desktoppath)

executable('mictray', 'src/mictray.vala',
sources,
dependencies : [gtk_dep, pulse_dep, pulse_loop_dep, gee_dep],
dependencies : [gtk_dep, pulse_dep, pulse_loop_dep, gee_dep,notify_dep],
install: true
)
41 changes: 35 additions & 6 deletions src/mictray.vala
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@

using Gtk;
using Notify;

static MicStatusIcon status_icon;

static Pulse pulse;
static ConfigFile config;
static MicTrayApp app;
static Notify.Notification vol_notification;

extern const string GETTEXT_PACKAGE;

Expand All @@ -18,17 +20,44 @@ class MicTrayApp : Gtk.Application
config = new ConfigFile();

pulse = new Pulse();
pulse.change_callback = () => {status_icon.update();};

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

pulse.change_callback = () =>
{
status_icon.update();

if (config.show_notifications && pulse.old_volume != pulse.volume)
{
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);
}
}
};

pulse.source_change_callback = () =>
{
if (config.show_notifications)
{
Notification notification = new Notification("Input source changed");
notification.set_icon(new ThemedIcon(status_icon.icon_name));
notification.set_body(pulse.current_source_description);

app.send_notification("source-changed", notification);
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);
}
}
};

Expand Down
2 changes: 2 additions & 0 deletions src/pulse.vala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class Pulse : Object
public string? current_source_name = null;
public string? current_source_description = null;
public int volume;
public int old_volume;
public bool muted;

public Callback change_callback;
Expand Down Expand Up @@ -181,6 +182,7 @@ public class Pulse : Object
{
if (eol > 0) return;

this.old_volume = this.volume;
this.volume = (int)((info.volume.avg() / (float)PulseAudio.Volume.NORM) * 100);
this.muted = (bool)info.mute;

Expand Down

0 comments on commit c918f22

Please sign in to comment.