diff --git a/tracker/gioui/instrumenteditor.go b/tracker/gioui/instrumenteditor.go index ec9f258..a2861dc 100644 --- a/tracker/gioui/instrumenteditor.go +++ b/tracker/gioui/instrumenteditor.go @@ -477,18 +477,27 @@ func (ie *InstrumentEditor) layoutInstrumentEditor(gtx C, t *Tracker) D { ie.unitTypeEditor.SetCaret(l, l) case key.NameDeleteForward: 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) + ie.unitDragList.SelectedItem2 = t.UnitIndex() contents, err := yaml.Marshal(units) if err == nil { clipboard.WriteOp{Text: string(contents)}.Add(gtx.Ops) - alertText := "Unit(s) copied to clipboard" - if e.Name == "X" { - alertText = "Unit(s) cut to clipboard" - } - t.Alert.Update(alertText, Notify, time.Second*3) + t.Alert.Update("Unit(s) cut to clipboard", Notify, time.Second*3) + } + case "C": + a := clamp(ie.unitDragList.SelectedItem, 0, len(t.Instrument().Units)-1) + 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: if e.Modifiers.Contain(key.ModShortcut) { t.AddUnit(true) @@ -521,3 +530,13 @@ func (ie *InstrumentEditor) layoutInstrumentEditor(gtx C, t *Tracker) D { 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 +} diff --git a/tracker/gioui/tracker.go b/tracker/gioui/tracker.go index 86e702d..b527818 100644 --- a/tracker/gioui/tracker.go +++ b/tracker/gioui/tracker.go @@ -68,11 +68,22 @@ type Tracker struct { func (t *Tracker) UnmarshalContent(bytes []byte) error { var units []sointu.Unit if errJSON := json.Unmarshal(bytes, &units); errJSON == nil { + if len(units) == 0 { + return nil + } 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 } if errYaml := yaml.Unmarshal(bytes, &units); errYaml == nil { + if len(units) == 0 { + return nil + } t.PasteUnits(units) + t.InstrumentEditor.unitDragList.SelectedItem = t.UnitIndex() + t.InstrumentEditor.unitDragList.SelectedItem2 = t.UnitIndex() + len(units) - 1 return nil } var instr sointu.Instrument