Working on making modes nicer

This commit is contained in:
Roberto Alsina 2022-06-08 10:27:34 -03:00
parent cb6d3932cd
commit afc6067d2b
5 changed files with 25 additions and 13 deletions

View File

@ -1,7 +1,7 @@
name: CI
on:
[push, pull_request]
[push]
jobs:
build:

2
TODO.md Normal file
View File

@ -0,0 +1,2 @@
* Parse refresh rate for modes and use it
* Test using older xrandr

View File

@ -20,6 +20,8 @@ def test_parse_modes(test_data):
data = test_data.read("monitor_1.txt", deserialize=False).splitlines()
m = Monitor(data)
assert len(m.modes) == 9
assert "0x56" in m.modes
assert str(m.modes["0x56"]) == "1920x1080 (0x56)"
assert m.enabled
assert m.primary
assert m.orientation == "normal"

View File

@ -3,12 +3,13 @@ import shlex
import subprocess
import sys
import parse
from PySide2.QtCore import QFile, QObject, QTimer
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication, QGraphicsScene, QLabel
from .monitor_item import MonitorItem
from . import xrandr
from .monitor_item import MonitorItem
class Window(QObject):
@ -44,7 +45,7 @@ class Window(QObject):
monitor.enabled = enabled
if enabled and not monitor.get_current_mode():
# Choose a mode
self.ui.modes.setCurrentText(monitor.get_preferred_mode_name())
self.ui.modes.setCurrentText(str(monitor.get_preferred_mode()))
self.mode_changed()
self.screen.update_replica_of()
for mon in self.screen.monitors.values():
@ -183,7 +184,11 @@ class Window(QObject):
for name, monitor in self.screen.monitors.items():
self.ui.screenCombo.addItem(name)
mon_item = MonitorItem(data=monitor, window=self, name=name,)
mon_item = MonitorItem(
data=monitor,
window=self,
name=name,
)
self.scene.addItem(mon_item)
monitor.item = mon_item
self.ui.screenCombo.setCurrentText(self.screen.choose_a_monitor())
@ -198,7 +203,7 @@ class Window(QObject):
def mode_changed(self):
mon = self.ui.screenCombo.currentText()
mode = self.ui.modes.currentText()
mode = parse.search("({mode_name})", self.ui.modes.currentText())["mode_name"]
if not mode:
return
print(f"Changing {mon} to {mode}")
@ -280,11 +285,11 @@ class Window(QObject):
# Show modes
self.ui.modes.clear()
monitor = self.screen.monitors[name]
for mode in monitor.modes:
self.ui.modes.addItem(mode)
for name, mode in monitor.modes.items():
self.ui.modes.addItem(str(mode))
mode = monitor.get_current_mode()
self.ui.modes.setCurrentText(mode.name)
self.ui.modes.setCurrentText(str(mode))
if monitor.orientation in ("normal", "inverted"):
h_scale = monitor.res_x / mode.res_x
v_scale = monitor.res_y / mode.res_y

View File

@ -10,10 +10,10 @@ def _split_by_lines_matching(pattern, lines):
matching the pattern. The line matching the pattern is
included in the SECOND group. Empty groups are removed."""
groups = [[]]
for l in lines:
if re.match(pattern, l): # Start a new group
for line in lines:
if re.match(pattern, line): # Start a new group
groups.append([])
groups[-1].append(l)
groups[-1].append(line)
return [g for g in groups if g]
@ -52,6 +52,9 @@ class Mode:
def __repr__(self):
return self.header.strip()
def __str__(self):
return f"{self.res_x}x{self.res_y} ({self.name})"
class Monitor:
"""Object representing a monitor according to xrandr."""
@ -123,10 +126,10 @@ class Monitor:
for k, v in self.modes.items():
v.current = k == mode_name
def get_preferred_mode_name(self):
def get_preferred_mode(self):
for k, v in self.modes.items():
if v.preferred:
return k
return v
return None
def guess_scale_mode(self):