~pierrec/giox

8b5b6cd9a4f1a665581b251b751ffc22205812fa — pierre 8 months ago f2af8f8
widgetx: replaced Modal ReleaseKeys with Keys as []string

Signed-off-by: pierre <pierre.curto@gmail.com>
2 files changed, 17 insertions(+), 6 deletions(-)

M widgetx/materialx/modal.go
M widgetx/modal.go
M widgetx/materialx/modal.go => widgetx/materialx/modal.go +2 -0
@@ 5,6 5,7 @@ import (
	"image/color"

	"gioui.org/f32"
	"gioui.org/io/key"
	"gioui.org/io/pointer"
	"gioui.org/layout"
	"gioui.org/op"


@@ 45,6 46,7 @@ func ModalConfirm(th *material.Theme) ModalAlertStyle {
	return ModalAlertStyle{
		Modal: widgetx.Modal{
			Background: colorx.MulAlpha(th.Palette.Bg, 32),
			Keys:       []string{key.NameEscape},
		},
		Background: th.Palette.Bg,
		Border: widget.Border{

M widgetx/modal.go => widgetx/modal.go +15 -6
@@ 17,22 17,31 @@ import (
// keyboard and mouse events as specified by the Release fields.
type Modal struct {
	Background   color.NRGBA
	ReleaseKeys  bool
	Keys         []string
	ReleaseMouse bool
	click        widget.Clickable
	changed      bool
}

func (m *Modal) update(gtx layout.Context) {
	defer func() {
		if m.changed {
			// Force a refresh as Modal is a defer op.
			op.InvalidateOp{}.Add(gtx.Ops)
		}
	}()
	if m.click.Clicked() {
		m.changed = true
		op.InvalidateOp{}.Add(gtx.Ops)
		return
	}
	for _, ev := range gtx.Events(m) {
		if e, ok := ev.(key.Event); ok && e.Name == key.NameEscape {
			m.changed = true
			break
		if e, ok := ev.(key.Event); ok {
			for _, k := range m.Keys {
				if e.Name == k {
					m.changed = true
					return
				}
			}
		}
	}
}


@@ 42,7 51,7 @@ func (m *Modal) Layout(gtx layout.Context) layout.Dimensions {
	m.update(gtx)
	macro := op.Record(gtx.Ops)
	size := gtx.Constraints.Min
	if !m.ReleaseKeys {
	if len(m.Keys) > 0 {
		key.InputOp{Tag: m}.Add(gtx.Ops)
		key.FocusOp{Tag: m}.Add(gtx.Ops)
	}