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

Setting the font of a toga.Selection (Android and Winforms) #1393

Closed
wants to merge 5 commits into from
Closed
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
40 changes: 39 additions & 1 deletion examples/selection/selection/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class SelectionApp(toga.App):
YTTERBIUM = "Ytterbium"
THULIUM = "Thulium"
OPTIONS = [CARBON, YTTERBIUM, THULIUM]
lbl_fontsize = None

def startup(self):
# Main window of the application with title and size
Expand All @@ -17,11 +18,36 @@ def startup(self):
label_style = Pack(flex=1, padding_right=24)
box_style = Pack(direction=ROW, padding=10)

# Change font size
lbl_fontlabel = toga.Label("Font size =")
self.lbl_fontsize = toga.Label("14")
btn_reduce_size = toga.Button(
" - ", on_press=self.reduce_fontsize, style=Pack(width=40)
)
btn_increase_size = toga.Button(
" + ", on_press=self.increase_fontsize, style=Pack(width=40)
)
font_box = toga.Box(
children=[
lbl_fontlabel,
self.lbl_fontsize,
btn_reduce_size,
btn_increase_size,
],
style=box_style,
)

# Add the content on the main window
self.selection = toga.Selection(items=self.OPTIONS)
self.selection = toga.Selection(
items=self.OPTIONS,
style=Pack(
font_family="monospace", font_size=int(self.lbl_fontsize.text), font_style="italic"
)
)

self.main_window.content = toga.Box(
children=[
font_box,
toga.Box(
style=box_style,
children=[
Expand Down Expand Up @@ -114,6 +140,18 @@ def my_on_select(self, selection):

print("The selection widget changed to {0}".format(selection.value))

def reduce_fontsize(self, widget):
font_size = int(self.lbl_fontsize.text) - 1
self.lbl_fontsize.text = str(font_size)
font = toga.Font("monospace", font_size, style="italic")
self.selection._impl.set_font(font)

def increase_fontsize(self, widget):
font_size = int(self.lbl_fontsize.text) + 1
self.lbl_fontsize.text = str(font_size)
font = toga.Font("monospace", font_size, style="italic")
self.selection._impl.set_font(font)


def main():
# App name and namespace
Expand Down
23 changes: 15 additions & 8 deletions src/android/toga_android/widgets/selection.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from rubicon.java import JavaClass
from travertino.size import at_least

from ..libs.android import R__layout
from ..libs.android.util import TypedValue
from ..libs.android.view import Gravity, View__MeasureSpec
from ..libs.android.widget import ArrayAdapter, OnItemSelectedListener, Spinner
from ..libs.android.widget import OnItemSelectedListener, Spinner
from .base import Widget, align

BeewareSpinnerAdapter = JavaClass("org/beeware/android/BeewareSpinnerAdapter")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to replicate "Beeware" in this class name; org.beeware.android.SpinnerAdapter is sufficiently unique.

Copy link
Contributor Author

@t-arn t-arn Jan 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done that because there already is a SpinnerAdapter in Android - and it is not a class, but an Interface.
Of course, technically, it would still work because we are in a different namespace, but I thought it would be less confusing if I use a new name.

How about CustomSpinnerAdapter or ExtendedSpinnerAdapter ?

Copy link
Contributor Author

@t-arn t-arn Jan 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The template is not really Toga specific.
The new Class could be used by any GUI. It just provides the necessary features to control the (size of the) font - which, in my opintion, is a short-coming of the standard implementation in Android.



class TogaOnItemSelectedListener(OnItemSelectedListener):
def __init__(self, impl):
Expand All @@ -23,13 +27,9 @@ def create(self):
impl=self
))
# On Android, the list of options is provided to the `Spinner` wrapped in
# an `ArrayAdapter`. We store `self.adapter` to avoid having to typecast it
# in `add_item()`.
self.adapter = ArrayAdapter(
self._native_activity,
R__layout.simple_spinner_item
)
self.adapter.setDropDownViewResource(R__layout.simple_spinner_dropdown_item)
# a `BeewareSpinnerAdapter`. We store `self.adapter` to avoid
# having to typecast it in `add_item()`.
self.adapter = BeewareSpinnerAdapter(self._native_activity, R__layout.simple_spinner_item)
self.native.setAdapter(self.adapter)
# Create a mapping from text to numeric index to support `select_item()`.
self._indexByItem = {}
Expand Down Expand Up @@ -61,3 +61,10 @@ def set_alignment(self, value):
def set_on_select(self, handler):
# No special handling is required.
pass

def set_font(self, font):
if font:
font_impl = font.bind(self.interface.factory)
self.adapter.setSpinnerTextSize(TypedValue.COMPLEX_UNIT_SP, int(font_impl.get_size()))
self.adapter.setSpinnerTypeFace(font_impl.get_typeface(), font_impl.get_style())
self.adapter.notifyDataSetChanged()
4 changes: 4 additions & 0 deletions src/winforms/toga_winforms/widgets/selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def get_selected_item(self):
def set_on_select(self, handler):
pass

def set_font(self, font):
if font:
self.native.Font = font.bind(self.interface.factory).native

def rehint(self):
self.interface.intrinsic.width = at_least(self.native.PreferredSize.Width)
self.interface.intrinsic.height = self.native.PreferredSize.Height