widget/material: add Float.Invert

Setting Float.Invert=true not only inverts the order of values (which was already easily done by swapping min and max), it also draws the widget inverted so that the track is darkened on the opposite side from usual.

This patch also fixes a bug wherein a vertical slider was drawn inverted by default.

Signed-off-by: Gordon Klaus <gordon.klaus@gmail.com>
This commit is contained in:
Gordon Klaus
2023-01-19 11:37:11 +01:00
committed by Elias Naur
parent ac2c284d16
commit 22aa00f476
2 changed files with 30 additions and 16 deletions
+7 -3
View File
@@ -13,8 +13,9 @@ import (
// Float is for selecting a value in a range.
type Float struct {
Value float32
Axis layout.Axis
Value float32
Axis layout.Axis
Invert bool
drag gesture.Drag
pos float32 // position normalized to [0, 1]
@@ -43,7 +44,10 @@ func (f *Float) Layout(gtx layout.Context, pointerMargin int, min, max float32)
if de != nil {
xy := de.Position.X
if f.Axis == layout.Vertical {
xy = de.Position.Y
xy = f.length - de.Position.Y
}
if f.Invert {
xy = f.length - xy
}
f.pos = xy / f.length
value = min + (max-min)*f.pos