formatting; Controls popup attached to monitor

This commit is contained in:
piotr
2021-01-30 04:44:25 +01:00
parent f4d3bcd965
commit cac891110b
9 changed files with 110 additions and 86 deletions

View File

@@ -23,7 +23,7 @@ dependencies = {
"netifaces": False,
}
icons_path = "" # "icons_light", "icons_dark" or "" (GTK icons)
icons_path = "" # "icons_light", "icons_dark" or "" (GTK icons)
commands = {
"get_battery": "upower -i $(upower -e | grep BAT) | grep --color=never -E 'state|to\\\\ full|to\\\\ empty|percentage'",
@@ -39,4 +39,4 @@ commands = {
"set_volume_alt": "amixer sset Master",
"systemctl": "systemctl",
"playerctl": "playerctl"
}
}

View File

@@ -30,16 +30,18 @@ from modules.cpu_avg import CpuAvg
common.sway = os.getenv('SWAYSOCK') is not None
if common.sway:
from i3ipc import Connection
common.i3 = Connection()
from modules.sway_taskbar import SwayTaskbar
from modules.sway_workspaces import SwayWorkspaces
try:
from pyalsa import alsamixer
common.dependencies["pyalsa"] = True
except:
print("pylsa module not found, will try amixer")
restart_cmd = ""
@@ -59,13 +61,13 @@ def check_tree():
if old != 0 and old != new:
print("Number of outputs changed")
restart()
# Do if tree changed
tree = common.i3.get_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()
@@ -94,13 +96,13 @@ def instantiate_content(panel, container, content_list):
taskbar = SwayTaskbar(panel["sway-taskbar"], common.i3, panel["position"],
display_name="{}".format(panel["output"]))
common.taskbars_list.append(taskbar)
container.pack_start(taskbar, False, False, panel["items-padding"])
else:
print("'sway-taskbar' ignored")
else:
print("'sway-taskbar' not defined in this panel instance")
if item == "sway-workspaces":
if common.sway:
if "sway-workspaces" in panel:
@@ -116,14 +118,14 @@ def instantiate_content(panel, container, content_list):
container.pack_start(button, False, False, panel["items-padding"])
else:
print("'{}' not defined in this panel instance".format(item))
if "executor-" in item:
if item in panel:
executor = Executor(panel[item])
container.pack_start(executor, False, False, panel["items-padding"])
else:
print("'{}' not defined in this panel instance".format(item))
if item == "clock":
if item in panel:
clock = Clock(panel[item])
@@ -131,7 +133,7 @@ def instantiate_content(panel, container, content_list):
else:
clock = Clock({})
container.pack_start(clock, False, False, panel["items-padding"])
if item == "playerctl":
if item in panel:
playerctl = Playerctl(panel[item])
@@ -146,7 +148,7 @@ def instantiate_content(panel, container, content_list):
def main():
common.config_dir = get_config_dir()
parser = argparse.ArgumentParser()
parser.add_argument("-c",
"--config",
@@ -174,7 +176,7 @@ def main():
except:
pass
save_string(str(os.getpid()), pid_file)
common.app_dirs = get_app_dirs()
common.dependencies["upower"] = is_command("upower")
@@ -214,10 +216,7 @@ def main():
# setups. Otherwise moving the pointer between displays over the panels remains undetected,
# and the Controls window may appear on the previous output.
if "output" in panel and panel["output"] and "width" not in panel:
if len(common.outputs) > 1:
panel["width"] = common.outputs[panel["output"]]["width"] - 12
else:
panel["width"] = common.outputs[panel["output"]]["width"]
panel["width"] = common.outputs[panel["output"]]["width"]
check_key(panel, "width", 0)
w = panel["width"]
@@ -245,7 +244,14 @@ def main():
left_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=panel["spacing"])
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))
monitor = None
try:
monitor = common.outputs[panel["output"]]["monitor"]
except KeyError:
pass
cc = Controls(panel["controls-settings"], panel["position"], panel["controls-settings"]["alignment"],
int(w / 6), monitor=monitor)
common.controls_list.append(cc)
left_box.pack_start(cc, False, False, 0)
instantiate_content(panel, left_box, panel["modules-left"])
@@ -262,7 +268,14 @@ def main():
instantiate_content(panel, right_box, panel["modules-right"])
if panel["controls"] and panel["controls-settings"]["alignment"] == "right":
cc = Controls(panel["controls-settings"], panel["position"], panel["controls-settings"]["alignment"], int(w/6))
monitor = None
try:
monitor = common.outputs[panel["output"]]["monitor"]
except KeyError:
pass
cc = Controls(panel["controls-settings"], panel["position"], panel["controls-settings"]["alignment"],
int(w / 6), monitor=monitor)
common.controls_list.append(cc)
right_box.pack_end(cc, False, False, 0)
@@ -277,7 +290,9 @@ def main():
pass
o = panel["output"] if "output" in panel else "undefined"
print("Display: {}, position: {}, layer: {}, width: {}, height: {}".format(o, panel["position"], panel["layer"], panel["width"], panel["height"]))
print("Display: {}, position: {}, layer: {}, width: {}, height: {}".format(o, panel["position"],
panel["layer"], panel["width"],
panel["height"]))
if monitor:
GtkLayerShell.set_monitor(window, monitor)
@@ -303,13 +318,13 @@ def main():
GtkLayerShell.set_anchor(window, GtkLayerShell.Edge.BOTTOM, 1)
window.show_all()
#window.connect('destroy', Gtk.main_quit)
# window.connect('destroy', Gtk.main_quit)
if common.key_missing:
print("Saving amended config")
save_json(panels, os.path.join(common.config_dir, "config_amended"))
#GLib.timeout_add(100, listener_reply)
# GLib.timeout_add(100, listener_reply)
if common.sway:
Gdk.threads_add_timeout(GLib.PRIORITY_DEFAULT_IDLE, 150, check_tree)

