-
-
Notifications
You must be signed in to change notification settings - Fork 671
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
Implemented Selection.set_font() for Android #2130
base: main
Are you sure you want to change the base?
Changes from 14 commits
37d6551
f8d428c
e95e04d
e94c038
057afb7
27e93cc
c5f1378
e3e95a6
7a26027
d019817
67a16fd
99644e4
c172e07
19951dd
c8a34db
9a50e01
aa1b2d3
5afcc4c
5417c42
de8694a
a8207cd
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 |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
|
||
from android import R | ||
from android.view import View | ||
from android.widget import AdapterView, ArrayAdapter, Spinner | ||
from android.widget import AdapterView, ArrayAdapter, Spinner, SpinnerAdapter | ||
|
||
from .base import Widget | ||
|
||
|
@@ -20,14 +20,93 @@ def onNothingSelected(self, parent): | |
self.impl.on_change(None) | ||
|
||
|
||
class TogaArrayAdapter(dynamic_proxy(SpinnerAdapter)): | ||
def __init__(self, impl): | ||
super().__init__() | ||
self.impl = impl | ||
self._default_textsize = -1 | ||
self._default_typeface = None | ||
self._textsize = -1 | ||
self._typeface = None | ||
self.adapter = ArrayAdapter( | ||
self.impl._native_activity, R.layout.simple_spinner_item | ||
) | ||
self.adapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item) | ||
|
||
def apply_font(self, tv): | ||
if self.impl._font_impl and tv: | ||
self.impl._font_impl.apply( | ||
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. Although GitHub isn't showing a merge conflict, there actually is one, because the 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 now use label.set_textview_font |
||
tv, | ||
self._default_textsize, | ||
self._default_typeface, | ||
) | ||
self._textsize = tv.getTextSize() | ||
self._typeface = tv.getTypeface() | ||
|
||
def cache_textview_defaults(self, tv): | ||
self._default_textsize = tv.getTextSize() | ||
self._default_typeface = tv.getTypeface() | ||
|
||
def getDropDownView(self, position, convertView, parent): | ||
tv = self.adapter.getDropDownView(position, convertView, parent) | ||
self.apply_font(tv) | ||
return tv | ||
|
||
def getView(self, position, convertView, parent): | ||
tv = self.adapter.getView(position, convertView, parent) | ||
if self._default_textsize == -1: | ||
self.cache_textview_defaults(tv) | ||
self.apply_font(tv) | ||
return tv | ||
|
||
def clear(self): | ||
return self.adapter.clear() | ||
|
||
def getAutofillOptions(self): | ||
return self.adapter.getAutofillOptions() | ||
|
||
def getCount(self): | ||
return self.adapter.getCount() | ||
|
||
def getItem(self, position): | ||
return self.adapter.getItem(position) | ||
|
||
def getItemId(self, position): | ||
return self.adapter.getItemId(position) | ||
|
||
def getItemViewType(self, position): | ||
return self.adapter.getItemViewType(position) | ||
|
||
def getViewTypeCount(self): | ||
return self.adapter.getViewTypeCount() | ||
|
||
def hasStableIds(self): | ||
return self.adapter.hasStableIds() | ||
|
||
def insert(self, object, index): | ||
return self.adapter.insert(object, index) | ||
|
||
def isEmpty(self): | ||
return self.adapter.isEmpty() | ||
|
||
def registerDataSetObserver(self, observer): | ||
self.adapter.registerDataSetObserver(observer) | ||
|
||
def remove(self, object): | ||
self.adapter.remove(object) | ||
|
||
def unregisterDataSetObserver(self, observer): | ||
self.adapter.unregisterDataSetObserver(observer) | ||
|
||
|
||
class Selection(Widget): | ||
focusable = False | ||
_font_impl = None | ||
|
||
def create(self): | ||
self.native = Spinner(self._native_activity, Spinner.MODE_DROPDOWN) | ||
self.native.setOnItemSelectedListener(TogaOnItemSelectedListener(impl=self)) | ||
self.adapter = ArrayAdapter(self._native_activity, R.layout.simple_spinner_item) | ||
self.adapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item) | ||
self.adapter = TogaArrayAdapter(impl=self) | ||
self.native.setAdapter(self.adapter) | ||
self.last_selection = None | ||
|
||
|
@@ -87,3 +166,18 @@ def rehint(self): | |
self.native.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED) | ||
self.interface.intrinsic.width = at_least(self.native.getMeasuredWidth()) | ||
self.interface.intrinsic.height = self.native.getMeasuredHeight() | ||
|
||
def set_font(self, font): | ||
self._font_impl = font._impl | ||
tv = self.native.getSelectedView() | ||
if tv: | ||
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. As above - how/when is this branch not triggered? 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. During the creation of the widget, I saw that set_font() is called and tv was null |
||
self.adapter.apply_font(tv) | ||
self.interface.refresh() | ||
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. The 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 removed the refresh |
||
|
||
def get_textsize(self): | ||
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. If this method only exists to support the probe, the detail should be captured there. It's OK for the probe to access internal implementation details of the widget - the probe is, by definition, probing internals. 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, removed get_textsize() and get_typeface() from impl |
||
"""Returns the text size in pixel; used by testbed application""" | ||
return self.adapter._textsize | ||
|
||
def get_typeface(self): | ||
"""Returns the Typeface; used by testbed application""" | ||
return self.adapter._typeface |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
The set_font() method has been implemented for Android | ||
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. It's specific to selection, not "whole of android". 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, changed the text accordingly |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import toga | ||
from toga.constants import COLUMN, ROW | ||
from toga.constants import COLUMN, ITALIC, NORMAL, ROW | ||
from toga.fonts import SYSTEM_DEFAULT_FONT_SIZE | ||
from toga.style import Pack | ||
|
||
|
||
|
@@ -18,6 +19,9 @@ def startup(self): | |
# Main window of the application with title and size | ||
self.main_window = toga.MainWindow(title=self.name, size=(640, 400)) | ||
|
||
# set font toggle | ||
self.big_font = False | ||
|
||
# set up common styles | ||
label_style = Pack(flex=1, padding_right=24) | ||
box_style = Pack(direction=ROW, padding=10) | ||
|
@@ -29,6 +33,14 @@ def startup(self): | |
accessor="name", | ||
items=self.DATA_OPTIONS, | ||
) | ||
self.styled_selection = toga.Selection( | ||
style=Pack( | ||
width=200, | ||
padding=24, | ||
font_family="serif", | ||
), | ||
items=["Curium", "Titanium", "Copernicium"], | ||
) | ||
|
||
self.main_window.content = toga.Box( | ||
children=[ | ||
|
@@ -86,10 +98,7 @@ def startup(self): | |
style=box_style, | ||
children=[ | ||
toga.Label("Use some style!", style=label_style), | ||
toga.Selection( | ||
style=Pack(width=200, padding=24), | ||
items=["Curium", "Titanium", "Copernicium"], | ||
), | ||
self.styled_selection, | ||
], | ||
), | ||
toga.Box( | ||
|
@@ -110,6 +119,15 @@ def startup(self): | |
), | ||
], | ||
), | ||
toga.Box( | ||
style=box_style, | ||
children=[ | ||
toga.Button("Change font", on_press=self.change_font), | ||
toga.Button( | ||
"Print font attrs", on_press=self.print_font_attributes | ||
), | ||
], | ||
), | ||
], | ||
style=Pack(direction=COLUMN, padding=24), | ||
) | ||
|
@@ -137,6 +155,19 @@ def report_selection(self, widget): | |
f"Source: {self.source_selection.value.name} has weight {self.source_selection.value.weight}" | ||
) | ||
|
||
def change_font(self, widget): | ||
self.big_font = not self.big_font | ||
if self.big_font: | ||
self.styled_selection.style.font_size = 20 | ||
self.styled_selection.style.font_style = ITALIC | ||
else: | ||
self.styled_selection.style.font_size = SYSTEM_DEFAULT_FONT_SIZE | ||
self.styled_selection.style.font_style = NORMAL | ||
|
||
def print_font_attributes(self, widget): | ||
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'm not sure this "print" option is adding much - it's not really helpful as a diagnostic, as it's not doing anything but reflecting the values that were set in change_font from the same data source. 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, removed the button and code behind from the Selection example again |
||
print(f"font style: {self.styled_selection.style.font_style}") | ||
print(f"font size: {self.styled_selection.style.font_size}") | ||
|
||
|
||
def main(): | ||
# App name and namespace | ||
|
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.
How/when does this method get invoked when tv is None?
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.
During the creation of the widget, this can happen.