Add window.GetBorderSizes

This commit is contained in:
Lea Anthony 2024-04-25 15:21:38 +10:00
commit 2cee1edcb5
No known key found for this signature in database
GPG key ID: 33DAF7BB90A58405
8 changed files with 52 additions and 113 deletions

View file

@ -1,3 +0,0 @@
# OAuth Example
This example is not ready for testing yet.

View file

@ -1,41 +0,0 @@
<!-- templates/index.html -->
<!doctype html>
<html>
<head>
<title>Google SignIn</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <!-- load bulma css -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> <!-- load fontawesome -->
</head>
<body>
<div id="main" class="container">
<div class="jumbotron text-center text-success" style="padding-top:70px;">
<h1><span class="fa fa-lock"></span> Social Authentication</h1>
<p>Login or Register with:</p>
<a wml-event="github-login" class="btn btn-primary"><span class="fa fa-github" style="color: #FFF"></span> SignIn with Github</a>
</div>
</div>
<div id="details" class="text-center" style="display: none">
<image id="logo" style="width:250px"></image>
<h3 id="name" style="padding-top: 10px"></h3>
<a wml-event="github-logout" class="btn btn-primary"><span class="fa fa-github" style="color: #FFF"></span> Logout </a>
</div>
<script type="module">
import * as wails from "/wails/runtime.js";
wails.Events.On("wails:oauth:success", (event) => {
document.getElementById("main").style.display = "none";
document.getElementById("details").style.display = "block";
document.getElementById("name").innerText = event.data.Name;
document.getElementById("logo").src = event.data.AvatarURL;
document.body.style.backgroundColor = "#000";
document.body.style.color = "#FFF";
});
wails.Events.On("wails:oauth:loggedout", (event) => {
document.getElementById("details").style.display = "none";
document.getElementById("main").style.display = "block";
document.body.style.backgroundColor = "#FFF";
document.body.style.color = "#000";
});
</script>
</body>
</html>

View file

@ -1,68 +0,0 @@
package main
import (
"embed"
_ "embed"
"log"
"os"
"github.com/markbates/goth"
"github.com/markbates/goth/providers/github"
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/plugins/oauth"
)
//go:embed assets
var assets embed.FS
func main() {
oAuthPlugin := oauth.NewPlugin(oauth.Config{
Providers: []goth.Provider{
github.New(
os.Getenv("clientkey"),
os.Getenv("secret"),
"http://localhost:9876/auth/github/callback",
"email",
"profile"),
},
})
app := application.New(application.Options{
Name: "OAuth Demo",
Description: "A demo of the oauth Plugin",
Mac: application.MacOptions{
ApplicationShouldTerminateAfterLastWindowClosed: true,
},
Assets: application.AssetOptions{
Handler: application.AssetFileServerFS(assets),
},
Plugins: map[string]application.Plugin{
"github.com/wailsapp/wails/v3/plugins/oauth": oAuthPlugin,
},
})
app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
Title: "OAuth Demo",
DevToolsEnabled: true,
OpenInspectorOnStartup: true,
Mac: application.MacWindow{
Backdrop: application.MacBackdropTranslucent,
TitleBar: application.MacTitleBarHiddenInsetUnified,
InvisibleTitleBarHeight: 50,
},
})
app.Events.On("github-login", func(e *application.WailsEvent) {
oAuthPlugin.Github()
})
app.Events.On("github-logout", func(e *application.WailsEvent) {
oAuthPlugin.LogoutGithub()
})
err := app.Run()
if err != nil {
log.Fatal(err.Error())
}
}

View file

