mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-04 20:29:37 +08:00
[v3] Support "--default-contextmenu" style. Fix URL handling in Windows.
This commit is contained in:
parent
3355d5f0af
commit
9816960995
@ -7,16 +7,19 @@
|
||||
<style>.file{ width: 100px; height: 100px; border: 3px solid black; }</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Events Demo</h1>
|
||||
<h1>Context Menu Demo</h1>
|
||||
<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>
|
||||
</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>
|
||||
</div>
|
||||
<div class="file" id="345abc" data-contextmenu="test" data-contextmenu-data="3" draggable="true" ondragstart="dragstart()" ondragend="dragend()">
|
||||
<h1>3</h1>
|
||||
<div class="file" id="123abcg" style="--default-contextmenu: show">
|
||||
<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 id="results"></div>
|
||||
</body>
|
||||
|
@ -15,7 +15,14 @@ export function enableContextMenus(enabled) {
|
||||
}
|
||||
|
||||
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) {
|
||||
@ -23,10 +30,12 @@ function processContextMenu(element, event) {
|
||||
if (id) {
|
||||
event.preventDefault();
|
||||
openContextMenu(id, event.clientX, event.clientY, element.getAttribute('data-contextmenu-data'));
|
||||
return true;
|
||||
} else {
|
||||
let parent = element.parentElement;
|
||||
if (parent) {
|
||||
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
@ -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)
|
||||
DevToolsEnabled bool
|
||||
|
||||
// If true, the window's default context menu will be disabled (default false)
|
||||
DefaultContextMenuDisabled bool
|
||||
}
|
||||
|
||||
var WebviewWindowDefaults = &WebviewWindowOptions{
|
||||
|
@ -13,6 +13,7 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@ -1172,7 +1173,7 @@ func (w *windowsWebviewWindow) setupChromium() {
|
||||
if err != nil {
|
||||
globalApplication.fatal(err.Error())
|
||||
}
|
||||
err = settings.PutAreDefaultContextMenusEnabled(debugMode)
|
||||
err = settings.PutAreDefaultContextMenusEnabled(debugMode || !w.parent.options.DefaultContextMenuDisabled)
|
||||
if err != nil {
|
||||
globalApplication.fatal(err.Error())
|
||||
}
|
||||
@ -1227,7 +1228,24 @@ func (w *windowsWebviewWindow) setupChromium() {
|
||||
chromium.Init(script)
|
||||
chromium.NavigateToString(w.parent.options.HTML)
|
||||
} 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)
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user