mirror of
https://github.com/vsariola/sointu.git
synced 2025-07-19 13:34:34 -04:00
refactor(tracker/gioui): rewrote Editor to link to String.Value()
This commit is contained in:
parent
840fe3ef0e
commit
b291959a97
@ -6,9 +6,11 @@ import (
|
||||
"gioui.org/font"
|
||||
"gioui.org/io/event"
|
||||
"gioui.org/io/key"
|
||||
"gioui.org/text"
|
||||
"gioui.org/unit"
|
||||
"gioui.org/widget"
|
||||
"gioui.org/widget/material"
|
||||
"github.com/vsariola/sointu/tracker"
|
||||
)
|
||||
|
||||
type (
|
||||
@ -17,7 +19,7 @@ type (
|
||||
// application while editing (particularly: to prevent triggering notes
|
||||
// while editing).
|
||||
Editor struct {
|
||||
Editor widget.Editor
|
||||
widgetEditor widget.Editor
|
||||
filters []event.Filter
|
||||
requestFocus bool
|
||||
}
|
||||
@ -28,20 +30,23 @@ type (
|
||||
Font font.Font
|
||||
TextSize unit.Sp
|
||||
}
|
||||
|
||||
EditorSubmitEvent struct{}
|
||||
EditorCancelEvent struct{}
|
||||
|
||||
EditorEvent interface{ isEditorEvent() }
|
||||
)
|
||||
|
||||
func NewEditor(e widget.Editor) *Editor {
|
||||
ret := &Editor{
|
||||
Editor: e,
|
||||
}
|
||||
func NewEditor(singleLine, submit bool, alignment text.Alignment) *Editor {
|
||||
ret := &Editor{widgetEditor: widget.Editor{SingleLine: singleLine, Submit: submit, Alignment: alignment}}
|
||||
for c := 'A'; c <= 'Z'; c++ {
|
||||
ret.filters = append(ret.filters, key.Filter{Name: key.Name(c), Focus: &ret.Editor})
|
||||
ret.filters = append(ret.filters, key.Filter{Name: key.Name(c), Focus: &ret.widgetEditor, Optional: key.ModAlt | key.ModShift | key.ModShortcut})
|
||||
}
|
||||
for c := '0'; c <= '9'; c++ {
|
||||
ret.filters = append(ret.filters, key.Filter{Name: key.Name(c), Focus: &ret.Editor})
|
||||
ret.filters = append(ret.filters, key.Filter{Name: key.Name(c), Focus: &ret.widgetEditor, Optional: key.ModAlt | key.ModShift | key.ModShortcut})
|
||||
}
|
||||
ret.filters = append(ret.filters, key.Filter{Name: key.NameSpace, Focus: &ret.Editor})
|
||||
ret.filters = append(ret.filters, key.Filter{Name: key.NameEscape, Focus: &ret.Editor})
|
||||
ret.filters = append(ret.filters, key.Filter{Name: key.NameSpace, Focus: &ret.widgetEditor, Optional: key.ModAlt | key.ModShift | key.ModShortcut})
|
||||
ret.filters = append(ret.filters, key.Filter{Name: key.NameEscape, Focus: &ret.widgetEditor, Optional: key.ModAlt | key.ModShift | key.ModShortcut})
|
||||
return ret
|
||||
}
|
||||
|
||||
@ -53,52 +58,57 @@ func (s *EditorStyle) AsLabelStyle() LabelStyle {
|
||||
}
|
||||
}
|
||||
|
||||
func MaterialEditor(th *Theme, style *EditorStyle, editor *Editor, hint string) material.EditorStyle {
|
||||
ret := material.Editor(&th.Material, &editor.Editor, hint)
|
||||
ret.Font = style.Font
|
||||
ret.TextSize = style.TextSize
|
||||
ret.Color = style.Color
|
||||
ret.HintColor = style.HintColor
|
||||
return ret
|
||||
}
|
||||
|
||||
func (e *Editor) SetText(s string) {
|
||||
if e.Editor.Text() != s {
|
||||
e.Editor.SetText(s)
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Editor) Text() string {
|
||||
return e.Editor.Text()
|
||||
}
|
||||
|
||||
func (e *Editor) Submitted(gtx C) bool {
|
||||
func (e *Editor) Layout(gtx C, str tracker.String, th *Theme, style *EditorStyle, hint string) D {
|
||||
for {
|
||||
ev, ok := e.Editor.Update(gtx)
|
||||
if _, ok := e.Update(gtx, str); !ok {
|
||||
break
|
||||
}
|
||||
}
|
||||
if e.widgetEditor.Text() != str.Value() {
|
||||
e.widgetEditor.SetText(str.Value())
|
||||
}
|
||||
me := material.Editor(&th.Material, &e.widgetEditor, hint)
|
||||
me.Font = style.Font
|
||||
me.TextSize = style.TextSize
|
||||
me.Color = style.Color
|
||||
me.HintColor = style.HintColor
|
||||
return me.Layout(gtx)
|
||||
}
|
||||
|
||||
func (e *Editor) Update(gtx C, str tracker.String) (ev EditorEvent, ok bool) {
|
||||
if e.requestFocus {
|
||||
e.requestFocus = false
|
||||
gtx.Execute(key.FocusCmd{Tag: &e.widgetEditor})
|
||||
l := len(e.widgetEditor.Text())
|
||||
e.widgetEditor.SetCaret(l, l)
|
||||
}
|
||||
for {
|
||||
ev, ok := e.widgetEditor.Update(gtx)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
_, ok = ev.(widget.SubmitEvent)
|
||||
if ok {
|
||||
return true
|
||||
if _, ok := ev.(widget.ChangeEvent); ok {
|
||||
str.SetValue(e.widgetEditor.Text())
|
||||
}
|
||||
if _, ok := ev.(widget.SubmitEvent); ok {
|
||||
return EditorSubmitEvent{}, true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (e *Editor) Cancelled(gtx C) bool {
|
||||
for {
|
||||
event, ok := gtx.Event(e.filters...)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
if e, ok := event.(key.Event); ok && e.State == key.Press && e.Name == key.NameEscape {
|
||||
return true
|
||||
return EditorCancelEvent{}, true
|
||||
}
|
||||
}
|
||||
return false
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (e *Editor) Focus() {
|
||||
e.requestFocus = true
|
||||
}
|
||||
|
||||
func (s EditorSubmitEvent) isEditorEvent() {}
|
||||
func (s EditorCancelEvent) isEditorEvent() {}
|
||||
|
@ -16,7 +16,6 @@ import (
|
||||
"gioui.org/op/clip"
|
||||
"gioui.org/text"
|
||||
"gioui.org/unit"
|
||||
"gioui.org/widget"
|
||||
"github.com/vsariola/sointu"
|
||||
"github.com/vsariola/sointu/tracker"
|
||||
"golang.org/x/exp/shiny/materialdesign/icons"
|
||||
@ -81,9 +80,9 @@ func NewInstrumentEditor(model *tracker.Model) *InstrumentEditor {
|
||||
presetMenuBtn: new(TipClickable),
|
||||
soloBtn: NewBoolClickable(model.Solo()),
|
||||
muteBtn: NewBoolClickable(model.Mute()),
|
||||
commentEditor: NewEditor(widget.Editor{}),
|
||||
nameEditor: NewEditor(widget.Editor{SingleLine: true, Submit: true, Alignment: text.Middle}),
|
||||
searchEditor: NewEditor(widget.Editor{SingleLine: true, Submit: true, Alignment: text.Start}),
|
||||
commentEditor: NewEditor(false, false, text.Start),
|
||||
nameEditor: NewEditor(true, true, text.Middle),
|
||||
searchEditor: NewEditor(true, true, text.Start),
|
||||
commentString: model.InstrumentComment(),
|
||||
nameString: model.InstrumentName(),
|
||||
instrumentDragList: NewDragList(model.Instruments().List(), layout.Horizontal),
|
||||
@ -265,13 +264,16 @@ func (ie *InstrumentEditor) layoutInstrumentHeader(gtx C, t *Tracker) D {
|
||||
layout.Rigid(header),
|
||||
layout.Rigid(func(gtx C) D {
|
||||
defer clip.Rect(image.Rect(0, 0, gtx.Constraints.Max.X, gtx.Constraints.Max.Y)).Push(gtx.Ops).Pop()
|
||||
ie.commentEditor.SetText(ie.commentString.Value())
|
||||
for ie.commentEditor.Submitted(gtx) || ie.commentEditor.Cancelled(gtx) {
|
||||
for {
|
||||
_, ok := ie.commentEditor.Update(gtx, ie.commentString)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
ie.instrumentDragList.Focus()
|
||||
}
|
||||
style := MaterialEditor(t.Theme, &t.Theme.InstrumentEditor.InstrumentComment, ie.commentEditor, "Comment")
|
||||
ret := layout.UniformInset(unit.Dp(6)).Layout(gtx, style.Layout)
|
||||
ie.commentString.SetValue(ie.commentEditor.Text())
|
||||
ret := layout.UniformInset(unit.Dp(6)).Layout(gtx, func(gtx C) D {
|
||||
return ie.commentEditor.Layout(gtx, ie.commentString, t.Theme, &t.Theme.InstrumentEditor.InstrumentComment, "Comment")
|
||||
})
|
||||
return ret
|
||||
}),
|
||||
)
|
||||
@ -301,17 +303,17 @@ func (ie *InstrumentEditor) layoutInstrumentList(gtx C, t *Tracker) D {
|
||||
s.Color = color.NRGBA{R: 255, G: k, B: 255, A: 255}
|
||||
}
|
||||
if i == ie.instrumentDragList.TrackerList.Selected() {
|
||||
ie.nameEditor.SetText(name)
|
||||
for ie.nameEditor.Submitted(gtx) || ie.nameEditor.Cancelled(gtx) {
|
||||
for {
|
||||
_, ok := ie.nameEditor.Update(gtx, ie.nameString)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
ie.instrumentDragList.Focus()
|
||||
}
|
||||
style := MaterialEditor(t.Theme, &s, ie.nameEditor, "Instr")
|
||||
dims := layout.Center.Layout(gtx, func(gtx C) D {
|
||||
return layout.Center.Layout(gtx, func(gtx C) D {
|
||||
defer clip.Rect(image.Rect(0, 0, gtx.Constraints.Max.X, gtx.Constraints.Max.Y)).Push(gtx.Ops).Pop()
|
||||
return style.Layout(gtx)
|
||||
return ie.nameEditor.Layout(gtx, ie.nameString, t.Theme, &s, "Instr")
|
||||
})
|
||||
ie.nameString.SetValue(ie.nameEditor.Text())
|
||||
return dims
|
||||
}
|
||||
if name == "" {
|
||||
name = "Instr"
|
||||
@ -351,9 +353,7 @@ func (ie *InstrumentEditor) layoutInstrumentList(gtx C, t *Tracker) D {
|
||||
case key.NameDownArrow:
|
||||
ie.unitDragList.Focus()
|
||||
case key.NameReturn, key.NameEnter:
|
||||
gtx.Execute(key.FocusCmd{Tag: &ie.nameEditor.Editor})
|
||||
l := len(ie.nameEditor.Editor.Text())
|
||||
ie.nameEditor.Editor.SetCaret(l, l)
|
||||
ie.nameEditor.Focus()
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -381,12 +381,6 @@ func (ie *InstrumentEditor) layoutUnitList(gtx C, t *Tracker) D {
|
||||
}
|
||||
count := min(ie.unitDragList.TrackerList.Count(), 256)
|
||||
|
||||
if ie.searchEditor.requestFocus {
|
||||
// for now, only the searchEditor has its requestFocus flag
|
||||
ie.searchEditor.requestFocus = false
|
||||
gtx.Execute(key.FocusCmd{Tag: &ie.searchEditor.Editor})
|
||||
}
|
||||
|
||||
element := func(gtx C, i int) D {
|
||||
gtx.Constraints.Max.Y = gtx.Dp(20)
|
||||
gtx.Constraints.Min.Y = gtx.Constraints.Max.Y
|
||||
@ -417,29 +411,25 @@ func (ie *InstrumentEditor) layoutUnitList(gtx C, t *Tracker) D {
|
||||
if i == ie.unitDragList.TrackerList.Selected() {
|
||||
defer clip.Rect(image.Rect(0, 0, gtx.Constraints.Max.X, gtx.Constraints.Max.Y)).Push(gtx.Ops).Pop()
|
||||
str := t.Model.UnitSearch()
|
||||
ie.searchEditor.SetText(str.Value())
|
||||
for ie.searchEditor.Submitted(gtx) {
|
||||
ie.unitDragList.Focus()
|
||||
if text := ie.searchEditor.Text(); text != "" {
|
||||
for _, n := range sointu.UnitNames {
|
||||
if strings.HasPrefix(n, ie.searchEditor.Text()) {
|
||||
t.Units().SetSelectedType(n)
|
||||
break
|
||||
for {
|
||||
ev, ok := ie.searchEditor.Update(gtx, str)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
if _, ok := ev.(EditorSubmitEvent); ok {
|
||||
if str.Value() != "" {
|
||||
for _, n := range sointu.UnitNames {
|
||||
if strings.HasPrefix(n, str.Value()) {
|
||||
t.Units().SetSelectedType(n)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
t.UnitSearching().SetValue(false)
|
||||
ie.searchEditor.SetText(str.Value())
|
||||
}
|
||||
for ie.searchEditor.Cancelled(gtx) {
|
||||
t.UnitSearching().SetValue(false)
|
||||
ie.searchEditor.SetText(str.Value())
|
||||
ie.unitDragList.Focus()
|
||||
t.UnitSearching().SetValue(false)
|
||||
}
|
||||
style := MaterialEditor(t.Theme, &editorStyle, ie.searchEditor, "---")
|
||||
ret := style.Layout(gtx)
|
||||
str.SetValue(ie.searchEditor.Text())
|
||||
return ret
|
||||
return ie.searchEditor.Layout(gtx, str, t.Theme, &editorStyle, "---")
|
||||
} else {
|
||||
text := u.Type
|
||||
if text == "" {
|
||||
@ -485,11 +475,11 @@ func (ie *InstrumentEditor) layoutUnitList(gtx C, t *Tracker) D {
|
||||
case key.NameDeleteBackward:
|
||||
t.Units().SetSelectedType("")
|
||||
t.UnitSearching().SetValue(true)
|
||||
gtx.Execute(key.FocusCmd{Tag: &ie.searchEditor.Editor})
|
||||
ie.searchEditor.Focus()
|
||||
case key.NameEnter, key.NameReturn:
|
||||
t.Model.AddUnit(e.Modifiers.Contain(key.ModCtrl)).Do()
|
||||
t.UnitSearching().SetValue(true)
|
||||
gtx.Execute(key.FocusCmd{Tag: &ie.searchEditor.Editor})
|
||||
ie.searchEditor.Focus()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ import (
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op"
|
||||
"gioui.org/op/clip"
|
||||
"gioui.org/text"
|
||||
"gioui.org/unit"
|
||||
"gioui.org/widget"
|
||||
"gioui.org/widget/material"
|
||||
@ -49,7 +50,7 @@ func NewUnitEditor(m *tracker.Model) *UnitEditor {
|
||||
DisableUnitBtn: NewBoolClickable(m.UnitDisabled()),
|
||||
CopyUnitBtn: new(TipClickable),
|
||||
SelectTypeBtn: new(Clickable),
|
||||
commentEditor: NewEditor(widget.Editor{SingleLine: true, Submit: true}),
|
||||
commentEditor: NewEditor(true, true, text.Start),
|
||||
sliderList: NewDragList(m.Params().List(), layout.Vertical),
|
||||
searchList: NewDragList(m.SearchResults().List(), layout.Vertical),
|
||||
}
|
||||
@ -159,15 +160,14 @@ func (pe *UnitEditor) layoutFooter(gtx C, t *Tracker) D {
|
||||
return hintText.Layout(gtx)
|
||||
}),
|
||||
layout.Flexed(1, func(gtx C) D {
|
||||
s := t.UnitComment()
|
||||
pe.commentEditor.SetText(s.Value())
|
||||
for pe.commentEditor.Submitted(gtx) || pe.commentEditor.Cancelled(gtx) {
|
||||
for {
|
||||
_, ok := pe.commentEditor.Update(gtx, t.UnitComment())
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
t.InstrumentEditor.Focus()
|
||||
}
|
||||
commentStyle := MaterialEditor(t.Theme, &t.Theme.InstrumentEditor.UnitComment, pe.commentEditor, "---")
|
||||
ret := commentStyle.Layout(gtx)
|
||||
s.SetValue(pe.commentEditor.Text())
|
||||
return ret
|
||||
return pe.commentEditor.Layout(gtx, t.UnitComment(), t.Theme, &t.Theme.InstrumentEditor.UnitComment, "---")
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
Reference in New Issue
Block a user