Commit Graph

98 Commits

Author SHA1 Message Date
Egon Elbre 9e85b43b0c layout: expose Direction.Position calculation
Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2021-03-05 10:19:10 +02:00
Elias Naur ffb26b0e17 io/pointer: rename button names to reflect their meaning, not placement
For example, ButtonLeft may be the right-most button for a left-handed user.
Rename the button names to match their intended use.

This is an API change. Use the following commands to update your
projects:

    $ gofmt -r 'pointer.ButtonLeft -> pointer.ButtonPrimary' -w .
    $ gofmt -r 'pointer.ButtonRight -> pointer.ButtonSecondary' -w .
    $ gofmt -r 'pointer.ButtonMiddle -> pointer.ButtonTertiary' -w .

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-03-03 11:08:41 +01:00
Egon Elbre e1248651c8 layout: automatically add system inset for NewContext
Currently every user needs to manually adjust for system insets.
This is rather verbose and most don't need to deviate from this behavior.

To disable the automatic adjustment, use:

  e.Insets = system.Insets{}
  ctx := layout.NewContext(ops, e)

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2021-02-28 19:08:43 +01:00
Elias Naur 4f45d9a567 io/router: rename Router.Add to the more specific Queue
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-01-22 16:36:14 +01:00
pierre a928c07a1c layout: added offset for last visible item to List
Signed-off-by: pierre <pierre.curto@gmail.com>
2021-01-21 18:31:41 +01:00
pierre 9bede80a3d layout: added number of visible children to List
Also fixed an edge case where the first visible child was off by 1 when it was just fully hidden.

Signed-off-by: pierre <pierre.curto@gmail.com>
2021-01-20 21:05:14 +01:00
pierre e088833caf layout: simplified Axis methods
Removed the Main and Cross Axis methods in favor of Convert.

Signed-off-by: pierre <pierre.curto@gmail.com>
2021-01-14 11:04:06 +01:00
Elias Naur d331dd2de8 op: rename StackOp/Push/Pop to StateOp/Save/Load
The semantics were relaxed in a previous commit; this change renames
to operations accordingly.

API change. Use gofmt to adjust your code accordingly:

gofmt -r 'op.Push(a).Pop() -> op.Save(a).Load()'
gofmt -r 'op.Push(a) -> op.Save(a)'
gofmt -r 'v.Pop() -> v.Load()'
gofmt -r 'op.StackOp -> op.StateOp'

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-01-12 21:28:59 +01:00
pierre 0e3e446393 layout: added Axis methods
Signed-off-by: pierre <pierre.curto@gmail.com>
2021-01-11 14:32:28 +01:00
pierre 62a1d8ae6e layout: avoid copying whole constraints in Stack
Signed-off-by: pierre <pierre.curto@gmail.com>
2021-01-01 12:19:35 +01:00
pierre 33103593a1 layout: updated comment, avoid copying whole context in Flex
Signed-off-by: pierre <pierre.curto@gmail.com>
2020-12-29 01:00:53 +01:00
pierre 0416fffc09 layout.List: store constraints instead of whole context
Signed-off-by: pierre <pierre.curto@gmail.com>
2020-12-24 12:38:56 +01:00
pierre d942b5c4d0 layout.List: reduce allocations when scrolling backward
Signed-off-by: pierre <pierre.curto@gmail.com>
2020-12-24 12:38:56 +01:00
Chris Waldon 18d4dbf60c layout: document how Inset modifies constraints
Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2020-12-20 10:04:21 +01:00
Walter Werner SCHNEIDER fd2d96adfc all: fix spelling errors
Signed-off-by: Walter Werner SCHNEIDER <contact@schnwalter.eu>
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-12-17 08:55:23 +01:00
Egon Elbre 679bf092cb layout: add Spacer
Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2020-12-09 17:41:49 +01:00
pierre e9cd8958de layout: fixed divisions by zero in Flex.Layout
Signed-off-by: pierre <pierre.curto@gmail.com>
2020-12-03 16:45:33 +01:00
Pierre.Curto f942db9e25 layout: added documentation
Added comment on the use of Dimension.Baseline and Direction.Layout constraint minimum clearing.
Also, renamed the Direction receiver for consistency and removed unnecessary conversions.

