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

Normalize naming of min/max arguments across widgets #2005

Merged
merged 15 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions changes/1999.removal.1.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``Slider.range`` has been replaced by ``Slider.min`` and ``Slider.max``.
1 change: 1 addition & 0 deletions changes/1999.removal.2.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``NumberInput.min_value`` and ``NumberInput.max_value`` have been renamed ``NumberInput.min`` and ``NumberInput.max``, respectively.
1 change: 1 addition & 0 deletions changes/1999.removal.3.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``DatePicker.min_date`` and ``DatePicker.max_date`` has been renamed ``DateInput.min`` and ``DateInput.max``, respectively.
1 change: 1 addition & 0 deletions changes/1999.removal.4.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``TimePicker.min_time`` and ``TimePicker.max_time`` has been renamed ``TimeInput.min`` and ``TimeInput.max``, respectively.
3 changes: 2 additions & 1 deletion cocoa/src/toga_cocoa/widgets/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from toga.colors import TRANSPARENT
from toga.fonts import SYSTEM_DEFAULT_FONT_SIZE
from toga.style.pack import NONE
from toga_cocoa.colors import native_color
from toga_cocoa.libs import (
SEL,
Expand Down Expand Up @@ -46,7 +47,7 @@ def _set_button_style(self):
# RegularSquare button.
if (
self.interface.style.font_size != SYSTEM_DEFAULT_FONT_SIZE
or self.interface.style.height
or self.interface.style.height != NONE
):
self.native.bezelStyle = NSBezelStyle.RegularSquare
else:
Expand Down
15 changes: 10 additions & 5 deletions cocoa/src/toga_cocoa/widgets/slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,17 @@ def get_value(self):
def set_value(self, value):
self.native.doubleValue = value

def get_range(self):
return self.native.minValue, self.native.maxValue
def get_min(self):
return self.native.minValue

def set_range(self, range):
self.native.minValue = range[0]
self.native.maxValue = range[1]
def set_min(self, value):
self.native.minValue = value

def get_max(self):
return self.native.maxValue

def set_max(self, value):
self.native.maxValue = value

def rehint(self):
content_size = self.native.intrinsicContentSize()
Expand Down
3 changes: 2 additions & 1 deletion cocoa/tests_backend/widgets/button.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pytest import xfail

from toga.style.pack import NONE
from toga_cocoa.libs import NSBezelStyle, NSButton, NSFont

from .base import SimpleProbe
Expand Down Expand Up @@ -33,7 +34,7 @@ def height(self):
# If the button has a manual height set, or has a non-default font size
# it should have a different bezel style.
if (
self.widget.style.height
self.widget.style.height != NONE
or self.native.font.pointSize != NSFont.systemFontSize
):
assert self.native.bezelStyle == NSBezelStyle.RegularSquare
Expand Down
42 changes: 15 additions & 27 deletions core/src/toga/platform.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import importlib
import os
import sys
import warnings
from functools import lru_cache

try:
Expand Down Expand Up @@ -30,44 +29,31 @@
}


try:
current_platform = os.environ["TOGA_PLATFORM"]
except KeyError:
def get_current_platform():
# Rely on `sys.getandroidapilevel`, which only exists on Android; see
# https://github.com/beeware/Python-Android-support/issues/8
if hasattr(sys, "getandroidapilevel"):
current_platform = "android"
return "android"
elif sys.platform.startswith("freebsd"):
current_platform = "freeBSD"
return "freeBSD"
else:
current_platform = _TOGA_PLATFORMS.get(sys.platform)
return _TOGA_PLATFORMS.get(sys.platform)


current_platform = get_current_platform()


@lru_cache(maxsize=1)
def get_platform_factory(factory=None):
"""This function figures out what the current host platform is and imports the
adequate factory. The factory is the interface to all platform specific
implementations.
def get_platform_factory():
"""Determine the current host platform and import the platform factory.

If the TOGA_BACKEND environment variable is set, the factory will be loaded
If the ``TOGA_BACKEND`` environment variable is set, the factory will be loaded
from that module.

Returns: The suitable factory for the current host platform.
Raises :any:`RuntimeError` if an appropriate host platform cannot be identified.

