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): update about dialog and menu items in help #532

Merged
merged 10 commits into from
Jun 26, 2023
Binary file added cmd/gtk/assets/icons/menu/website.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cmd/gtk/assets/images/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 7 additions & 4 deletions cmd/gtk/assets/ui/dialog_about.ui
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<!-- Generated with glade 3.40.0 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkAboutDialog" id="id_dialog_about">
<property name="can-focus">False</property>
<property name="title" translatable="yes">About Pactus</property>
<property name="type-hint">dialog</property>
<property name="program-name">Pactus</property>
<property name="comments" translatable="yes">&lt;TODO&gt;</property>
<property name="website">https://github.com/pactus-project/pactus</property>
<property name="website-label" translatable="yes">Pactus</property>
<property name="version">0.0.0</property>
<property name="comments" translatable="yes">
Building Decentralized Future Together!
Pactus is a low-cost, high-performance, and scalable decentralized blockchain protocol with a novel PoS consensus.
</property>
<property name="logo">../images/logo.png</property>
<property name="license-type">mit-x11</property>
<child internal-child="vbox">
<object class="GtkBox">
Expand Down
36 changes: 33 additions & 3 deletions cmd/gtk/assets/ui/main_window.ui
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<!-- Generated with glade 3.40.0 -->
<interface>
<requires lib="gtk+" version="3.24"/>
<object class="GtkApplicationWindow" id="id_main_window">
Expand Down Expand Up @@ -103,11 +103,41 @@
<object class="GtkMenu">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkMenuItem" id="id_website_menu">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">_Website</property>
<property name="use-underline">True</property>
</object>
</child>
<child>
<object class="GtkMenuItem" id="id_explorer_menu">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">_Explorer</property>
<property name="use-underline">True</property>
</object>
</child>
<child>
<object class="GtkMenuItem" id="id_learn_menu">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">_Learn</property>
<property name="use-underline">True</property>
</object>
</child>
<child>
<object class="GtkSeparatorMenuItem">
<property name="visible">True</property>
<property name="can-focus">False</property>
</object>
</child>
<child>
<object class="GtkMenuItem">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">About _Pactus</property>
<property name="label" translatable="yes">About _Pactus</property>
<property name="use-underline">True</property>
<signal name="activate" handler="on_about" swapped="no"/>
</object>
Expand All @@ -116,7 +146,7 @@
<object class="GtkMenuItem">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label">About _GTK</property>
<property name="label" translatable="yes">About _GTK</property>
<property name="use-underline">True</property>
<signal name="activate" handler="on_about_gtk" swapped="no"/>
</object>
Expand Down
17 changes: 14 additions & 3 deletions cmd/gtk/dialog_about.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,30 @@ package main
import (
_ "embed"

"github.com/gotk3/gotk3/gdk"
"github.com/gotk3/gotk3/gtk"
"github.com/pactus-project/pactus/version"
)

//go:embed assets/ui/dialog_about.ui
var uiAboutDialog []byte
var (
//go:embed assets/ui/dialog_about.ui
uiAboutDialog []byte

//go:embed assets/images/logo.png
pactusLogo []byte
)

func showAboutDialog() {
builder, err := gtk.BuilderNewFromString(string(uiAboutDialog))
fatalErrorCheck(err)

dlg := getAboutDialogObj(builder, "id_dialog_about")

dlg.SetModal(true)
pxLogo, err := gdk.PixbufNewFromBytesOnly(pactusLogo)
fatalErrorCheck(err)

dlg.SetLogo(pxLogo)
dlg.SetVersion(version.Version())

dlg.Show()
}
28 changes: 28 additions & 0 deletions cmd/gtk/main_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/gotk3/gotk3/gtk"
"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/util"
)

//go:embed assets/ui/main_window.ui
Expand Down Expand Up @@ -43,6 +44,15 @@ func buildMainWindow(nodeModel *nodeModel, walletModel *walletModel) *mainWindow
widgetWallet: widgetWallet,
}