Signed-off-by: Pierre.Curto <pierre.curto@gmail.com>
2020-10-11 11:39:30 +02:00
Larry Clapp a54b460595 layout: refactor List.Layout and related functions
I found the interplay of List's Layout/init/next/more/end methods
somewhat confusing and hard to reason about, so I refactored them.

Signed-off-by: Larry Clapp <larry@theclapp.org>
2020-07-12 22:40:02 +02:00
Elias Naur d572aa23ac op/clip: split Rect into pixel-aligned Rect and rounded RRect
The pixel-aligned Rect is more efficient and easier to use in the common case
of layout clipping.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-07-09 18:33:00 +02:00
Elias Naur 4818538ef8 op/clip: unexport Rect.Op
It wasn't used anywhere outside Rect.Add.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-07-09 17:29:31 +02:00
Elias Naur 878131189b all: remove redundant op.TransformOp.Offset
Use op.Offset instead, or create and manipulate a f32.Affine2D.

API change. Update your code with a gofmt rule:

	gofmt -r 'op.TransformOp{}.Offset -> op.Offset'

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-06-21 22:41:56 +02:00
Elias Naur 596e321610 all: make unit.Converter concrete and rename to Metric
An interface for scaling dp and sp is overkill, at least for all
current uses. Make it a concrete struct type, and rename it to the
shorter and more precise Metric.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-06-17 11:47:14 +02:00
Elias Naur 3b54c665ca layout: add WeightSum to Flex for overriding the Flexed total weight
Updates gio#139

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-06-17 09:43:11 +02:00
Elias Naur 5272dc9fa5 layout: make Flexed weight a weight, not a ratio
It's more intuitive to specify the weight as a ratio of the total
weight of all Flexed children than to specify the ratio of the
flexable space.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-06-10 11:25:25 +02:00
Elias Naur 6380baacb6 all: move Now from system.Config to system.FrameEvent
Then, make layout.Context.Now a field, copied from FrameEvent.Now.

API change:

	gofmt -r 'gtx.Now() -> gtx.Now'

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-06-09 23:23:08 +02:00
Elias Naur d1ea9339d5 layout: change NewContext to take the FrameEvent directly
The layout package imports io/system anyway, so depending on
FrameEvent does not introduce new dependencies.

API change. Use gofmt to adjust your code:

	gofmt -r 'layout.NewContext(a, b, c, d) -> layout.NewContext(a, e)'

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-06-09 22:57:19 +02:00
Chris Waldon 3a31045dc9 layout: add Disabled method to Context
This adds a simple method that returns a copy of the Context
with no event queue. Widgets laid out with this Context will
never receive events, and can check whether the event queue
is nil as a hint for whether or not to draw themselves as
disabled.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2020-06-08 10:16:50 +02:00
Elias Naur 4484674ab1 layout: don't run alloc tests with -race
The race runtime allocates where the non-race runtime doesn't.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-06-03 10:18:57 +02:00
Elias Naur c19ed05342 op: change CallOp to be a return value from MacroOp.Stop
Converting

	macro := op.Record(ops)
	...
	macro.Stop()

	macro.Add()

to

	macro := op.Record(ops)
	...
	call := macro.Stop()

	call.Add(ops)

Which is more general (call.Add can take a different ops than the op.Record
that started it), and enforced the order between Stop and the subsequent Add.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-06-02 12:07:20 +02:00
Elias Naur ce0cc706ad layout: add test that Flex doesn't allocate
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-06-02 11:37:45 +02:00
Elias Naur 2bdf8c3851 layout: add test that Stack doesn't allocate
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-06-02 11:36:36 +02:00
Thomas Bruyelle ae8a377cda op: add op.Push and op.Record funcs
The funcs replace stack.Push and macro.Record, which become private.
This makes stack and macro faster to write, in particular for stacks
where you can just write the following line to save and restore the
state :

  defer op.Push(ops).Pop()

This usage requires Push to return a pointer (since Pop has a pointer
receiver), or else the code doesn't compile.

For consistancy, I tried to do the same for op.Record, but this implied
to turn all the MacroOp fields into pointers, and this caused some
panics. As a result, op.Record doesn't return a pointer.

An other side effect pointed by Larry Clapp: StackOp and MacroOp are not
re-usable any more, you have to allocate a new one for each usage, using
the described funcs above.

