From 1f117da0874656499e6395e77448d5c77b05fbfa Mon Sep 17 00:00:00 2001 From: hanhsuan Date: Mon, 2 Dec 2024 10:58:42 +0800 Subject: [PATCH] change the resolution filter as callable to make user could use any logic as they want. --- .../checkbox_support/dbus/gnome_monitor.py | 38 ++++--------------- .../dbus/tests/test_gnome_monitor.py | 2 +- 2 files changed, 9 insertions(+), 31 deletions(-) diff --git a/checkbox-support/checkbox_support/dbus/gnome_monitor.py b/checkbox-support/checkbox_support/dbus/gnome_monitor.py index a220b5d96f..ea7ebc8c1f 100644 --- a/checkbox-support/checkbox_support/dbus/gnome_monitor.py +++ b/checkbox-support/checkbox_support/dbus/gnome_monitor.py @@ -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 @@ -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 ): @@ -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 @@ -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: @@ -177,8 +176,6 @@ 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: @@ -186,25 +183,6 @@ def cycle( # 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 diff --git a/checkbox-support/checkbox_support/dbus/tests/test_gnome_monitor.py b/checkbox-support/checkbox_support/dbus/tests/test_gnome_monitor.py index c7abbe1c10..35b2ae0ad5 100644 --- a/checkbox-support/checkbox_support/dbus/tests/test_gnome_monitor.py +++ b/checkbox-support/checkbox_support/dbus/tests/test_gnome_monitor.py @@ -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, )