~whereswaldon/gio-x

ref: b0198a1b547035c76d814b60de9aec3fce6353a8 gio-x/component/shadow.go -rw-r--r-- 8.0 KiB
b0198a1bChris Waldon component: document and refactor tooltip types 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// SPDX-License-Identifier: Unlicense OR MIT

package component

/*
This file is derived from work by Egon Elbre in his gio experiments
repository available here:

https://github.com/egonelbre/expgio/tree/master/box-shadows

He generously licensed it under the Unlicense, and thus is is
reproduced here under the same terms.
*/

import (
	"image"
	"image/color"
	"math"

	"gioui.org/f32"
	"gioui.org/layout"
	"gioui.org/op"
	"gioui.org/op/clip"
	"gioui.org/op/paint"
	"gioui.org/unit"
)

// ShadowStyle defines a shadow cast by a rounded rectangle.
//
// TODO(whereswaldon): make this support RRects that do not have
// uniform corner radii.
type ShadowStyle struct {
	// The radius of the corners of the rectangle casting the surface.
	// Non-rounded rectangles can just provide a zero.
	CornerRadius unit.Value
	// Elevation is how high the surface casting the shadow is above
	// the background, and therefore determines how diffuse and large
	// the shadow is.
	Elevation unit.Value
	// The colors of various components of the shadow. The Shadow()
	// constructor populates these with reasonable defaults.
	AmbientColor, PenumbraColor, UmbraColor color.NRGBA
}

// Shadow defines a shadow cast by a rounded rectangle with the given
// corner radius and elevation. It sets reasonable defaults for the
// shadow colors.
func Shadow(radius, elevation unit.Value) ShadowStyle {
	return ShadowStyle{
		CornerRadius:  radius,
		Elevation:     elevation,
		AmbientColor:  color.NRGBA{A: 0x10},
		PenumbraColor: color.NRGBA{A: 0x20},
		UmbraColor:    color.NRGBA{A: 0x30},
	}
}

// Layout renders the shadow into the gtx. The shadow's size will assume
// that the rectangle casting the shadow is of size gtx.Constraints.Min.
func (s ShadowStyle) Layout(gtx layout.Context) layout.Dimensions {
	sz := gtx.Constraints.Min
	rr := float32(gtx.Px(s.CornerRadius))

	r := f32.Rect(0, 0, float32(sz.X), float32(sz.Y))
	s.layoutShadow(gtx, r, rr)

	return layout.Dimensions{Size: sz}
}

func (s ShadowStyle) layoutShadow(gtx layout.Context, r f32.Rectangle, rr float32) {
	if s.Elevation.V <= 0 {
		return
	}

	offset := pxf(gtx.Metric, s.Elevation)

	ambient := r
	gradientBox(gtx.Ops, ambient, rr, offset/2, s.AmbientColor)

	penumbra := r.Add(f32.Pt(0, offset/2))
	gradientBox(gtx.Ops, penumbra, rr, offset, s.PenumbraColor)

	umbra := outset(penumbra, -offset/2)
	gradientBox(gtx.Ops, umbra, rr/4, offset/2, s.UmbraColor)
}

