Skip to content

Commit

Permalink
Show battery details on activate (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
andirsun authored Jul 14, 2021
1 parent a63116d commit 949d33a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 20 deletions.
20 changes: 20 additions & 0 deletions src/Widgets/DeviceList.vala
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,45 @@

public class Power.Widgets.DeviceList : Gtk.ListBox {
public Gee.HashMap<string, Power.Widgets.DeviceRow> entries;
public Gee.HashMap<Gtk.ListBoxRow, string> path_entries;

construct {
selection_mode = Gtk.SelectionMode.NONE;
set_sort_func (sort_function);

entries = new Gee.HashMap<string, Power.Widgets.DeviceRow> ();
path_entries = new Gee.HashMap<Gtk.ListBoxRow, string> ();

var dm = Services.DeviceManager.get_default ();
dm.battery_registered.connect (add_battery);
dm.battery_deregistered.connect (remove_battery);

// load all battery information.
dm.read_devices ();

this.row_activated.connect ((value) => {
string device_path = path_entries.@get (value);
try {
AppInfo statistics_app = AppInfo.create_from_commandline (
"gnome-power-statistics --device " + device_path,
"",
AppInfoCreateFlags.NONE
);
statistics_app.launch (null, null);

Gtk.Popover popover = (Gtk.Popover) get_ancestor (typeof (Gtk.Popover));
popover.popdown ();
} catch (Error e) {
print ("Error opening Gnome Power Statistics: %s\n", e.message);
}
});
}

private void add_battery (string device_path, Services.Device battery) {
var device_row = new Power.Widgets.DeviceRow (battery);

entries.@set (device_path, device_row);
path_entries.@set (device_row, device_path);

add (device_row);
show_all ();
Expand Down
46 changes: 26 additions & 20 deletions src/Widgets/DeviceRow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,45 @@ public class Power.Widgets.DeviceRow : Gtk.ListBoxRow {
}

construct {
device_image = new Gtk.Image.from_icon_name ("battery", Gtk.IconSize.DIALOG);
device_image.pixel_size = 48;
device_image.margin_end = 3;
device_image = new Gtk.Image.from_icon_name ("battery", Gtk.IconSize.DIALOG) {
pixel_size = 48,
margin_end = 3
};

battery_image = new Gtk.Image ();
battery_image.pixel_size = 32;
battery_image.halign = Gtk.Align.END;
battery_image.valign = Gtk.Align.END;
battery_image = new Gtk.Image () {
pixel_size = 32,
halign = Gtk.Align.END,
valign = Gtk.Align.END
};

var overlay = new Gtk.Overlay ();
overlay.add (device_image);
overlay.add_overlay (battery_image);

var title_label = new Gtk.Label (get_title ());
title_label.use_markup = true;
title_label.halign = Gtk.Align.START;
title_label.valign = Gtk.Align.END;
var title_label = new Gtk.Label (get_title ()) {
use_markup = true,
halign = Gtk.Align.START,
valign = Gtk.Align.END
};

var info_label = new Gtk.Label (battery.get_info ());
info_label.halign = Gtk.Align.START;
info_label.valign = Gtk.Align.START;

var grid = new Gtk.Grid ();
grid.column_spacing = 3;
grid.margin = 3;
grid.margin_start = 6;
grid.margin_end = 12;
var info_label = new Gtk.Label (battery.get_info ()) {
halign = Gtk.Align.START,
valign = Gtk.Align.START
};

var grid = new Gtk.Grid () {
column_spacing = 3,
margin = 3,
margin_start = 6,
margin_end = 12
};
grid.attach (overlay, 0, 0, 1, 2);
grid.attach (title_label, 1, 0);
grid.attach (info_label, 1, 1);

add (grid);

add (grid);
update_icons ();

battery.properties_updated.connect (() => {
Expand Down

0 comments on commit 949d33a

Please sign in to comment.