@ -20,6 +20,14 @@ var Enabled = u.True
// Disabled means the feature should be disabled
var Disabled = u.False
// LRTB is a struct that holds Left, Right, Top, Bottom values
type LRTB struct {
Left int
Right int
Top int
Bottom int
}
type (
webviewWindowImpl interface {
setTitle(title string)
@ -81,6 +89,7 @@ type (
setAbsolutePosition(x int, y int)
flash(enabled bool)
handleKeyEvent(acceleratorString string)
getBorderSizes() *LRTB
}
)
@ -400,6 +409,13 @@ func (w *WebviewWindow) SetURL(s string) Window {
return w
}
func (w *WebviewWindow) GetBorderSizes() *LRTB {
if w.impl != nil {
return InvokeSyncWithResult(w.impl.getBorderSizes)
}
return &LRTB{}
}
// SetZoom sets the zoom level of the window.
func (w *WebviewWindow) SetZoom(magnification float64) Window {
w.options.Zoom = magnification

View file

@ -776,6 +776,10 @@ func (w *macosWebviewWindow) handleKeyEvent(acceleratorString string) {
w.parent.processKeyBinding(accelerator.String())
}
func (w *macosWebviewWindow) getBorderSizes() *LRTB {
return &LRTB{}
}
func (w *macosWebviewWindow) isFocused() bool {
return bool(C.windowIsFocused(w.nsWindow))
}

View file

@ -167,6 +167,10 @@ func (w *linuxWebviewWindow) setMinSize(width, height int) {
w.setMinMaxSize(width, height, w.parent.options.MaxWidth, w.parent.options.MaxHeight)
}
func (w *linuxWebviewWindow) getBorderSizes() *LRTB {
return &LRTB{}
}
func (w *linuxWebviewWindow) setMaxSize(width, height int) {
w.setMinMaxSize(w.parent.options.MinWidth, w.parent.options.MinHeight, width, height)
}

View file

@ -69,9 +69,23 @@ func (w *windowsWebviewWindow) handleKeyEvent(_ string) {
// Unused on windows
}
// getBorderSizes returns the extended border size for the window
func (w *windowsWebviewWindow) getBorderSizes() *LRTB {
var result LRTB
var frame w32.RECT
w32.DwmGetWindowAttribute(w.hwnd, w32.DWMWA_EXTENDED_FRAME_BOUNDS, unsafe.Pointer(&frame), unsafe.Sizeof(frame))
rect := w32.GetWindowRect(w.hwnd)
result.Left = int(frame.Left - rect.Left)
result.Top = int(frame.Top - rect.Top)
result.Right = int(rect.Right - frame.Right)
result.Bottom = int(rect.Bottom - frame.Bottom)
return &result
}
func (w *windowsWebviewWindow) setAbsolutePosition(x int, y int) {
// Set the window's absolute position
w32.SetWindowPos(w.hwnd, 0, x, y, 0, 0, w32.SWP_NOSIZE|w32.SWP_NOZORDER)
borderSize := w.getBorderSizes()
w32.SetWindowPos(w.hwnd, 0, x-borderSize.Left, y-borderSize.Top, 0, 0, w32.SWP_NOSIZE|w32.SWP_NOZORDER)
}
func (w *windowsWebviewWindow) absolutePosition() (int, int) {
@ -482,6 +496,9 @@ func (w *windowsWebviewWindow) setRelativePosition(x int, y int) {
//x, y = w.scaleWithWindowDPI(x, y)
info := w32.GetMonitorInfoForWindow(w.hwnd)
workRect := info.RcWork
borderSize := w.getBorderSizes()
x -= borderSize.Left
y -= borderSize.Top
w32.SetWindowPos(w.hwnd, w32.HWND_TOP, int(workRect.Left)+x, int(workRect.Top)+y, 0, 0, w32.SWP_NOSIZE)
}

View file

@ -11,6 +11,7 @@ var (
moddwmapi = syscall.NewLazyDLL("dwmapi.dll")
procDwmSetWindowAttribute = moddwmapi.NewProc("DwmSetWindowAttribute")
procDwmGetWindowAttribute = moddwmapi.NewProc("DwmGetWindowAttribute")
procDwmExtendFrameIntoClientArea = moddwmapi.NewProc("DwmExtendFrameIntoClientArea")
)
@ -23,6 +24,15 @@ func DwmSetWindowAttribute(hwnd HWND, dwAttribute DWMWINDOWATTRIBUTE, pvAttribut
return HRESULT(ret)
}
func DwmGetWindowAttribute(hwnd HWND, dwAttribute DWMWINDOWATTRIBUTE, pvAttribute unsafe.Pointer, cbAttribute uintptr) HRESULT {
ret, _, _ := procDwmGetWindowAttribute.Call(
hwnd,
uintptr(dwAttribute),
uintptr(pvAttribute),
cbAttribute)
return HRESULT(ret)
}
func dwmExtendFrameIntoClientArea(hwnd uintptr, margins *MARGINS) error {
ret, _, _ := procDwmExtendFrameIntoClientArea.Call(
hwnd,