// TODO(whereswaldon): switch back to commented implementation when radial
// gradients are available in core.
func gradientBox(ops *op.Ops, r f32.Rectangle, rr, spread float32, col color.NRGBA) {
	/*
		transparent := col
		transparent.A = 0

		// ensure we are aligned to pixel grid
		r = round(r)
		rr = float32(math.Ceil(float64(rr)))
		spread = float32(math.Ceil(float64(spread)))

		// calculate inside and outside boundaries
		inside := imageRect(outset(r, -rr))
		center := imageRect(r)
		outside := imageRect(outset(r, spread))

		radialStop2 := image.Pt(0, int(spread+rr))
		radialOffset1 := rr / (spread + rr)

		corners := []func(image.Rectangle) image.Point{
			topLeft,
			topRight,
			bottomRight,
			bottomLeft,
		}

		for _, corner := range corners {
			func() {
				defer op.Save(ops).Load()
				clipr := image.Rectangle{
					Min: corner(inside),
					Max: corner(outside),
				}.Canon()
				clip.Rect(clipr).Add(ops)
				paint.RadialGradientOp{
					Color1: col, Color2: transparent,
					Stop1:   layout.FPt(corner(inside)),
					Stop2:   layout.FPt(corner(inside).Add(radialStop2)),
					Offset1: radialOffset1,
				}.Add(ops)
				paint.PaintOp{}.Add(ops)
			}()
		}

		// top
		func() {
			defer op.Save(ops).Load()
			clipr := image.Rectangle{
				Min: image.Point{
					X: inside.Min.X,
					Y: outside.Min.Y,
				},
				Max: image.Point{
					X: inside.Max.X,
					Y: center.Min.Y,
				},
			}
			clip.Rect(clipr).Add(ops)
			paint.LinearGradientOp{
				Color1: col, Color2: transparent,
				Stop1: layout.FPt(image.Point{
					X: inside.Min.X,
					Y: center.Min.Y,
				}),
				Stop2: layout.FPt(image.Point{
					X: inside.Min.X,
					Y: outside.Min.Y,
				}),
			}.Add(ops)
			paint.PaintOp{}.Add(ops)
		}()

		// right
		func() {
			defer op.Save(ops).Load()
			clipr := image.Rectangle{
				Min: image.Point{
					X: center.Max.X,
					Y: inside.Min.Y,
				},
				Max: image.Point{
					X: outside.Max.X,
					Y: inside.Max.Y,
				},
			}
			clip.Rect(clipr).Add(ops)
			paint.LinearGradientOp{
				Color1: col, Color2: transparent,
				Stop1: layout.FPt(image.Point{
					X: center.Max.X,
					Y: inside.Min.Y,
				}),
				Stop2: layout.FPt(image.Point{
					X: outside.Max.X,
					Y: inside.Min.Y,
				}),
			}.Add(ops)
			paint.PaintOp{}.Add(ops)
		}()

		// bottom
		func() {
			defer op.Save(ops).Load()
			clipr := image.Rectangle{
				Min: image.Point{
					X: inside.Min.X,
					Y: center.Max.Y,
				},
				Max: image.Point{
					X: inside.Max.X,
					Y: outside.Max.Y,
				},
			}
			clip.Rect(clipr).Add(ops)
			paint.LinearGradientOp{
				Color1: col, Color2: transparent,
				Stop1: layout.FPt(image.Point{
					X: inside.Min.X,
					Y: center.Max.Y,
				}),
				Stop2: layout.FPt(image.Point{
					X: inside.Min.X,
					Y: outside.Max.Y,
				}),
			}.Add(ops)
			paint.PaintOp{}.Add(ops)
		}()

		// left
		func() {
			defer op.Save(ops).Load()
			clipr := image.Rectangle{
				Min: image.Point{
					X: outside.Min.X,
					Y: inside.Min.Y,
				},
				Max: image.Point{
					X: center.Min.X,
					Y: inside.Max.Y,
				},
			}
			clip.Rect(clipr).Add(ops)
			paint.LinearGradientOp{
				Color1: col, Color2: transparent,
				Stop1: layout.FPt(image.Point{
					X: center.Min.X,
					Y: inside.Min.Y,
				}),
				Stop2: layout.FPt(image.Point{
					X: outside.Min.X,
					Y: inside.Min.Y,
				}),
			}.Add(ops)
			paint.PaintOp{}.Add(ops)
		}()

		func() {
			defer op.Save(ops).Load()
			var p clip.Path
			p.Begin(ops)

			inside := layout.FRect(inside)
			center := layout.FRect(center)

			p.MoveTo(inside.Min)
			p.LineTo(f32.Point{X: inside.Min.X, Y: center.Min.Y})
			p.LineTo(f32.Point{X: inside.Max.X, Y: center.Min.Y})
			p.LineTo(f32.Point{X: inside.Max.X, Y: inside.Min.Y})
			p.LineTo(f32.Point{X: center.Max.X, Y: inside.Min.Y})
			p.LineTo(f32.Point{X: center.Max.X, Y: inside.Max.Y})
			p.LineTo(f32.Point{X: inside.Max.X, Y: inside.Max.Y})
			p.LineTo(f32.Point{X: inside.Max.X, Y: center.Max.Y})
			p.LineTo(f32.Point{X: inside.Min.X, Y: center.Max.Y})
			p.LineTo(f32.Point{X: inside.Min.X, Y: inside.Max.Y})
			p.LineTo(f32.Point{X: center.Min.X, Y: inside.Max.Y})
			p.LineTo(f32.Point{X: center.Min.X, Y: inside.Min.Y})
			p.LineTo(inside.Min)

			clip.Outline{Path: p.End()}.Op().Add(ops)
			paint.ColorOp{Color: col}.Add(ops)
			paint.PaintOp{}.Add(ops)
		}()
	*/
	paint.FillShape(ops, col, clip.RRect{
		Rect: outset(r, spread),
		SE:   rr + spread, SW: rr + spread, NW: rr + spread, NE: rr + spread,
	}.Op(ops))
}

func imageRect(r f32.Rectangle) image.Rectangle {
	return image.Rectangle{
		Min: image.Point{
			X: int(math.Round(float64(r.Min.X))),
			Y: int(math.Round(float64(r.Min.Y))),
		},
		Max: image.Point{
			X: int(math.Round(float64(r.Max.X))),
			Y: int(math.Round(float64(r.Max.Y))),
		},
	}
}

func round(r f32.Rectangle) f32.Rectangle {
	return f32.Rectangle{
		Min: f32.Point{
			X: float32(math.Round(float64(r.Min.X))),
			Y: float32(math.Round(float64(r.Min.Y))),
		},
		Max: f32.Point{
			X: float32(math.Round(float64(r.Max.X))),
			Y: float32(math.Round(float64(r.Max.Y))),
		},
	}
}

func outset(r f32.Rectangle, rr float32) f32.Rectangle {
	r.Min.X -= rr
	r.Min.Y -= rr
	r.Max.X += rr
	r.Max.Y += rr
	return r
}

func pxf(c unit.Metric, v unit.Value) float32 {
	switch v.U {
	case unit.UnitPx:
		return v.V
	case unit.UnitDp:
		s := c.PxPerDp
		if s == 0 {
			s = 1
		}
		return s * v.V
	case unit.UnitSp:
		s := c.PxPerSp
		if s == 0 {
			s = 1
		}
		return s * v.V
	default:
		panic("unknown unit")
	}
}

func topLeft(r image.Rectangle) image.Point     { return r.Min }
func topRight(r image.Rectangle) image.Point    { return image.Point{X: r.Max.X, Y: r.Min.Y} }
func bottomRight(r image.Rectangle) image.Point { return r.Max }
func bottomLeft(r image.Rectangle) image.Point  { return image.Point{X: r.Min.X, Y: r.Max.Y} }