Skip to content

Commit

Permalink
fix(gui): support ctrl+c for interrupt gui (#1385)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ja7ad authored Jul 1, 2024
1 parent 08559b4 commit ed1c5ae
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions cmd/gtk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import (
"flag"
"log"
"os"
"os/signal"
"path/filepath"
"syscall"

"github.com/gofrs/flock"
"github.com/gotk3/gotk3/gdk"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
"github.com/pactus-project/pactus/cmd"
"github.com/pactus-project/pactus/genesis"
"github.com/pactus-project/pactus/node"
"github.com/pactus-project/pactus/util"
"github.com/pactus-project/pactus/version"
"github.com/pactus-project/pactus/wallet"
Expand Down Expand Up @@ -83,6 +86,9 @@ func main() {
log.Println("application startup")
})

n, wlt, err := newNode(workingDir)
fatalErrorCheck(err)

// Connect function to application activate event
app.Connect("activate", func() {
log.Println("application activate")
Expand All @@ -105,9 +111,9 @@ func main() {
gtk.MainIteration()
}

// Running the start-up logic in a separate goroutine
// Running the run-up logic in a separate goroutine
glib.TimeoutAdd(uint(100), func() bool {
start(workingDir, app)
run(n, wlt, app)
splashDlg.Destroy()

// Ensures the function is not called again
Expand All @@ -117,20 +123,32 @@ func main() {

// Connect function to application shutdown event, this is not required.
app.Connect("shutdown", func() {
n.Stop()
_ = fileLock.Unlock()
log.Println("application shutdown")
})

interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)

go func() {
s := <-interrupt
log.Printf("signal %s received", s.String())
n.Stop()
_ = fileLock.Unlock()
os.Exit(0)
}()

// Launch the application
os.Exit(app.Run(nil))
}

func start(workingDir string, app *gtk.Application) {
func newNode(workingDir string) (*node.Node, *wallet.Wallet, error) {
// change working directory
if err := os.Chdir(workingDir); err != nil {
log.Println("Aborted! Unable to changes working directory. " + err.Error())

return
return nil, nil, err
}

passwordFetcher := func(wlt *wallet.Wallet) (string, bool) {
Expand All @@ -140,16 +158,22 @@ func start(workingDir string, app *gtk.Application) {

return getWalletPassword(wlt)
}
node, wlt, err := cmd.StartNode(workingDir, passwordFetcher)
fatalErrorCheck(err)
n, wlt, err := cmd.StartNode(workingDir, passwordFetcher)
if err != nil {
return nil, nil, err
}

return n, wlt, nil
}

grpcAddr := node.GRPC().Address()
func run(n *node.Node, wlt *wallet.Wallet, app *gtk.Application) {
grpcAddr := n.GRPC().Address()
cmd.PrintInfoMsgf("connect wallet to grpc server: %s\n", grpcAddr)

wlt.SetServerAddr(grpcAddr)

nodeModel := newNodeModel(node)
walletModel := newWalletModel(wlt, node)
nodeModel := newNodeModel(n)
walletModel := newWalletModel(wlt, n)

// building main window
win := buildMainWindow(nodeModel, walletModel)
Expand Down

0 comments on commit ed1c5ae

Please sign in to comment.