Skip to content

Commit ae3fc72

Browse files
authored
icon & center & debug (jchv#43)
1 parent 20bee3c commit ae3fc72

File tree

4 files changed

+69
-9
lines changed

4 files changed

+69
-9
lines changed

cmd/demo/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ func main() {
1414
Title: "Minimal webview example",
1515
Width: 800,
1616
Height: 600,
17+
IconId: 2, // icon resource id
18+
Center: true,
1719
},
1820
})
1921
if w == nil {

internal/w32/w32.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,27 @@ var (
4343
User32GetAncestor = user32.NewProc("GetAncestor")
4444
)
4545

46+
const (
47+
SM_CXSCREEN = 0
48+
SM_CYSCREEN = 1
49+
)
50+
51+
const (
52+
CW_USEDEFAULT = 0x80000000
53+
)
54+
55+
const (
56+
LR_DEFAULTCOLOR = 0x0000
57+
LR_MONOCHROME = 0x0001
58+
LR_LOADFROMFILE = 0x0010
59+
LR_LOADTRANSPARENT = 0x0020
60+
LR_DEFAULTSIZE = 0x0040
61+
LR_VGACOLOR = 0x0080
62+
LR_LOADMAP3DCOLORS = 0x1000
63+
LR_CREATEDIBSECTION = 0x2000
64+
LR_SHARED = 0x8000
65+
)
66+
4667
const (
4768
SystemMetricsCxIcon = 11
4869
SystemMetricsCyIcon = 12

pkg/edge/chromium.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ type Chromium struct {
3131
environment *ICoreWebView2Environment
3232

3333
// Settings
34-
Debug bool
3534
DataPath string
3635

3736
// permissions

webview.go

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ type WindowOptions struct {
6161
Title string
6262
Width uint
6363
Height uint
64+
IconId uint
65+
Center bool
6466
}
6567

6668
type WebViewOptions struct {
@@ -98,7 +100,6 @@ func NewWithOptions(options WebViewOptions) WebView {
98100

99101
chromium := edge.NewChromium()
100102
chromium.MessageCallback = w.msgcb
101-
chromium.Debug = options.Debug
102103
chromium.DataPath = options.DataPath
103104
chromium.SetPermission(edge.CoreWebView2PermissionKindClipboardRead, edge.CoreWebView2PermissionStateAllow)
104105

@@ -107,6 +108,22 @@ func NewWithOptions(options WebViewOptions) WebView {
107108
if !w.CreateWithOptions(options.WindowOptions) {
108109
return nil
109110
}
111+
112+
settings, err := chromium.GetSettings()
113+
if err != nil {
114+
log.Fatal(err)
115+
}
116+
// disable context menu
117+
err = settings.PutAreDefaultContextMenusEnabled(options.Debug)
118+
if err != nil {
119+
log.Fatal(err)
120+
}
121+
// disable developer tools
122+
err = settings.PutAreDevToolsEnabled(options.Debug)
123+
if err != nil {
124+
log.Fatal(err)
125+
}
126+
110127
return w
111128
}
112129

@@ -252,10 +269,16 @@ func (w *webview) CreateWithOptions(opts WindowOptions) bool {
252269
var hinstance windows.Handle
253270
_ = windows.GetModuleHandleEx(0, nil, &hinstance)
254271

255-
icow, _, _ := w32.User32GetSystemMetrics.Call(w32.SystemMetricsCxIcon)
256-
icoh, _, _ := w32.User32GetSystemMetrics.Call(w32.SystemMetricsCyIcon)
257-
258-
icon, _, _ := w32.User32LoadImageW.Call(uintptr(hinstance), 32512, icow, icoh, 0)
272+
var icon uintptr
273+
if opts.IconId == 0 {
274+
// load default icon
275+
icow, _, _ := w32.User32GetSystemMetrics.Call(w32.SystemMetricsCxIcon)
276+
icoh, _, _ := w32.User32GetSystemMetrics.Call(w32.SystemMetricsCyIcon)
277+
icon, _, _ = w32.User32LoadImageW.Call(uintptr(hinstance), 32512, icow, icoh, 0)
278+
} else {
279+
// load icon from resource
280+
icon, _, _ = w32.User32LoadImageW.Call(uintptr(hinstance), uintptr(opts.IconId), 1, 0, 0, w32.LR_DEFAULTSIZE|w32.LR_SHARED)
281+
}
259282

260283
className, _ := windows.UTF16PtrFromString("webview")
261284
wc := w32.WndClassExW{
@@ -278,13 +301,28 @@ func (w *webview) CreateWithOptions(opts WindowOptions) bool {
278301
if windowHeight == 0 {
279302
windowHeight = 480
280303
}
304+
305+
var posX, posY uint
306+
if opts.Center {
307+
// get screen size
308+
screenWidth, _, _ := w32.User32GetSystemMetrics.Call(w32.SM_CXSCREEN)
309+
screenHeight, _, _ := w32.User32GetSystemMetrics.Call(w32.SM_CYSCREEN)
310+
// calculate window position
311+
posX = (uint(screenWidth) - windowWidth) / 2
312+
posY = (uint(screenHeight) - windowHeight) / 2
313+
} else {
314+
// use default position
315+
posX = w32.CW_USEDEFAULT
316+
posY = w32.CW_USEDEFAULT
317+
}
318+
281319
w.hwnd, _, _ = w32.User32CreateWindowExW.Call(
282320
0,
283321
uintptr(unsafe.Pointer(className)),
284322
uintptr(unsafe.Pointer(windowName)),
285-
0xCF0000, // WS_OVERLAPPEDWINDOW
286-
0x80000000, // CW_USEDEFAULT
287-
0x80000000, // CW_USEDEFAULT
323+
0xCF0000, // WS_OVERLAPPEDWINDOW
324+
uintptr(posX),
325+
uintptr(posY),
288326
uintptr(windowWidth),
289327
uintptr(windowHeight),
290328
0,

0 commit comments

Comments
 (0)