Skip to content

Commit

Permalink
Merge pull request #184 from mildred/master
Browse files Browse the repository at this point in the history
Implement scroll events on workspace
  • Loading branch information
nwg-piotr authored Mar 5, 2023
2 parents 8750520 + 01811b8 commit f9a5847
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 50 deletions.
33 changes: 27 additions & 6 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
#!/usr/bin/env bash

python3 setup.py install --optimize=1
cp nwg-panel.svg /usr/share/pixmaps/
cp nwg-shell.svg /usr/share/pixmaps/
cp nwg-processes.svg /usr/share/pixmaps/
cp nwg-panel-config.desktop /usr/share/applications/
cp nwg-processes.desktop /usr/share/applications/
prefix=
opts=()

while true; do
case "$1" in
--prefix=*)
opts+=("$1")
eval "prefix=${1##--prefix=}"
shift
;;
*)
break
;;
esac
done

opts+=("$@")

: ${prefix:="/usr"}

python3 setup.py install --optimize=1 "${opts[@]}"
mkdir -p $prefix/share/pixmaps/ $prefix/share/applications/
cp nwg-panel.svg $prefix/share/pixmaps/
cp nwg-shell.svg $prefix/share/pixmaps/
cp nwg-processes.svg $prefix/share/pixmaps/
cp nwg-panel-config.desktop $prefix/share/applications/
cp nwg-processes.desktop $prefix/share/applications/
4 changes: 2 additions & 2 deletions nwg_panel/modules/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ def on_button_press(self, widget, event):
def on_scroll(self, widget, event):
if event.direction == Gdk.ScrollDirection.UP and self.settings["on-scroll-up"]:
self.launch(self.settings["on-scroll-up"])
elif event.direction == Gdk.ScrollDirection.DOWN and self.settings["on-scroll-up"]:
self.launch(self.settings["on-scroll-up"])
elif event.direction == Gdk.ScrollDirection.DOWN and self.settings["on-scroll-down"]:
self.launch(self.settings["on-scroll-down"])
else:
print("No command assigned")

Expand Down
124 changes: 82 additions & 42 deletions nwg_panel/modules/sway_workspaces.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3

from gi.repository import Gtk, GdkPixbuf
from gi.repository import Gtk, GdkPixbuf, Gdk

