Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smart trimming of window titles #9

Open
wants to merge 1 commit into
base: pygi-python3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions dbx_preference
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,9 @@ class PrefDialog():
self.preview_keep_cb = Gtk.CheckButton.new_with_label(_("Keep previews for minimized windows (experimental)"))
self.preview_keep_cb.connect("toggled", self.__checkbutton_toggled, "preview_keep")
vbox.pack_start(self.preview_keep_cb, False, True, 0)
self.preview_trim_cb = Gtk.CheckButton.new_with_label(_("Smart title trimming"))
self.preview_trim_cb.connect("toggled", self.__checkbutton_toggled, "preview_trim")
vbox.pack_start(self.preview_trim_cb, False, True, 0)
popup_box.pack_start(vbox, False, True, 5)

self.preview_size_spinbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 0)
Expand Down Expand Up @@ -1769,6 +1772,8 @@ class PrefDialog():
self.preview_cb.set_active(self.globals.settings["preview"])
self.preview_keep_cb.set_active(self.globals.settings["preview_keep"])
self.preview_keep_cb.set_sensitive(self.globals.settings["preview"])
self.preview_trim_cb.set_active(self.globals.settings["preview_trim"])
self.preview_trim_cb.set_sensitive(self.globals.settings["preview"])
self.preview_size_spin.set_value(self.globals.settings["preview_size"])
self.preview_size_spin.set_sensitive(self.globals.settings["preview"])
self.window_title_width_spin.set_value(
Expand Down
46 changes: 46 additions & 0 deletions dockbarx/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,47 @@
DBusGMainLoop(set_as_default=True) # for async calls
BUS = dbus.SessionBus()

def find_common_prefix(lst):
if not lst: return ""
for pos in range(len(lst[0])+1):
current = lst[0][:pos]
if not all([x.startswith(current) for x in lst]):
return lst[0][:pos-1]
return lst[0]

def find_common_suffix(lst):
if not lst: return ""
for pos in range(1, len(lst[0])+1):
current = lst[0][-pos:]
if not all([x.endswith(current) for x in lst]):
if pos == 1: return ""
return lst[0][-pos+1:]
return lst[0]

def test_find_common_functions():
tests_prefix = [
(["abc", "abd", "abe", "abcdefgh"], "ab"),
(["abc", "abd", "abe", "vabcdefgh"], ""),
(["abcdefghi", "abd", "abe", "abcdefgh"], "ab"),
([], ""),
(["abc"], "abc"),
(["abc", "abc", "abc"], "abc")
]
for lst, expected in tests_prefix:
p = find_common_prefix(lst)
assert p == expected, f"find_common_prefix({lst}) should be {expected}, was {p}"
tests_suffix = [
(["xyz", "ayz", "byz"], "yz"),
(["xyz", "xyz", "xyz"], "xyz"),
(["xyz", "ayzs", "byz"], ""),
([], ""),
(["xyz"], "xyz"),
(["xyzzzzzzzzz", "ayz", "byz"], "z")
]
for lst, expected in tests_suffix:
p = find_common_suffix(lst)
assert p == expected, f"find_common_suffix({lst}) should be {expected}, was {p}"

# workaround for an old version python-xlib bug, see Issue 113
try:
XDisplay = display.Display()
Expand Down Expand Up @@ -643,6 +684,8 @@ class Globals(GObject.GObject):
None,()),
"keep-previews-changed": (GObject.SignalFlags.RUN_FIRST,
None,()),
"trim-previews-changed": (GObject.SignalFlags.RUN_FIRST,
None,()),
"preview-size-changed": (GObject.SignalFlags.RUN_FIRST,
None,()),
"window-title-width-changed": (GObject.SignalFlags.RUN_FIRST,
Expand Down Expand Up @@ -694,6 +737,7 @@ class Globals(GObject.GObject):
"preview": True,
"preview_size": 150,
"preview_keep": False,
"preview_trim": False,
"old_menu": False,
"show_close_button": True,
"locked_list_in_menu": True,
Expand Down Expand Up @@ -925,6 +969,8 @@ def __on_gsettings_changed(self, settings, gkey, data=None):
self.emit("preview-size-changed")
elif "preview_keep" == key:
self.emit("keep-previews-changed")
elif "preview_trim" == key:
self.emit("trim-previews-changed")
elif "window_title_width" == key:
self.emit("window-title-width-changed")
elif "groupbutton_show_tooltip" == key:
Expand Down
33 changes: 33 additions & 0 deletions dockbarx/groupbutton.py
Original file line number Diff line number Diff line change
Expand Up @@ -2511,6 +2511,33 @@ def destroy(self, *args, **kvargs):
self.globals.disconnect(self.globals_events.pop())
Gtk.Box.destroy(self, *args, **kvargs)

def __recalculate_item_titles(self):
"""Implements 'smart title trimming'
That is: in a window list with titles
stuart@machine: ~ - Terminal
stuart@machine: ~/Pictures - Terminal
stuart@machine: ~/Documents - Terminal
stuart@machine: ~/.local/share - Terminal
it will remove common prefixes and suffixes, resulting in
… ~ …
… ~/Pictures …
… ~/Documents …
… ~/.local/share …
"""
if not self.globals.settings["preview_trim"]: return
group = self.group_r()
if not group: return
window_names = [str(i.item.window_r().wnck.get_name()) for i in group]
common_prefix = find_common_prefix(window_names)
common_suffix = find_common_suffix(window_names)
# Don't strip off short prefixes
if len(common_prefix) < 4: common_prefix = ""
if len(common_suffix) < 4: common_suffix = ""
for i in group:
i.item.common_prefix = common_prefix
i.item.common_suffix = common_suffix
i.item.name_changed()

def show_all(self):
group = self.group_r()
for window in group:
Expand Down Expand Up @@ -2546,12 +2573,14 @@ def can_be_shown(self):
return False

def add_item(self, item):
self.__recalculate_item_titles()
if self.show_previews:
item.update_preview_size()
item.set_show_preview(self.show_previews)
self.window_box.pack_start(item, True, True, 0)

def reorder_item(self, index, item):
self.__recalculate_item_titles()
self.window_box.reorder_child(item, index)

def shrink_size(self, locked_popup=False):
Expand Down Expand Up @@ -2597,6 +2626,7 @@ def __on_show_previews_changed(self, arg=None):
self.set_show_previews(self.globals.settings["preview"])

def __rebuild_list(self, locked_popup_list=False):
self.__recalculate_item_titles()
oldbox = self.window_box
if self.mini_mode:
self.window_box = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 2)
Expand Down Expand Up @@ -2670,6 +2700,7 @@ def remove_plugin(self, plugin):

