🎉 Add option to disable effects if on battery power

This commit is contained in:
Simon Schneegans
2022-05-25 12:59:03 +02:00
parent 8dc73ff9f6
commit b215379dac
7 changed files with 114 additions and 6 deletions
+13
View File
@@ -73,6 +73,12 @@ class Extension {
// We will use extensionThis to refer to the extension inside the patched methods. // We will use extensionThis to refer to the extension inside the patched methods.
const extensionThis = this; const extensionThis = this;
// This is used to get the battery state.
const UPowerProxy = Gio.DBusProxy.makeProxyWrapper(
utils.getStringResource('/interfaces/org.freedesktop.UPower.xml'));
this._upowerProxy = new UPowerProxy(Gio.DBus.system, 'org.freedesktop.UPower',
'/org/freedesktop/UPower');
// We will monkey-patch these methods. Let's store the original ones. // We will monkey-patch these methods. Let's store the original ones.
this._origAddWindowClone = Workspace.prototype._addWindowClone; this._origAddWindowClone = Workspace.prototype._addWindowClone;
this._origWindowRemoved = Workspace.prototype._windowRemoved; this._origWindowRemoved = Workspace.prototype._windowRemoved;
@@ -365,6 +371,13 @@ class Extension {
return; return;
} }
// We may have to do nothing if running on battery power.
if (this._settings.get_boolean('disable-on-battery') && this._upowerProxy.OnBattery &&
previewNick == '') {
this._fixAnimationTimes(isDialogWindow, forOpening, null);
return;
}
// There is the weird case where an animation is already ongoing. This happens when a // There is the weird case where an animation is already ongoing. This happens when a
// window is closed which has been created before the session was started (e.g. when // window is closed which has been created before the session was started (e.g. when
// GNOME Shell has been restarted in the meantime). // GNOME Shell has been restarted in the meantime).
+2 -5
View File
@@ -14,7 +14,6 @@
'use strict'; 'use strict';
const {Gio, Gtk, Gdk, GLib, GObject} = imports.gi; const {Gio, Gtk, Gdk, GLib, GObject} = imports.gi;
const ByteArray = imports.byteArray;
// libadwaita is available starting with GNOME Shell 42. // libadwaita is available starting with GNOME Shell 42.
let Adw = null; let Adw = null;
@@ -95,7 +94,7 @@ var PreferencesDialog = class PreferencesDialog {
// Bind general options properties. // Bind general options properties.
this.bindSwitch('destroy-dialogs'); this.bindSwitch('destroy-dialogs');
this.bindSwitch('disable-on-battery');
// Starting with GNOME Shell 42, the settings dialog uses libadwaita (at least most of // Starting with GNOME Shell 42, the settings dialog uses libadwaita (at least most of
// the time - it seems that pop!_OS does not support libadwaita even on GNOME 42). We // the time - it seems that pop!_OS does not support libadwaita even on GNOME 42). We
@@ -460,9 +459,7 @@ var PreferencesDialog = class PreferencesDialog {
// Reads the contents of a JSON file contained in the global resources archive. The data // Reads the contents of a JSON file contained in the global resources archive. The data
// is parsed and returned as a JavaScript object / array. // is parsed and returned as a JavaScript object / array.
_getJSONResource(path) { _getJSONResource(path) {
const data = Gio.resources_lookup_data(path, 0); return JSON.parse(utils.getStringResource(path));
const string = ByteArray.toString(ByteArray.fromGBytes(data));
return JSON.parse(string);
} }
// This traverses the widget tree below the given parent recursively and returns the // This traverses the widget tree below the given parent recursively and returns the
@@ -0,0 +1,6 @@
<node>
<interface name="org.freedesktop.UPower">
<property name="OnBattery" type="b" access="read" />
</interface>
</node>
+44
View File
@@ -227,6 +227,50 @@
</object> </object>
</child> </child>
<child>
<object class="GtkListBoxRow">
<property name="margin-start">10</property>
<property name="margin-end">10</property>
<property name="margin-top">10</property>
<property name="margin-bottom">10</property>
<property name="activatable">0</property>
<child>
<object class="GtkBox">
<property name="spacing">8</property>
<child>
<object class="GtkLabel">
<property name="label" translatable="yes">Disable on Battery Power</property>
<property name="xalign">0</property>
<property name="halign">start</property>
<property name="valign">center</property>
<property name="hexpand">1</property>
</object>
</child>
<child>
<object class="GtkSwitch" id="disable-on-battery">
<property name="halign">end</property>
<property name="valign">center</property>
</object>
</child>
<child>
<object class="GtkButton" id="reset-disable-on-battery">
<child>
<object class="GtkImage">
<property name="icon-name">edit-clear-symbolic</property>
<property name="icon-size">1</property>
</object>
</child>
<property name="tooltip-text" translatable="yes">Reset to Default Value</property>
<style>
<class name="flat" />
</style>
</object>
</child>
</object>
</child>
</object>
</child>
</object> </object>
</child> </child>
</object> </object>
+34
View File
@@ -174,6 +174,40 @@
</object> </object>
</child> </child>
<child>
<object class="GtkListBoxRow">
<property name="activatable">0</property>
<child>
<object class="GtkBox">
<child>
<object class="GtkLabel">
<property name="label" translatable="yes">Disable on Battery Power</property>
<property name="xalign">0</property>
<property name="halign">start</property>
<property name="valign">center</property>
<property name="hexpand">1</property>
</object>
</child>
<child>
<object class="GtkSwitch" id="disable-on-battery">
<property name="halign">end</property>
<property name="valign">center</property>
</object>
</child>
<child>
<object class="GtkButton" id="reset-disable-on-battery">
<property name="icon-name">edit-clear-symbolic</property>
<property name="tooltip-text" translatable="yes">Reset to Default Value</property>
<style>
<class name="flat" />
</style>
</object>
</child>
</object>
</child>
</object>
</child>
</object> </object>
</child> </child>
</object> </object>
@@ -13,6 +13,12 @@
<description>Set to true to burn dialogs as well.</description> <description>Set to true to burn dialogs as well.</description>
</key> </key>
<key name="disable-on-battery" type="b">
<default>false</default>
<summary>Disable on Battery</summary>
<description>Enable effects only if plugged in.</description>
</key>
<key name="close-preview-effect" type="s"> <key name="close-preview-effect" type="s">
<default>""</default> <default>""</default>
<summary>Close Preview Effect</summary> <summary>Close Preview Effect</summary>
+9 -1
View File
@@ -13,7 +13,8 @@
'use strict'; 'use strict';
const {Gtk} = imports.gi; const {Gtk, Gio} = imports.gi;
const ByteArray = imports.byteArray;
// Returns the given argument, except for "alpha", "beta", and "rc". In these cases -3, // Returns the given argument, except for "alpha", "beta", and "rc". In these cases -3,
// -2, and -1 are returned respectively. // -2, and -1 are returned respectively.
@@ -83,4 +84,11 @@ function shellVersionIsAtLeast(major, minor) {
} }
return false; return false;
}
// Reads the contents of a file contained in the global resources archive. The data
// is returned as a string.
function getStringResource(path) {
const data = Gio.resources_lookup_data(path, 0);
return ByteArray.toString(ByteArray.fromGBytes(data));
} }