Skip to content

Commit

Permalink
Send battery notification instead of infobar (#821)
Browse files Browse the repository at this point in the history
  • Loading branch information
danirabbit authored Oct 8, 2024
1 parent 11ecfdd commit deae7d0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 55 deletions.
29 changes: 29 additions & 0 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,35 @@ public class Installer.App : Gtk.Application {
base.startup ();

Granite.init ();

try {
UPower upower = Bus.get_proxy_sync (BusType.SYSTEM, "org.freedesktop.UPower", "/org/freedesktop/UPower", GLib.DBusProxyFlags.GET_INVALIDATED_PROPERTIES);

send_withdraw_battery_notification (upower.on_battery);

((DBusProxy) upower).g_properties_changed.connect ((changed, invalid) => {
var _on_battery = changed.lookup_value ("OnBattery", GLib.VariantType.BOOLEAN);
if (_on_battery != null) {
send_withdraw_battery_notification (upower.on_battery);
}
});
} catch (Error e) {
critical ("Can't connect to UPower; unable to send battery notifications: %s", e.message);
}
}

private void send_withdraw_battery_notification (bool on_battery) {
if (!on_battery) {
withdraw_notification ("on-battery");
return;
}

var notification = new GLib.Notification (_("Connect to a Power Source"));
notification.set_body (_("Installation will not succeed if this device loses power."));
notification.set_icon (new ThemedIcon ("battery-ac-adapter"));
notification.set_priority (NotificationPriority.URGENT);

send_notification ("on-battery", notification);
}

public override void activate () {
Expand Down
56 changes: 1 addition & 55 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class Installer.MainWindow : Gtk.ApplicationWindow {
// Minimum 15 GB
private const uint64 MINIMUM_SPACE = 15 * ONE_GB;

private Gtk.Label infobar_label;
private Adw.NavigationView navigation_view;
private LanguageView language_view;
private TryInstallView try_install_view;
Expand All @@ -42,29 +41,7 @@ public class Installer.MainWindow : Gtk.ApplicationWindow {
navigation_view = new Adw.NavigationView ();
navigation_view.add (language_view);

infobar_label = new Gtk.Label ("") {
use_markup = true
};
set_infobar_string ();

var battery_infobar = new Gtk.InfoBar () {
message_type = Gtk.MessageType.WARNING,
margin_end = 7,
margin_bottom = 7,
margin_start = 7,
show_close_button = true,
halign = Gtk.Align.START, // Can't cover action area; need to select language
valign = Gtk.Align.END
};
battery_infobar.add_child (infobar_label);
battery_infobar.add_css_class (Granite.STYLE_CLASS_FRAME);

var overlay = new Gtk.Overlay () {
child = navigation_view
};
overlay.add_overlay (battery_infobar);

child = overlay;
child = navigation_view;
titlebar = new Gtk.Grid () { visible = false };

var back_action = new SimpleAction ("back", null);
Expand All @@ -80,31 +57,9 @@ public class Installer.MainWindow : Gtk.ApplicationWindow {
Source.remove (orca_timeout_id);
}

// Reset when language selection changes
set_infobar_string ();
load_keyboard_view ();
});

try {
UPower upower = Bus.get_proxy_sync (BusType.SYSTEM, "org.freedesktop.UPower", "/org/freedesktop/UPower", GLib.DBusProxyFlags.GET_INVALIDATED_PROPERTIES);

battery_infobar.revealed = upower.on_battery;

((DBusProxy) upower).g_properties_changed.connect ((changed, invalid) => {
var _on_battery = changed.lookup_value ("OnBattery", GLib.VariantType.BOOLEAN);
if (_on_battery != null) {
battery_infobar.revealed = upower.on_battery;
}
});
} catch (Error e) {
warning (e.message);
battery_infobar.revealed = false;
}

battery_infobar.response.connect (() => {
battery_infobar.revealed = false;
});

var mediakeys_settings = new Settings ("org.gnome.settings-daemon.plugins.media-keys");
var a11y_settings = new Settings ("org.gnome.desktop.a11y.applications");

Expand Down Expand Up @@ -263,13 +218,4 @@ public class Installer.MainWindow : Gtk.ApplicationWindow {

navigation_view.push (error_view);
}

private void set_infobar_string () {
var infobar_string = "%s\n%s".printf (
_("Connect to a Power Source"),
Granite.TOOLTIP_SECONDARY_TEXT_MARKUP.printf (_("Your device is running on battery power. It's recommended to be plugged in while installing."))
);

infobar_label.label = infobar_string;
}
}

0 comments on commit deae7d0

Please sign in to comment.