Commit Graph

119 Commits

Author SHA1 Message Date
Elias Naur 916efb4612 all: apply suggestions from staticcheck.io
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-06-07 12:28:28 +02:00
Elias Naur fc79ec5c94 layout: [API] remove FRect
We're about to unexport f32.Rectangle, this change removes the only
public API for it.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-05-31 10:24:09 +02:00
Elias Naur 3d37491342 all: [API] replace unit.Value with separate unit.Dp, unit.Sp types
The unit.Value is a struct and thus more inconvenient to use than its
underlying float32 type. In addition, most uses don't need a general
value, but rather a specific unit given by the context. This change
replaces unit.Value with two float32 units, Dp and Sp. It also changes
variables and parameters of unit.Value to a specific unit type matching
the context. That is, unit.Dp everywhere except for text sizes which are
in Sp.

Switching to typed float32s has multiple advantages

- They can be constants:

const touchSlop = unit.Dp(16)

- Casting untyped constants is no longer necessary:

insets := layout.UniformInset(16)

- Calculation with values is natural:

func (s ScrollbarStyle) Width() unit.Dp {
	return s.Indicator.MinorWidth + s.Track.MinorPadding + s.Track.MinorPadding
}

The main API change is that calls to gtx.Px must be replaced with either
gtx.Dp or gtx.Sp depending on the unit.

Idea by Christophe Meessen.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-05-31 10:24:09 +02:00
Elias Naur a63e0cb44a all: [API] change op.Offset to take integer coordinates
op.Offset is a convenience function most often used by layouts. Layouts
usually operate in integer coordinates, and the float32 version of op.Offset
needlessly force conversions from int to float32. This change makes op.Offset
take integer coordinates, to better match its intended use.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-05-31 10:24:09 +02:00
Thomas Mathews 45e8c781e2 layout: improve layout.List documentation
Updated the documentation for layout.List to include the details about how
drawing is performed for items in it. This gives the user an understanding about
how so many items can be drawn for performance.

Signed-off-by: Thomas Mathews <thomas.c.mathews@gmail.com>
2022-05-03 07:57:28 +02:00
Elias Naur 36919ef756 layout: don't clip List children
Clipping all children once to the entire List area is enough. The
change was motivated by #389 where individual child clips would
make it harder for focus scroll heuristics to work.

References: https://todo.sr.ht/~eliasnaur/gio/389
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-04-02 15:46:27 +02:00
Elias Naur afd39a6bfe layout: compute Position.Offset correctly for ScrollToEnd Lists
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-03-31 11:55:48 +02:00
Elias Naur 508330e818 layout: layout one invisible child at each end of a List
A recent change added automatic scrolling to move focused widgets
into view. This change modifies List to layout an extra child at
each of its ends, to enable focus to move to them and trigger
automatic scrolling of the list.

For https://github.com/tailscale/tailscale/issues/4278.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-03-31 11:55:48 +02:00
Elias Naur a699fb89ac layout: default List scroll bounds to infinity
Before, List would only report the remaining scrollable area of the visible
children when positioned close to either end. Now, List always report infinite
scroll bounds *unless* it is positioned at an extremum.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-03-31 11:54:53 +02:00
Chris Waldon db82d12372 layout: add Locale to Context
This commit adds a system.Locale to the layout.Context,
providing an easy means to plumb language information
throughout an application.

References: https://todo.sr.ht/~eliasnaur/gio/146
Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2022-03-18 07:58:12 +01:00
Pierre Curto 3bb0171e7a layout: reorder fields in Inset to match system.Insets
This makes it simple to convert from one into the other.

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
2022-01-25 09:54:05 +01:00
Pierre Curto 2d75181b51 layout: fix dimensions of empty list
When the Min constraints are set but the list
has no item to display, use those as the list
returned dimensions.

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
2021-12-07 12:18:28 +01:00
Elias Naur 3e0b72304a all: replace deprecated pointer.Rect with clip.Rect
Converted with

gofmt -w -r 'pointer.Rect(r) -> clip.Rect(r)' .
gofmt -w -r 'pointer.Ellipse(r) -> clip.Ellipse(layout.FRect(r))' .

combined with 'goimports -w .' to clean up imports.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-11-03 14:12:31 +01:00
Elias Naur 936c266b03 all: [API] split operation stack into per-state stacks
The op.Save and Load methods exist to support the need for
transformation, clip, pointer area state to behave as stacks. For
example, layout needs to apply an offset to its children but not
subsequent operations.

Before this change, op.Save and Load were used to save and restore the
state:

    ops := new(op.Ops)
    // Save state.
    state := op.Save(ops)
    // Apply offset.
    op.Offset(...).Add(ops)
    // Draw with offset applied.
    draw(ops)
    // Restore state.
    state.Load()

A drawback with the op.Save mechanism is that there is no direct
connection between the state change and the saving and loading of state.
This causes confusion as to when a Save/Load is needed and who is
responsible for performing them, which leads to subtle bugs and over-use
of Save/Loads.

This change gets rid of the general state stack and replaces it with
per-state stacks. There is now a stack for transformation, clip, pointer
areas, and they can only be restored by the code pushing state to them.
The example above now becomes:

    ops := new(op.Ops)
    // Push offset to the transformation stack.
    stack := op.Offset(...).Push(ops)
    // Draw with offset applied.
    draw(ops)
    // Restore state.
    stack.Pop()

For convenience, transformation also be Add'ed if the stack operation is
not required.

Simple state such as the current material no longer has a way to be
restored; it is assumed the client of a PaintOp adds their desired
material operation before it.

API change: replace op.Save/Load with explicit Push/Pop scopes for
op.TransformOps, pointer.AreaOps, clip.Ops.

To ease porting, this change retains a version of op.Save/Load that
saves and restores the transformation and clip stacks. It also retains
an Add method for clip.Op.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-10-08 17:21:56 +02:00
Pierre Curto 64bcb1ccd5 layout: do not reset cross axis constraints in Direction for N, S, E and W
Fixes #268

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
2021-09-19 11:04:02 +02:00
Elias Naur 060ae1cdf9 all: add //go:build lines
They're automatically added by Go 1.17 source formatters. This change
adds them all now.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-07-26 15:17:51 +02:00
Chris Waldon 990029985a layout: make List approximate its length in Position
This commit adds a Length field to the
layout.Position. This field contains an approximation
of the overall length of the list's contents.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2021-07-05 16:08:30 +02:00
Chris Waldon cf778ecd06 layout: add Axis.FConvert for f32.Points
This creates a floating-point analog to layout.Axis.Convert
for converting from (x,y) coordinate space to (main,cross)
coordinate space.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2021-06-29 09:06:15 +02:00
Dan Kortschak 0a91858163 layout: fix spelling of Alignment
Signed-off-by: Dan Kortschak <dan@kortschak.io>
2021-04-16 11:07:21 +02:00
pierre 5e1a662b94 io/pointer: support nested scrollables
Fixes #185.

Signed-off-by: pierre <pierre.curto@gmail.com>
2021-03-31 09:57:13 +02:00
pierre b796dd8e3b layout: make list example use List.Position.Count
Counting the number of displayed elements via the ListElement function is incorrect.

Signed-off-by: pierre <pierre.curto@gmail.com>
2021-03-25 11:11:25 +01:00
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