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

Feature: set browser path #1448

Merged
merged 12 commits into from
Jun 24, 2022
4 changes: 3 additions & 1 deletion v2/cmd/wails/internal/commands/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func AddBuildSubcommand(app *clir.Cli, w io.Writer) {
wv2rtstrategy := ""
webview2 = strings.ToLower(webview2)
if webview2 != "" {
validWV2Runtime := slicer.String([]string{"download", "embed", "browser", "error"})
validWV2Runtime := slicer.String([]string{"download", "embed", "browser", "error", "manual"})
if !validWV2Runtime.Contains(webview2) {
return fmt.Errorf("invalid option for flag 'webview2': %s", webview2)
}
Expand All @@ -150,6 +150,8 @@ func AddBuildSubcommand(app *clir.Cli, w io.Writer) {
wv2rtstrategy = "wv2runtime.error"
case "browser":
wv2rtstrategy = "wv2runtime.browser"
case "manual":
wv2rtstrategy = "wv2runtime.manual"
}
}

Expand Down
3 changes: 2 additions & 1 deletion v2/internal/frontend/desktop/windows/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,9 @@ func (f *Frontend) Quit() {
func (f *Frontend) setupChromium() {
chromium := edge.NewChromium()
f.chromium = chromium
if opts := f.frontendOptions.Windows; opts != nil && opts.WebviewUserDataPath != "" {
if opts := f.frontendOptions.Windows; opts != nil {
chromium.DataPath = opts.WebviewUserDataPath
chromium.BrowserPath = opts.WebviewBrowserPath
}
chromium.MessageCallback = f.processMessage
chromium.WebResourceRequestedCallback = f.processRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package edge

import (
"errors"
"log"
"os"
"path/filepath"
Expand All @@ -30,8 +31,9 @@ type Chromium struct {
environment *ICoreWebView2Environment

// Settings
Debug bool
DataPath string
Debug bool
DataPath string
BrowserPath string

// permissions
permissions map[CoreWebView2PermissionKind]CoreWebView2PermissionState
Expand Down Expand Up @@ -84,7 +86,16 @@ func (e *Chromium) Embed(hwnd uintptr) bool {
dataPath = filepath.Join(os.Getenv("AppData"), currentExeName)
}

res, err := createCoreWebView2EnvironmentWithOptions(nil, windows.StringToUTF16Ptr(dataPath), 0, e.envCompleted)
var browserPathPtr *uint16 = nil
if e.BrowserPath != "" {
if _, err := os.Stat(e.BrowserPath); !errors.Is(err, os.ErrNotExist) {
browserPathPtr = windows.StringToUTF16Ptr(e.BrowserPath)
} else {
log.Printf("Browser path %s does not exist, will use webview2 installed in the system", e.BrowserPath)
stffabi marked this conversation as resolved.
Show resolved Hide resolved
}
}

res, err := createCoreWebView2EnvironmentWithOptions(browserPathPtr, windows.StringToUTF16Ptr(dataPath), 0, e.envCompleted)
stffabi marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Printf("Error calling Webview2Loader: %v", err)
return false
Expand Down
4 changes: 2 additions & 2 deletions v2/internal/wv2installer/download.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build !wv2runtime.error && !wv2runtime.browser && !wv2runtime.embed
// +build !wv2runtime.error,!wv2runtime.browser,!wv2runtime.embed
//go:build !wv2runtime.error && !wv2runtime.browser && !wv2runtime.embed && !wv2runtime.manual
// +build !wv2runtime.error,!wv2runtime.browser,!wv2runtime.embed,!wv2runtime.manual

package wv2installer

Expand Down
11 changes: 11 additions & 0 deletions v2/internal/wv2installer/manual.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build wv2runtime.manual
// +build wv2runtime.manual

package wv2installer

import "github.com/wailsapp/wails/v2/pkg/options/windows"

func doInstallationStrategy(installStatus installationStatus, messages *windows.Messages) error {
// fallback for manually specifying webview2
return nil
stffabi marked this conversation as resolved.
Show resolved Hide resolved
}
3 changes: 3 additions & 0 deletions v2/pkg/options/windows/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ type Options struct {
// If the path is not valid, a messagebox will be displayed with the error and the app will exit with error code.
WebviewUserDataPath string

// Path to the directory with WebView2 executables. If empty WebView2 installed in the system will be used.
WebviewBrowserPath string

// Dark/Light or System Default Theme
Theme Theme

Expand Down
28 changes: 28 additions & 0 deletions website/docs/guides/windows.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ The four options are:
2. Embed
3. Browser
4. Error
5. Manual

### Download

Expand All @@ -35,3 +36,30 @@ up to the user.
### Error

If no suitable runtime is found, an error is given to the user and no further action taken.

### Manual

This option will require you to specify path to installed webview2 runtime in `windows.Options` structure.

```go title="main.go"
func main() {
app := NewApp()

err := wails.Run(&options.App{
Title: "My App",
Width: 800,
Height: 600,
OnStartup: app.startup,
OnShutdown: app.shutdown,
Bind: []interface{}{
app,
},
Windows: &windows.Options{
WebviewBrowserPath: "path/to/runtime",
},
})
if err != nil {
log.Fatal(err)
}
}
```
9 changes: 9 additions & 0 deletions website/docs/reference/options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func main() {
DisableWindowIcon: false,
DisableFramelessWindowDecorations: false,
WebviewUserDataPath: "",
WebviewBrowserPath: "",
Theme: windows.SystemDefault,
CustomTheme: &windows.ThemeSettings{
DarkModeTitleBar: windows.RGB(20, 20, 20),
Expand Down Expand Up @@ -430,6 +431,14 @@ Type: string

This defines the path where the WebView2 stores the user data. If empty `%APPDATA%\[BinaryName.exe]` will be used.

### WebviewBrowserPath

Name: WebviewBrowserPath

Type: string

This defines the path to a directory with WebView2 executable files and libraries. If empty, webview2 installed in the system will be used.
NanoNik marked this conversation as resolved.
Show resolved Hide resolved

### Theme

Name: Theme
Expand Down