From e460e4f4bfa052e4d53a8cc7d4d1f8ef58e6a176 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Sun, 3 May 2020 12:22:32 +0200 Subject: [PATCH] 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 --- widget/{material => }/icon.go | 4 ++-- widget/{material => }/image.go | 22 ++++++++++++---------- widget/material/button.go | 4 ++-- widget/material/checkable.go | 7 +++---- widget/material/theme.go | 19 ++++++++++--------- 5 files changed, 29 insertions(+), 27 deletions(-) rename widget/{material => }/icon.go (94%) rename widget/{material => }/image.go (73%) diff --git a/widget/material/icon.go b/widget/icon.go similarity index 94% rename from widget/material/icon.go rename to widget/icon.go index bff84384..e8483c52 100644 --- a/widget/material/icon.go +++ b/widget/icon.go @@ -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) { diff --git a/widget/material/image.go b/widget/image.go similarity index 73% rename from widget/material/image.go rename to widget/image.go index 4f442512..b8392476 100644 --- a/widget/material/image.go +++ b/widget/image.go @@ -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)} +} diff --git a/widget/material/button.go b/widget/material/button.go index d3ee9207..c78d7204 100644 --- a/widget/material/button.go +++ b/widget/material/button.go @@ -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, diff --git a/widget/material/checkable.go b/widget/material/checkable.go index 2ee7c7b8..dc1199a8 100644 --- a/widget/material/checkable.go +++ b/widget/material/checkable.go @@ -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 { diff --git a/widget/material/theme.go b/widget/material/theme.go index 46e1821c..581ea1af 100644 --- a/widget/material/theme.go +++ b/widget/material/theme.go @@ -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) }