From 046d8de3856c9a1bc31a46da0427e0525c68e9a4 Mon Sep 17 00:00:00 2001 From: proneon267 Date: Mon, 2 Dec 2024 09:37:09 -0800 Subject: [PATCH 1/7] Set default background color for widgets on iOS --- changes/767.bugfix.rst | 1 + iOS/src/toga_iOS/colors.py | 15 +++++++++- iOS/src/toga_iOS/widgets/base.py | 30 +++++++++---------- iOS/src/toga_iOS/widgets/box.py | 6 ++-- iOS/src/toga_iOS/widgets/button.py | 7 ++--- iOS/src/toga_iOS/widgets/canvas.py | 6 ++-- iOS/src/toga_iOS/widgets/imageview.py | 9 ++---- iOS/src/toga_iOS/widgets/label.py | 8 ++--- .../toga_iOS/widgets/multilinetextinput.py | 3 -- iOS/src/toga_iOS/widgets/numberinput.py | 3 -- iOS/src/toga_iOS/widgets/progressbar.py | 3 ++ iOS/src/toga_iOS/widgets/scrollcontainer.py | 6 ++-- iOS/src/toga_iOS/widgets/selection.py | 3 -- iOS/src/toga_iOS/widgets/slider.py | 3 ++ iOS/src/toga_iOS/widgets/textinput.py | 3 -- testbed/tests/widgets/test_button.py | 7 +++-- 16 files changed, 58 insertions(+), 55 deletions(-) create mode 100644 changes/767.bugfix.rst diff --git a/changes/767.bugfix.rst b/changes/767.bugfix.rst new file mode 100644 index 0000000000..46592cd08c --- /dev/null +++ b/changes/767.bugfix.rst @@ -0,0 +1 @@ +On iOS, the default background color is now TRANSPARENT for Box, Canvas, ImageView, Label, ProgressBar, ScrollContainer and Slider widgets. diff --git a/iOS/src/toga_iOS/colors.py b/iOS/src/toga_iOS/colors.py index 25d80f3360..9f7eb2f3d2 100644 --- a/iOS/src/toga_iOS/colors.py +++ b/iOS/src/toga_iOS/colors.py @@ -1,4 +1,8 @@ -from toga.colors import TRANSPARENT +from ctypes import byref + +from rubicon.objc import CGFloat + +from toga.colors import TRANSPARENT, rgba from toga_iOS.libs import UIColor CACHE = {TRANSPARENT: UIColor.clearColor} @@ -18,3 +22,12 @@ def native_color(c): CACHE[c] = color return color + + +def toga_color(c): + red = CGFloat() + green = CGFloat() + blue = CGFloat() + alpha = CGFloat() + c.getRed(byref(red), green=byref(green), blue=byref(blue), alpha=byref(alpha)) + return rgba(red.value * 255, green.value * 255, blue.value * 255, alpha.value) diff --git a/iOS/src/toga_iOS/widgets/base.py b/iOS/src/toga_iOS/widgets/base.py index 59cb4f241b..0125c25977 100644 --- a/iOS/src/toga_iOS/widgets/base.py +++ b/iOS/src/toga_iOS/widgets/base.py @@ -1,6 +1,6 @@ from abc import abstractmethod -from toga_iOS.colors import native_color +from toga_iOS.colors import native_color, toga_color from toga_iOS.constraints import Constraints from toga_iOS.libs import UIColor @@ -13,6 +13,15 @@ def __init__(self, interface): self._container = None self.constraints = None self.native = None + + # Set default background color + try: + # systemBackgroundColor() was introduced in iOS 13 + # We don't test on iOS 12, so mark the other branch as nocover + self._default_background_color = toga_color(UIColor.systemBackgroundColor()) + except AttributeError: # pragma: no cover + self._default_background_color = toga_color(UIColor.whiteColor) + self.create() self.interface.style.reapply() @@ -90,20 +99,11 @@ def set_color(self, color): pass def set_background_color(self, color): - # By default, background color can't be changed - pass - - # TODO: check if it's safe to make this the default implementation. - def set_background_color_simple(self, value): - if value: - self.native.backgroundColor = native_color(value) - else: - try: - # systemBackgroundColor() was introduced in iOS 13 - # We don't test on iOS 12, so mark the other branch as nocover - self.native.backgroundColor = UIColor.systemBackgroundColor() - except AttributeError: # pragma: no cover - self.native.backgroundColor = UIColor.whiteColor + self.native.backgroundColor = ( + native_color(self._default_background_color) + if color is None + else native_color(color) + ) # INTERFACE def add_child(self, child): diff --git a/iOS/src/toga_iOS/widgets/box.py b/iOS/src/toga_iOS/widgets/box.py index 7a110c303c..678d618907 100644 --- a/iOS/src/toga_iOS/widgets/box.py +++ b/iOS/src/toga_iOS/widgets/box.py @@ -1,6 +1,7 @@ from rubicon.objc import objc_property from travertino.size import at_least +from toga.colors import TRANSPARENT from toga_iOS.libs import UIView from toga_iOS.widgets.base import Widget @@ -19,12 +20,11 @@ def create(self): self.native.interface = self.interface self.native.impl = self + self._default_background_color = TRANSPARENT + # Add the layout constraints self.add_constraints() - def set_background_color(self, value): - self.set_background_color_simple(value) - def rehint(self): self.interface.intrinsic.width = at_least(0) self.interface.intrinsic.height = at_least(0) diff --git a/iOS/src/toga_iOS/widgets/button.py b/iOS/src/toga_iOS/widgets/button.py index c53bbb73ab..40dcc6355f 100644 --- a/iOS/src/toga_iOS/widgets/button.py +++ b/iOS/src/toga_iOS/widgets/button.py @@ -68,10 +68,9 @@ def set_color(self, color): ) def set_background_color(self, color): - if color == TRANSPARENT or color is None: - self.native.backgroundColor = None - else: - self.native.backgroundColor = native_color(color) + super().set_background_color( + self._default_background_color if color in {None, TRANSPARENT} else color + ) def set_font(self, font): self.native.titleLabel.font = font._impl.native diff --git a/iOS/src/toga_iOS/widgets/canvas.py b/iOS/src/toga_iOS/widgets/canvas.py index 9fc213f304..b77c61414f 100644 --- a/iOS/src/toga_iOS/widgets/canvas.py +++ b/iOS/src/toga_iOS/widgets/canvas.py @@ -15,7 +15,7 @@ ) from travertino.size import at_least -from toga.colors import BLACK, TRANSPARENT, color +from toga.colors import BLACK, TRANSPARENT, color as named_color from toga.constants import Baseline, FillRule from toga_iOS.colors import native_color from toga_iOS.images import nsdata_to_bytes @@ -70,6 +70,8 @@ def create(self): self.native.interface = self.interface self.native.impl = self + self._default_background_color = TRANSPARENT + # Add the layout constraints self.add_constraints() @@ -260,7 +262,7 @@ def _line_height(self, font): def measure_text(self, text, font): # We need at least a fill color to render, but that won't change the size. sizes = [ - self._render_string(line, font, fill_color=color(BLACK)).size() + self._render_string(line, font, fill_color=named_color(BLACK)).size() for line in text.splitlines() ] return ( diff --git a/iOS/src/toga_iOS/widgets/imageview.py b/iOS/src/toga_iOS/widgets/imageview.py index 02f5fc2866..efd2cf7d49 100644 --- a/iOS/src/toga_iOS/widgets/imageview.py +++ b/iOS/src/toga_iOS/widgets/imageview.py @@ -1,6 +1,5 @@ from toga.colors import TRANSPARENT from toga.widgets.imageview import rehint_imageview -from toga_iOS.colors import native_color from toga_iOS.libs import UIImageView, UIViewContentMode from toga_iOS.widgets.base import Widget @@ -15,13 +14,9 @@ def create(self): self.native.setTranslatesAutoresizingMaskIntoConstraints_(False) self.native.setAutoresizesSubviews_(False) - self.add_constraints() + self._default_background_color = TRANSPARENT - def set_background_color(self, color): - if color == TRANSPARENT or color is None: - self.native.backgroundColor = native_color(TRANSPARENT) - else: - self.native.backgroundColor = native_color(color) + self.add_constraints() def set_image(self, image): if image: diff --git a/iOS/src/toga_iOS/widgets/label.py b/iOS/src/toga_iOS/widgets/label.py index 0426fc3cc8..f2519a1a77 100644 --- a/iOS/src/toga_iOS/widgets/label.py +++ b/iOS/src/toga_iOS/widgets/label.py @@ -38,6 +38,8 @@ def create(self): # We shouldn't ever word wrap; if faced with that option, clip. self.native.lineBreakMode = NSLineBreakByClipping + self._default_background_color = TRANSPARENT + # Add the layout constraints self.add_constraints() @@ -47,12 +49,6 @@ def set_alignment(self, value): def set_color(self, value): self.native.textColor = native_color(value) - def set_background_color(self, color): - if color == TRANSPARENT or color is None: - self.native.backgroundColor = native_color(TRANSPARENT) - else: - self.native.backgroundColor = native_color(color) - def set_font(self, font): self.native.font = font._impl.native diff --git a/iOS/src/toga_iOS/widgets/multilinetextinput.py b/iOS/src/toga_iOS/widgets/multilinetextinput.py index 9959712e6f..81756699d0 100644 --- a/iOS/src/toga_iOS/widgets/multilinetextinput.py +++ b/iOS/src/toga_iOS/widgets/multilinetextinput.py @@ -141,9 +141,6 @@ def set_color(self, value): self.native.textColor = color self.placeholder_label.textColor = color - def set_background_color(self, color): - self.set_background_color_simple(color) - def set_alignment(self, value): self.native.textAlignment = NSTextAlignment(value) diff --git a/iOS/src/toga_iOS/widgets/numberinput.py b/iOS/src/toga_iOS/widgets/numberinput.py index cbb7ca0b45..4b99be0882 100644 --- a/iOS/src/toga_iOS/widgets/numberinput.py +++ b/iOS/src/toga_iOS/widgets/numberinput.py @@ -117,9 +117,6 @@ def set_font(self, font): def set_color(self, color): self.native.textColor = native_color(color) - def set_background_color(self, color): - self.set_background_color_simple(color) - def rehint(self): # Height of a text input is known. fitting_size = self.native.systemLayoutSizeFittingSize(CGSize(0, 0)) diff --git a/iOS/src/toga_iOS/widgets/progressbar.py b/iOS/src/toga_iOS/widgets/progressbar.py index 3cbb80c03b..533916f0b3 100644 --- a/iOS/src/toga_iOS/widgets/progressbar.py +++ b/iOS/src/toga_iOS/widgets/progressbar.py @@ -2,6 +2,7 @@ from travertino.size import at_least +from toga.colors import TRANSPARENT from toga_iOS.libs import CGSize, UIProgressView, UIProgressViewStyle from toga_iOS.widgets.base import Widget @@ -41,6 +42,8 @@ def create(self): self.native = UIProgressView.alloc().initWithProgressViewStyle_( UIProgressViewStyle.Default ) + self._default_background_color = TRANSPARENT + self.add_constraints() self._running = False diff --git a/iOS/src/toga_iOS/widgets/scrollcontainer.py b/iOS/src/toga_iOS/widgets/scrollcontainer.py index de4fe7f1bf..4595c9dd34 100644 --- a/iOS/src/toga_iOS/widgets/scrollcontainer.py +++ b/iOS/src/toga_iOS/widgets/scrollcontainer.py @@ -1,6 +1,7 @@ from rubicon.objc import SEL, NSMakePoint, NSMakeSize, objc_method, objc_property from travertino.size import at_least +from toga.colors import TRANSPARENT from toga_iOS.container import Container from toga_iOS.libs import UIScrollView from toga_iOS.widgets.base import Widget @@ -31,6 +32,8 @@ def create(self): self.native.impl = self self.native.delegate = self.native + self._default_background_color = TRANSPARENT + # UIScrollView doesn't have a native ability to disable a scrolling direction; # it's handled by controlling the scrollable area. self._allow_horizontal = True @@ -68,9 +71,6 @@ def content_refreshed(self, container): self.native.contentSize = NSMakeSize(width, height) - def set_background_color(self, value): - self.set_background_color_simple(value) - def rehint(self): self.interface.intrinsic.width = at_least(self.interface._MIN_WIDTH) self.interface.intrinsic.height = at_least(self.interface._MIN_HEIGHT) diff --git a/iOS/src/toga_iOS/widgets/selection.py b/iOS/src/toga_iOS/widgets/selection.py index 52af947f42..5cf2381948 100644 --- a/iOS/src/toga_iOS/widgets/selection.py +++ b/iOS/src/toga_iOS/widgets/selection.py @@ -83,9 +83,6 @@ def set_alignment(self, value): def set_color(self, color): self.native.textColor = native_color(color) - def set_background_color(self, color): - self.set_background_color_simple(color) - def set_font(self, font): self.native.font = font._impl.native diff --git a/iOS/src/toga_iOS/widgets/slider.py b/iOS/src/toga_iOS/widgets/slider.py index 6db9758378..642fae666b 100644 --- a/iOS/src/toga_iOS/widgets/slider.py +++ b/iOS/src/toga_iOS/widgets/slider.py @@ -1,6 +1,7 @@ from rubicon.objc import SEL, CGSize, objc_method, objc_property from travertino.size import at_least +from toga.colors import TRANSPARENT from toga_iOS.libs import ( UIControlEventTouchCancel, UIControlEventTouchDown, @@ -49,6 +50,8 @@ def create(self): self.native.interface = self.interface self.native.impl = self + self._default_background_color = TRANSPARENT + # Dummy values used during initialization. self.value = 0 self.min_value = 0 diff --git a/iOS/src/toga_iOS/widgets/textinput.py b/iOS/src/toga_iOS/widgets/textinput.py index 2397249bf7..9fc2d4a00a 100644 --- a/iOS/src/toga_iOS/widgets/textinput.py +++ b/iOS/src/toga_iOS/widgets/textinput.py @@ -138,9 +138,6 @@ def set_alignment(self, value): def set_color(self, color): self.native.textColor = native_color(color) - def set_background_color(self, color): - self.set_background_color_simple(color) - def set_font(self, font): self.native.font = font._impl.native diff --git a/testbed/tests/widgets/test_button.py b/testbed/tests/widgets/test_button.py index 05025eb308..3def3771cf 100644 --- a/testbed/tests/widgets/test_button.py +++ b/testbed/tests/widgets/test_button.py @@ -101,6 +101,9 @@ async def test_press(widget, probe): async def test_background_color_transparent(widget, probe): "Buttons treat background transparency as a color reset." + del widget.style.background_color + original_background_color = probe.background_color + widget.style.background_color = TRANSPARENT - await probe.redraw("Button background color should be transparent") - assert_color(probe.background_color, None) + await probe.redraw("Button background color should be reset to the default color") + assert_color(probe.background_color, original_background_color) From 481ea9352a8fb9ac76c5aef9a03c62adce30bb7b Mon Sep 17 00:00:00 2001 From: proneon267 Date: Mon, 2 Dec 2024 23:58:57 -0800 Subject: [PATCH 2/7] Remove unnecessary color conversion --- iOS/src/toga_iOS/colors.py | 15 +---------- iOS/src/toga_iOS/widgets/base.py | 28 +++++++++------------ iOS/src/toga_iOS/widgets/box.py | 6 ++--- iOS/src/toga_iOS/widgets/button.py | 4 ++- iOS/src/toga_iOS/widgets/canvas.py | 6 ++--- iOS/src/toga_iOS/widgets/imageview.py | 5 ++-- iOS/src/toga_iOS/widgets/label.py | 4 +-- iOS/src/toga_iOS/widgets/progressbar.py | 5 ++-- iOS/src/toga_iOS/widgets/scrollcontainer.py | 5 ++-- iOS/src/toga_iOS/widgets/slider.py | 4 +-- 10 files changed, 31 insertions(+), 51 deletions(-) diff --git a/iOS/src/toga_iOS/colors.py b/iOS/src/toga_iOS/colors.py index 9f7eb2f3d2..25d80f3360 100644 --- a/iOS/src/toga_iOS/colors.py +++ b/iOS/src/toga_iOS/colors.py @@ -1,8 +1,4 @@ -from ctypes import byref - -from rubicon.objc import CGFloat - -from toga.colors import TRANSPARENT, rgba +from toga.colors import TRANSPARENT from toga_iOS.libs import UIColor CACHE = {TRANSPARENT: UIColor.clearColor} @@ -22,12 +18,3 @@ def native_color(c): CACHE[c] = color return color - - -def toga_color(c): - red = CGFloat() - green = CGFloat() - blue = CGFloat() - alpha = CGFloat() - c.getRed(byref(red), green=byref(green), blue=byref(blue), alpha=byref(alpha)) - return rgba(red.value * 255, green.value * 255, blue.value * 255, alpha.value) diff --git a/iOS/src/toga_iOS/widgets/base.py b/iOS/src/toga_iOS/widgets/base.py index 0125c25977..49ec357003 100644 --- a/iOS/src/toga_iOS/widgets/base.py +++ b/iOS/src/toga_iOS/widgets/base.py @@ -1,6 +1,6 @@ from abc import abstractmethod -from toga_iOS.colors import native_color, toga_color +from toga_iOS.colors import native_color from toga_iOS.constraints import Constraints from toga_iOS.libs import UIColor @@ -13,15 +13,6 @@ def __init__(self, interface): self._container = None self.constraints = None self.native = None - - # Set default background color - try: - # systemBackgroundColor() was introduced in iOS 13 - # We don't test on iOS 12, so mark the other branch as nocover - self._default_background_color = toga_color(UIColor.systemBackgroundColor()) - except AttributeError: # pragma: no cover - self._default_background_color = toga_color(UIColor.whiteColor) - self.create() self.interface.style.reapply() @@ -98,12 +89,17 @@ def set_color(self, color): # By default, color can't be changed pass - def set_background_color(self, color): - self.native.backgroundColor = ( - native_color(self._default_background_color) - if color is None - else native_color(color) - ) + def set_background_color(self, color, is_native_color=False): + if is_native_color: + self.native.backgroundColor = color + else: + default_background_color = getattr(self, "_default_background_color", None) + if default_background_color is None: + default_background_color = UIColor.systemBackgroundColor() + + self.native.backgroundColor = ( + default_background_color if color is None else native_color(color) + ) # INTERFACE def add_child(self, child): diff --git a/iOS/src/toga_iOS/widgets/box.py b/iOS/src/toga_iOS/widgets/box.py index 678d618907..8df0af9e03 100644 --- a/iOS/src/toga_iOS/widgets/box.py +++ b/iOS/src/toga_iOS/widgets/box.py @@ -1,8 +1,7 @@ from rubicon.objc import objc_property from travertino.size import at_least -from toga.colors import TRANSPARENT -from toga_iOS.libs import UIView +from toga_iOS.libs import UIColor, UIView from toga_iOS.widgets.base import Widget @@ -20,8 +19,7 @@ def create(self): self.native.interface = self.interface self.native.impl = self - self._default_background_color = TRANSPARENT - + self._default_background_color = UIColor.clearColor # Add the layout constraints self.add_constraints() diff --git a/iOS/src/toga_iOS/widgets/button.py b/iOS/src/toga_iOS/widgets/button.py index 40dcc6355f..bc7a4124f3 100644 --- a/iOS/src/toga_iOS/widgets/button.py +++ b/iOS/src/toga_iOS/widgets/button.py @@ -38,6 +38,7 @@ def create(self): self._icon = None + self._default_background_color = None # Add the layout constraints self.add_constraints() @@ -69,7 +70,8 @@ def set_color(self, color): def set_background_color(self, color): super().set_background_color( - self._default_background_color if color in {None, TRANSPARENT} else color + (None if color in {None, TRANSPARENT} else native_color(color)), + is_native_color=True, ) def set_font(self, font): diff --git a/iOS/src/toga_iOS/widgets/canvas.py b/iOS/src/toga_iOS/widgets/canvas.py index b77c61414f..d241c8cc47 100644 --- a/iOS/src/toga_iOS/widgets/canvas.py +++ b/iOS/src/toga_iOS/widgets/canvas.py @@ -15,7 +15,7 @@ ) from travertino.size import at_least -from toga.colors import BLACK, TRANSPARENT, color as named_color +from toga.colors import BLACK, TRANSPARENT, color as toga_color from toga.constants import Baseline, FillRule from toga_iOS.colors import native_color from toga_iOS.images import nsdata_to_bytes @@ -70,7 +70,7 @@ def create(self): self.native.interface = self.interface self.native.impl = self - self._default_background_color = TRANSPARENT + self._default_background_color = UIColor.clearColor # Add the layout constraints self.add_constraints() @@ -262,7 +262,7 @@ def _line_height(self, font): def measure_text(self, text, font): # We need at least a fill color to render, but that won't change the size. sizes = [ - self._render_string(line, font, fill_color=named_color(BLACK)).size() + self._render_string(line, font, fill_color=toga_color(BLACK)).size() for line in text.splitlines() ] return ( diff --git a/iOS/src/toga_iOS/widgets/imageview.py b/iOS/src/toga_iOS/widgets/imageview.py index efd2cf7d49..bfe022bdf3 100644 --- a/iOS/src/toga_iOS/widgets/imageview.py +++ b/iOS/src/toga_iOS/widgets/imageview.py @@ -1,6 +1,5 @@ -from toga.colors import TRANSPARENT from toga.widgets.imageview import rehint_imageview -from toga_iOS.libs import UIImageView, UIViewContentMode +from toga_iOS.libs import UIColor, UIImageView, UIViewContentMode from toga_iOS.widgets.base import Widget @@ -14,7 +13,7 @@ def create(self): self.native.setTranslatesAutoresizingMaskIntoConstraints_(False) self.native.setAutoresizesSubviews_(False) - self._default_background_color = TRANSPARENT + self._default_background_color = UIColor.clearColor self.add_constraints() diff --git a/iOS/src/toga_iOS/widgets/label.py b/iOS/src/toga_iOS/widgets/label.py index f2519a1a77..008a82946e 100644 --- a/iOS/src/toga_iOS/widgets/label.py +++ b/iOS/src/toga_iOS/widgets/label.py @@ -3,11 +3,11 @@ from rubicon.objc import CGRect, NSInteger, NSMakeRect, objc_method, send_super from travertino.size import at_least -from toga.colors import TRANSPARENT from toga_iOS.colors import native_color from toga_iOS.libs import ( NSLineBreakByClipping, NSTextAlignment, + UIColor, UILabel, ) from toga_iOS.widgets.base import Widget @@ -38,7 +38,7 @@ def create(self): # We shouldn't ever word wrap; if faced with that option, clip. self.native.lineBreakMode = NSLineBreakByClipping - self._default_background_color = TRANSPARENT + self._default_background_color = UIColor.clearColor # Add the layout constraints self.add_constraints() diff --git a/iOS/src/toga_iOS/widgets/progressbar.py b/iOS/src/toga_iOS/widgets/progressbar.py index 533916f0b3..90cf6f6f35 100644 --- a/iOS/src/toga_iOS/widgets/progressbar.py +++ b/iOS/src/toga_iOS/widgets/progressbar.py @@ -2,8 +2,7 @@ from travertino.size import at_least -from toga.colors import TRANSPARENT -from toga_iOS.libs import CGSize, UIProgressView, UIProgressViewStyle +from toga_iOS.libs import CGSize, UIColor, UIProgressView, UIProgressViewStyle from toga_iOS.widgets.base import Widget # Implementation notes @@ -42,7 +41,7 @@ def create(self): self.native = UIProgressView.alloc().initWithProgressViewStyle_( UIProgressViewStyle.Default ) - self._default_background_color = TRANSPARENT + self._default_background_color = UIColor.clearColor self.add_constraints() diff --git a/iOS/src/toga_iOS/widgets/scrollcontainer.py b/iOS/src/toga_iOS/widgets/scrollcontainer.py index 4595c9dd34..8fa4e1074f 100644 --- a/iOS/src/toga_iOS/widgets/scrollcontainer.py +++ b/iOS/src/toga_iOS/widgets/scrollcontainer.py @@ -1,9 +1,8 @@ from rubicon.objc import SEL, NSMakePoint, NSMakeSize, objc_method, objc_property from travertino.size import at_least -from toga.colors import TRANSPARENT from toga_iOS.container import Container -from toga_iOS.libs import UIScrollView +from toga_iOS.libs import UIColor, UIScrollView from toga_iOS.widgets.base import Widget @@ -32,7 +31,7 @@ def create(self): self.native.impl = self self.native.delegate = self.native - self._default_background_color = TRANSPARENT + self._default_background_color = UIColor.clearColor # UIScrollView doesn't have a native ability to disable a scrolling direction; # it's handled by controlling the scrollable area. diff --git a/iOS/src/toga_iOS/widgets/slider.py b/iOS/src/toga_iOS/widgets/slider.py index 642fae666b..d7936930c5 100644 --- a/iOS/src/toga_iOS/widgets/slider.py +++ b/iOS/src/toga_iOS/widgets/slider.py @@ -1,8 +1,8 @@ from rubicon.objc import SEL, CGSize, objc_method, objc_property from travertino.size import at_least -from toga.colors import TRANSPARENT from toga_iOS.libs import ( + UIColor, UIControlEventTouchCancel, UIControlEventTouchDown, UIControlEventTouchUpInside, @@ -50,7 +50,7 @@ def create(self): self.native.interface = self.interface self.native.impl = self - self._default_background_color = TRANSPARENT + self._default_background_color = UIColor.clearColor # Dummy values used during initialization. self.value = 0 From c7553ed3be1610deb4034c5ef05b41f4115fe121 Mon Sep 17 00:00:00 2001 From: proneon267 Date: Tue, 3 Dec 2024 06:21:11 -0800 Subject: [PATCH 3/7] Enable background color tests for slider widget --- testbed/tests/widgets/test_slider.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/testbed/tests/widgets/test_slider.py b/testbed/tests/widgets/test_slider.py index 6b8eabf993..be8543597e 100644 --- a/testbed/tests/widgets/test_slider.py +++ b/testbed/tests/widgets/test_slider.py @@ -9,6 +9,8 @@ from ..assertions import assert_set_get from .conftest import build_cleanup_test from .properties import ( # noqa: F401 + test_background_color, + test_background_color_reset, test_enabled, test_flex_horizontal_widget_size, ) From f18cbf1cbbc3d9b437b9ff21cf9c3dc02397dd5b Mon Sep 17 00:00:00 2001 From: proneon267 Date: Tue, 3 Dec 2024 06:39:41 -0800 Subject: [PATCH 4/7] Revert enabled slider background color tests --- testbed/tests/widgets/test_slider.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/testbed/tests/widgets/test_slider.py b/testbed/tests/widgets/test_slider.py index be8543597e..6b8eabf993 100644 --- a/testbed/tests/widgets/test_slider.py +++ b/testbed/tests/widgets/test_slider.py @@ -9,8 +9,6 @@ from ..assertions import assert_set_get from .conftest import build_cleanup_test from .properties import ( # noqa: F401 - test_background_color, - test_background_color_reset, test_enabled, test_flex_horizontal_widget_size, ) From 5813bc03c2806139422d4a050f2f75820e2fc3b3 Mon Sep 17 00:00:00 2001 From: proneon267 Date: Tue, 3 Dec 2024 19:30:27 -0800 Subject: [PATCH 5/7] Use systemBackgroundColor as default fallback --- iOS/src/toga_iOS/widgets/base.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/iOS/src/toga_iOS/widgets/base.py b/iOS/src/toga_iOS/widgets/base.py index 49ec357003..3dee86777b 100644 --- a/iOS/src/toga_iOS/widgets/base.py +++ b/iOS/src/toga_iOS/widgets/base.py @@ -93,10 +93,9 @@ def set_background_color(self, color, is_native_color=False): if is_native_color: self.native.backgroundColor = color else: - default_background_color = getattr(self, "_default_background_color", None) - if default_background_color is None: - default_background_color = UIColor.systemBackgroundColor() - + default_background_color = getattr( + self, "_default_background_color", UIColor.systemBackgroundColor() + ) self.native.backgroundColor = ( default_background_color if color is None else native_color(color) ) From a68c36622ea0ced5296f33431b212165815902db Mon Sep 17 00:00:00 2001 From: proneon267 Date: Wed, 4 Dec 2024 00:09:26 -0800 Subject: [PATCH 6/7] Remove complex background color assignment on button --- iOS/src/toga_iOS/widgets/base.py | 17 +++++++---------- iOS/src/toga_iOS/widgets/button.py | 5 +---- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/iOS/src/toga_iOS/widgets/base.py b/iOS/src/toga_iOS/widgets/base.py index 3dee86777b..bd4c85fe8f 100644 --- a/iOS/src/toga_iOS/widgets/base.py +++ b/iOS/src/toga_iOS/widgets/base.py @@ -89,16 +89,13 @@ def set_color(self, color): # By default, color can't be changed pass - def set_background_color(self, color, is_native_color=False): - if is_native_color: - self.native.backgroundColor = color - else: - default_background_color = getattr( - self, "_default_background_color", UIColor.systemBackgroundColor() - ) - self.native.backgroundColor = ( - default_background_color if color is None else native_color(color) - ) + def set_background_color(self, color): + default_background_color = getattr( + self, "_default_background_color", UIColor.systemBackgroundColor() + ) + self.native.backgroundColor = ( + default_background_color if color is None else native_color(color) + ) # INTERFACE def add_child(self, child): diff --git a/iOS/src/toga_iOS/widgets/button.py b/iOS/src/toga_iOS/widgets/button.py index bc7a4124f3..5c54060a0a 100644 --- a/iOS/src/toga_iOS/widgets/button.py +++ b/iOS/src/toga_iOS/widgets/button.py @@ -69,10 +69,7 @@ def set_color(self, color): ) def set_background_color(self, color): - super().set_background_color( - (None if color in {None, TRANSPARENT} else native_color(color)), - is_native_color=True, - ) + super().set_background_color(None if color in {None, TRANSPARENT} else color) def set_font(self, font): self.native.titleLabel.font = font._impl.native From 2d39724fa12e154772b9bad1bbee0f6ca1c4423a Mon Sep 17 00:00:00 2001 From: proneon267 Date: Wed, 4 Dec 2024 05:59:15 -0800 Subject: [PATCH 7/7] Set default background color on activityindicator --- iOS/src/toga_iOS/widgets/activityindicator.py | 3 ++- iOS/src/toga_iOS/widgets/base.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/iOS/src/toga_iOS/widgets/activityindicator.py b/iOS/src/toga_iOS/widgets/activityindicator.py index 4d17e4c8a0..36a480c918 100644 --- a/iOS/src/toga_iOS/widgets/activityindicator.py +++ b/iOS/src/toga_iOS/widgets/activityindicator.py @@ -1,6 +1,6 @@ from rubicon.objc import CGSize -from toga_iOS.libs import UIActivityIndicatorView +from toga_iOS.libs import UIActivityIndicatorView, UIColor from toga_iOS.widgets.base import Widget @@ -11,6 +11,7 @@ def create(self): self.native.translatesAutoresizingMaskIntoConstraints = False self.native.sizeToFit() + self._default_background_color = UIColor.clearColor self.add_constraints() def set_hidden(self, hidden): diff --git a/iOS/src/toga_iOS/widgets/base.py b/iOS/src/toga_iOS/widgets/base.py index bd4c85fe8f..80d22c5481 100644 --- a/iOS/src/toga_iOS/widgets/base.py +++ b/iOS/src/toga_iOS/widgets/base.py @@ -91,7 +91,7 @@ def set_color(self, color): def set_background_color(self, color): default_background_color = getattr( - self, "_default_background_color", UIColor.systemBackgroundColor() + self, "_default_background_color", UIColor.secondarySystemBackgroundColor() ) self.native.backgroundColor = ( default_background_color if color is None else native_color(color)