Optimization

This commit is contained in:
piotr
2021-01-29 23:51:58 +01:00
parent bb565ffd39
commit d55c5cce52
5 changed files with 94 additions and 37 deletions

View File

@@ -9,6 +9,7 @@ ipc_data = None
outputs = {}
taskbars_list = []
controls_list = []
config_dir = ""
app_dirs = []

View File

@@ -25,6 +25,7 @@ from modules.executor import Executor
from modules.clock import Clock
from modules.controls import Controls
from modules.playerctl import Playerctl
from modules.cpu_avg import CpuAvg
common.sway = os.getenv('SWAYSOCK') is not None
if common.sway:
@@ -51,6 +52,10 @@ def check_tree():
if tree.ipc_data != common.ipc_data:
for item in common.taskbars_list:
item.refresh()
for item in common.controls_list:
if item.popup_window.get_visible():
item.popup_window.hide()
common.ipc_data = common.i3.get_tree().ipc_data
@@ -121,6 +126,10 @@ def instantiate_content(panel, container, content_list):
else:
print("'{}' not defined in this panel instance".format(item))
if item == "cpu-avg":
cpu_avg = CpuAvg()
container.pack_start(cpu_avg, False, False, panel["items-padding"])
def main():
common.config_dir = get_config_dir()
@@ -221,6 +230,7 @@ def main():
inner_box.pack_start(left_box, False, True, 0)
if panel["controls"] and panel["controls-settings"]["alignment"] == "left":
cc = Controls(panel["controls-settings"], panel["position"], panel["controls-settings"]["alignment"], int(w/6))
common.controls_list.append(cc)
left_box.pack_start(cc, False, False, 0)
instantiate_content(panel, left_box, panel["modules-left"])
@@ -237,6 +247,7 @@ def main():
if panel["controls"] and panel["controls-settings"]["alignment"] == "right":
cc = Controls(panel["controls-settings"], panel["position"], panel["controls-settings"]["alignment"], int(w/6))
common.controls_list.append(cc)
right_box.pack_end(cc, False, False, 0)
window.add(vbox)

View File

@@ -13,7 +13,7 @@ import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')
from gi.repository import Gtk, Gdk, GdkPixbuf
from gi.repository import Gtk, Gdk
class Clock(Gtk.EventBox):

View File

