mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 15:45:38 +00:00
060cff257f
The multitude of widget methods on Theme is unnecessary coupling in that all possible widgets either have to be included in package material, or be different than 3rd party widgets: var th *Theme // Core widget, calling a method on Theme. th.Button(...).Layout(...) // 3rd party widget, calling a function taking a Theme. datepicker.New(th, ...).Layout(...) Another reason for the Theme methods was to enable a poor man's theme replacement, so that you could use the same code for compatible themes. For example, mat.Button(...).Layout(...) would not need to change if the type of mat changed, as long as the new type had a compatible method Button. However, that point misses the fact that the mat variable had to be declared somewhere, naming the theme package: var mat *material.Theme (or, say, *cocoa.Theme) A better and complete way to replace a theme is to use import renaming. For example, to replace the material theme with a hypothetical Windows theme, replace import theme "gioui.org/widget/material" with import theme "github.com/somebody/windows This change moves all Theme widget methods to be standalone functions, and renames the widget style types accordingly. For example, instead of the method func (t *Theme) Button(...) Button there is now a function func Button(t *Theme, ...) ButtonStyle Signed-off-by: Elias Naur <mail@eliasnaur.com>
69 lines
1.4 KiB
Go
69 lines
1.4 KiB
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
package material
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
|
|
"gioui.org/f32"
|
|
"gioui.org/layout"
|
|
"gioui.org/op/clip"
|
|
"gioui.org/op/paint"
|
|
"gioui.org/unit"
|
|
)
|
|
|
|
type ProgressBarStyle struct {
|
|
Color color.RGBA
|
|
}
|
|
|
|
func ProgressBar(th *Theme) ProgressBarStyle {
|
|
return ProgressBarStyle{
|
|
Color: th.Color.Primary,
|
|
}
|
|
}
|
|
|
|
func (b ProgressBarStyle) Layout(gtx *layout.Context, progress int) {
|
|
shader := func(width float32, color color.RGBA) {
|
|
maxHeight := unit.Dp(4)
|
|
rr := float32(gtx.Px(unit.Dp(2)))
|
|
|
|
d := image.Point{X: int(width), Y: gtx.Px(maxHeight)}
|
|
dr := f32.Rectangle{
|
|
Max: f32.Point{X: float32(d.X), Y: float32(d.Y)},
|
|
}
|
|
|
|
clip.Rect{
|
|
Rect: f32.Rectangle{Max: f32.Point{X: width, Y: float32(gtx.Px(maxHeight))}},
|
|
NE: rr, NW: rr, SE: rr, SW: rr,
|
|
}.Op(gtx.Ops).Add(gtx.Ops)
|
|
|
|
paint.ColorOp{Color: color}.Add(gtx.Ops)
|
|
paint.PaintOp{Rect: dr}.Add(gtx.Ops)
|
|
|
|
gtx.Dimensions = layout.Dimensions{Size: d}
|
|
}
|
|
|
|
if progress > 100 {
|
|
progress = 100
|
|
} else if progress < 0 {
|
|
progress = 0
|
|
}
|
|
|
|
progressBarWidth := float32(gtx.Constraints.Width.Max)
|
|
|
|
layout.Stack{Alignment: layout.W}.Layout(gtx,
|
|
layout.Stacked(func() {
|
|
// Use a transparent equivalent of progress color.
|
|
backgroundColor := b.Color
|
|
backgroundColor.A = 100
|
|
|
|
shader(progressBarWidth, backgroundColor)
|
|
}),
|
|
layout.Stacked(func() {
|
|
fillWidth := (progressBarWidth / 100) * float32(progress)
|
|
shader(fillWidth, b.Color)
|
|
}),
|
|
)
|
|
}
|