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

cs_gestures.py: Cleaning up Python #11726

Merged
merged 1 commit into from
Jun 26, 2023
Merged
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
67 changes: 32 additions & 35 deletions files/usr/share/cinnamon/cinnamon-settings/modules/cs_gestures.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from SettingsWidgets import SidePage, SettingsWidget
from xapp.GSettingsWidgets import *

SCHEMA = "org.cinnamon.gestures";
SCHEMA = "org.cinnamon.gestures"
NON_GESTURE_KEYS = [
"enabled",
"swipe-percent-threshold",
Expand All @@ -18,6 +18,7 @@

DEBUG_SHOW_ALL = False


class Module:
name = "gestures"
category = "prefs"
Expand All @@ -33,7 +34,7 @@ def __init__(self, content_box):

def on_module_selected(self):
have_touchpad = DEBUG_SHOW_ALL
have_touchscreen = DEBUG_SHOW_ALL
have_touchscreen = DEBUG_SHOW_ALL

# Detect devices.
out = subprocess.getoutput("csd-input-helper").replace("\t", " ").split("\n")[:4]
Expand Down Expand Up @@ -70,7 +71,7 @@ def on_module_selected(self):

self.disabled_label = Gtk.Label(expand=True)
box.pack_start(self.disabled_label, False, False, 0)

self.disabled_page_switch = Gtk.Switch(active=self.gesture_settings.get_boolean("enabled"), no_show_all=True)
self.disabled_page_switch.connect("notify::active", self.enabled_switch_changed)
box.pack_start(self.disabled_page_switch, False, False, 0)
Expand All @@ -79,23 +80,25 @@ def on_module_selected(self):
self.disabled_retry_button.connect("clicked", lambda w: self.on_module_selected())
box.pack_start(self.disabled_retry_button, False, False, 0)

ssource = Gio.SettingsSchemaSource.get_default();
schema = ssource.lookup(SCHEMA, True);
all_keys = schema.list_keys();
ssource = Gio.SettingsSchemaSource.get_default()
schema = ssource.lookup(SCHEMA, True)
all_keys = schema.list_keys()

order = [ "left", "right", "up", "down", "in", "out" ]
order = ["left", "right", "up", "down", "in", "out"]

def sort_by_direction(key1, key2):
v1 = 0
v2 = 0
for i in range(0, len(order)):
if order[i] in key1:
for i, k in enumerate(order):
if k in key1:
v1 = i
if order[i] in key2:
if k in key2:
v2 = i

if v1 < v2: return -1
if v1 > v2: return 1
if v1 < v2:
return -1
if v1 > v2:
return 1
return 0

keys = sorted([key for key in all_keys if key not in NON_GESTURE_KEYS], key=cmp_to_key(sort_by_direction))
Expand Down Expand Up @@ -184,7 +187,7 @@ def sort_by_direction(key1, key2):

if have_touchpad or have_touchscreen:
for fingers in range(2, 5):
section = page.add_section(_("Pinch with %d fingers") % fingers)
section = page.add_section(_(f"Pinch with {fingers} fingers"))

for key in keys:
label = self.get_key_label(key, "pinch", fingers)
Expand Down Expand Up @@ -253,13 +256,13 @@ def sort_by_direction(key1, key2):
text = _("The Touchegg service is not running")
self.disabled_retry_button.show()
elif not have_touchpad and not have_touchscreen:
text = _("No compatible devices found")
text = _("No compatible devices found")
self.disabled_retry_button.show()
else:
self.disabled_page_switch.set_visible(True)
text = _("Gestures are disabled")

self.disabled_label.set_markup("<big><b>%s</b></big>" % text)
self.disabled_label.set_markup(f"<big><b>{text}</b></big>")

self.sidePage.stack.set_transition_type(Gtk.StackTransitionType.NONE)

Expand Down Expand Up @@ -306,24 +309,18 @@ def get_key_label(self, key, gtype, fingers):
if gtype != parts[0]:
return None

if gtype == "swipe":
if int(parts[2]) != fingers:
return None
direction = parts[1]
if direction == "left": return _("Left")
elif direction == "right": return _("Right")
elif direction == "up": return _("Up")
elif direction == "down": return _("Down")
elif gtype == "pinch":
gesture_directions = {"left": _("Left"), "right": _("Right"),
"up": _("Up"), "down": _("Down"),
"in": _("In"), "out": _("Out")}
if gtype in ("swipe", "pinch"):
if int(parts[2]) != fingers:
return None
direction = parts[1]
if direction == "in": return _("In")
elif direction == "out": return _("Out")
elif gtype == "tap":
return gesture_directions.get(direction, None)
if gtype == "tap":
if int(parts[1]) != fingers:
return
return _("Tap with %d fingers") % fingers
return None
return _(f"Tap with {fingers} fingers")

return None

Expand All @@ -334,7 +331,7 @@ def test_daemon_alive(self):
None, None)
conn.close_sync(None)
return True
except GLib.Error as e:
except GLib.Error:
pass

return False
Expand Down Expand Up @@ -376,10 +373,10 @@ def __init__(self, label, settings=None, key=None, options=[], size_group=None):

def on_my_value_changed(self, widget):
tree_iter = widget.get_active_iter()
if tree_iter != None:
if tree_iter is not None:
self.value = self.model[tree_iter][0]

if self.value not in list(self.option_map.keys())[0:-1]:
if self.value not in list(self.option_map)[0:-1]:
self.custom_entry.show()
self.settings.set_string(self.key, "EXEC:" + self.custom_entry.get_text())
else:
Expand All @@ -392,7 +389,7 @@ def on_custom_entry_changed(self, entry):
self.settings.set_string(self.key, "EXEC:" + entry.get_text())

def on_setting_changed(self, settings, key):
self.updating_from_setting = True
self.updating_from_setting = True

self.value = settings.get_string(key)
try:
Expand All @@ -403,7 +400,7 @@ def on_setting_changed(self, settings, key):
self.custom_entry.show()
self.custom_entry.set_text(self.value.replace("EXEC:", ""))

self.updating_from_setting = False
self.updating_from_setting = False

def set_options(self, options):
self.model = Gtk.ListStore(str, str)
Expand All @@ -412,4 +409,4 @@ def set_options(self, options):
self.option_map[option[0]] = self.model.append([option[0], option[1]])

self.content_widget.set_model(self.model)
self.content_widget.set_id_column(0)
self.content_widget.set_id_column(0)