mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
all: [API] change clip.RRect and UniformRRect to take integer coordinates
Like the change to op.Offset before this, clip.RRect and UniformRRect is usually used with integer coordinates. Change to integer coordinates to eliminate many useless conversions to float32. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
+7
-7
@@ -3,9 +3,9 @@
|
||||
package widget
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
|
||||
"gioui.org/f32"
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op/clip"
|
||||
"gioui.org/op/paint"
|
||||
@@ -21,21 +21,21 @@ type Border struct {
|
||||
|
||||
func (b Border) Layout(gtx layout.Context, w layout.Widget) layout.Dimensions {
|
||||
dims := w(gtx)
|
||||
sz := layout.FPt(dims.Size)
|
||||
sz := dims.Size
|
||||
|
||||
rr := float32(gtx.Px(b.CornerRadius))
|
||||
width := float32(gtx.Px(b.Width))
|
||||
rr := gtx.Px(b.CornerRadius)
|
||||
width := gtx.Px(b.Width)
|
||||
sz.X -= width
|
||||
sz.Y -= width
|
||||
|
||||
r := f32.Rectangle{Max: sz}
|
||||
r = r.Add(f32.Point{X: width * 0.5, Y: width * 0.5})
|
||||
r := image.Rectangle{Max: sz}
|
||||
r = r.Add(image.Point{X: width / 2, Y: width / 2})
|
||||
|
||||
paint.FillShape(gtx.Ops,
|
||||
b.Color,
|
||||
clip.Stroke{
|
||||
Path: clip.UniformRRect(r, rr).Path(gtx.Ops),
|
||||
Width: width,
|
||||
Width: float32(width),
|
||||
}.Op(),
|
||||
)
|
||||
|
||||
|
||||
+14
-23
@@ -7,7 +7,6 @@ import (
|
||||
"image/color"
|
||||
"math"
|
||||
|
||||
"gioui.org/f32"
|
||||
"gioui.org/internal/f32color"
|
||||
"gioui.org/io/semantic"
|
||||
"gioui.org/layout"
|
||||
@@ -130,11 +129,8 @@ func (b ButtonLayoutStyle) Layout(gtx layout.Context, w layout.Widget) layout.Di
|
||||
semantic.Button.Add(gtx.Ops)
|
||||
return layout.Stack{Alignment: layout.Center}.Layout(gtx,
|
||||
layout.Expanded(func(gtx layout.Context) layout.Dimensions {
|
||||
rr := float32(gtx.Px(b.CornerRadius))
|
||||
defer clip.UniformRRect(f32.Rectangle{Max: f32.Point{
|
||||
X: float32(gtx.Constraints.Min.X),
|
||||
Y: float32(gtx.Constraints.Min.Y),
|
||||
}}, rr).Push(gtx.Ops).Pop()
|
||||
rr := gtx.Px(b.CornerRadius)
|
||||
defer clip.UniformRRect(image.Rectangle{Max: gtx.Constraints.Min}, rr).Push(gtx.Ops).Pop()
|
||||
background := b.Background
|
||||
switch {
|
||||
case gtx.Queue == nil:
|
||||
@@ -165,12 +161,8 @@ func (b IconButtonStyle) Layout(gtx layout.Context) layout.Dimensions {
|
||||
}
|
||||
return layout.Stack{Alignment: layout.Center}.Layout(gtx,
|
||||
layout.Expanded(func(gtx layout.Context) layout.Dimensions {
|
||||
sizex, sizey := gtx.Constraints.Min.X, gtx.Constraints.Min.Y
|
||||
sizexf, sizeyf := float32(sizex), float32(sizey)
|
||||
rr := (sizexf + sizeyf) * .25
|
||||
defer clip.UniformRRect(f32.Rectangle{
|
||||
Max: f32.Point{X: sizexf, Y: sizeyf},
|
||||
}, rr).Push(gtx.Ops).Pop()
|
||||
rr := (gtx.Constraints.Min.X + gtx.Constraints.Min.Y) / 4
|
||||
defer clip.UniformRRect(image.Rectangle{Max: gtx.Constraints.Min}, rr).Push(gtx.Ops).Pop()
|
||||
background := b.Background
|
||||
switch {
|
||||
case gtx.Queue == nil:
|
||||
@@ -199,7 +191,7 @@ func (b IconButtonStyle) Layout(gtx layout.Context) layout.Dimensions {
|
||||
)
|
||||
})
|
||||
c := m.Stop()
|
||||
bounds := f32.Rectangle{Max: layout.FPt(dims.Size)}
|
||||
bounds := image.Rectangle{Max: dims.Size}
|
||||
defer clip.Ellipse(bounds).Push(gtx.Ops).Pop()
|
||||
c.Add(gtx.Ops)
|
||||
return dims
|
||||
@@ -282,25 +274,24 @@ func drawInk(gtx layout.Context, c widget.Press) {
|
||||
// Beziér ease-in curve.
|
||||
alphaBezier := t2 * t2 * (3.0 - 2.0*t2)
|
||||
sizeBezier := sizet * sizet * (3.0 - 2.0*sizet)
|
||||
size := float32(gtx.Constraints.Min.X)
|
||||
if h := float32(gtx.Constraints.Min.Y); h > size {
|
||||
size := gtx.Constraints.Min.X
|
||||
if h := gtx.Constraints.Min.Y; h > size {
|
||||
size = h
|
||||
}
|
||||
// Cover the entire constraints min rectangle.
|
||||
size *= 2 * float32(math.Sqrt(2))
|
||||
// Apply curve values to size and color.
|
||||
size *= sizeBezier
|
||||
// Cover the entire constraints min rectangle and
|
||||
// apply curve values to size and color.
|
||||
size = int(float32(size) * 2 * float32(math.Sqrt(2)) * sizeBezier)
|
||||
alpha := 0.7 * alphaBezier
|
||||
const col = 0.8
|
||||
ba, bc := byte(alpha*0xff), byte(col*0xff)
|
||||
rgba := f32color.MulAlpha(color.NRGBA{A: 0xff, R: bc, G: bc, B: bc}, ba)
|
||||
ink := paint.ColorOp{Color: rgba}
|
||||
ink.Add(gtx.Ops)
|
||||
rr := size * .5
|
||||
rr := size / 2
|
||||
defer op.Offset(c.Position.Add(image.Point{
|
||||
X: -int(rr),
|
||||
Y: -int(rr),
|
||||
X: -rr,
|
||||
Y: -rr,
|
||||
})).Push(gtx.Ops).Pop()
|
||||
defer clip.UniformRRect(f32.Rectangle{Max: f32.Pt(size, size)}, rr).Push(gtx.Ops).Pop()
|
||||
defer clip.UniformRRect(image.Rectangle{Max: image.Pt(size, size)}, rr).Push(gtx.Ops).Pop()
|
||||
paint.PaintOp{}.Add(gtx.Ops)
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"image"
|
||||
"image/color"
|
||||
|
||||
"gioui.org/f32"
|
||||
"gioui.org/internal/f32color"
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op/clip"
|
||||
@@ -50,7 +49,7 @@ func (c *checkable) layout(gtx layout.Context, checked, hovered bool) layout.Dim
|
||||
|
||||
background := f32color.MulAlpha(c.IconColor, 70)
|
||||
|
||||
b := f32.Rectangle{Max: f32.Pt(float32(size), float32(size))}
|
||||
b := image.Rectangle{Max: image.Pt(size, size)}
|
||||
paint.FillShape(gtx.Ops, background, clip.Ellipse(b).Op(gtx.Ops))
|
||||
|
||||
return dims
|
||||
|
||||
@@ -144,19 +144,18 @@ func minimizeWindow(gtx layout.Context) layout.Dimensions {
|
||||
// maximizeWindow draws a rectangle representing the maximize action.
|
||||
func maximizeWindow(gtx layout.Context) layout.Dimensions {
|
||||
size := gtx.Px(winIconSize)
|
||||
size32 := float32(size)
|
||||
margin := float32(gtx.Px(winIconMargin))
|
||||
width := float32(gtx.Px(winIconStroke))
|
||||
margin := gtx.Px(winIconMargin)
|
||||
width := gtx.Px(winIconStroke)
|
||||
r := clip.RRect{
|
||||
Rect: f32.Rect(margin, margin, size32-margin, size32-margin),
|
||||
Rect: image.Rect(margin, margin, size-margin, size-margin),
|
||||
}
|
||||
st := clip.Stroke{
|
||||
Path: r.Path(gtx.Ops),
|
||||
Width: width,
|
||||
Width: float32(width),
|
||||
}.Op().Push(gtx.Ops)
|
||||
paint.PaintOp{}.Add(gtx.Ops)
|
||||
st.Pop()
|
||||
r.Rect.Max = f32.Pt(size32-margin, 2*margin)
|
||||
r.Rect.Max = image.Pt(size-margin, 2*margin)
|
||||
st = clip.Outline{
|
||||
Path: r.Path(gtx.Ops),
|
||||
}.Op().Push(gtx.Ops)
|
||||
@@ -168,24 +167,23 @@ func maximizeWindow(gtx layout.Context) layout.Dimensions {
|
||||
// maximizedWindow draws interleaved rectangles representing the un-maximize action.
|
||||
func maximizedWindow(gtx layout.Context) layout.Dimensions {
|
||||
size := gtx.Px(winIconSize)
|
||||
size32 := float32(size)
|
||||
margin := float32(gtx.Px(winIconMargin))
|
||||
width := float32(gtx.Px(winIconStroke))
|
||||
margin := gtx.Px(winIconMargin)
|
||||
width := gtx.Px(winIconStroke)
|
||||
r := clip.RRect{
|
||||
Rect: f32.Rect(margin, margin, size32-2*margin, size32-2*margin),
|
||||
Rect: image.Rect(margin, margin, size-2*margin, size-2*margin),
|
||||
}
|
||||
st := clip.Stroke{
|
||||
Path: r.Path(gtx.Ops),
|
||||
Width: width,
|
||||
Width: float32(width),
|
||||
}.Op().Push(gtx.Ops)
|
||||
paint.PaintOp{}.Add(gtx.Ops)
|
||||
st.Pop()
|
||||
r = clip.RRect{
|
||||
Rect: f32.Rect(2*margin, 2*margin, size32-margin, size32-margin),
|
||||
Rect: image.Rect(2*margin, 2*margin, size-margin, size-margin),
|
||||
}
|
||||
st = clip.Stroke{
|
||||
Path: r.Path(gtx.Ops),
|
||||
Width: width,
|
||||
Width: float32(width),
|
||||
}.Op().Push(gtx.Ops)
|
||||
paint.PaintOp{}.Add(gtx.Ops)
|
||||
st.Pop()
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"image/color"
|
||||
"math"
|
||||
|
||||
"gioui.org/f32"
|
||||
"gioui.org/io/pointer"
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op"
|
||||
@@ -188,15 +187,14 @@ func (s ScrollbarStyle) layout(gtx layout.Context, axis layout.Axis, viewportSta
|
||||
X: indicatorLen,
|
||||
Y: gtx.Px(s.Indicator.MinorWidth),
|
||||
})
|
||||
indicatorDimsF := layout.FPt(indicatorDims)
|
||||
radius := float32(gtx.Px(s.Indicator.CornerRadius))
|
||||
radius := gtx.Px(s.Indicator.CornerRadius)
|
||||
|
||||
// Lay out the indicator.
|
||||
offset := axis.Convert(image.Pt(viewStart, 0))
|
||||
defer op.Offset(offset).Push(gtx.Ops).Pop()
|
||||
paint.FillShape(gtx.Ops, s.Indicator.Color, clip.RRect{
|
||||
Rect: f32.Rectangle{
|
||||
Max: indicatorDimsF,
|
||||
Rect: image.Rectangle{
|
||||
Max: indicatorDims,
|
||||
},
|
||||
SW: radius,
|
||||
NW: radius,
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"image"
|
||||
"image/color"
|
||||
|
||||
"gioui.org/f32"
|
||||
"gioui.org/internal/f32color"
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op/clip"
|
||||
@@ -29,27 +28,26 @@ func ProgressBar(th *Theme, progress float32) ProgressBarStyle {
|
||||
}
|
||||
|
||||
func (p ProgressBarStyle) Layout(gtx layout.Context) layout.Dimensions {
|
||||
shader := func(width float32, color color.NRGBA) layout.Dimensions {
|
||||
maxHeight := unit.Dp(4)
|
||||
rr := float32(gtx.Px(unit.Dp(2)))
|
||||
shader := func(width int, color color.NRGBA) layout.Dimensions {
|
||||
var maxHeight = unit.Dp(4)
|
||||
rr := gtx.Px(unit.Dp(2))
|
||||
|
||||
d := image.Point{X: int(width), Y: gtx.Px(maxHeight)}
|
||||
d := image.Point{X: width, Y: gtx.Px(maxHeight)}
|
||||
|
||||
height := float32(gtx.Px(maxHeight))
|
||||
defer clip.UniformRRect(f32.Rectangle{Max: f32.Pt(width, height)}, rr).Push(gtx.Ops).Pop()
|
||||
defer clip.UniformRRect(image.Rectangle{Max: image.Pt(width, d.Y)}, rr).Push(gtx.Ops).Pop()
|
||||
paint.ColorOp{Color: color}.Add(gtx.Ops)
|
||||
paint.PaintOp{}.Add(gtx.Ops)
|
||||
|
||||
return layout.Dimensions{Size: d}
|
||||
}
|
||||
|
||||
progressBarWidth := float32(gtx.Constraints.Max.X)
|
||||
progressBarWidth := gtx.Constraints.Max.X
|
||||
return layout.Stack{Alignment: layout.W}.Layout(gtx,
|
||||
layout.Stacked(func(gtx layout.Context) layout.Dimensions {
|
||||
return shader(progressBarWidth, p.TrackColor)
|
||||
}),
|
||||
layout.Stacked(func(gtx layout.Context) layout.Dimensions {
|
||||
fillWidth := progressBarWidth * clamp1(p.Progress)
|
||||
fillWidth := int(float32(progressBarWidth) * clamp1(p.Progress))
|
||||
fillColor := p.Color
|
||||
if gtx.Queue == nil {
|
||||
fillColor = f32color.Disabled(fillColor)
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"image"
|
||||
"image/color"
|
||||
|
||||
"gioui.org/f32"
|
||||
"gioui.org/internal/f32color"
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op"
|
||||
@@ -78,9 +77,9 @@ func (s SliderStyle) Layout(gtx layout.Context) layout.Dimensions {
|
||||
|
||||
// Draw thumb.
|
||||
pt := axis.Convert(image.Pt(thumbPos, sizeCross/2))
|
||||
thumb := f32.Rectangle{
|
||||
Min: f32.Pt(float32(pt.X-thumbRadius), float32(pt.Y-thumbRadius)),
|
||||
Max: f32.Pt(float32(pt.X+thumbRadius), float32(pt.Y+thumbRadius)),
|
||||
thumb := image.Rectangle{
|
||||
Min: image.Pt(pt.X-thumbRadius, pt.Y-thumbRadius),
|
||||
Max: image.Pt(pt.X+thumbRadius, pt.Y+thumbRadius),
|
||||
}
|
||||
paint.FillShape(gtx.Ops, color, clip.Ellipse(thumb).Op(gtx.Ops))
|
||||
|
||||
|
||||
+16
-17
@@ -6,7 +6,6 @@ import (
|
||||
"image"
|
||||
"image/color"
|
||||
|
||||
"gioui.org/f32"
|
||||
"gioui.org/internal/f32color"
|
||||
"gioui.org/io/semantic"
|
||||
"gioui.org/layout"
|
||||
@@ -47,10 +46,10 @@ func (s SwitchStyle) Layout(gtx layout.Context) layout.Dimensions {
|
||||
trackOff := (thumbSize - trackHeight) / 2
|
||||
|
||||
// Draw track.
|
||||
trackCorner := float32(trackHeight) / 2
|
||||
trackRect := f32.Rectangle{Max: f32.Point{
|
||||
X: float32(trackWidth),
|
||||
Y: float32(trackHeight),
|
||||
trackCorner := trackHeight / 2
|
||||
trackRect := image.Rectangle{Max: image.Point{
|
||||
X: trackWidth,
|
||||
Y: trackHeight,
|
||||
}}
|
||||
col := s.Color.Disabled
|
||||
if s.Switch.Value {
|
||||
@@ -69,14 +68,14 @@ func (s SwitchStyle) Layout(gtx layout.Context) layout.Dimensions {
|
||||
|
||||
// Draw thumb ink.
|
||||
inkSize := gtx.Px(unit.Dp(44))
|
||||
rr := float32(inkSize) * .5
|
||||
rr := inkSize / 2
|
||||
inkOff := image.Point{
|
||||
X: trackWidth/2 - int(rr),
|
||||
Y: -int(rr) + trackHeight/2 + trackOff,
|
||||
X: trackWidth/2 - rr,
|
||||
Y: -rr + trackHeight/2 + trackOff,
|
||||
}
|
||||
t = op.Offset(inkOff).Push(gtx.Ops)
|
||||
gtx.Constraints.Min = image.Pt(inkSize, inkSize)
|
||||
cl = clip.UniformRRect(f32.Rectangle{Max: layout.FPt(gtx.Constraints.Min)}, rr).Push(gtx.Ops)
|
||||
cl = clip.UniformRRect(image.Rectangle{Max: gtx.Constraints.Min}, rr).Push(gtx.Ops)
|
||||
for _, p := range s.Switch.History() {
|
||||
drawInk(gtx, p)
|
||||
}
|
||||
@@ -89,18 +88,18 @@ func (s SwitchStyle) Layout(gtx layout.Context) layout.Dimensions {
|
||||
defer op.Offset(image.Point{X: xoff}).Push(gtx.Ops).Pop()
|
||||
}
|
||||
|
||||
thumbRadius := float32(thumbSize) / 2
|
||||
thumbRadius := thumbSize / 2
|
||||
|
||||
circle := func(x, y, r float32) clip.Op {
|
||||
b := f32.Rectangle{
|
||||
Min: f32.Pt(x-r, y-r),
|
||||
Max: f32.Pt(x+r, y+r),
|
||||
circle := func(x, y, r int) clip.Op {
|
||||
b := image.Rectangle{
|
||||
Min: image.Pt(x-r, y-r),
|
||||
Max: image.Pt(x+r, y+r),
|
||||
}
|
||||
return clip.Ellipse(b).Op(gtx.Ops)
|
||||
}
|
||||
// Draw hover.
|
||||
if s.Switch.Hovered() || s.Switch.Focused() {
|
||||
r := 1.7 * thumbRadius
|
||||
r := thumbRadius * 10 / 17
|
||||
background := f32color.MulAlpha(s.Color.Enabled, 70)
|
||||
paint.FillShape(gtx.Ops, background, circle(thumbRadius, thumbRadius, r))
|
||||
}
|
||||
@@ -108,7 +107,7 @@ func (s SwitchStyle) Layout(gtx layout.Context) layout.Dimensions {
|
||||
// Draw thumb shadow, a translucent disc slightly larger than the
|
||||
// thumb itself.
|
||||
// Center shadow horizontally and slightly adjust its Y.
|
||||
paint.FillShape(gtx.Ops, argb(0x55000000), circle(thumbRadius, thumbRadius+.25, thumbRadius+1))
|
||||
paint.FillShape(gtx.Ops, argb(0x55000000), circle(thumbRadius, thumbRadius+gtx.Px(unit.Dp(.25)), thumbRadius+1))
|
||||
|
||||
// Draw thumb.
|
||||
paint.FillShape(gtx.Ops, col, circle(thumbRadius, thumbRadius, thumbRadius))
|
||||
@@ -121,7 +120,7 @@ func (s SwitchStyle) Layout(gtx layout.Context) layout.Dimensions {
|
||||
}
|
||||
defer op.Offset(clickOff).Push(gtx.Ops).Pop()
|
||||
sz := image.Pt(clickSize, clickSize)
|
||||
defer clip.Ellipse(f32.Rectangle{Max: layout.FPt(sz)}).Push(gtx.Ops).Pop()
|
||||
defer clip.Ellipse(image.Rectangle{Max: sz}).Push(gtx.Ops).Pop()
|
||||
s.Switch.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
|
||||
if d := s.Description; d != "" {
|
||||
semantic.DescriptionOp(d).Add(gtx.Ops)
|
||||
|
||||
Reference in New Issue
Block a user