From 4194a776e3f20e1a16f05c78d1a0e972da14eb24 Mon Sep 17 00:00:00 2001 From: lucasgpulcinelli Date: Tue, 6 Feb 2024 14:39:27 -0300 Subject: [PATCH 1/5] add file dialog using external package, because fyne's is kinda bad --- display/widgets.go | 44 ++++++++++++++------------------------------ go.mod | 5 ++++- go.sum | 4 ++++ 3 files changed, 22 insertions(+), 31 deletions(-) diff --git a/display/widgets.go b/display/widgets.go index 82d5f7f..1a52258 100644 --- a/display/widgets.go +++ b/display/widgets.go @@ -1,18 +1,19 @@ package display import ( - "errors" "fmt" + "io" "math" - "path/filepath" + "os" "strconv" - "strings" "time" "fyne.io/fyne/v2" "fyne.io/fyne/v2/container" "fyne.io/fyne/v2/dialog" "fyne.io/fyne/v2/widget" + + fileDialog "github.com/sqweek/dialog" ) var ( @@ -23,48 +24,31 @@ var ( viewMode int = 1 // view type of instruction list (-1 -> raw, 1 -> op name) ) -// validateFileAndShowError checks if a file can be opened and if it's a .mif file. -// If the file cannot be opened or is not a .mif file, it displays an error to the user. -func validateFileAndShowError(f fyne.URIReadCloser, err error) { +// fileSelect opens a file selection window and executes the callback after +// opening the file and checking for errors. +func fileSelect(callback func(io.ReadCloser)) { + fileName, err := fileDialog.File().Load() if err != nil { dialog.ShowError(err, window) return } - if f == nil { - dialog.ShowError(errors.New("could not open a file"), window) + file, err := os.OpenFile(fileName, os.O_RDONLY, 0) + if err != nil { + dialog.ShowError(err, window) return } - // Checks if the file has the .mif extension - if strings.ToLower(filepath.Ext(f.URI().Path())) != ".mif" { - dialog.ShowError(errors.New("file is not a .mif file"), window) - return - } + callback(file) } // makeMainMenu adds in window the main menubar with all code actions // associated. Most code actions are complex and defined in menuActions.go. func makeMainMenu() { - // both file dialog window popup instance (creates a little window to choose - // a file for either a code or char MIF file) - openCodeDialog := dialog.NewFileOpen( - func(f fyne.URIReadCloser, err error) { - validateFileAndShowError(f, err) - fyneReadMIFCode(f) - - }, window) - - openCharDialog := dialog.NewFileOpen( - func(f fyne.URIReadCloser, err error) { - validateFileAndShowError(f, err) - fyneReadMIFChar(f) - }, window) - // "file" menu toolbar file := fyne.NewMenu("file", - fyne.NewMenuItem("open code MIF", func() { openCodeDialog.Show() }), - fyne.NewMenuItem("open char MIF", func() { openCharDialog.Show() }), + fyne.NewMenuItem("open code MIF", func() { fileSelect(fyneReadMIFCode) }), + fyne.NewMenuItem("open char MIF", func() { fileSelect(fyneReadMIFChar) }), ) // "options" menu toolbar diff --git a/go.mod b/go.mod index 4a1edf2..c32ee6d 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/lucasgpulcinelli/goICMCsim go 1.13 -require fyne.io/fyne/v2 v2.4.3 +require ( + fyne.io/fyne/v2 v2.4.3 + github.com/sqweek/dialog v0.0.0-20220809060634-e981b270ebbf +) diff --git a/go.sum b/go.sum index 0cdca8e..7553b41 100644 --- a/go.sum +++ b/go.sum @@ -47,6 +47,8 @@ fyne.io/systray v1.10.1-0.20231115130155-104f5ef7839e/go.mod h1:oM2AQqGJ1AMo4nNq github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= +github.com/TheTitanrain/w32 v0.0.0-20180517000239-4f5cfb03fabf h1:FPsprx82rdrX2jiKyS17BH6IrTmUBYqZa/CXT4uvb+I= +github.com/TheTitanrain/w32 v0.0.0-20180517000239-4f5cfb03fabf/go.mod h1:peYoMncQljjNS6tZwI9WVyQB3qZS6u79/N3mBOcnd3I= github.com/akavel/rsrc v0.10.2/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= @@ -271,6 +273,8 @@ github.com/spf13/cobra v1.2.1/go.mod h1:ExllRjgxM/piMAM+3tAZvg8fsklGAf3tPfi+i8t6 github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/viper v1.8.1/go.mod h1:o0Pch8wJ9BVSWGQMbra6iw0oQ5oktSIBaujf1rJH9Ns= +github.com/sqweek/dialog v0.0.0-20220809060634-e981b270ebbf h1:pCxn3BCfu8n8VUhYl4zS1BftoZoYY0J4qVF3dqAQ4aU= +github.com/sqweek/dialog v0.0.0-20220809060634-e981b270ebbf/go.mod h1:/qNPSY91qTz/8TgHEMioAUc6q7+3SOybeKczHMXFcXw= github.com/srwiley/oksvg v0.0.0-20221011165216-be6e8873101c h1:km8GpoQut05eY3GiYWEedbTT0qnSxrCjsVbb7yKY1KE= github.com/srwiley/oksvg v0.0.0-20221011165216-be6e8873101c/go.mod h1:cNQ3dwVJtS5Hmnjxy6AgTPd0Inb3pW05ftPSX7NZO7Q= github.com/srwiley/rasterx v0.0.0-20210519020934-456a8d69b780/go.mod h1:mvWM0+15UqyrFKqdRjY6LuAVJR0HOVhJlEgZ5JWtSWU= From 620a1f1d5a25319e16f645b9f4b983fbd5ff5047 Mon Sep 17 00:00:00 2001 From: lucasgpulcinelli Date: Wed, 7 Feb 2024 11:28:40 -0300 Subject: [PATCH 2/5] add new required package in linux to compile the code --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 39f901e..0e1c1ce 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,8 @@ The first thing you will want to do is add a program to run and test it. This ca # How to Compile from the Source Code First, install a recent version of go (at least 1.13), either from your package manager or from [here](https://go.dev/doc/install). After that, you will also need git and a C compiler (on windows, you need to use MinGW). -On debian/ubuntu based systems, you will need to install `libgl1-mesa-dev xorg-dev`; -On fedora and red hat based systems, you will need to install `libX11-devel libXcursor-devel libXrandr-devel libXinerama-devel mesa-libGL-devel libXi-devel libXxf86vm-devel`; +On debian/ubuntu based systems, you will need to install `libgl1-mesa-dev xorg-dev gtk3-dev`; +On fedora and red hat based systems, you will need to install `libX11-devel libXcursor-devel libXrandr-devel libXinerama-devel mesa-libGL-devel libXi-devel libXxf86vm-devel gtk3-devel`; Then, Just use `go build .` to compile and `./goICMCsim` to start an empty processor. You can see the command line options with `--help`. From 6ec08f68eb39bbe38b324a9c58c09e4ad618b14b Mon Sep 17 00:00:00 2001 From: lucasgpulcinelli Date: Wed, 7 Feb 2024 11:31:54 -0300 Subject: [PATCH 3/5] remove pprof and log errors from main Co-Authored-By: Isaac Santos Soares --- main.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index bc6d73d..6c19b50 100644 --- a/main.go +++ b/main.go @@ -3,12 +3,10 @@ package main import ( "flag" "io" - "log" "os" "github.com/lucasgpulcinelli/goICMCsim/display" - "net/http" - _ "net/http/pprof" + "github.com/sqweek/dialog" ) var ( @@ -27,22 +25,23 @@ func getFiles() (codem, charm io.ReadCloser) { if *initialCode != "" { codem, err = os.Open(*initialCode) if err != nil { - log.Printf("error opening %s: %v\n", *initialCode, err.Error()) // TODO: log -> dialog/logFile + dialog.Message( + "Reading %v failed:\n%v\n", *initialCode, err.Error()).Error() + os.Exit(-1) } } if *initialChar != "" { charm, err = os.Open(*initialChar) if err != nil { - log.Printf("error opening %s: %v\n", *initialChar, err.Error()) // TODO: log -> dialog/logFile + dialog.Message( + "Reading %v failed:\n%v\n", *initialCode, err.Error()).Error() + os.Exit(-1) } } return } func main() { - go func() { - http.ListenAndServe("localhost:6060", nil) - }() codem, charm := getFiles() display.StartSimulatorWindow(codem, charm) } From 10bc163305c981095a134e7e76953ec93695a6de Mon Sep 17 00:00:00 2001 From: lucasgpulcinelli Date: Wed, 7 Feb 2024 11:40:32 -0300 Subject: [PATCH 4/5] solve error in main.go relating to file names --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 6c19b50..b21b9c1 100644 --- a/main.go +++ b/main.go @@ -34,7 +34,7 @@ func getFiles() (codem, charm io.ReadCloser) { charm, err = os.Open(*initialChar) if err != nil { dialog.Message( - "Reading %v failed:\n%v\n", *initialCode, err.Error()).Error() + "Reading %v failed:\n%v\n", *initialChar, err.Error()).Error() os.Exit(-1) } } From 1b5f971861b48776c0a3aecb8e65d91b7a5f3ee2 Mon Sep 17 00:00:00 2001 From: Isaac Soares Date: Wed, 7 Feb 2024 13:47:42 -0300 Subject: [PATCH 5/5] MIF check Ensuring that the user can only open .mif files and adding a title to the file opening window. --- display/widgets.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/display/widgets.go b/display/widgets.go index 1a52258..edccf4a 100644 --- a/display/widgets.go +++ b/display/widgets.go @@ -26,8 +26,8 @@ var ( // fileSelect opens a file selection window and executes the callback after // opening the file and checking for errors. -func fileSelect(callback func(io.ReadCloser)) { - fileName, err := fileDialog.File().Load() +func fileSelect(callback func(io.ReadCloser), title string) { + fileName, err := fileDialog.File().Filter("MIF file", "mif").Title(title).Load() if err != nil { dialog.ShowError(err, window) return @@ -47,8 +47,8 @@ func fileSelect(callback func(io.ReadCloser)) { func makeMainMenu() { // "file" menu toolbar file := fyne.NewMenu("file", - fyne.NewMenuItem("open code MIF", func() { fileSelect(fyneReadMIFCode) }), - fyne.NewMenuItem("open char MIF", func() { fileSelect(fyneReadMIFChar) }), + fyne.NewMenuItem("open code MIF", func() { fileSelect(fyneReadMIFCode, "Open code MIF") }), + fyne.NewMenuItem("open char MIF", func() { fileSelect(fyneReadMIFChar, "Open char MIF") }), ) // "options" menu toolbar