mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
layout: change Widget to take explicit Context and return explicit Dimensions
Change the definition of Widget from the implicit
type Widget func()
to the explicit functional
type Widget func(gtx layout.Context) layout.Dimensions
The advantages are numerous:
- Clearer connection between the incoming context and the output dimensions.
- Returning the Dimensions are impossible to omit.
- Contexts passed by value, so its fields can be exported
and freely mutated by the program.
The only disadvantage is the longer function literals and the many "returns".
What tipped the scales in favour of the explicit Widget variant is that type
aliases can dramatically shorten the literals:
type (
C = layout.Context
D = layout.Dimensions
)
widget := func(gtx C) D {
...
}
Note that the aliases are not part of the Gio API and it is up to each user
whether they want to use them.
Finally the Go proposal for lightweight function literals,
https://github.com/golang/go/issues/21498, may remove the disadvantage
completely in future.
Context becomes a plain struct with only public fields, and its Reset is
replaced by a NewContext convenience constructor.
Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+2
-2
@@ -23,7 +23,7 @@ func index(vs []string, t string) int {
|
||||
|
||||
// Update the Value according to incoming events, and
|
||||
// reports whether Value changed.
|
||||
func (e *Enum) Update(gtx *layout.Context) bool {
|
||||
func (e *Enum) Update(gtx layout.Context) bool {
|
||||
was := e.Value
|
||||
for i := range e.clicks {
|
||||
for _, ev := range e.clicks[i].Events(gtx) {
|
||||
@@ -37,7 +37,7 @@ func (e *Enum) Update(gtx *layout.Context) bool {
|
||||
}
|
||||
|
||||
// Layout adds the event handler for key.
|
||||
func (e *Enum) Layout(gtx *layout.Context, key string) {
|
||||
func (e *Enum) Layout(gtx layout.Context, key string) {
|
||||
if index(e.values, key) == -1 {
|
||||
e.values = append(e.values, key)
|
||||
e.clicks = append(e.clicks, gesture.Click{})
|
||||
|
||||
Reference in New Issue
Block a user