Can an applet drain the battery?

About programming and getting involved with Linux Mint development
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Post Reply
Shorten3353
Level 1
Level 1
Posts: 4
Joined: Sat Sep 23, 2023 8:56 am

Can an applet drain the battery?

Post by Shorten3353 »

I created an applet with ChatGPT with the following functionality:
1- Activate the VPN by clicking on the panel icon.
2- The panel icon changes when the VPN is activated, and changes again when it is deactivated.
However, I noticed a significant reduction in battery time. I'm not sure if it's due to the applet, because I made some other changes in LM. So the question came to me: can applets consume a lot of battery power?

This is the code:

Code: Select all

const Applet = imports.ui.applet;
const Main = imports.ui.main;
const GLib = imports.gi.GLib;
const Util = imports.misc.util;

function MyVPNApplet(orientation, panelHeight, instanceId) {
    this._init(orientation, panelHeight, instanceId);
}

MyVPNApplet.prototype = {
    __proto__: Applet.TextIconApplet.prototype,

    _init: function(orientation, panelHeight, instanceId) {
        Applet.TextIconApplet.prototype._init.call(this, orientation, panelHeight, instanceId);
        this.set_applet_icon_name("network-vpn-no-route-symbolic");

        // Conectar la señal de clic del applet a la función on_applet_clicked
        this.connect('button-press-event', this.on_applet_clicked.bind(this));
    },

    _updateStatus: function() {
        let [res, out] = GLib.spawn_command_line_sync("protonvpn-cli s");
        let status = new TextDecoder().decode(out).trim();

        if (status === "No active Proton VPN connection.") {
            this.set_applet_icon_name("network-vpn-no-route-symbolic");
        } else {
            this.set_applet_icon_name("network-vpn-symbolic");
        }
    },

    on_applet_clicked: function() {
        // Ejecutar el script al hacer clic en el applet
        Util.spawnCommandLine("VPN_Script.sh");

        // Actualizar el estado después de ejecutar el script
        GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 6, this._updateStatus.bind(this));
    }
};

function main(metadata, orientation, panelHeight, instanceId) {
    return new MyVPNApplet(orientation, panelHeight, instanceId);
}
VPN_script.sh

Code: Select all

#  Check VPN status
vpn_status=$(protonvpn-cli s)

if [[ $vpn_status == *"No active Proton VPN connection."* ]]; then
  # You are not connected, connect the VPN
  echo "Connecting..."
  protonvpn-cli c -f
else
  # You are connected, disconnect the VPN.
  echo "Disconnecting..."
  protonvpn-cli d
fi
Post Reply

Return to “Programming & Development”