From 2b4093354db95461066bb44b5de8a24b59c09181 Mon Sep 17 00:00:00 2001
From: "Andrew E. Torda" <torda@zbh.uni-hamburg.de>
Date: Tue, 8 Feb 2022 14:57:43 +0100
Subject: [PATCH] Rudiments of file saving. Added build tag to turn off
 graphics, no_gfx.

---
 mc_work/ToDo        |  5 +--
 ui/scrnplt.go       | 86 ++++++++++++++++++++++++++++++++++++++++++---
 ui/scrnplt_nogfx.go |  9 +++++
 3 files changed, 93 insertions(+), 7 deletions(-)
 create mode 100644 ui/scrnplt_nogfx.go

diff --git a/mc_work/ToDo b/mc_work/ToDo
index c5fe2bc..bec88f2 100644
--- a/mc_work/ToDo
+++ b/mc_work/ToDo
@@ -1,11 +1,12 @@
+For debugging without graphics,
+dlv test --build-flags "-tags=no_gfx"
+
 In dorun.go
 
 * do we have the random number seed as a settable parameter ?
 
 * make the csv files optional
 
-* Change the tests so all files are written to a temporary directory
-
 Note.. Benchmarking suggests that most of the time is spent in Ackley function, and, to be exact, in the cosine and exp() function.
 
 In the plottin functions,
diff --git a/ui/scrnplt.go b/ui/scrnplt.go
index 673caf0..f383ede 100644
--- a/ui/scrnplt.go
+++ b/ui/scrnplt.go
@@ -1,31 +1,107 @@
 // 25 Jan 2020
 // Given a buffer or two with a plot picture, send it to the screen
 
-// I think this might be better in its own package
+// +build !no_gfx
+
 package ui
 
 import (
 	"bytes"
+	"fmt"
 	"fyne.io/fyne/v2"
 	"fyne.io/fyne/v2/app"
 	"fyne.io/fyne/v2/canvas"
 	"fyne.io/fyne/v2/container"
-	//	"fyne.io/fyne/v2/layout"
+	"fyne.io/fyne/v2/dialog"
+	"fyne.io/fyne/v2/storage"
+	"fyne.io/fyne/v2/widget"
 )
 
+func coffee() {
+	print("coffee\n")
+}
+func tea() {
+	print("tea\n")
+}
+
+func trydsave(uriwc fyne.URIWriteCloser, err error) {
+	uriwc.Write([]byte("hello from trydsave\n"))
+	fmt.Printf("trydsave called with %+v %+v", uriwc, err)
+}
+func dsave(parent fyne.Window) {
+	filter := storage.NewExtensionFileFilter([]string{"csv", "abc"})
+	t := dialog.NewFileSave(trydsave, parent)
+	t.SetFileName("f_val.csv")
+	t.SetFilter(filter)
+	t.Show()
+	fmt.Println("dsave called and returning")
+}
+
+func stopme() {
+	print("stopme\n")
+}
+
+type fssavefunc func(fyne.URIReadCloser, error)
+
+func topmenu(parent fyne.Window) *fyne.Menu {
+	ds := func() { dsave(parent) }
+	a := fyne.NewMenuItem("coffee", coffee)
+	b := fyne.NewMenuItem("tea", tea)
+	c := fyne.NewMenuItem("save func plot", ds)
+	d := fyne.NewMenuItem("frueh feierabend", stopme)
+	return fyne.NewMenu("a simple menu", a, b, c, d)
+}
+
+func fileSaved(f fyne.URIWriteCloser, w fyne.Window) {
+	defer f.Close()
+	_, err := f.Write([]byte("Written by Fyne demo\n"))
+	if err != nil {
+		dialog.ShowError(err, w)
+	}
+	err = f.Close()
+	if err != nil {
+		dialog.ShowError(err, w)
+	}
+	fmt.Println("Saved to...", f.URI())
+}
+func nothing(...interface{}) {}
+
+// buf2file takes an array of bytes and writes them to a file.
+func buf2file(buf []byte) {
+	var z fyne.URIWriteCloser
+
+	//	NewFileSave(callback func(fyne.URIWriteCloser, error), parent fyne.Window) *FileDialog
+	var win fyne.Window
+	fs := widget.NewButton("File Save", func() {
+		dialog.ShowFileSave(func(writer fyne.URIWriteCloser, err error) {
+			if err != nil {
+				dialog.ShowError(err, win)
+				return
+			}
+			if writer == nil {
+				fmt.Println("Cancelled")
+				return
+			}
+
+			fileSaved(writer, win)
+		}, win)
+	})
+	nothing(fs, z)
+}
+
 func breaker(...interface{}) {}
 func Scrnplt(fdata []byte, xdata []byte) {
 	a := app.NewWithID("ackley") // Find a way to check if it is happy
 	w := a.NewWindow("container")
+
+	w.SetMainMenu(fyne.NewMainMenu(topmenu(w)))
 	fImage := canvas.NewImageFromReader(bytes.NewReader(fdata), "func.png")
 	fImage.FillMode = canvas.ImageFillContain
 	xImage := canvas.NewImageFromReader(bytes.NewReader(xdata), "xdata.png")
 	xImage.FillMode = canvas.ImageFillContain
-	//	content := container.NewVSplit(fImage, xImage) works well
 	content := container.NewGridWithRows(2, fImage, xImage)
 
-	w.Resize(fyne.NewSize(500, 320))
+	w.Resize(fyne.NewSize(500, 300))
 	w.SetContent(content)
-
 	w.ShowAndRun()
 }
diff --git a/ui/scrnplt_nogfx.go b/ui/scrnplt_nogfx.go
new file mode 100644
index 0000000..aa5e821
--- /dev/null
+++ b/ui/scrnplt_nogfx.go
@@ -0,0 +1,9 @@
+// +build no_gfx
+// 
+// If we do not have graphics
+
+package ui
+
+func Scrnplt(fdata []byte, xdata []byte) {
+	print ("No graphics")
+}
-- 
GitLab