const ExtensionUtils = imports.misc.extensionUtils
const UPower = imports.ui.status.power.UPower
const Main = imports.ui.main

let batteryWatching, settingsWatching, settings

function show() {
  getBattery((proxy, icon) => {
    icon.show()
  })
}

function hide() {
  getBattery((proxy, icon) => {
    icon.hide()
  })
}

function update() {
  let hideOn = settings.get_int('hide-on')
  getBattery(proxy => {
    let isDischarging = proxy.State === UPower.DeviceState.DISCHARGING
    let isFullyCharged = proxy.State === UPower.DeviceState.FULLY_CHARGED
    if (proxy.Type !== UPower.DeviceKind.BATTERY) {
      show()
    } else if (isFullyCharged) {
      hide()
    } else if (proxy.Percentage >= hideOn && !isDischarging) {
      hide()
    } else {
      show()
    }
  })
}

function getBattery(callback) {
  let menu = Main.panel.statusArea.aggregateMenu
  if (menu && menu._power) {
    callback(menu._power._proxy, menu._power)
  }
}

function enable() {
  settings = ExtensionUtils.getSettings('ru.sitnik.autohide-battery')
  settingsWatching = settings.connect('changed::hide-on', update)
  getBattery(proxy => {
    batteryWatching = proxy.connect('g-properties-changed', update)
  })
  update()
}

function disable() {
  if (settings) settings.disconnect(settingsWatching)
  getBattery(proxy => {
    proxy.disconnect(batteryWatching)
  })
  show()
}

@@ -167,16 +167,26 @@
 settings
+, disabled
 %0A%0Afuncti
@@ -969,16 +969,57 @@
 ble() %7B%0A
+  if (disabled) %7B%0A    disabled = false%0A  
   settin
@@ -1082,16 +1082,18 @@
 ery')%0A  
+ 
+ 
 settings
@@ -1148,16 +1148,18 @@
 update)%0A
+  
   getBat
@@ -1178,16 +1178,18 @@
 %3E %7B%0A    
+ 
+ 
 batteryW
@@ -1244,21 +1244,25 @@
 update)%0A
+  
   %7D)%0A
+  
   update
@@ -1264,16 +1264,20 @@
 pdate()%0A
+  %7D%0A
 %7D%0A%0Afunct
@@ -1288,24 +1288,102 @@
 disable() %7B%0A
+  if(Main.sessionMode.currentMode != 'unlock-dialog') %7B%0A    disabled = true%0A  
   if (settin
@@ -1424,16 +1424,18 @@
 tching)%0A
+  
   getBat
@@ -1454,16 +1454,18 @@
 %3E %7B%0A    
+ 
+ 
 proxy.di
@@ -1490,21 +1490,25 @@
 tching)%0A
+  
   %7D)%0A
+  
   show()
@@ -1508,10 +1508,14 @@
  show()%0A
+  %7D%0A
 %7D%0A

Make extension work on lock screen