explorerItemMenu := getMenuItem(builder, "id_explorer_menu")
explorerItemMenu.Connect("activate", mw.onMenuItemActivateExplorer)

websiteItemMenu := getMenuItem(builder, "id_website_menu")
websiteItemMenu.Connect("activate", mw.onMenuItemActivateWebsite)

learnItemMenu := getMenuItem(builder, "id_learn_menu")
learnItemMenu.Connect("activate", mw.onMenuItemActivateLearn)

// Map the handlers to callback functions, and connect the signals
// to the Builder.
signals := map[string]interface{}{
Expand Down Expand Up @@ -73,6 +83,24 @@ func (mw *mainWindow) OnTransactionTransfer() {
broadcastTransactionSend(mw.widgetWallet.model.wallet)
}

func (mw *mainWindow) onMenuItemActivateWebsite(_ *gtk.MenuItem) {
if err := util.OpenURLInBrowser("https://pactus.org/"); err != nil {
fatalErrorCheck(err)
}
}

func (mw *mainWindow) onMenuItemActivateExplorer(_ *gtk.MenuItem) {
if err := util.OpenURLInBrowser("https://pactusscan.com/"); err != nil {
fatalErrorCheck(err)
}
}

func (mw *mainWindow) onMenuItemActivateLearn(_ *gtk.MenuItem) {
if err := util.OpenURLInBrowser("https://pactus.org/learn/"); err != nil {
fatalErrorCheck(err)
}
}

func (mw *mainWindow) OnTransactionBond() {
valAddrs := []crypto.Address{}
consMgr := mw.widgetNode.model.node.ConsManager()
Expand Down
4 changes: 4 additions & 0 deletions cmd/gtk/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ func getProgressBarObj(builder *gtk.Builder, name string) *gtk.ProgressBar {
return getObj(builder, name).(*gtk.ProgressBar)
}

func getMenuItem(builder *gtk.Builder, name string) *gtk.MenuItem {
return getObj(builder, name).(*gtk.MenuItem)
}

func getTextViewContent(tv *gtk.TextView) string {
buf, _ := tv.GetBuffer()
startIter, endIter := buf.GetBounds()
Expand Down
33 changes: 33 additions & 0 deletions util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import (
crand "crypto/rand"
"errors"
"fmt"
"math/big"
"net/url"
"os"
"os/exec"
"runtime"
"strconv"
)

Expand Down Expand Up @@ -196,3 +200,32 @@
coin := ChangeToCoin(change)
return strconv.FormatFloat(coin, 'f', -1, 64)
}

// OpenURLInBrowser open specific url in browser base on os
func OpenURLInBrowser(address string) error {
cmd := ""
args := make([]string, 0)

addr, err := url.Parse(address)
if err != nil {
return err
}

switch addr.Scheme {
case "http", "https":
default:
return errors.New("address scheme is invalid")
}

switch runtime.GOOS {
case "windows":
cmd = "cmd"
args = []string{"/c", "start"}
case "darwin":
cmd = "open"

Check warning on line 225 in util/utils.go

View check run for this annotation

Codecov / codecov/patch

util/utils.go#L221-L225

Added lines #L221 - L225 were not covered by tests
default: // "linux", "freebsd", "openbsd", "netbsd"
cmd = "xdg-open"
}
args = append(args, address)
return exec.Command(cmd, args...).Start()
}
24 changes: 24 additions & 0 deletions util/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,27 @@ func TestCoinToChangeConversion(t *testing.T) {
}
}
}

func TestOpenURLInBrowser(t *testing.T) {
tests := []string{
"https://pactus.org",
"pactus.org",
"pactus",
"test",
"http://example.com",
"http://example.com",
"",
"###{DEf1&*@^^!))@#",
"user:abc{DEf1=ghi@example.com:5432/db?sslmode=require",
"Segment%%2815197306101420000%29.ts",
}

for _, tt := range tests {
t.Run(tt, func(t *testing.T) {
err := OpenURLInBrowser(tt)
if err != nil {
t.Skipf("skip url %s and got error %s", tt, err.Error())
}
})
}
}