Signed-off-by: Thomas Bruyelle <thomas.bruyelle@gmail.com>
2020-06-02 10:39:56 +02:00
Elias Naur 28bf9e2001 layout: create input group for List
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-06-01 00:04:44 +02:00
Elias Naur 3af01a3f43 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>
2020-05-23 22:28:49 +02:00
Elias Naur 5a8e1c5acf layout: expand Constraints documentation
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-05-19 15:21:27 +02:00
Elias Naur 013ea395b4 all: use new rectangle and point convenience functions
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-05-19 11:03:30 +02:00
Elias Naur d82eb8fc66 layout,f32: add convenience functions for rectangles and points
layout.FRect, layout.FPt for converting from integer to floating point,
useful for drawing operations.

f32.Pt is a shorthand that mirrors image.Pt.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-05-19 10:59:32 +02:00
Elias Naur dac8ffc002 layout: add Exact for constructing rigid Constraints matching a size
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-05-19 10:12:11 +02:00
Elias Naur 8a6d4f699c layout: expand Stack documentation
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-05-19 10:05:27 +02:00
Elias Naur 7bf3265ccd layout,widget: transpose Constraints to use image.Points for limits
Instead of

    type Contraints struct {
	    Width, Height Constraint
    }

use

    type Constraints struct {
	    Min, Max image.Point
    }

which leads to simpler use. For example, the Min method is trivally replaced by
the field, and the RigidConstraints constructor is no longer a net win.

API Change. Rewrites:

    gofmt -r 'gtx.Constraints.Min() -> gtx.Constraints.Min'
    gofmt -r 'gtx.Constraints.Width.Min -> gtx.Constraints.Min.X'
    gofmt -r 'gtx.Constraints.Height.Min -> gtx.Constraints.Min.Y'
    gofmt -r 'gtx.Constraints.Height.Max -> gtx.Constraints.Max.Y'
    gofmt -r 'gtx.Constraints.Width.Max -> gtx.Constraints.Max.X'

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-05-19 09:58:07 +02:00
Elias Naur 03db2817ac all: rename io/event.Key to Tag
Key had an unfortunate association with keyboard input.

This is an API change. The following rewrites were run to fixup
Gio code:

        $ gofmt -r 'pointer.InputOp{Key:a} -> pointer.InputOp{Tag:a}' -w .
        $ gofmt -r 'pointer.InputOp{Key:a, Grab:b} -> pointer.InputOp{Tag:a, Grab:b}' -w .
        $ gofmt -r 'key.InputOp{Key:a} -> key.InputOp{Tag:a}' -w .
        $ gofmt -r 'key.InputOp{Key:a, Focus:b} -> key.InputOp{Tag:a, Focus:b}' -w .
        $ gofmt -r 'event.Key -> event.Tag' -w .

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-05-17 19:48:12 +02:00
Wagner Riffel 4bbc6379ed doc: rewrite references to preivous api to current
Signed-off-by: Wagner Riffel <wgrriffel@gmail.com>
2020-05-13 12:59:33 +02:00
Elias Naur 4e20ea83a1 widget/material: use cosntraints for setting pointer hit areas
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-05-11 12:23:18 +02:00
Elias Naur 289fe02b90 layout: replace NewContext with a Queue argument to Context.Reset
We're about to reduce the scope of the Window.Queue by moving it
to FrameEvent. As a consequence, Context can no longer rely on a
Queue constant over its lifetime.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-05-03 21:03:40 +02:00
Elias Naur d350f82011 layout: fix typo
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-05-03 17:13:08 +02:00
Elias Naur 148a2828e7 layout: don't force Expanded Stack children larger than their minimum
Instead, honor the constraints after laying out both Stacked and
Expanded children.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-02-29 14:06:25 +01:00
Elias Naur bfb50cef5d all: remove unused fields, functions and add missing error handling
Credit to staticcheck.io.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-02-27 22:26:54 +01:00
Elias Naur 60904dd9a6 layout: fix typos
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-02-14 21:17:44 +01:00
Elias Naur fb7337f794 layout: replace Align with a Layout method on Direction
It's one less type (Align) and shorter:

Before:

	layout.Align(layout.Center).Layout(...)

After

	layout.Center.Layout(...)

It is also safer: since `layout.Align(...)` was a casting operation,
the Go compiler would not complain about an incompatible constant.

For example, the widget/material package contained a wrong cast:

	layout.Align(layout.Start)

which should have been

	layout.Align(layout.W)

After this change, attempting `layout.Start.Layout(...)` result
in a compile error.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-02-02 17:13:02 +01:00