Skip to content

Commit

Permalink
Adjustments on GUI handling
Browse files Browse the repository at this point in the history
  • Loading branch information
esimov committed Jul 9, 2024
1 parent bd1d829 commit 4ac7178
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
4 changes: 4 additions & 0 deletions gui/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"image"
"image/color"
"os"

"gioui.org/app"
"gioui.org/io/key"
Expand Down Expand Up @@ -35,6 +36,9 @@ func (ui *GUI) Draw() error {
), app.Title(ui.title))

if err := ui.Run(w); err != nil {
defer func() {
os.Exit(0)
}()
return fmt.Errorf("GUI rendering error: %w", err)
}

Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"time"

"gioui.org/app"
"github.com/esimov/diagram/canvas"
"github.com/esimov/diagram/gui"
"github.com/esimov/diagram/io"
Expand Down Expand Up @@ -79,4 +80,6 @@ func main() {
} else {
ui.InitApp(*fontPath)
}

go app.Main()
}
26 changes: 14 additions & 12 deletions ui/panels.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ui
import (
"fmt"
"image"
"log"
"math"
"os"
"path/filepath"
Expand Down Expand Up @@ -474,34 +475,35 @@ func (ui *UI) generateDiagram(name string) error {
})
})

defer func() error {
defer func() {
if err == nil {
diagram := filePath + output
f, err := os.Open(diagram)
if err != nil {
return fmt.Errorf("failed opening the image %q: %w", diagram, err)
log.Fatalf("failed opening the image %q: %v", diagram, err)
}

source, _, err := image.Decode(f)
if err != nil {
return fmt.Errorf("failed to decode the image %q: %w", diagram, err)
log.Fatalf("failed to decode the image %q: %v", diagram, err)
}

gui := gui.NewGUI(source, "Diagram preview")
go func() error {
if err := gui.Draw(); err != nil {
return fmt.Errorf("error drawing the diagram: %w", err)
}
return nil
}()
// Lunch Gio GUI thread.
go ui.showPreview(source, "Diagram preview")
}

return nil
}()

return nil
}

func (ui *UI) showPreview(img image.Image, title string) {
gui := gui.NewGUI(img, title)

if err := gui.Draw(); err != nil {
log.Fatalf("error drawing the diagram: %v", err)
}
}

// showSaveModal show the save modal.
func (ui *UI) showSaveModal(name string) error {
var saveBtn, cancelBtn *gocui.View
Expand Down

0 comments on commit 4ac7178

Please sign in to comment.