|
8 | 8 | from ._debug import info as debug_info # noqa: F401 |
9 | 9 | from ._version import __author__, __version__ # noqa: F401 |
10 | 10 | from .exceptions import NoValidDisplayError, format_exc |
11 | | -from .helpers import MONITOR_MANUFACTURER_CODES # noqa: F401 |
| 11 | +from .helpers import MONITOR_MANUFACTURER_CODES, percentage # noqa: F401 |
12 | 12 | from .helpers import BrightnessMethod, ScreenBrightnessError, logarithmic_range |
13 | 13 |
|
14 | 14 | logger = logging.getLogger(__name__) |
@@ -99,34 +99,32 @@ def set_brightness( |
99 | 99 | ``` |
100 | 100 | ''' |
101 | 101 | if isinstance(value, str) and ('+' in value or '-' in value): |
102 | | - value = int(float(value)) |
103 | | - monitors = filter_monitors(display=display, method=method) |
104 | | - if len(monitors) > 1: |
105 | | - output = [] |
106 | | - for monitor in monitors: |
107 | | - identifier = Monitor.get_identifier(monitor)[1] |
108 | | - tmp = set_brightness( |
109 | | - get_brightness(display=identifier)[0] + value, |
110 | | - display=identifier, |
111 | | - force=force, |
112 | | - verbose_error=verbose_error, |
113 | | - no_return=no_return |
114 | | - ) |
115 | | - if isinstance(tmp, list): |
116 | | - output += tmp |
117 | | - else: # otherwise the output should be None |
118 | | - output.append(tmp) |
119 | | - return output |
120 | | - |
121 | | - value += get_brightness(display=Monitor.get_identifier(monitors[0])[1])[0] |
122 | | - else: |
123 | | - value = int(float(str(value))) |
| 102 | + output = [] |
| 103 | + for monitor in filter_monitors(display=display, method=method): |
| 104 | + identifier = Monitor.get_identifier(monitor)[1] |
| 105 | + current_value = get_brightness(display=identifier)[0] |
| 106 | + result = set_brightness( |
| 107 | + # don't need to calculate lower bound here because it will be |
| 108 | + # done by the other path in `set_brightness` |
| 109 | + percentage(value, current=current_value), |
| 110 | + display=identifier, |
| 111 | + force=force, |
| 112 | + verbose_error=verbose_error, |
| 113 | + no_return=no_return |
| 114 | + ) |
| 115 | + if result is None: |
| 116 | + output.append(result) |
| 117 | + else: |
| 118 | + output += result |
124 | 119 |
|
125 | | - # make sure value is within bounds |
126 | | - value = max(min(100, value), 0) |
| 120 | + return output |
127 | 121 |
|
128 | 122 | if platform.system() == 'Linux' and not force: |
129 | | - value = max(1, value) |
| 123 | + lower_bound = 1 |
| 124 | + else: |
| 125 | + lower_bound = 0 |
| 126 | + |
| 127 | + value = percentage(value, lower_bound=lower_bound) |
130 | 128 |
|
131 | 129 | return __brightness( |
132 | 130 | value, display=display, method=method, |
@@ -498,19 +496,18 @@ def set_brightness(self, value: Union[int, str], no_return: bool = True, force: |
498 | 496 | # refresh display info, in case another display has been unplugged or something |
499 | 497 | # which would change the index of this display |
500 | 498 | self.get_info() |
501 | | - # min brightness value |
| 499 | + |
| 500 | + # convert brightness value to percentage |
502 | 501 | if platform.system() == 'Linux' and not force: |
503 | 502 | lower_bound = 1 |
504 | 503 | else: |
505 | 504 | lower_bound = 0 |
506 | 505 |
|
507 | | - # changing string value to int |
508 | | - if isinstance(value, str) and ('+' in value or '-' in value): |
509 | | - value = int(float(value)) |
510 | | - value += self.method.get_brightness(display=self.index)[0] |
511 | | - else: |
512 | | - value = int(float(str(value))) |
513 | | - value = max(lower_bound, min(value, 100)) |
| 506 | + value = percentage( |
| 507 | + value, |
| 508 | + current=lambda: self.method.get_brightness(display=self.index)[0], |
| 509 | + lower_bound=lower_bound |
| 510 | + ) |
514 | 511 | self.method.set_brightness(value, display=self.index) |
515 | 512 | if no_return: |
516 | 513 | return None |
|
0 commit comments