commit faa311d474e0714848a5448988ddd2fcb91b0c63
parent 211c9db761b17f31e630fc90d33fdcf8cc321619
Author: Jack Mordaunt <jackmordaunt@gmail.com>
Date: Fri, 16 Nov 2018 18:01:35 +1300
[~] Make webview implementation a compile-time dependency.
- Avoids depdency bloat for the final binary.
- Options are for mutally exclusive purposes and thus should not be used at the
same time.
- Makes cgo dependency opt-in instead of default - pure Go!
Diffstat:
4 files changed, 72 insertions(+), 47 deletions(-)
diff --git a/cmd/desktop/main.go b/cmd/desktop/main.go
@@ -1,17 +1,13 @@
package main
import (
- "strings"
"context"
- "syscall"
- "os/signal"
- "os"
- "github.com/zserge/lorca"
"flag"
"fmt"
"log"
"net/http"
"net/url"
+ "strings"
"sync"
"time"
@@ -20,8 +16,6 @@ import (
"github.com/GeertJohan/go.rice"
"github.com/gorilla/mux"
-
- "github.com/zserge/webview"
)
var (
@@ -30,8 +24,6 @@ var (
devServer string
verbose bool
headless bool
- browser bool
- chrome bool
static http.Handler // responsible for serving UI files.
)
@@ -41,14 +33,12 @@ func init() {
flag.StringVar(&devServer, "dev-proxy", "", "proxy to forward to (eg, yarn run serve)")
flag.BoolVar(&verbose, "v", false, "verbose mode")
flag.BoolVar(&headless, "headless", false, "headless mode; run only the server")
- flag.BoolVar(&browser, "browser", false, "open in default browser instead of webview; overriden by [headless]")
- flag.BoolVar(&chrome, "chrome", false, "use chrome to render the UI instead of the native webview")
flag.Parse()
if devServer != "" {
original := devServer
if devServer[0] == ':' {
devServer = fmt.Sprintf("127.0.0.1:%s", devServer[1:])
- }
+ }
if !strings.HasPrefix(devServer, "http://") {
devServer = fmt.Sprintf("http://%s", devServer)
}
@@ -98,41 +88,8 @@ func main() {
return
}
url := fmt.Sprintf("http://%s:%s", host, port)
- if browser {
- b := &Browser{
- OnErr: func(err error) {
- log.Printf("browser: %v", err)
- },
- Loop: true,
- OnRestart: func() {
- log.Printf("restarting browser")
- },
- }
- if err := b.Run(url); err != nil {
- log.Fatalf("opening browser: %v", err)
- }
- } else if chrome {
- b, err := lorca.New(url, "", 800, 600)
- if err != nil {
- log.Fatalf("opening chrome: %v", err)
- }
- defer b.Close()
- sigc := make(chan os.Signal)
- signal.Notify(sigc, os.Interrupt, syscall.SIGTERM)
- select {
- case <-sigc:
- case <-b.Done():
- }
- } else {
- view := webview.New(webview.Settings{
- Title: "Giffer",
- URL: url,
- Width: 800,
- Height: 600,
- Resizable: true,
- Debug: true,
- })
- view.Run()
+ if err := Webview(url, "Giffer", 800, 600); err != nil {
+ log.Printf("webview: %v", err)
}
if err := svr.Shutdown(context.Background()); err != nil {
log.Printf("shutting down server: %v", err)
diff --git a/cmd/desktop/webview.go b/cmd/desktop/webview.go
@@ -0,0 +1,19 @@
+// +build !chrome,!native
+
+package main
+
+import "log"
+
+// Webview renders the UI through the system's default browser.
+func Webview(url, title string, w, h int) error {
+ b := &Browser{
+ OnErr: func(err error) {
+ log.Printf("browser: %v", err)
+ },
+ Loop: true,
+ OnRestart: func() {
+ log.Printf("restarting browser")
+ },
+ }
+ return b.Run(url)
+}
diff --git a/cmd/desktop/webview_chrome.go b/cmd/desktop/webview_chrome.go
@@ -0,0 +1,29 @@
+// +build chrome
+
+package main
+
+import (
+ "os"
+ "os/signal"
+ "syscall"
+
+ "github.com/pkg/errors"
+ "github.com/zserge/lorca"
+)
+
+// Webview (+chrome) renders the UI via a chrome window.
+// Requires Chrome to be installed.
+func Webview(url, title string, w, h int) error {
+ b, err := lorca.New(url, "", 800, 600)
+ if err != nil {
+ return errors.Wrap(err, "opening chrome")
+ }
+ defer b.Close()
+ sigc := make(chan os.Signal)
+ signal.Notify(sigc, os.Interrupt, syscall.SIGTERM)
+ select {
+ case <-sigc:
+ case <-b.Done():
+ }
+ return nil
+}
diff --git a/cmd/desktop/webview_native.go b/cmd/desktop/webview_native.go
@@ -0,0 +1,20 @@
+// +build native
+
+package main
+
+import "github.com/zserge/webview"
+
+// Webview (+native) renders the UI via a native webview window.
+// Requires cgo as a compile time dependency.
+func Webview(url, title string, w, h int) error {
+ view := webview.New(webview.Settings{
+ Title: title,
+ URL: url,
+ Width: w,
+ Height: h,
+ Resizable: true,
+ Debug: true,
+ })
+ view.Run()
+ return nil
+}