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

fix(gui): support ctrl+c for interrupt gui #1385

Merged
merged 6 commits into from
Jul 1, 2024
Merged
Changes from all commits
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
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
Loading