5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-04 22:53:19 +08:00

[v3] Support "--default-contextmenu" style. Fix URL handling in Windows.

This commit is contained in:
Lea Anthony 2023-06-20 19:44:01 +10:00
parent 3355d5f0af
commit 9816960995
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
10 changed files with 77 additions and 17 deletions

View File

@ -7,16 +7,19 @@
<style>.file{ width: 100px; height: 100px; border: 3px solid black; }</style> <style>.file{ width: 100px; height: 100px; border: 3px solid black; }</style>
</head> </head>
<body> <body>
<h1>Events Demo</h1> <h1>Context Menu Demo</h1>
<br/> <br/>
<div class="file" id="123abc" data-contextmenu="test" data-contextmenu-data="1" draggable="true" ondragstart="dragstart()" ondragend="dragend()"> <div class="file" id="123abc" data-contextmenu="test" data-contextmenu-data="1">
<h1>1</h1> <h1>1</h1>
</div> </div>
<div class="file" id="234abc" data-contextmenu="test" data-contextmenu-data="2" draggable="true" ondragstart="dragstart()" ondragend="dragend()"> <div class="file" id="234abc" data-contextmenu="test" data-contextmenu-data="2">
<h1>2</h1> <h1>2</h1>
</div> </div>
<div class="file" id="345abc" data-contextmenu="test" data-contextmenu-data="3" draggable="true" ondragstart="dragstart()" ondragend="dragend()"> <div class="file" id="123abcg" style="--default-contextmenu: show">
<h1>3</h1> <h1>Default Context Menu shown here</h1>
</div>
<div class="file" id="234abcs" style="--default-contextmenu: hide">
<h1>Default Context Menu hidden here</h1>
</div> </div>
<div id="results"></div> <div id="results"></div>
</body> </body>

View File

@ -15,7 +15,14 @@ export function enableContextMenus(enabled) {
} }
function contextMenuHandler(event) { function contextMenuHandler(event) {
processContextMenu(event.target, event); let processed = processContextMenu(event.target, event);
if (!processed) {
let defaultContextMenuAction = window.getComputedStyle(event.target).getPropertyValue("--default-contextmenu");
defaultContextMenuAction = defaultContextMenuAction ? defaultContextMenuAction.trim() : "";
if (defaultContextMenuAction === 'hide') {
event.preventDefault();
}
}
} }
function processContextMenu(element, event) { function processContextMenu(element, event) {
@ -23,10 +30,12 @@ function processContextMenu(element, event) {
if (id) { if (id) {
event.preventDefault(); event.preventDefault();
openContextMenu(id, event.clientX, event.clientY, element.getAttribute('data-contextmenu-data')); openContextMenu(id, event.clientX, event.clientY, element.getAttribute('data-contextmenu-data'));
return true;
} else { } else {
let parent = element.parentElement; let parent = element.parentElement;
if (parent) { if (parent) {
processContextMenu(parent, event); processContextMenu(parent, event);
} }
} }
return false;
} }

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -49,6 +49,9 @@ type WebviewWindowOptions struct {
// If true, the window's devtools will be available (default true in builds without the `production` build tag) // If true, the window's devtools will be available (default true in builds without the `production` build tag)
DevToolsEnabled bool DevToolsEnabled bool
// If true, the window's default context menu will be disabled (default false)
DefaultContextMenuDisabled bool
} }
var WebviewWindowDefaults = &WebviewWindowOptions{ var WebviewWindowDefaults = &WebviewWindowOptions{

View File

@ -13,6 +13,7 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"path"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -1172,7 +1173,7 @@ func (w *windowsWebviewWindow) setupChromium() {
if err != nil { if err != nil {
globalApplication.fatal(err.Error()) globalApplication.fatal(err.Error())
} }
err = settings.PutAreDefaultContextMenusEnabled(debugMode) err = settings.PutAreDefaultContextMenusEnabled(debugMode || !w.parent.options.DefaultContextMenuDisabled)
if err != nil { if err != nil {
globalApplication.fatal(err.Error()) globalApplication.fatal(err.Error())
} }
@ -1227,7 +1228,24 @@ func (w *windowsWebviewWindow) setupChromium() {
chromium.Init(script) chromium.Init(script)
chromium.NavigateToString(w.parent.options.HTML) chromium.NavigateToString(w.parent.options.HTML)
} else { } else {
chromium.Navigate("http://wails.localhost") var startURL = "http://wails.localhost"
if w.parent.options.URL != "" {
// parse the url
parsedURL, err := url.Parse(w.parent.options.URL)
if err != nil {
globalApplication.fatal("Error parsing URL: %s", err)
}
if parsedURL.Scheme == "" {
startURL = path.Join(startURL, w.parent.options.URL)
// if the original URL had a trailing slash, add it back
if strings.HasSuffix(w.parent.options.URL, "/") {
startURL = startURL + "/"
}
} else {
startURL = w.parent.options.URL
}
}
chromium.Navigate(startURL)
} }
} }