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>
80 lines
1.8 KiB
Go
80 lines
1.8 KiB
Go
// SPDX-License-Identifier: Unlicense OR MIT
|
|
|
|
package material
|
|
|
|
import (
|
|
"image/color"
|
|
|
|
"gioui.org/layout"
|
|
"gioui.org/op/paint"
|
|
"gioui.org/text"
|
|
"gioui.org/unit"
|
|
"gioui.org/widget"
|
|
)
|
|
|
|
type LabelStyle struct {
|
|
// Face defines the text style.
|
|
Font text.Font
|
|
// Color is the text color.
|
|
Color color.RGBA
|
|
// Alignment specify the text alignment.
|
|
Alignment text.Alignment
|
|
// MaxLines limits the number of lines. Zero means no limit.
|
|
MaxLines int
|
|
Text string
|
|
TextSize unit.Value
|
|
|
|
shaper text.Shaper
|
|
}
|
|
|
|
func H1(th *Theme, txt string) LabelStyle {
|
|
return Label(th, th.TextSize.Scale(96.0/16.0), txt)
|
|
}
|
|
|
|
func H2(th *Theme, txt string) LabelStyle {
|
|
return Label(th, th.TextSize.Scale(60.0/16.0), txt)
|
|
}
|
|
|
|
func H3(th *Theme, txt string) LabelStyle {
|
|
return Label(th, th.TextSize.Scale(48.0/16.0), txt)
|
|
}
|
|
|
|
func H4(th *Theme, txt string) LabelStyle {
|
|
return Label(th, th.TextSize.Scale(34.0/16.0), txt)
|
|
}
|
|
|
|
func H5(th *Theme, txt string) LabelStyle {
|
|
return Label(th, th.TextSize.Scale(24.0/16.0), txt)
|
|
}
|
|
|
|
func H6(th *Theme, txt string) LabelStyle {
|
|
return Label(th, th.TextSize.Scale(20.0/16.0), txt)
|
|
}
|
|
|
|
func Body1(th *Theme, txt string) LabelStyle {
|
|
return Label(th, th.TextSize, txt)
|
|
}
|
|
|
|
func Body2(th *Theme, txt string) LabelStyle {
|
|
return Label(th, th.TextSize.Scale(14.0/16.0), txt)
|
|
}
|
|
|
|
func Caption(th *Theme, txt string) LabelStyle {
|
|
return Label(th, th.TextSize.Scale(12.0/16.0), txt)
|
|
}
|
|
|
|
func Label(th *Theme, size unit.Value, txt string) LabelStyle {
|
|
return LabelStyle{
|
|
Text: txt,
|
|
Color: th.Color.Text,
|
|
TextSize: size,
|
|
shaper: th.Shaper,
|
|
}
|
|
}
|
|
|
|
func (l LabelStyle) Layout(gtx *layout.Context) {
|
|
paint.ColorOp{Color: l.Color}.Add(gtx.Ops)
|
|
tl := widget.Label{Alignment: l.Alignment, MaxLines: l.MaxLines}
|
|
tl.Layout(gtx, l.shaper, l.Font, l.TextSize, l.Text)
|
|
}
|