~pierrec/giox

8683249eb14cacc13b299a4d3743bafde59dd266 — pierre 9 months ago ca3812d
widgetx: modified Hover with foreground color

Signed-off-by: pierre <pierre.curto@gmail.com>
M cmd/iconx/main.go => cmd/iconx/main.go +6 -2
@@ 157,11 157,15 @@ func (ui *uiMain) init() {
			Children: iconsTree.Children,
			Axis:     layout.Vertical,
		}
		ui.Hover = widgetx.Hover{Color: color.NRGBA(colornames.Lightgrey)}
		ui.Hover = widgetx.Hover{
			Background: color.NRGBA(colornames.Lightgrey),
			Foreground: ui.th.Palette.Fg,
		}
		ui.menu = widgetx.Icons{
			Hover: ui.Hover,
			Color: ui.th.Palette.Fg,
			Size:  ui.th.TextSize,
			Icons: widgetx.LoadIcons(ui.th.Palette.Fg, icons.NavigationUnfoldLess, icons.NavigationUnfoldMore),
			Icons: widgetx.LoadIcons(icons.NavigationUnfoldLess, icons.NavigationUnfoldMore),
			List:  layout.List{Axis: layout.Horizontal},
		}
		ui.num.Editor = &widget.Editor{

M widgetx/click.go => widgetx/click.go +16 -4
@@ 15,11 15,15 @@ import (
	"git.sr.ht/~pierrec/giox/layoutx"
)

// Hover fills its min area with Color when hovered.
// Hover fills its min area with Background when hovered.
//
// Typical use is with layout.Expanded.
type Hover struct {
	Color  color.NRGBA
	// Background is the filling color when hovered.
	Background color.NRGBA
	// Foreground is the icon color when hovered.
	Foreground color.NRGBA
	// Radius is the filling radius.
	Radius unit.Value
}



@@ 29,7 33,7 @@ func (h Hover) Layout(gtx layout.Context, hovered bool) layout.Dimensions {
		r := image.Rectangle{Max: gtx.Constraints.Min}
		radius := gtx.Metric.Px(h.Radius)
		clip.UniformRRect(layout.FRect(r), float32(radius)).Add(gtx.Ops)
		paint.Fill(gtx.Ops, h.Color)
		paint.Fill(gtx.Ops, h.Background)
	}
	return layout.Dimensions{Size: gtx.Constraints.Min}
}


