-
-
Notifications
You must be signed in to change notification settings - Fork 685
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
Set default background color for widgets on iOS #3009
base: main
Are you sure you want to change the base?
Changes from 1 commit
046d8de
481ea93
c7553ed
f18cbf1
5813bc0
a68c366
2d39724
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
On iOS, the default background color is now TRANSPARENT for Box, Canvas, ImageView, Label, ProgressBar, ScrollContainer and Slider widgets. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems unnecessarily complex - doing a round trip from UIColor to toga.Color to UIColor just to get a (cached, and possibly stale) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, I've modified it to work with the native colors directly. |
||
if color is None | ||
else native_color(color) | ||
) | ||
|
||
# INTERFACE | ||
def add_child(self, child): | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -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 | ||||||||
) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAICT, this is a change in behavior. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've restored it. But it's worth noting that
According to: https://developer.apple.com/documentation/uikit/uiview/1622591-backgroundcolor?language=objc:
But according to: https://developer.apple.com/documentation/uikit/uibackgroundconfiguration/3600317-backgroundcolor
So, I have set the background color of widgets which should have transparent background by default to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok - I think that interpretation of "None = Transparent" is what I was remembering. Based on the examples you've presented here, it looks like you've preserved the historical behavior. |
||||||||
|
||||||||
def set_font(self, font): | ||||||||
self.native.titleLabel.font = font._impl.native | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Worth noting that this is a little misleading - There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've renamed it to toga_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 ( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We no longer need this branch; as a result of the PEP 730 changes, iOS 13 is the minimum supported iOS version. The iOS platform docs should also be updated to reflect this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I've removed that branch.