View File

@@ -36,12 +36,12 @@ class Clock(Gtk.EventBox):
if "css-name" in settings:
self.label.set_property("name", settings["css-name"])
if "format" not in settings:
self.settings["format"] = "%a, %d. %b %H:%M:%S"
if settings["on-left-click"] or settings["on-right-click"] or settings["on-middle-click"] or settings[
"on-scroll-up"] or settings["on-scroll-down"]:
"on-scroll-up"] or settings["on-scroll-down"]:
self.connect('button-press-event', self.on_button_press)
self.add_events(Gdk.EventMask.SCROLL_MASK)
self.connect('scroll-event', self.on_scroll)

View File

@@ -4,6 +4,7 @@ import threading
import subprocess
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')
gi.require_version('GtkLayerShell', '0.1')
@@ -16,13 +17,14 @@ from nwg_panel.common import dependencies
try:
import netifaces
dependencies["netifaces"] = True
except ModuleNotFoundError:
pass
class Controls(Gtk.EventBox):
def __init__(self, settings, position, alignment, width):
def __init__(self, settings, position, alignment, width, monitor=None):
self.settings = settings
self.position = position
self.alignment = alignment
@@ -65,7 +67,7 @@ class Controls(Gtk.EventBox):
self.box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
self.add(self.box)
self.popup_window = PopupWindow(position, settings, width)
self.popup_window = PopupWindow(position, settings, width, monitor=monitor)
self.connect('button-press-event', self.on_button_press)
self.connect('enter-notify-event', self.on_enter_notify_event)
@@ -108,19 +110,19 @@ class Controls(Gtk.EventBox):
box.pack_start(self.bt_image, False, False, 4)
if self.bt_label:
box.pack_start(self.bt_label, False, False, 0)
if item == "battery":
box.pack_start(self.bat_image, False, False, 4)
if self.bat_label:
box.pack_start(self.bat_label, False, False, 0)
box.pack_start(self.pan_image, False, False, 4)
def get_output(self):
if "net" in self.settings["components"] and self.settings["net-interface"]:
ip = get_interface(self.settings["net-interface"])
GLib.idle_add(self.update_net, ip)
if bt_service_enabled() and "bluetooth" in self.settings["components"]:
is_on = bt_on()
name = bt_name()
@@ -135,7 +137,7 @@ class Controls(Gtk.EventBox):
print("Couldn't get brightness, is 'light' installed?")
except Exception as e:
print(e)
if "volume" in self.settings["components"]:
try:
value, switch = get_volume()
@@ -173,18 +175,18 @@ class Controls(Gtk.EventBox):
if icon_name != self.net_icon_name:
update_image(self.net_image, icon_name, self.icon_size)
self.net_icon_name = icon_name
if self.net_label:
self.net_label.set_text("{}".format(self.settings["net-interface"]))
def update_bt(self, is_on, name):
icon_name = "bluetooth-active-symbolic" if is_on else "bluetooth-disabled-symbolic"
if icon_name != self.bt_icon_name:
update_image(self.bt_image, icon_name, self.icon_size)
if self.bt_label:
self.bt_label.set_text(name)
def update_brightness(self, value):
icon_name = bri_icon_name(value)
@@ -194,20 +196,20 @@ class Controls(Gtk.EventBox):
if self.bri_label:
self.bri_label.set_text("{}%".format(value))
def update_battery(self, value):
icon_name = bat_icon_name(value)
if icon_name != self.bat_icon_name:
update_image(self.bat_image, icon_name, self.icon_size)
self.bat_icon_name = icon_name
if self.bat_label:
self.bat_label.set_text("{}%".format(value))
def update_volume(self, value, switch):
icon_name = vol_icon_name(value, switch)
if icon_name != self.vol_icon_name:
update_image(self.vol_image, icon_name, self.settings["icon-size"])
self.vol_icon_name = icon_name
@@ -238,19 +240,22 @@ class Controls(Gtk.EventBox):
class PopupWindow(Gtk.Window):
def __init__(self, position, settings, width):
def __init__(self, position, settings, width, monitor=None):
Gtk.Window.__init__(self, type_hint=Gdk.WindowTypeHint.NORMAL)
GtkLayerShell.init_for_window(self)
if monitor:
GtkLayerShell.set_monitor(self, monitor)
check_key(settings, "css-name", "controls-window")
self.set_property("name", settings["css-name"])
self.icon_size = settings["icon-size"]
self.settings = settings
self.position = position
self.bt_icon_name = ""
self.bt_image = Gtk.Image()
eb = Gtk.EventBox()
eb.set_above_child(False)
self.connect("leave_notify_event", self.on_window_exit)
@@ -281,14 +286,14 @@ class PopupWindow(Gtk.Window):
GtkLayerShell.set_anchor(self, GtkLayerShell.Edge.BOTTOM, True)
else:
GtkLayerShell.set_anchor(self, GtkLayerShell.Edge.TOP, True)
check_key(settings, "commands", {"battery": "", "net": ""})
add_sep = False
if "brightness" in settings["components"]:
inner_hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
v_box.pack_start(inner_hbox, False, False, 0)
self.bri_icon_name = "wtf"
self.bri_image = Gtk.Image.new_from_icon_name(self.bri_icon_name, Gtk.IconSize.MENU)
@@ -306,7 +311,7 @@ class PopupWindow(Gtk.Window):
inner_hbox.pack_start(scale, True, True, 5)
add_sep = True
if "volume" in settings["components"]:
inner_hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
v_box.pack_start(inner_hbox, False, False, 6)
@@ -330,7 +335,7 @@ class PopupWindow(Gtk.Window):
inner_hbox.pack_start(scale, True, True, 5)
add_sep = True
if add_sep:
sep = Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL)
v_box.pack_start(sep, True, True, 10)
@@ -403,7 +408,7 @@ class PopupWindow(Gtk.Window):
inner_hbox.pack_end(img, False, True, 4)
event_box.add(inner_vbox)
if "battery" in settings["components"]:
event_box = Gtk.EventBox()
if "battery" in settings["commands"] and settings["commands"]["battery"]:
@@ -411,7 +416,7 @@ class PopupWindow(Gtk.Window):
event_box.connect("leave_notify_event", self.on_leave_notify_event)
event_box.connect('button-press-event', self.launch, settings["commands"]["battery"])
inner_vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
inner_hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
inner_vbox.pack_start(inner_hbox, True, True, 6)
@@ -428,10 +433,10 @@ class PopupWindow(Gtk.Window):
self.bat_icon_name = icon_name
inner_hbox.pack_start(self.bat_image, False, False, 6)
self.bat_label = Gtk.Label(msg)
inner_hbox.pack_start(self.bat_label, False, True, 6)
if "battery" in settings["commands"] and settings["commands"]["battery"]:
img = Gtk.Image()
update_image(img, "pan-end-symbolic", self.icon_size)
@@ -448,7 +453,7 @@ class PopupWindow(Gtk.Window):
check_key(settings, "menu", {})
if settings["menu"]:
template = settings["menu"]
sep = Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL)
v_box.pack_start(sep, True, True, 10)
@@ -466,7 +471,7 @@ class PopupWindow(Gtk.Window):
check_key(template, "name", "Menu name")
label = Gtk.Label(template["name"])
inner_hbox.pack_start(label, False, False, 6)
check_key(template, "items", [])
if template["items"]:
img = Gtk.Image()
@@ -497,13 +502,13 @@ class PopupWindow(Gtk.Window):
def on_window_exit(self, w, e):
self.hide()
def switch_menu_box(self, widget, event):
if self.menu_box.get_visible():
self.menu_box.hide()
else:
self.menu_box.show_all()
def custom_item(self, name, icon, cmd):
eb = Gtk.EventBox()
@@ -523,13 +528,13 @@ class PopupWindow(Gtk.Window):
eb.connect("enter_notify_event", self.on_enter_notify_event)
eb.connect("leave_notify_event", self.on_leave_notify_event)
eb.connect('button-press-event', self.launch, cmd)
img = Gtk.Image()
update_image(img, "pan-end-symbolic", self.icon_size)
h_box.pack_end(img, False, True, 4)
return eb
def refresh(self):
if self.get_visible():
if "net" in self.settings["components"] and dependencies["netifaces"]:
@@ -585,10 +590,10 @@ class PopupWindow(Gtk.Window):
if icon_name != self.vol_icon_name:
update_image(self.vol_image, icon_name, self.icon_size)
self.vol_icon_name = icon_name
def close_win(self, w, e):
self.hide()
def handle_keyboard(self, w, e):
if e.type == Gdk.EventType.KEY_RELEASE and e.keyval == Gdk.KEY_Escape:
self.close_win(w, e)