@@ -37,6 +37,8 @@ class Controls(Gtk.EventBox):
check_key(settings, "components", ["net", "brightness", "volume", "battery"])
check_key(settings, "net-interface", "")
self.icon_size = settings["icon-size"]
self.net_icon_name = "wtf"
self.net_image = Gtk.Image.new_from_icon_name(self.net_icon_name, Gtk.IconSize.MENU)
self.net_label = Gtk.Label("?") if settings["show-values"] else None
@@ -45,7 +47,6 @@ class Controls(Gtk.EventBox):
self.bri_image = Gtk.Image.new_from_icon_name(self.bri_icon_name, Gtk.IconSize.MENU)
self.bri_label = Gtk.Label("0%") if settings["show-values"] else None
self.bri_slider = None
self.icon_size = settings["icon-size"]
self.vol_icon_name = "wtf"
self.vol_image = Gtk.Image.new_from_icon_name(self.vol_icon_name, Gtk.IconSize.MENU)
@@ -496,12 +497,6 @@ class PopupWindow(Gtk.Window):
def on_window_exit(self, w, e):
self.hide()
def open_menu(self, widget, event, menu, at_widget, position):
if position == "top":
menu.popup_at_widget(at_widget, Gdk.Gravity.SOUTH, Gdk.Gravity.NORTH, None)
else:
menu.popup_at_widget(at_widget, Gdk.Gravity.CENTER, Gdk.Gravity.SOUTH, None)
def switch_menu_box(self, widget, event):
if self.menu_box.get_visible():
@@ -536,39 +531,40 @@ class PopupWindow(Gtk.Window):
return eb
def refresh(self):
if "net" in self.settings["components"] and dependencies["netifaces"]:
ip_addr = get_interface(self.settings["net-interface"])
icon_name = "network-wired-symbolic" if ip_addr else "network-wired-disconnected-symbolic"
if self.get_visible():
if "net" in self.settings["components"] and dependencies["netifaces"]:
ip_addr = get_interface(self.settings["net-interface"])
icon_name = "network-wired-symbolic" if ip_addr else "network-wired-disconnected-symbolic"
if icon_name != self.net_icon_name:
update_image(self.net_image, icon_name, self.icon_size)
self.net_icon_name = icon_name
if icon_name != self.net_icon_name:
update_image(self.net_image, icon_name, self.icon_size)
self.net_icon_name = icon_name
if not ip_addr:
ip_addr = "disconnected"
self.net_label.set_text("{}: {}".format(self.settings["net-interface"], ip_addr))
if not ip_addr:
ip_addr = "disconnected"
self.net_label.set_text("{}: {}".format(self.settings["net-interface"], ip_addr))
if bt_service_enabled() and "bluetooth" in self.settings["components"]:
icon_name = bt_icon_name(bt_on())
if bt_service_enabled() and "bluetooth" in self.settings["components"]:
icon_name = bt_icon_name(bt_on())
if icon_name != self.bt_icon_name:
update_image(self.bt_image, icon_name, self.icon_size)
self.bt_icon_name = icon_name
if icon_name != self.bt_icon_name:
update_image(self.bt_image, icon_name, self.icon_size)
self.bt_icon_name = icon_name
self.bat_label.set_text(bt_name())
if "battery" in self.settings["components"]:
msg, level = get_battery()
icon_name = bat_icon_name(level)
self.bat_label.set_text(bt_name())
if icon_name != self.bat_icon_name:
update_image(self.bat_image, icon_name, self.icon_size)
self.bat_icon_name = icon_name
if "battery" in self.settings["components"]:
msg, level = get_battery()
icon_name = bat_icon_name(level)
self.bat_label.set_text(msg)
if icon_name != self.bat_icon_name:
update_image(self.bat_image, icon_name, self.icon_size)
self.bat_icon_name = icon_name
self.bat_label.set_text(msg)
return True
def on_enter_notify_event(self, widget, event):
widget.get_style_context().set_state(Gtk.StateFlags.SELECTED)
@@ -603,11 +599,6 @@ class PopupWindow(Gtk.Window):
subprocess.Popen('exec {}'.format(cmd), shell=True)
self.hide()
def launch_from_menu(self, w, cmd):
print("From menu '{}'".format(cmd))
subprocess.Popen('exec {}'.format(cmd), shell=True)
self.hide()
def bri_icon_name(value):
icon_name = "display-brightness-low-symbolic"

View File

@@ -0,0 +1,54 @@
#!/usr/bin/env python3
from gi.repository import GLib
import threading
import psutil
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')
from gi.repository import Gtk, Gdk
class CpuAvg(Gtk.EventBox):
def __init__(self):
self.avg = 0
self.cnt = 0
Gtk.EventBox.__init__(self)
self.box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
self.add(self.box)
self.label = Gtk.Label()
self.label.set_property("name", "executor-label")
self.build_box()
Gdk.threads_add_timeout_seconds(GLib.PRIORITY_LOW, 2, self.refresh)
def update_widget(self, val, cnt):
self.label.set_text(val)
self.label.set_tooltip_text("{} checks".format(cnt))
return False
def get_output(self):
try:
val = psutil.cpu_percent(interval=1)
self.avg = self.avg + val
self.cnt += 1
val = "{}%".format(round(self.avg / self.cnt, 1))
GLib.idle_add(self.update_widget, val, str(self.cnt))
except Exception as e:
print(e)
def refresh(self):
thread = threading.Thread(target=self.get_output)
thread.daemon = True
thread.start()
return True
def build_box(self):
self.box.pack_start(self.label, False, False, 4)
self.label.show()