import nwg_panel.common
from nwg_panel.tools import check_key, get_icon_name, update_image, load_autotiling
Expand All @@ -11,6 +11,7 @@ def __init__(self, settings, i3, icons_path):
Gtk.Box.__init__(self, orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
self.settings = settings
self.i3 = i3
self.num_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
self.ws_num2box = {}
self.ws_num2lbl = {}
self.name_label = Gtk.Label()
Expand All @@ -23,7 +24,7 @@ def __init__(self, settings, i3, icons_path):
self.build_box()

def build_box(self):
check_key(self.settings, "numbers", [1, 2, 3, 4, 5, 6, 7, 8])
check_key(self.settings, "numbers", [])
check_key(self.settings, "custom-labels", [])
check_key(self.settings, "focused-labels", [])
check_key(self.settings, "show-icon", True)
Expand All @@ -32,6 +33,7 @@ def build_box(self):
check_key(self.settings, "name-length", 40)
check_key(self.settings, "mark-autotiling", True)
check_key(self.settings, "mark-content", True)
check_key(self.settings, "hide-empty", False)
check_key(self.settings, "show-layout", True)
check_key(self.settings, "angle", 0.0)
if self.settings["angle"] != 0.0:
Expand All @@ -40,7 +42,7 @@ def build_box(self):
# prevent from #142
ws_num = -1
if self.i3.get_tree().find_focused():
ws_num, win_name, win_id, non_empty, win_layout = self.find_details()
ws_num, win_name, win_id, non_empty, win_layout, numbers = self.find_details()

if len(self.settings["custom-labels"]) == 1 or len(self.settings["custom-labels"]) == len(self.settings["numbers"]):
self.settings["custom-labels"] *= len(self.settings["numbers"])
Expand All @@ -52,42 +54,18 @@ def build_box(self):
else:
self.settings["focused-labels"] = []

for idx, num in enumerate(self.settings["numbers"]):
eb = Gtk.EventBox()
eb.connect("enter_notify_event", self.on_enter_notify_event)
eb.connect("leave_notify_event", self.on_leave_notify_event)
eb.connect("button-release-event", self.on_click, num)
self.pack_start(eb, False, False, 0)

box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
if self.settings["angle"] != 0.0:
box.set_orientation(Gtk.Orientation.VERTICAL)
eb.add(box)
self.pack_start(self.num_box, False, False, 0)

for idx, num in enumerate(self.settings["numbers"]):
if num == str(ws_num) and self.settings["focused-labels"]:
label = self.settings["focused-labels"][idx]
elif self.settings["custom-labels"]:
label = self.settings["custom-labels"][idx]
else:
label = str(num)

if self.settings["mark-autotiling"]:
try:
at = int(num) in self.autotiling
except:
at = False
autotiling = "a" if at in self.autotiling else ""
lbl = Gtk.Label("{}{}".format(autotiling, label))
else:
lbl = Gtk.Label("{}".format(label))
if self.settings["angle"] != 0.0:
lbl.set_angle(self.settings["angle"])
self.name_label.set_angle(self.settings["angle"])

self.ws_num2box[num] = eb
self.ws_num2lbl[num] = lbl

box.pack_start(lbl, False, False, 6)
eb, lbl = self.build_number(num, label)
self.num_box.pack_start(eb, False, False, 0)

if num == str(ws_num):
eb.set_property("name", "task-box-focused")
Expand All @@ -103,27 +81,82 @@ def build_box(self):
if self.settings["show-layout"]:
self.pack_start(self.layout_icon, False, False, 6)

def build_number(self, num, label):
eb = Gtk.EventBox()
eb.connect("enter_notify_event", self.on_enter_notify_event)
eb.connect("leave_notify_event", self.on_leave_notify_event)
eb.connect("button-release-event", self.on_click, num)
eb.add_events(Gdk.EventMask.SCROLL_MASK)
eb.connect('scroll-event', self.on_scroll)

box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
if self.settings["angle"] != 0.0:
box.set_orientation(Gtk.Orientation.VERTICAL)
eb.add(box)

if self.settings["mark-autotiling"]:
try:
at = int(num) in self.autotiling
except:
at = False
autotiling = "a" if at in self.autotiling else ""
lbl = Gtk.Label("{}{}".format(autotiling, label))
else:
lbl = Gtk.Label("{}".format(label))
if self.settings["angle"] != 0.0:
lbl.set_angle(self.settings["angle"])
self.name_label.set_angle(self.settings["angle"])

self.ws_num2box[num] = eb
self.ws_num2lbl[num] = lbl

box.pack_start(lbl, False, False, 6)

return eb, lbl

def refresh(self):
if self.i3.get_tree().find_focused():
ws_num, win_name, win_id, non_empty, win_layout = self.find_details()
ws_num, win_name, win_id, non_empty, win_layout, numbers = self.find_details()

if len(self.settings["numbers"]) > 0:
numbers = self.settings["numbers"]

if ws_num > 0:
for idx, num in enumerate(self.settings["numbers"]):
if num == str(ws_num) and self.settings["focused-labels"]:
for num in self.ws_num2lbl:
self.ws_num2lbl[num].hide()

for _idx, num in enumerate(numbers):
idx = None
if num in self.settings["numbers"]:
idx = self.settings["numbers"].index(num)
try:
int_num = int(num)
except:
int_num = 0

if idx is None:
text = str(num)
elif num == str(ws_num) and self.settings["focused-labels"]:
text = self.settings["focused-labels"][idx]
elif self.settings["custom-labels"]:
text = self.settings["custom-labels"][idx]
else:
text = str(num)


if num not in self.ws_num2lbl:
eb, lbl = self.build_number(num, text)
self.num_box.pack_start(eb, False, False, 0)
eb.show_all()

lbl = self.ws_num2lbl[num]

if not self.settings["hide-empty"] or int_num in non_empty or num == str(ws_num):
lbl.show()
else:
lbl.hide()

# mark non-empty WS with a dot
if self.settings["mark-content"]:
try:
int_num = int(num)
except:
int_num = 0
if int_num in non_empty:
if not text.endswith("."):
text += "."
Expand Down Expand Up @@ -195,11 +228,12 @@ def find_details(self):
win_name = ""
win_id = "" # app_id if available, else window_class
layout = None
numbers = []

for ws in workspaces:
numbers.append(str(ws.num))
if ws.focused:
ws_num = ws.num
break

non_empty = []
if self.settings["show-name"] or self.settings["show-icon"]:
Expand All @@ -215,7 +249,7 @@ def find_details(self):
for item in tree.descendants():
if item.type == "workspace":
# find non-empty workspaces
if self.settings["mark-content"]:
if self.settings["mark-content"] or self.settings["hide-empty"]:
tasks_num = 0
for d in item.descendants():
if d.type == "con" and d.name:
Expand All @@ -239,11 +273,17 @@ def find_details(self):
if not layout:
layout = f.parent.layout

return ws_num, win_name, win_id, non_empty, layout
return ws_num, win_name, win_id, non_empty, layout, numbers

def on_click(self, event_box, event_button, num):
nwg_panel.common.i3.command("workspace number {}".format(num))

def on_scroll(self, event_box, event):
if event.direction == Gdk.ScrollDirection.UP:
nwg_panel.common.i3.command("workspace prev")
elif event.direction == Gdk.ScrollDirection.DOWN:
nwg_panel.common.i3.command("workspace next")

def on_enter_notify_event(self, widget, event):
widget.set_state_flags(Gtk.StateFlags.DROP_ACTIVE, clear=False)
widget.set_state_flags(Gtk.StateFlags.SELECTED, clear=False)
Expand Down

0 comments on commit f9a5847

Please sign in to comment.