mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-05 01:15:35 +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
|
// SPDX-License-Identifier: Unlicense OR MIT
|
||||||
|
|
||||||
package material
|
package widget
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"image"
|
"image"
|
||||||
@@ -29,7 +29,7 @@ func NewIcon(data []byte) (*Icon, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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) {
|
func (ic *Icon) Layout(gtx *layout.Context, sz unit.Value) {
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
// SPDX-License-Identifier: Unlicense OR MIT
|
// SPDX-License-Identifier: Unlicense OR MIT
|
||||||
|
|
||||||
package material
|
package widget
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"image"
|
"image"
|
||||||
@@ -18,21 +18,19 @@ type Image struct {
|
|||||||
// Src is the image to display.
|
// Src is the image to display.
|
||||||
Src paint.ImageOp
|
Src paint.ImageOp
|
||||||
// Scale is the ratio of image pixels to
|
// 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
|
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) {
|
func (im Image) Layout(gtx *layout.Context) {
|
||||||
|
scale := im.Scale
|
||||||
|
if scale == 0 {
|
||||||
|
scale = 160.0 / 72.0
|
||||||
|
}
|
||||||
size := im.Src.Rect.Size()
|
size := im.Src.Rect.Size()
|
||||||
wf, hf := float32(size.X), float32(size.Y)
|
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
|
cs := gtx.Constraints
|
||||||
d := image.Point{X: cs.Width.Constrain(w), Y: cs.Height.Constrain(h)}
|
d := image.Point{X: cs.Width.Constrain(w), Y: cs.Height.Constrain(h)}
|
||||||
var s op.StackOp
|
var s op.StackOp
|
||||||
@@ -43,3 +41,7 @@ func (im Image) Layout(gtx *layout.Context) {
|
|||||||
s.Pop()
|
s.Pop()
|
||||||
gtx.Dimensions = layout.Dimensions{Size: d}
|
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 {
|
type IconButton struct {
|
||||||
Background color.RGBA
|
Background color.RGBA
|
||||||
Color color.RGBA
|
Color color.RGBA
|
||||||
Icon *Icon
|
Icon *widget.Icon
|
||||||
Size unit.Value
|
Size unit.Value
|
||||||
Padding unit.Value
|
Padding unit.Value
|
||||||
Inset layout.Inset
|
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{
|
return IconButton{
|
||||||
Background: t.Color.Primary,
|
Background: t.Color.Primary,
|
||||||
Color: t.Color.InvText,
|
Color: t.Color.InvText,
|
||||||
|
|||||||
@@ -22,13 +22,12 @@ type checkable struct {
|
|||||||
IconColor color.RGBA
|
IconColor color.RGBA
|
||||||
Size unit.Value
|
Size unit.Value
|
||||||
shaper text.Shaper
|
shaper text.Shaper
|
||||||
checkedStateIcon *Icon
|
checkedStateIcon *widget.Icon
|
||||||
uncheckedStateIcon *Icon
|
uncheckedStateIcon *widget.Icon
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *checkable) layout(gtx *layout.Context, checked bool) {
|
func (c *checkable) layout(gtx *layout.Context, checked bool) {
|
||||||
|
var icon *widget.Icon
|
||||||
var icon *Icon
|
|
||||||
if checked {
|
if checked {
|
||||||
icon = c.checkedStateIcon
|
icon = c.checkedStateIcon
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"gioui.org/op/paint"
|
"gioui.org/op/paint"
|
||||||
"gioui.org/text"
|
"gioui.org/text"
|
||||||
"gioui.org/unit"
|
"gioui.org/unit"
|
||||||
|
"gioui.org/widget"
|
||||||
"golang.org/x/exp/shiny/materialdesign/icons"
|
"golang.org/x/exp/shiny/materialdesign/icons"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -24,10 +25,10 @@ type Theme struct {
|
|||||||
InvText color.RGBA
|
InvText color.RGBA
|
||||||
}
|
}
|
||||||
TextSize unit.Value
|
TextSize unit.Value
|
||||||
checkBoxCheckedIcon *Icon
|
checkBoxCheckedIcon *widget.Icon
|
||||||
checkBoxUncheckedIcon *Icon
|
checkBoxUncheckedIcon *widget.Icon
|
||||||
radioCheckedIcon *Icon
|
radioCheckedIcon *widget.Icon
|
||||||
radioUncheckedIcon *Icon
|
radioUncheckedIcon *widget.Icon
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTheme() *Theme {
|
func NewTheme() *Theme {
|
||||||
@@ -40,15 +41,15 @@ func NewTheme() *Theme {
|
|||||||
t.Color.InvText = rgb(0xffffff)
|
t.Color.InvText = rgb(0xffffff)
|
||||||
t.TextSize = unit.Sp(16)
|
t.TextSize = unit.Sp(16)
|
||||||
|
|
||||||
t.checkBoxCheckedIcon = mustIcon(NewIcon(icons.ToggleCheckBox))
|
t.checkBoxCheckedIcon = mustIcon(widget.NewIcon(icons.ToggleCheckBox))
|
||||||
t.checkBoxUncheckedIcon = mustIcon(NewIcon(icons.ToggleCheckBoxOutlineBlank))
|
t.checkBoxUncheckedIcon = mustIcon(widget.NewIcon(icons.ToggleCheckBoxOutlineBlank))
|
||||||
t.radioCheckedIcon = mustIcon(NewIcon(icons.ToggleRadioButtonChecked))
|
t.radioCheckedIcon = mustIcon(widget.NewIcon(icons.ToggleRadioButtonChecked))
|
||||||
t.radioUncheckedIcon = mustIcon(NewIcon(icons.ToggleRadioButtonUnchecked))
|
t.radioUncheckedIcon = mustIcon(widget.NewIcon(icons.ToggleRadioButtonUnchecked))
|
||||||
|
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
func mustIcon(ic *Icon, err error) *Icon {
|
func mustIcon(ic *widget.Icon, err error) *widget.Icon {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user