View File

@@ -46,7 +46,7 @@ class Executor(Gtk.EventBox):
self.set_tooltip_text(settings["tooltip-text"])
if settings["on-left-click"] or settings["on-right-click"] or settings["on-middle-click"] or settings[
"on-scroll-up"] or settings["on-scroll-down"]:
"on-scroll-up"] or settings["on-scroll-down"]:
self.connect('button-press-event', self.on_button_press)
self.add_events(Gdk.EventMask.SCROLL_MASK)
self.connect('scroll-event', self.on_scroll)
@@ -83,7 +83,7 @@ class Executor(Gtk.EventBox):
self.label.set_text(output[0].strip())
if not self.label.get_visible():
self.label.show()
elif len(output) == 2:
new_path = output[0].strip()
if new_path != self.icon_path:
@@ -96,7 +96,7 @@ class Executor(Gtk.EventBox):
print("Failed setting image from {}".format(output[0].strip()))
if not self.image.get_visible():
self.image.show()
self.label.set_text(output[1].strip())
else:
if self.image.get_visible():

View File

@@ -30,7 +30,7 @@ class Playerctl(Gtk.EventBox):
if self.settings["button-css-name"]:
self.play_pause_btn.set_property("name", self.settings["button-css-name"])
self.status = ""
self.retries = 2 # to avoid hiding the module on forward / backward btn when playing from the browser
self.retries = 2 # to avoid hiding the module on forward / backward btn when playing from the browser
check_key(settings, "interval", 0)
check_key(settings, "label-css-name", "")
@@ -54,14 +54,16 @@ class Playerctl(Gtk.EventBox):
self.retries = 2
if not self.get_visible():
self.show()
if not self.status == status:
if status == "Playing":
update_image(self.play_pause_btn.get_image(), "media-playback-pause-symbolic", self.settings["icon-size"])
update_image(self.play_pause_btn.get_image(), "media-playback-pause-symbolic",
self.settings["icon-size"])
elif status == "Paused":
update_image(self.play_pause_btn.get_image(), "media-playback-start-symbolic", self.settings["icon-size"])
update_image(self.play_pause_btn.get_image(), "media-playback-start-symbolic",
self.settings["icon-size"])
metadata = "{} - paused".format(metadata)
self.label.set_text(metadata)
else:
if self.get_visible():

