finish up tray icon

This commit is contained in:
2020-08-07 16:17:15 -04:00
parent 130a131214
commit a6e1c031c3
10 changed files with 184 additions and 42 deletions

View File

@@ -149,7 +149,7 @@ namespace TUFManager {
* Used to restore the previous config from donf
* TODO: Move this to a status bar app and/or user daemon
*/
public void restore () {
private void restore () {
var mode = settings.get_int ("fan-mode");
if (mode >= 0 && mode <= 2) {
if (get_fan_mode () != mode) {

View File

@@ -49,7 +49,8 @@ if build_gui
tray_dependencies = [
appindicator_dep,
gtk_dep,
libnotify_dep
libnotify_dep,
meson.get_compiler('vala').find_library('posix')
]
endif
endif
@@ -126,7 +127,8 @@ if build_gui
tray_vala_sources = files(
'tray.vala',
'tray-icon.vala',
'server-interface.vala'
'server-interface.vala',
'common.vala'
)
endif
endif

View File

@@ -20,24 +20,66 @@ namespace TUFManager {
* The tray namespace handles everything related to the system tray
*/
namespace Tray {
private TUFManagerApp parent;
private Thread<void>? thread = null;
private bool poll = true;
public class TrayIcon {
private AppIndicator.Indicator indicator;
private Notify.Notification notification;
private Gtk.IconTheme icon_theme;
public TrayIcon (TUFManagerApp parent) {
/**
* The settings object from gschema/dconf
*/
private Settings settings;
public TrayIcon () {
Process.signal (ProcessSignal.INT, on_exit);
Process.signal (ProcessSignal.TERM, on_exit);
settings = new Settings ("org.tuf.manager");
try {
connect_tuf_server ();
}
catch (TUFError e) {
warning (e.message);
}
icon_theme = Gtk.IconTheme.get_default ();
icon_theme.changed.connect (on_icon_theme_changed);
indicator = new AppIndicator.Indicator (_ ("TUF Manager"), "tuf-manager", AppIndicator.IndicatorCategory.APPLICATION_STATUS);
indicator.set_status (AppIndicator.IndicatorStatus.ACTIVE);
var menu = new Gtk.Menu ();
var item = new Gtk.MenuItem.with_label (_ ("TUF Manager"));
var item = new Gtk.MenuItem.with_mnemonic (_ ("_TUF Manager"));
item.activate.connect (execute_manager);
menu.append (item);
item = new Gtk.MenuItem.with_mnemonic (_ ("_Quit"));
item.activate.connect (parent.release);
// Submenu fan
item = new Gtk.MenuItem.with_mnemonic (_ ("_Fan"));
menu.append (item);
var submenu = new Gtk.Menu ();
item.set_submenu (submenu);
// Fan modes
var subitem = new Gtk.MenuItem.with_mnemonic (_ ("_Balanced"));
subitem.activate.connect (set_fan_balanced);
submenu.append (subitem);
subitem = new Gtk.MenuItem.with_mnemonic (_ ("_Turbo"));
subitem.activate.connect (set_fan_turbo);
submenu.append (subitem);
subitem = new Gtk.MenuItem.with_mnemonic (_ ("_Silent"));
subitem.activate.connect (set_fan_silent);
submenu.append (subitem);
// Quit
item = new Gtk.MenuItem.with_mnemonic (_ ("_Quit"));
item.activate.connect (quit);
menu.append (item);
menu.show_all ();
indicator.set_menu (menu);
@@ -48,13 +90,55 @@ namespace TUFManager {
Timeout.add_seconds (30, () => {
indicator.set_status (AppIndicator.IndicatorStatus.ACTIVE);
show_notification ("TUF Manager started");
close_notification ();
return false;
});
Notify.init (_ ("TUF Manager"));
//show_notification ("test");
//update_notification ("test2");
restore ();
thread = new Thread<void> ("poll_fan", this.poll_fan);
}
private void quit () {
on_exit (ProcessSignal.TERM);
}
private static void on_exit (int signum) {
poll = false;
if (thread != null) {
thread.join ();
}
parent.release () ;
}
private void poll_fan () {
int ret;
Posix.pollfd[] fan_fd = {};
fan_fd += Posix.pollfd ();
fan_fd[0].fd = Posix.open (THERMAL_PATH, Posix.O_RDONLY);
fan_fd[0].events = Posix.POLLERR | Posix.POLLPRI;
Posix.read (fan_fd[0].fd, null, 1);
string content = "";
while (poll) {
ret = Posix.poll (fan_fd, 1000);
if (ret > 0) {
Posix.read (fan_fd[0].fd, content, 1);
Posix.lseek (fan_fd[0].fd, Posix.SEEK_SET, 0);
int mode = int.parse (content);
if (mode == 0) {
show_notification (_ ("Fan set to balanced"));
}
else if (mode == 1) {
show_notification (_ ("Fan set to turbo"));
}
else if (mode == 2) {
show_notification (_ ("Fan set to silent"));
}
}
}
}
private void execute_manager () {
@@ -66,6 +150,54 @@ namespace TUFManager {
}
}
private void set_fan_balanced () {
set_fan_mode (0);
//show_notification (_ ("Fan set to balanced"));
settings.set_int ("fan-mode", 0);
}
private void set_fan_turbo () {
set_fan_mode (1);
//show_notification (_ ("Fan set to turbo"));
settings.set_int ("fan-mode", 1);
}
private void set_fan_silent () {
set_fan_mode (2);
//show_notification (_ ("Fan set to silenced"));
settings.set_int ("fan-mode", 2);
}
private void restore () {
var mode = settings.get_int ("fan-mode");
if (mode >= 0 && mode <= 2) {
if (get_fan_mode () != mode) {
set_fan_mode (mode);
}
}
mode = settings.get_int ("keyboard-mode");
if (mode >= 0 && mode <= 3) {
if (get_keyboard_mode () != mode) {
set_keyboard_mode (mode);
}
}
var speed = settings.get_int ("keyboard-speed");
if (speed >= 0 && speed <= 2) {
if (get_keyboard_speed () != speed) {
set_keyboard_speed (speed);
}
}
var color = settings.get_string ("keyboard-color");
var rgba = Gdk.RGBA ();
rgba.parse (color);
if (!get_keyboard_color ().equal (rgba)) {
set_keyboard_color (rgba);
}
}
private void set_icon_visible (bool visible) {
if (visible) {
indicator.set_status (AppIndicator.IndicatorStatus.ACTIVE);
@@ -75,12 +207,12 @@ namespace TUFManager {
}
}
private void show_notification (string info) {
private void show_notification (string message) {
try {
close_notification ();
notification = new Notify.Notification (_ ("TUF Manager"), info, "tuf-manager");
notification = new Notify.Notification (_ ("TUF Manager"), message, "tuf-manager");
notification.set_timeout (Notify.EXPIRES_DEFAULT);
notification.add_action ("default", _ ("Details"), execute_manager);
notification.add_action ("default", _ ("Details"), close_notification);
notification.show ();
}
catch (Error e) {
@@ -88,23 +220,6 @@ namespace TUFManager {
}
}
private void update_notification (string info) {
try {
if (notification != null) {
if (notification.get_closed_reason () == -1 && notification.body != info) {
notification.update (_ ("TUF Manager"), info, "tuf-manager");
notification.show ();
}
}
else {
show_notification (info);
}
}
catch (Error e) {
warning (e.message);
}
}
private void close_notification () {
try {
if (notification != null && notification.get_closed_reason () == -1) {

View File

@@ -26,7 +26,8 @@ namespace TUFManager {
}
public override void activate () {
new TrayIcon (this);
parent = this;
new TrayIcon ();
}
public override void startup () {