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