mirror of
https://github.com/vsariola/sointu.git
synced 2025-07-18 21:14:31 -04:00
feat(gioui): implement own file save / load dialogs
Removes the dependency on sqweek/dialogs, which was always very buggy. Closes #12
This commit is contained in:
@ -12,39 +12,60 @@ import (
|
||||
"gioui.org/app"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/sqweek/dialog"
|
||||
"github.com/vsariola/sointu"
|
||||
)
|
||||
|
||||
func (t *Tracker) LoadSongFile() {
|
||||
if t.ChangedSinceSave() {
|
||||
func (t *Tracker) OpenSongFile(forced bool) {
|
||||
if !forced && t.ChangedSinceSave() {
|
||||
t.ConfirmSongActionType = ConfirmLoad
|
||||
t.ConfirmSongDialog.Visible = true
|
||||
return
|
||||
}
|
||||
t.loadSong()
|
||||
if p := t.FilePath(); p != "" {
|
||||
d, _ := filepath.Split(p)
|
||||
d = filepath.Clean(d)
|
||||
t.OpenSongDialog.Directory.SetText(d)
|
||||
t.OpenSongDialog.FileName.SetText("")
|
||||
}
|
||||
t.OpenSongDialog.Visible = true
|
||||
}
|
||||
|
||||
func (t *Tracker) SaveSongFile() bool {
|
||||
if p := t.FilePath(); p != "" {
|
||||
return t.saveSong(p)
|
||||
}
|
||||
return t.SaveSongAsFile()
|
||||
t.SaveSongAsFile()
|
||||
return false
|
||||
}
|
||||
|
||||
func (t *Tracker) SaveSongAsFile() bool {
|
||||
filename, err := dialog.File().Filter("Sointu YAML song", "yml").Filter("Sointu JSON song", "json").Title("Save song").Save()
|
||||
if err != nil {
|
||||
return false
|
||||
func (t *Tracker) SaveSongAsFile() {
|
||||
t.SaveSongDialog.Visible = true
|
||||
if p := t.FilePath(); p != "" {
|
||||
d, f := filepath.Split(p)
|
||||
d = filepath.Clean(d)
|
||||
t.SaveSongDialog.Directory.SetText(d)
|
||||
t.SaveSongDialog.FileName.SetText(f)
|
||||
}
|
||||
return t.saveSong(filename)
|
||||
}
|
||||
|
||||
func (t *Tracker) loadSong() {
|
||||
filename, err := dialog.File().Filter("Sointu YAML song", "yml").Filter("Sointu JSON song", "json").Title("Load song").Load()
|
||||
if err != nil {
|
||||
return
|
||||
func (t *Tracker) ExportWav() {
|
||||
t.ExportWavDialog.Visible = true
|
||||
if p := t.FilePath(); p != "" {
|
||||
d, _ := filepath.Split(p)
|
||||
d = filepath.Clean(d)
|
||||
t.ExportWavDialog.Directory.SetText(d)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Tracker) LoadInstrument() {
|
||||
t.OpenInstrumentDialog.Visible = true
|
||||
}
|
||||
|
||||
func (t *Tracker) SaveInstrument() {
|
||||
t.SaveInstrumentDialog.Visible = true
|
||||
}
|
||||
|
||||
func (t *Tracker) loadSong(filename string) {
|
||||
bytes, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return
|
||||
@ -52,10 +73,12 @@ func (t *Tracker) loadSong() {
|
||||
var song sointu.Song
|
||||
if errJSON := json.Unmarshal(bytes, &song); errJSON != nil {
|
||||
if errYaml := yaml.Unmarshal(bytes, &song); errYaml != nil {
|
||||
t.Alert.Update(fmt.Sprintf("Error unmarshaling a song file: %v / %v", errYaml, errJSON), Error, time.Second*3)
|
||||
return
|
||||
}
|
||||
}
|
||||
if song.Score.Length <= 0 || len(song.Score.Tracks) == 0 || len(song.Patch) == 0 {
|
||||
t.Alert.Update("The song file is malformed", Error, time.Second*3)
|
||||
return
|
||||
}
|
||||
t.SetSong(song)
|
||||
@ -69,12 +92,13 @@ func (t *Tracker) saveSong(filename string) bool {
|
||||
var extension = filepath.Ext(filename)
|
||||
var contents []byte
|
||||
var err error
|
||||
if extension == "json" {
|
||||
if extension == ".json" {
|
||||
contents, err = json.Marshal(t.Song())
|
||||
} else {
|
||||
contents, err = yaml.Marshal(t.Song())
|
||||
}
|
||||
if err != nil {
|
||||
t.Alert.Update(fmt.Sprintf("Error marshaling a song file: %v", err), Error, time.Second*3)
|
||||
return false
|
||||
}
|
||||
if extension == "" {
|
||||
@ -87,51 +111,7 @@ func (t *Tracker) saveSong(filename string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (t *Tracker) SaveInstrument() bool {
|
||||
filename, err := dialog.File().Filter("Sointu YAML instrument", "yml").Filter("Sointu JSON song", "json").Title(fmt.Sprintf("Save instrument %v", t.Instrument().Name)).Save()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
var extension = filepath.Ext(filename)
|
||||
var contents []byte
|
||||
if extension == "json" {
|
||||
contents, err = json.Marshal(t.Instrument())
|
||||
} else {
|
||||
contents, err = yaml.Marshal(t.Instrument())
|
||||
}
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if extension == "" {
|
||||
filename = filename + ".yml"
|
||||
}
|
||||
ioutil.WriteFile(filename, contents, 0644)
|
||||
return true
|
||||
}
|
||||
|
||||
func (t *Tracker) LoadInstrument() {
|
||||
filename, err := dialog.File().Filter("Sointu YAML instrument", "yml").Filter("Sointu JSON instrument", "json").Title("Load instrument").Load()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
bytes, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var instrument sointu.Instrument
|
||||
if errJSON := json.Unmarshal(bytes, &instrument); errJSON != nil {
|
||||
if errYaml := yaml.Unmarshal(bytes, &instrument); errYaml != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
t.SetInstrument(instrument)
|
||||
}
|
||||
|
||||
func (t *Tracker) exportWav(pcm16 bool) {
|
||||
filename, err := dialog.File().Filter(".wav file", "wav").Title("Export .wav").Save()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
func (t *Tracker) exportWav(filename string, pcm16 bool) {
|
||||
var extension = filepath.Ext(filename)
|
||||
if extension == "" {
|
||||
filename = filename + ".wav"
|
||||
@ -156,3 +136,43 @@ func (t *Tracker) exportWav(pcm16 bool) {
|
||||
}
|
||||
ioutil.WriteFile(filename, buffer, 0644)
|
||||
}
|
||||
|
||||
func (t *Tracker) saveInstrument(filename string) bool {
|
||||
var extension = filepath.Ext(filename)
|
||||
var contents []byte
|
||||
var err error
|
||||
if extension == ".json" {
|
||||
contents, err = json.Marshal(t.Instrument())
|
||||
} else {
|
||||
contents, err = yaml.Marshal(t.Instrument())
|
||||
}
|
||||
if err != nil {
|
||||
t.Alert.Update(fmt.Sprintf("Error marshaling a ínstrument file: %v", err), Error, time.Second*3)
|
||||
return false
|
||||
}
|
||||
if extension == "" {
|
||||
filename = filename + ".yml"
|
||||
}
|
||||
ioutil.WriteFile(filename, contents, 0644)
|
||||
return true
|
||||
}
|
||||
|
||||
func (t *Tracker) loadInstrument(filename string) bool {
|
||||
bytes, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
var instrument sointu.Instrument
|
||||
if errJSON := json.Unmarshal(bytes, &instrument); errJSON != nil {
|
||||
if errYaml := yaml.Unmarshal(bytes, &instrument); errYaml != nil {
|
||||
t.Alert.Update(fmt.Sprintf("Error unmarshaling an instrument file: %v / %v", errYaml, errJSON), Error, time.Second*3)
|
||||
return false
|
||||
}
|
||||
}
|
||||
if len(instrument.Units) == 0 {
|
||||
t.Alert.Update("The instrument file is malformed", Error, time.Second*3)
|
||||
return false
|
||||
}
|
||||
t.SetInstrument(instrument)
|
||||
return true
|
||||
}
|
||||
|
Reference in New Issue
Block a user