View File

@@ -42,12 +42,12 @@ class SwayTaskbar(Gtk.Box):
for item in i3_tree:
if item.type == "output" and not item.name.startswith("__"):
displays_tree.append(item)
# sort by x, y coordinates
displays_tree = sorted(displays_tree, key=lambda d: (d.rect.x, d.rect.y))
return displays_tree
def build_box(self):
self.displays_tree = self.list_tree()
@@ -61,7 +61,7 @@ class SwayTaskbar(Gtk.Box):
self.ws_box.pack_start(win_box, False, False, 0)
self.pack_start(self.ws_box, False, False, 0)
self.show_all()
def refresh(self):
for item in self.get_children():
item.destroy()
@@ -81,7 +81,7 @@ class WorkspaceBox(Gtk.Box):
widget = Gtk.Label("{}:".format(con.num))
self.pack_start(widget, False, False, 4)
def on_click(self, button):
nwg_panel.common.i3.command("{} number {} focus".format(self.con.type, self.con.num))
@@ -96,7 +96,7 @@ class WindowBox(Gtk.EventBox):
self.add(self.box)
self.con = con
self.pid = con.pid
self.old_name = ""
if con.focused:
@@ -115,11 +115,13 @@ class WindowBox(Gtk.EventBox):
icon_from_desktop = get_icon(name)
if icon_from_desktop:
if "/" not in icon_from_desktop and not icon_from_desktop.endswith(".svg") and not icon_from_desktop.endswith(".png"):
if "/" not in icon_from_desktop and not icon_from_desktop.endswith(
".svg") and not icon_from_desktop.endswith(".png"):
image = Gtk.Image()
update_image(image, icon_from_desktop, settings["image-size"])
else:
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(icon_from_desktop, settings["image-size"], settings["image-size"])
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(icon_from_desktop, settings["image-size"],
settings["image-size"])
image = Gtk.Image.new_from_pixbuf(pixbuf)
self.box.pack_start(image, False, False, 4)
@@ -150,12 +152,12 @@ class WindowBox(Gtk.EventBox):
else:
image = Gtk.Image()
update_image(image, "window-new", 16)
self.box.pack_start(image, False, False, 4)
def on_enter_notify_event(self, widget, event):
self.get_style_context().set_state(Gtk.StateFlags.SELECTED)
def on_leave_notify_event(self, widget, event):
self.get_style_context().set_state(Gtk.StateFlags.NORMAL)
@@ -190,15 +192,15 @@ class WindowBox(Gtk.EventBox):
item = Gtk.MenuItem(text)
item.connect("activate", self.execute, i)
menu.append(item)
item = Gtk.SeparatorMenuItem()
menu.append(item)
item = Gtk.MenuItem("Kill")
item.connect("activate", self.kill)
menu.append(item)
return menu
def con_ws_num(self, con):
ws_num = 0
leave = con
@@ -208,16 +210,16 @@ class WindowBox(Gtk.EventBox):
leave = parent
if leave.type == "workspace":
ws_num = leave.num
return ws_num
def execute(self, item, ws_num):
cmd = "[con_id=\"{}\"] move to workspace number {}".format(self.con.id, ws_num)
nwg_panel.common.i3.command(cmd)
cmd = "[con_id=\"{}\"] focus".format(self.con.id)
nwg_panel.common.i3.command(cmd)
def kill(self, item):
cmd = "[con_id=\"{}\"] kill".format(self.con.id)
nwg_panel.common.i3.command(cmd)

View File

@@ -18,7 +18,7 @@ class SwayWorkspaces(Gtk.Box):
btn = Gtk.Button.new_with_label("{}".format(num))
btn.connect("clicked", self.on_click, num)
self.pack_start(btn, False, False, 0)
self.show_all()
def on_click(self, button, num):

View File

@@ -168,10 +168,10 @@ def list_outputs(silent=False):
y = int(x_y[1])
if name is not None and w is not None and h is not None and x is not None and y is not None:
outputs_dict[name] = {'name': name,
'x': x,
'y': y,
'width': w,
'height': h}
'x': x,
'y': y,
'width': w,
'height': h}
else:
print("'wlr-randr' command not found, terminating")
sys.exit(1)