From 4ac71781571772434d422b34a357c942d8e4eb6b Mon Sep 17 00:00:00 2001 From: esimov Date: Tue, 9 Jul 2024 16:38:45 +0300 Subject: [PATCH] Adjustments on GUI handling --- gui/window.go | 4 ++++ main.go | 3 +++ ui/panels.go | 26 ++++++++++++++------------ 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/gui/window.go b/gui/window.go index 534ab00..ce9da37 100644 --- a/gui/window.go +++ b/gui/window.go @@ -4,6 +4,7 @@ import ( "fmt" "image" "image/color" + "os" "gioui.org/app" "gioui.org/io/key" @@ -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) } diff --git a/main.go b/main.go index e72db04..429df6e 100644 --- a/main.go +++ b/main.go @@ -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" @@ -79,4 +80,6 @@ func main() { } else { ui.InitApp(*fontPath) } + + go app.Main() } diff --git a/ui/panels.go b/ui/panels.go index 7dc79e1..ab4903c 100644 --- a/ui/panels.go +++ b/ui/panels.go @@ -3,6 +3,7 @@ package ui import ( "fmt" "image" + "log" "math" "os" "path/filepath" @@ -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