mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
widget,widget/material: move Image and Icon to widget package
There is nothing theme-specific about displaying images and icons, so move the types from the material package to the generic widget package. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
// SPDX-License-Identifier: Unlicense OR MIT
|
||||
|
||||
package material
|
||||
package widget
|
||||
|
||||
import (
|
||||
"image"
|
||||
@@ -29,7 +29,7 @@ func NewIcon(data []byte) (*Icon, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Icon{src: data, Color: rgb(0x000000)}, nil
|
||||
return &Icon{src: data, Color: color.RGBA{A: 0xff}}, nil
|
||||
}
|
||||
|
||||
func (ic *Icon) Layout(gtx *layout.Context, sz unit.Value) {
|
||||
@@ -1,6 +1,6 @@
|
||||
// SPDX-License-Identifier: Unlicense OR MIT
|
||||
|
||||
package material
|
||||
package widget
|
||||
|
||||
import (
|
||||
"image"
|
||||
@@ -18,21 +18,19 @@ type Image struct {
|
||||
// Src is the image to display.
|
||||
Src paint.ImageOp
|
||||
// Scale is the ratio of image pixels to
|
||||
// dps.
|
||||
// dps. If Scale is zero Image falls back to
|
||||
// a scale that match a standard 72 DPI.
|
||||
Scale float32
|
||||
}
|
||||
|
||||
func (t *Theme) Image(img paint.ImageOp) Image {
|
||||
return Image{
|
||||
Src: img,
|
||||
Scale: 160 / 72, // About 72 DPI.
|
||||
}
|
||||
}
|
||||
|
||||
func (im Image) Layout(gtx *layout.Context) {
|
||||
scale := im.Scale
|
||||
if scale == 0 {
|
||||
scale = 160.0 / 72.0
|
||||
}
|
||||
size := im.Src.Rect.Size()
|
||||
wf, hf := float32(size.X), float32(size.Y)
|
||||
w, h := gtx.Px(unit.Dp(wf*im.Scale)), gtx.Px(unit.Dp(hf*im.Scale))
|
||||
w, h := gtx.Px(unit.Dp(wf*scale)), gtx.Px(unit.Dp(hf*scale))
|
||||
cs := gtx.Constraints
|
||||
d := image.Point{X: cs.Width.Constrain(w), Y: cs.Height.Constrain(h)}
|
||||
var s op.StackOp
|
||||
@@ -43,3 +41,7 @@ func (im Image) Layout(gtx *layout.Context) {
|
||||
s.Pop()
|
||||
gtx.Dimensions = layout.Dimensions{Size: d}
|
||||
}
|
||||
|
||||
func toPointF(p image.Point) f32.Point {
|
||||
return f32.Point{X: float32(p.X), Y: float32(p.Y)}
|
||||
}
|
||||
@@ -39,7 +39,7 @@ type ButtonLayout struct {
|
||||
type IconButton struct {
|
||||
Background color.RGBA
|
||||
Color color.RGBA
|
||||
Icon *Icon
|
||||
Icon *widget.Icon
|
||||
Size unit.Value
|
||||
Padding unit.Value
|
||||
Inset layout.Inset
|
||||
@@ -69,7 +69,7 @@ func (t *Theme) ButtonLayout() ButtonLayout {
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Theme) IconButton(icon *Icon) IconButton {
|
||||
func (t *Theme) IconButton(icon *widget.Icon) IconButton {
|
||||
return IconButton{
|
||||
Background: t.Color.Primary,
|
||||
Color: t.Color.InvText,
|
||||
|
||||
@@ -22,13 +22,12 @@ type checkable struct {
|
||||
IconColor color.RGBA
|
||||
Size unit.Value
|
||||
shaper text.Shaper
|
||||
checkedStateIcon *Icon
|
||||
uncheckedStateIcon *Icon
|
||||
checkedStateIcon *widget.Icon
|
||||
uncheckedStateIcon *widget.Icon
|
||||
}
|
||||
|
||||
func (c *checkable) layout(gtx *layout.Context, checked bool) {
|
||||
|
||||
var icon *Icon
|
||||
var icon *widget.Icon
|
||||
if checked {
|
||||
icon = c.checkedStateIcon
|
||||
} else {
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"gioui.org/op/paint"
|
||||
"gioui.org/text"
|
||||
"gioui.org/unit"
|
||||
"gioui.org/widget"
|
||||
"golang.org/x/exp/shiny/materialdesign/icons"
|
||||
)
|
||||
|
||||
@@ -24,10 +25,10 @@ type Theme struct {
|
||||
InvText color.RGBA
|
||||
}
|
||||
TextSize unit.Value
|
||||
checkBoxCheckedIcon *Icon
|
||||
checkBoxUncheckedIcon *Icon
|
||||
radioCheckedIcon *Icon
|
||||
radioUncheckedIcon *Icon
|
||||
checkBoxCheckedIcon *widget.Icon
|
||||
checkBoxUncheckedIcon *widget.Icon
|
||||
radioCheckedIcon *widget.Icon
|
||||
radioUncheckedIcon *widget.Icon
|
||||
}
|
||||
|
||||
func NewTheme() *Theme {
|
||||
@@ -40,15 +41,15 @@ func NewTheme() *Theme {
|
||||
t.Color.InvText = rgb(0xffffff)
|
||||
t.TextSize = unit.Sp(16)
|
||||
|
||||
t.checkBoxCheckedIcon = mustIcon(NewIcon(icons.ToggleCheckBox))
|
||||
t.checkBoxUncheckedIcon = mustIcon(NewIcon(icons.ToggleCheckBoxOutlineBlank))
|
||||
t.radioCheckedIcon = mustIcon(NewIcon(icons.ToggleRadioButtonChecked))
|
||||
t.radioUncheckedIcon = mustIcon(NewIcon(icons.ToggleRadioButtonUnchecked))
|
||||
t.checkBoxCheckedIcon = mustIcon(widget.NewIcon(icons.ToggleCheckBox))
|
||||
t.checkBoxUncheckedIcon = mustIcon(widget.NewIcon(icons.ToggleCheckBoxOutlineBlank))
|
||||
t.radioCheckedIcon = mustIcon(widget.NewIcon(icons.ToggleRadioButtonChecked))
|
||||
t.radioUncheckedIcon = mustIcon(widget.NewIcon(icons.ToggleRadioButtonUnchecked))
|
||||
|
||||
return t
|
||||
}
|
||||
|
||||
func mustIcon(ic *Icon, err error) *Icon {
|
||||
func mustIcon(ic *widget.Icon, err error) *widget.Icon {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user