Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Novo diálogo de arquivos e remoção de log.printf da main #8

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 14 additions & 30 deletions display/widgets.go
Original file line number Diff line number Diff line change
@@ -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"
lucasgpulcinelli marked this conversation as resolved.
Show resolved Hide resolved
)

var (
Expand All @@ -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" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Talvez tirando esse check o erro que será gerado, quando um arquivo de extensão distinta de .mif (tipo o .asm) for carregado, fique um pouco confuso pra um usuário mais leigo do simulador. Porque ele dá um erro na criação do parser só.

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
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down