Skip to content
This repository has been archived by the owner on Mar 29, 2024. It is now read-only.

Commit

Permalink
Open URLs from App in default browser
Browse files Browse the repository at this point in the history
Fixes #49
  • Loading branch information
dhaavi committed Apr 30, 2020
1 parent fa0e96a commit e560636
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 36 deletions.
19 changes: 15 additions & 4 deletions app/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions app/Gopkg.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
ignored = ["github.com/safing/portbase/*"]

[[constraint]]
name = "github.com/zserge/webview"
version = "0.1.0"

[prune]
go-tests = true
unused-packages = true

[[constraint]]
name = "github.com/zserge/webview"
branch = "master"
57 changes: 29 additions & 28 deletions app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,26 @@ import (
"os/signal"
"syscall"

"github.com/safing/portbase/modules"

"github.com/safing/portbase/info"
"github.com/safing/portbase/log"
"github.com/safing/portbase/modules"

"github.com/zserge/webview"
)

var (
dataDir string
databaseDir string
urlFlag string
showVersion bool

url = "http://127.0.0.1:817/"
)

func init() {
flag.StringVar(&dataDir, "data", "", "set data directory")
flag.StringVar(&databaseDir, "db", "", "alias to --data (deprecated)")
flag.BoolVar(&showVersion, "version", false, "show version and exit")
}

func main() {
Expand All @@ -50,7 +52,8 @@ func main() {
}

// print version
if info.PrintVersion() {
if showVersion {
fmt.Println(info.FullVersion())
os.Exit(0)
}

Expand All @@ -74,34 +77,34 @@ func main() {
}

// start log writer
log.Start()

// configure
settings := webview.Settings{
// WebView main window title
Title: "Portmaster",
// URL to open in a webview
URL: url,
// Window width in pixels
Width: 1400,
// Window height in pixels
Height: 900,
// Allows/disallows window resizing
Resizable: true,
// Enable debugging tools (Linux/BSD/MacOS, on Windows use Firebug)
Debug: true,
err = log.Start()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to start logging: %s\n", err)
os.Exit(1)
}

wv := webview.New(settings)
// create webview
wv := webview.New(true)
go shutdownHandler(wv)

wv.SetColor(68, 68, 68, 1)
// configure
wv.SetTitle("Portmaster")
wv.SetSize(1400, 900, webview.HintNone)
wv.Navigate(url)

// register helper to open links in default browser
err = registerUrlOpener(wv)
if err != nil {
log.Warningf("failed to register URL opener: %s", err)
}

// render
wv.Run()
}

func shutdownHandler(wv webview.WebView) {
// catch interrupt for clean shutdown
signalCh := make(chan os.Signal)
signalCh := make(chan os.Signal, 1)
signal.Notify(
signalCh,
os.Interrupt,
Expand All @@ -112,12 +115,10 @@ func shutdownHandler(wv webview.WebView) {
)

// wait for shutdown
select {
case <-signalCh:
fmt.Println(" <INTERRUPT>")
log.Warning("program was interrupted, shutting down")
}
<-signalCh
fmt.Println(" <INTERRUPT>")
log.Warning("program was interrupted, shutting down")

// exit
wv.Dispatch(wv.Exit)
wv.Dispatch(wv.Destroy)
}
20 changes: 20 additions & 0 deletions app/openurl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"github.com/safing/portbase/log"

"github.com/skratchdot/open-golang/open"
"github.com/zserge/webview"
)

func registerUrlOpener(wv webview.WebView) error {
return wv.Bind("openWithOS", openWithOS)
}

func openWithOS(thing string) error {
err := open.Run(thing)
if err != nil {
log.Warningf("failed to open %s: %s", thing, err)
}
return err
}
26 changes: 26 additions & 0 deletions modules/base/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,43 @@ import App from "./App.vue";
import PortAPI from "../../../assets/js/portapi.js";
import jQuery from "jquery";

// load jquery
window.$ = jQuery;
window.jQuery = jQuery;
Vue.config.productionTip = false;

// load css
require("../../../assets/themed/fomantic/semantic.min.js");

// load portbase api
Vue.use(PortAPI, {
url: "ws://127.0.0.1:817/api/database/v1",
debug: true
});

// handle URLs via OS
function handleClick(e) {
var target = e.target || e.srcElement;
if (target.tagName === 'A') {
// do not navigate
e.preventDefault();
// open with OS
var href = target.getAttribute('href');
if (typeof openWithOS !== 'undefined') { // eslint-disable-line
openWithOS(href); // eslint-disable-line
} else {
window.location.href = href;
}
}
}
// listen for clicks
if (document.addEventListener) {
document.addEventListener('click', handleClick);
} else if (document.attachEvent) {
document.attachEvent('onclick', handleClick);
}

// render app
new Vue({
render: h => h(App)
// created() {
Expand Down

0 comments on commit e560636

Please sign in to comment.