Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
leaanthony committed May 25, 2023
0 parents commit 020fc1e
Show file tree
Hide file tree
Showing 59 changed files with 4,989 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2020 John Chadwick
Some portions Copyright (c) 2017 Serge Zaitsev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

This is a locally maintained fork of [go-webview2](https://github.com/jchv/go-webview2)
that is intended to be used with Wails applications. It is not intended to be used
as a standalone package.

----------------

# go-webview2

This is a proof of concept for embedding Webview2 into Go without CGo. It is based
on [webview/webview](https://github.com/webview/webview) and provides a compatible API.

## Notice

Because this version doesn't currently have an EdgeHTML fallback, it will not work unless you have a Webview2 runtime
installed. In addition, it requires the Webview2Loader DLL in order to function. Adding an EdgeHTML fallback should be
technically possible but will likely require much worse hacks since the API is not strictly COM to my knowledge.

## Demo

For now, you'll need to install the Webview2 runtime, as it does not ship with Windows.

[WebView2 runtime](https://developer.microsoft.com/en-us/microsoft-edge/webview2/)

After that, you should be able to run go-webview2 directly:

```
go run go-webview2/cmd/demo
```

This will use go-winloader to load an embedded copy of WebView2Loader.dll.

If this does not work, please try running from a directory that has an appropriate copy of `WebView2Loader.dll` for your
GOARCH. If _that_ worked, *please* file a bug so we can figure out what's wrong with go-winloader :)
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/wailsapp/go-webview2

go 1.20

require (
github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e
golang.org/x/sys v0.8.0
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e h1:Q3+PugElBCf4PFpxhErSzU3/PY5sFL5Z6rfv4AbGAck=
github.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e/go.mod h1:alcuEEnZsY1WQsagKhZDsoPCRoOijYqhZvPwLG0kzVs=
golang.org/x/sys v0.0.0-20200810151505-1b9f1253b3ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
157 changes: 157 additions & 0 deletions internal/w32/w32.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
//go:build windows

package w32

import (
"syscall"
"unicode/utf16"
"unsafe"

"golang.org/x/sys/windows"
)

var (
ole32 = windows.NewLazySystemDLL("ole32")
Ole32CoInitializeEx = ole32.NewProc("CoInitializeEx")

kernel32 = windows.NewLazySystemDLL("kernel32")
Kernel32GetCurrentThreadID = kernel32.NewProc("GetCurrentThreadId")

shlwapi = windows.NewLazySystemDLL("shlwapi")
shlwapiSHCreateMemStream = shlwapi.NewProc("SHCreateMemStream")

user32 = windows.NewLazySystemDLL("user32")
User32LoadImageW = user32.NewProc("LoadImageW")
User32GetSystemMetrics = user32.NewProc("GetSystemMetrics")
User32RegisterClassExW = user32.NewProc("RegisterClassExW")
User32CreateWindowExW = user32.NewProc("CreateWindowExW")
User32DestroyWindow = user32.NewProc("DestroyWindow")
User32ShowWindow = user32.NewProc("ShowWindow")
User32UpdateWindow = user32.NewProc("UpdateWindow")
User32SetFocus = user32.NewProc("SetFocus")
User32GetMessageW = user32.NewProc("GetMessageW")
User32TranslateMessage = user32.NewProc("TranslateMessage")
User32DispatchMessageW = user32.NewProc("DispatchMessageW")
User32DefWindowProcW = user32.NewProc("DefWindowProcW")
User32GetClientRect = user32.NewProc("GetClientRect")
User32PostQuitMessage = user32.NewProc("PostQuitMessage")
User32SetWindowTextW = user32.NewProc("SetWindowTextW")
User32PostThreadMessageW = user32.NewProc("PostThreadMessageW")
User32GetWindowLongPtrW = user32.NewProc("GetWindowLongPtrW")
User32SetWindowLongPtrW = user32.NewProc("SetWindowLongPtrW")
User32AdjustWindowRect = user32.NewProc("AdjustWindowRect")
User32SetWindowPos = user32.NewProc("SetWindowPos")
)

const (
SystemMetricsCxIcon = 11
SystemMetricsCyIcon = 12
)

const (
SWShow = 5
)

const (
SWPNoZOrder = 0x0004
SWPNoActivate = 0x0010
SWPNoMove = 0x0002
SWPFrameChanged = 0x0020
)

const (
WMDestroy = 0x0002
WMMove = 0x0003
WMSize = 0x0005
WMClose = 0x0010
WMQuit = 0x0012
WMGetMinMaxInfo = 0x0024
WMNCLButtonDown = 0x00A1
WMMoving = 0x0216
WMApp = 0x8000
)

const (
GWLStyle = -16
)

const (
WSOverlapped = 0x00000000
WSMaximizeBox = 0x00020000
WSThickFrame = 0x00040000
WSCaption = 0x00C00000
WSSysMenu = 0x00080000
WSMinimizeBox = 0x00020000
WSOverlappedWindow = (WSOverlapped | WSCaption | WSSysMenu | WSThickFrame | WSMinimizeBox | WSMaximizeBox)
)

type WndClassExW struct {
CbSize uint32
Style uint32
LpfnWndProc uintptr
CnClsExtra int32
CbWndExtra int32
HInstance windows.Handle
HIcon windows.Handle
HCursor windows.Handle
HbrBackground windows.Handle
LpszMenuName *uint16
LpszClassName *uint16
HIconSm windows.Handle
}

type Rect struct {
Left int32
Top int32
Right int32
Bottom int32
}

type MinMaxInfo struct {
PtReserved Point
PtMaxSize Point
PtMaxPosition Point
PtMinTrackSize Point
PtMaxTrackSize Point
}

type Point struct {
X, Y int32
}

type Msg struct {
Hwnd syscall.Handle
Message uint32
WParam uintptr
LParam uintptr
Time uint32
Pt Point
LPrivate uint32
}

func Utf16PtrToString(p *uint16) string {
if p == nil {
return ""
}
// Find NUL terminator.
end := unsafe.Pointer(p)
n := 0
for *(*uint16)(end) != 0 {
end = unsafe.Pointer(uintptr(end) + unsafe.Sizeof(*p))
n++
}
s := (*[(1 << 30) - 1]uint16)(unsafe.Pointer(p))[:n:n]
return string(utf16.Decode(s))
}

func SHCreateMemStream(data []byte) (uintptr, error) {
ret, _, err := shlwapiSHCreateMemStream.Call(
uintptr(unsafe.Pointer(&data[0])),
uintptr(len(data)),
)
if ret == 0 {
return 0, err
}

return ret, nil
}
Loading

0 comments on commit 020fc1e

Please sign in to comment.