Introduce namedtuple types for position and size #2388
Labels
enhancement
New features, or improvements to existing features.
good first issue
Is this your first time contributing? This could be a good place to start!
What is the problem or limitation you are having?
Toga makes extensive use of tuples to pass around size and position values (e.g., the size and position of a window)
Tuples work for this purpose, and are extremely convenient from the perspective of the user providing input; but can make usage obscure because they rely on understanding that, for example
mysize[0]
is width, andmysize[1]
is height.Describe the solution you'd like
Toga should define namedtuples for Size and Position data types. This would allow the usage of
mysize.width
andmysize.height
,myposition.x
andmyposition.y
at runtime.These type declarations should be in a
toga.types
package, but exposed through thetoga
namespace, sotoga.Size()
andtoga.Position()
would be legal references.Any setter or method that accepts a tuple should continue to do so in addition to the new type; however, internally, Toga should cast the "raw" tuple to the named tuple as early as is convenient. Any value returned by a Toga API should be in the namedtuple form.
A
SizeT
type alias for "Size in nametuple or raw tuple form" should be defined (and similar for position), so that there's a convenient way for inputs to declare that both forms are valid. SizeT would only be used for input; output should always have the type annotationtoga.Size
.Any internal code referencing
size[0]
could then be updated to referencesize.width
etc; however, this should be considered optional, as the existingsize[0]
usage should continue to work.Describe alternatives you've considered
Additional context
Care should be taken to ensure that backends return data in the new types, rather than tuples. Adding type checks to the testbed backend may be advisable to ensure that the returned types are always honoured.
The text was updated successfully, but these errors were encountered: