Skip to content

Commit

Permalink
change the resolution filter as callable to make user could use any
Browse files Browse the repository at this point in the history
logic as they want.
  • Loading branch information
hanhsuan committed Dec 2, 2024
1 parent 009ec45 commit 1f117da
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 31 deletions.
38 changes: 8 additions & 30 deletions checkbox-support/checkbox_support/dbus/gnome_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from collections import namedtuple
from typing import Dict, List, Tuple, Set, Callable, Any
from gi.repository import GLib, Gio
from fractions import Fraction
import itertools

from checkbox_support.monitor_config import MonitorConfig
Expand Down Expand Up @@ -109,9 +108,8 @@ def set_extended_mode(self) -> Dict[str, str]:
def cycle(
self,
res: bool = True,
max_res_amount: int = 5,
transform: bool = False,
log: Callable[..., Any] = None,
resolution_filter: Callable[List[Mode], List[Mode]] = None,
action: Callable[..., Any] = None,
**kwargs
):
Expand All @@ -121,13 +119,11 @@ def cycle(
Args:
res: Cycling the resolution or not.
max_res_amount: Limit the number of tested configurations
to avoid an unnecessarily large test matrix.
transform: Cycling the transform or not.
log: For logging, the string is constructed by
[monitor name]_[resolution]_[transform]_.
resolution_filter: For filtering resolution then returning needed,
it will take List[Mode] as parameter and return
the same data type
action: For extra steps for each cycle,
the string is constructed by
Expand All @@ -144,7 +140,10 @@ def cycle(
state = self._get_current_state()
for monitor, modes in state[1].items():
monitors.append(monitor)
modes_list.append(self._resolution_filter(modes, max_res_amount))
if resolution_filter:
modes_list.append(resolution_filter(modes))
else:
modes_list.append(modes)
mode_combination = list(itertools.product(*modes_list))

for mode in mode_combination:
Expand Down Expand Up @@ -177,34 +176,13 @@ def cycle(
xy = 1 if (trans == 1 or trans == 3) else 0
position_x += int(m.resolution.split("x")[xy])
self._apply_monitors_config(state[0], logical_monitors)
if log:
log(uni_string, **kwargs)
if action:
action(uni_string, **kwargs)
if not res:
break
# change back to preferred monitor configuration
self.set_extended_mode()

def _resolution_filter(self, modes: List[Mode], max_res_amount: int):
new_modes = []
tmp_resolution = []
sort_modes = sorted(
modes, key=lambda m: int(m.resolution.split("x")[0]), reverse=True
)
for m in sort_modes:
width, height = [int(x) for x in m.resolution.split("x")]
aspect = Fraction(width, height)
if width < 675 or width / aspect < 530:
continue
if m.resolution in tmp_resolution:
continue
if len(new_modes) >= max_res_amount:
break
new_modes.append(m)
tmp_resolution.append(m.resolution)
return new_modes

def _get_current_state(self) -> Tuple[str, Dict[str, List[Mode]]]:
"""
Using DBus signal 'GetCurrentState' to get the available monitors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ def test_cycle_no_cycling(self, mock_dbus_proxy):
gnome_monitor.cycle(
res=False,
transform=False,
log=mock_callback,
resoultion_filter=mock_callback,
action=mock_callback,
)

Expand Down

0 comments on commit 1f117da

Please sign in to comment.