fix: copying and pasting units messed selection and Ctrl-C actually cut

This commit is contained in:
5684185+vsariola@users.noreply.github.com 2023-08-27 15:34:03 +03:00
parent 568aa1d76d
commit c7d79035ce
2 changed files with 37 additions and 7 deletions

View File

@ -477,18 +477,27 @@ func (ie *InstrumentEditor) layoutInstrumentEditor(gtx C, t *Tracker) D {
ie.unitTypeEditor.SetCaret(l, l) ie.unitTypeEditor.SetCaret(l, l)
case key.NameDeleteForward: case key.NameDeleteForward:
t.DeleteUnits(true, ie.unitDragList.SelectedItem, ie.unitDragList.SelectedItem2) t.DeleteUnits(true, ie.unitDragList.SelectedItem, ie.unitDragList.SelectedItem2)
case "C", "X": ie.unitDragList.SelectedItem2 = t.UnitIndex()
case "X":
units := t.DeleteUnits(true, ie.unitDragList.SelectedItem, ie.unitDragList.SelectedItem2) units := t.DeleteUnits(true, ie.unitDragList.SelectedItem, ie.unitDragList.SelectedItem2)
ie.unitDragList.SelectedItem2 = t.UnitIndex()
contents, err := yaml.Marshal(units) contents, err := yaml.Marshal(units)
if err == nil { if err == nil {
clipboard.WriteOp{Text: string(contents)}.Add(gtx.Ops) clipboard.WriteOp{Text: string(contents)}.Add(gtx.Ops)
alertText := "Unit(s) copied to clipboard" t.Alert.Update("Unit(s) cut to clipboard", Notify, time.Second*3)
if e.Name == "X" { }
alertText = "Unit(s) cut to clipboard" case "C":
} a := clamp(ie.unitDragList.SelectedItem, 0, len(t.Instrument().Units)-1)
t.Alert.Update(alertText, Notify, time.Second*3) b := clamp(ie.unitDragList.SelectedItem2, 0, len(t.Instrument().Units)-1)
if a > b {
a, b = b, a
}
units := t.Instrument().Units[a : b+1]
contents, err := yaml.Marshal(units)
if err == nil {
clipboard.WriteOp{Text: string(contents)}.Add(gtx.Ops)
t.Alert.Update("Unit(s) copied to clipboard", Notify, time.Second*3)
} }
ie.unitDragList.SelectedItem2 = t.UnitIndex()
case key.NameReturn: case key.NameReturn:
if e.Modifiers.Contain(key.ModShortcut) { if e.Modifiers.Contain(key.ModShortcut) {
t.AddUnit(true) t.AddUnit(true)
@ -521,3 +530,13 @@ func (ie *InstrumentEditor) layoutInstrumentEditor(gtx C, t *Tracker) D {
layout.Rigid(ie.paramEditor.Bind(t))) layout.Rigid(ie.paramEditor.Bind(t)))
}) })
} }
func clamp(i, min, max int) int {
if i < min {
return min
}
if i > max {
return max
}
return i
}

View File

@ -68,11 +68,22 @@ type Tracker struct {
func (t *Tracker) UnmarshalContent(bytes []byte) error { func (t *Tracker) UnmarshalContent(bytes []byte) error {
var units []sointu.Unit var units []sointu.Unit
if errJSON := json.Unmarshal(bytes, &units); errJSON == nil { if errJSON := json.Unmarshal(bytes, &units); errJSON == nil {
if len(units) == 0 {
return nil
}
t.PasteUnits(units) t.PasteUnits(units)
// TODO: this is a bit hacky, but works for now. How to change the selection to the pasted units more elegantly?
t.InstrumentEditor.unitDragList.SelectedItem = t.UnitIndex()
t.InstrumentEditor.unitDragList.SelectedItem2 = t.UnitIndex() + len(units) - 1
return nil return nil
} }
if errYaml := yaml.Unmarshal(bytes, &units); errYaml == nil { if errYaml := yaml.Unmarshal(bytes, &units); errYaml == nil {
if len(units) == 0 {
return nil
}
t.PasteUnits(units) t.PasteUnits(units)
t.InstrumentEditor.unitDragList.SelectedItem = t.UnitIndex()
t.InstrumentEditor.unitDragList.SelectedItem2 = t.UnitIndex() + len(units) - 1
return nil return nil
} }
var instr sointu.Instrument var instr sointu.Instrument