@@ 73,6 77,8 @@ type ClickListWrap struct {
// ClickIcon provides a clickable icon with hover shadow.
type ClickIcon struct {
	Hover Hover
	// Color is the icon color when not hovered.
	Color color.NRGBA
	Size  unit.Value
	Icon  widget.Icon
	click *widget.Bool


@@ 95,11 101,17 @@ func (cl *ClickIcon) init() {

func (cl *ClickIcon) Layout(gtx layout.Context) layout.Dimensions {
	cl.init()
	hovered := cl.click.Hovered()
	return layout.Stack{}.Layout(gtx,
		layout.Expanded(func(gtx layout.Context) layout.Dimensions {
			return cl.Hover.Layout(gtx, cl.click.Hovered())
			return cl.Hover.Layout(gtx, hovered)
		}),
		layout.Stacked(func(gtx layout.Context) layout.Dimensions {
			if hovered {
				cl.Icon.Color = cl.Hover.Foreground
			} else {
				cl.Icon.Color = cl.Color
			}
			return cl.Icon.Layout(gtx, cl.Size)
		}),
		layout.Expanded(cl.click.Layout),

M widgetx/file.go => widgetx/file.go +15 -12
@@ 100,8 100,9 @@ type File struct {
	favs   fileStringPanel
	favSep Resizable
	// Recent directories.
	recs      fileStringPanel
	favrecSep Resizable
	recs        fileStringPanel
	favrecSep   Resizable
	favrecRatio float32
	// Active path.
	pBar  Icons
	pList ClickList


@@ 340,7 341,11 @@ func (p *filePanel) Init(sz unit.Value, stop widget.Icon) {
	p.search = fileInput{
		Icon: LoadIcon(icons.ActionSearch),
		Stop: ClickIcon{
			Hover: Hover{Color: colorx.Hover(p.Area.Palette.Bg)},
			Hover: Hover{
				Background: colorx.Hover(p.Area.Palette.Bg),
				Foreground: p.Area.Palette.Fg,
			},
			Color: p.Area.Palette.Fg,
			Size:  sz,
			Icon:  stop,
		},


@@ 358,8 363,8 @@ func (p *filePanel) Init(sz unit.Value, stop widget.Icon) {
			},
		},
		ListCue: ListCue{
			Color1: colorx.MulAlpha(p.Bar.Hover.Color, 16),
			Color2: colorx.MulAlpha(p.Bar.Hover.Color, 80),
			Color1: colorx.MulAlpha(p.Bar.Hover.Background, 16),
			Color2: colorx.MulAlpha(p.Bar.Hover.Background, 80),
			Width:  unit.Dp(30),
		},
	}


@@ 713,7 718,7 @@ func (cc *clickColumn) Layout(cols *clickColumns, gtx layout.Context, label func
	return layout.Stack{}.Layout(gtx,
		layout.Expanded(func(gtx layout.Context) layout.Dimensions {
			if cc.click.Hovered() {
				paint.FillShape(gtx.Ops, cols.Hover.Color, clip.Rect{Max: gtx.Constraints.Min}.Op())
				paint.FillShape(gtx.Ops, cols.Hover.Background, clip.Rect{Max: gtx.Constraints.Min}.Op())
			}
			return layout.Dimensions{Size: gtx.Constraints.Min}
		}),


@@ 998,6 1003,7 @@ func (f *File) init() {
		iconBar := func(icons ...widget.Icon) Icons {
			return Icons{
				Hover: f.Hover,
				Color: f.Palettes.Area.Fg,
				Size:  f.Text.Label.Size,
				Icons: icons,
				List: layout.List{


@@ 1029,7 1035,6 @@ func (f *File) init() {
		}
		f.favrecSep = Resizable{
			Axis: layout.Horizontal,
			Min:  0.2,
		}

		f.pBar = iconBar(f.Icons.FavoriteHistory, f.Icons.Refresh, f.Icons.Partitions)


@@ 1105,7 1110,6 @@ func (f *File) init() {
		f.sSep = Resizable{
			Axis:  layout.Vertical,
			Ratio: 0.8,
			Max:   0.9,
		}
		f.selected = fileStringPanel{
			Area: fileArea{


@@ 1316,12 1320,11 @@ func (f *File) layoutPaths(gtx layout.Context) layout.Dimensions {
				switch idx {
				case barFavRec:
					if f.pBar.Status(barFavRec) {
						// Restore the ratio from Min.
						f.favrecSep.Ratio = f.favrecSep.Min
						f.favrecSep.Min = 0
						f.favrecSep.Ratio = f.favrecRatio
						f.favrecRatio = 0
					} else {
						// Save the current ratio into Min.
						f.favrecSep.Min = f.favrecSep.Ratio
						f.favrecRatio = f.favrecSep.Ratio
						f.favrecSep.Ratio = 0
					}
				case barRefresh:

M widgetx/icons.go => widgetx/icons.go +19 -10
@@ 7,14 7,19 @@ import (
	"gioui.org/op"
	"gioui.org/unit"
	"gioui.org/widget"

	"git.sr.ht/~pierrec/giox/internal/bitmap"
)

// Icons represents a toolbar made of icons.
type Icons struct {
	Hover  Hover
	Hover Hover
	// Color is the icon color when not hovered.
	Color  color.NRGBA
	Size   unit.Value
	Icons  []widget.Icon
	status []widget.Bool
	hidden bitmap.Bitmap
	List   layout.List
}



@@ 40,12 45,18 @@ func (s *Icons) Layout(gtx layout.Context, onClick func(idx int)) layout.Dimensi
	}
	return s.List.Layout(gtx, len(s.Icons), func(gtx layout.Context, idx int) layout.Dimensions {
		ic := &s.Icons[idx]
		if ic.Color.A == 0 {
		if s.Hidden(idx) {
			return layout.Dimensions{}
		}
		hovered := s.status[idx].Hovered()
		if hovered {
			ic.Color = s.Hover.Foreground
		} else {
			ic.Color = s.Color
		}
		return layout.Stack{}.Layout(gtx,
			layout.Expanded(func(gtx layout.Context) layout.Dimensions {
				return s.Hover.Layout(gtx, s.status[idx].Hovered())
				return s.Hover.Layout(gtx, hovered)
			}),
			layout.Stacked(func(gtx layout.Context) layout.Dimensions {
				return ic.Layout(gtx, s.Size)


@@ 74,29 85,27 @@ func (s *Icons) Status(idx int) bool {
func (s *Icons) Hide(indexes ...int) {
	s.init()
	for _, idx := range indexes {
		s.Icons[idx].Color.A = 0
		s.hidden.Set(idx)
	}
}

func (s *Icons) Hidden(idx int) bool {
	s.init()
	return s.Icons[idx].Color.A == 0
	return s.hidden.Has(idx)
}

// Show restores the icon at indexes in the menu.
func (s *Icons) Show(indexes ...int) {
	s.init()
	for _, idx := range indexes {
		s.Icons[idx].Color.A = 255
		s.hidden.Unset(idx)
	}
}

// LoadIcons returns a slice of icons from their data, setting their color to c.
func LoadIcons(c color.NRGBA, data ...[]byte) []widget.Icon {
// LoadIcons returns a slice of icons from their data.
func LoadIcons(data ...[]byte) []widget.Icon {
	s := make([]widget.Icon, len(data))
	for i, d := range data {
		ic, _ := widget.NewIcon(d)
		ic.Color = c
		s[i] = *ic
	}
	return s

M widgetx/materialx/file.go => widgetx/materialx/file.go +2 -1
@@ 39,7 39,8 @@ func FileLayout(th *material.Theme) FileLayoutStyle {
				},
			},
			Hover: widgetx.Hover{
				Color: colorx.Hover(th.Palette.Bg),
				Background: colorx.Hover(th.Palette.Bg),
				Foreground: colorx.Hover(th.Palette.Fg),
			},
			Selection: widgetx.SelectFile,
		},

M widgetx/materialx/tree.go => widgetx/materialx/tree.go +17 -3
@@ 1,6 1,8 @@
package materialx

import (
	"image/color"

	"gioui.org/layout"
	"gioui.org/unit"
	"gioui.org/widget"


@@ 19,6 21,8 @@ type TreeLayoutStyle struct {
	Opened *widget.Icon
	// Hover defines the color to be used when hovering over an element.
	Hover widgetx.Hover
	// Color is the icon color when not hovered.
	Color color.NRGBA
	// Size is the size used for Closed and Opened icons.
	// If the size is zero, then non leaf items item are made clickable
	// to be able to open or close the branch.


@@ 33,9 37,14 @@ func TreeLayout(th *material.Theme, t *widgetx.Tree) TreeLayoutStyle {
	closed, _ := widget.NewIcon(icons.HardwareKeyboardArrowRight)
	opened, _ := widget.NewIcon(icons.HardwareKeyboardArrowDown)
	return TreeLayoutStyle{
		Closed:  closed,
		Opened:  opened,
		Hover:   widgetx.Hover{Color: colorx.MulAlpha(th.Palette.Bg, 64)},
		Closed: closed,
		Opened: opened,
		Hover: widgetx.Hover{
			Background: colorx.MulAlpha(th.Palette.Bg, 64),
			Foreground: th.Palette.Fg,
			Radius:     unit.Dp(6),
		},
		Color:   th.Palette.Fg,
		Size:    unit.Dp(20),
		Padding: unit.Dp(20),
		Tree:    t,


@@ 78,6 87,11 @@ func (t TreeLayoutStyle) Layout(gtx layout.Context, el layout.ListElement) layou
				default:
					return layout.Dimensions{}
				}
				if hovered {
					ic.Color = t.Hover.Foreground
				} else {
					ic.Color = t.Color
				}
				return layout.Stack{}.Layout(gtx,
					hovering,
					layout.Stacked(func(gtx layout.Context) layout.Dimensions {