#### Mini list
def apply_mini_mode(self):
self.__recalculate_item_titles()
group = self.group_r()
self.set_spacing(0)
self.title.set_no_show_all(True)
Expand All @@ -2681,6 +2712,7 @@ def apply_mini_mode(self):
self.__rebuild_list(True)

def apply_normal_mode(self):
self.__recalculate_item_titles()
self.set_spacing(2)
self.title.set_no_show_all(False)
self.title.show()
Expand Down Expand Up @@ -3050,3 +3082,4 @@ def __on_item_activated(self, *args):
def __on_submenu_toggled(self, *args):
self.emit("menu-resized")


9 changes: 9 additions & 0 deletions dockbarx/windowbutton.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,9 @@ def __init__(self, window, group):
self.press_sid = None
self.pressed = False

self.common_prefix = ""
self.common_suffix = ""

self.area.set_needs_attention(window.wnck.needs_attention())

self.close_button = CairoCloseButton()
Expand Down Expand Up @@ -409,6 +412,8 @@ def __init__(self, window, group):
self.__update_label))
self.globals_events.append(self.globals.connect("keep-previews-changed",
self.__clear_saved_preview))
self.globals_events.append(self.globals.connect("trim-previews-changed",
self.__clear_saved_preview))
self.globals_events.append(self.globals.connect("show-previews-changed",
self.__clear_saved_preview))

Expand Down Expand Up @@ -452,6 +457,10 @@ def __update_label(self, arg=None):
window = self.window_r()
group = self.group_r()
text = escape(str(window.wnck.get_name()))
if text.startswith(self.common_prefix) and self.common_prefix:
text = "…" + text[len(self.common_prefix):]
if text.endswith(self.common_suffix) and self.common_suffix:
text = text[:-len(self.common_suffix)] + "…"
if window.wnck.is_minimized():
color = self.globals.colors["color4"]
else:
Expand Down
8 changes: 8 additions & 0 deletions org.dockbar.dockbarx.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@
</description>
</key>

<key name='preview-trim' type='b'>
<default>false</default>
<summary>Smart title trimming</summary>
<description>
Remove common elements in a list of window titles
</description>
</key>

<key name='old-menu' type='b'>
<default>false</default>
<summary>Use the old gtk style menu</summary>
Expand Down