Raises:
RuntimeError: If no supported host platform can be identified.
:returns: The factory for the host platform.
"""

######################################################################
# 2022-09: Backwards compatibility
######################################################################
# factory no longer used
if factory:
warnings.warn("The factory argument is no longer used.", DeprecationWarning)
######################################################################
# End backwards compatibility.
######################################################################

toga_backends = entry_points(group="toga.backends")
if not toga_backends:
raise RuntimeError("No Toga backend could be loaded.")
Expand Down Expand Up @@ -104,7 +90,9 @@ def get_platform_factory(factory=None):
)
raise RuntimeError(
f"Multiple Toga backends are installed ({toga_backends_string}), "
f"but none of them match your current platform ({current_platform!r})."
f"but none of them match your current platform ({current_platform!r}). "
"Install a backend for your current platform, or use "
"TOGA_BACKEND to specify a backend."
)
if len(matching_backends) > 1:
toga_backends_string = ", ".join(
Expand Down
94 changes: 56 additions & 38 deletions core/src/toga/widgets/dateinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def __init__(
id=None,
style=None,
value: datetime.date | None = None,
min_value: datetime.date | None = None,
max_value: datetime.date | None = None,
min: datetime.date | None = None,
max: datetime.date | None = None,
on_change: callable | None = None,
):
"""Create a new DateInput widget.
Expand All @@ -36,8 +36,8 @@ def __init__(
will be applied to the widget.
:param value: The initial date to display. If not specified, the current date
will be used.
:param min_value: The earliest date (inclusive) that can be selected.
:param max_value: The latest date (inclusive) that can be selected.
:param min: The earliest date (inclusive) that can be selected.
:param max: The latest date (inclusive) that can be selected.
:param on_change: A handler that will be invoked when the value changes.
"""
super().__init__(id=id, style=style)
Expand All @@ -46,8 +46,8 @@ def __init__(
self._impl = self.factory.DateInput(interface=self)

self.on_change = None
self.min_value = min_value
self.max_value = max_value
self.min = min
self.max = max

self.value = value
self.on_change = on_change
Expand Down Expand Up @@ -88,60 +88,62 @@ def _convert_date(self, value, *, check_range):
def value(self, value):
value = self._convert_date(value, check_range=False)

if value < self.min_value:
value = self.min_value
elif value > self.max_value:
value = self.max_value
if value < self.min:
value = self.min
elif value > self.max:
value = self.max

self._impl.set_value(value)

@property
def min_value(self) -> datetime.date:
def min(self) -> datetime.date:
"""The minimum allowable date (inclusive). A value of ``None`` will be converted
into the lowest supported date of 1800-01-01.

The existing ``value`` and ``max_value`` will be clipped to the new minimum.
When setting this property, the current :attr:`value` and :attr:`max` will be
clipped against the new minimum value.

:raises ValueError: If set to a date outside of the supported range.
"""
return self._impl.get_min_date()

@min_value.setter
def min_value(self, value):
@min.setter
def min(self, value):
if value is None:
min_value = MIN_DATE
min = MIN_DATE
else:
min_value = self._convert_date(value, check_range=True)
min = self._convert_date(value, check_range=True)

if self.max_value < min_value:
self._impl.set_max_date(min_value)
self._impl.set_min_date(min_value)
if self.value < min_value:
self.value = min_value
if self.max < min:
self._impl.set_max_date(min)
self._impl.set_min_date(min)
if self.value < min:
self.value = min

@property
def max_value(self) -> datetime.date:
def max(self) -> datetime.date:
"""The maximum allowable date (inclusive). A value of ``None`` will be converted
into the highest supported date of 8999-12-31.

The existing ``value`` and ``min_value`` will be clipped to the new maximum.
When setting this property, the current :attr:`value` and :attr:`min` will be
clipped against the new maximum value.

:raises ValueError: If set to a date outside of the supported range.
"""
return self._impl.get_max_date()

@max_value.setter
def max_value(self, value):
@max.setter
def max(self, value):
if value is None:
max_value = MAX_DATE
max = MAX_DATE
else:
max_value = self._convert_date(value, check_range=True)
max = self._convert_date(value, check_range=True)

if self.min_value > max_value:
self._impl.set_min_date(max_value)
self._impl.set_max_date(max_value)
if self.value > max_value:
self.value = max_value
if self.min > max:
self._impl.set_min_date(max)
self._impl.set_max_date(max)
if self.value > max:
self.value = max

@property
def on_change(self) -> callable:
Expand All @@ -159,11 +161,15 @@ def __init__(self, *args, **kwargs):
warnings.warn("DatePicker has been renamed DateInput.", DeprecationWarning)

for old_name, new_name in [
("min_date", "min_value"),
("max_date", "max_value"),
("min_date", "min"),
("max_date", "max"),
]:
try:
value = kwargs.pop(old_name)
warnings.warn(
f"DatePicker.{old_name} has been renamed DateInput.{new_name}",
DeprecationWarning,
)
except KeyError:
pass
else:
Expand All @@ -173,16 +179,28 @@ def __init__(self, *args, **kwargs):

@property
def min_date(self):
return self.min_value
warnings.warn(
"DatePicker.min_date has been renamed DateInput.min", DeprecationWarning
)
return self.min

@min_date.setter
def min_date(self, value):
self.min_value = value
warnings.warn(
"DatePicker.min_date has been renamed DateInput.min", DeprecationWarning
)
self.min = value

@property
def max_date(self):
return self.max_value
warnings.warn(
"DatePicker.max_date has been renamed DateInput.max", DeprecationWarning
)
return self.max

@max_date.setter
def max_date(self, value):
self.max_value = value
warnings.warn(
"DatePicker.max_date has been renamed DateInput.max", DeprecationWarning
)
self.max = value
Loading