detached 5438b70

examples/window: add macOS and Windows window example (#227)

commit
5438b70b69e03bc9e65b674af42a4a2bc594e50d
author
TotallyGamerJet · · 2024-04-06 12:53 UTC
committer
GitHub · · 2024-04-06 12:53 UTC
parent
f88cc4c
signature
GPG UnverifiedB5690EEEBB952194 The key is unknown
2 files · +204 -0
A examples/window/main_darwin.go +60 -0
@@ -0,0 +1,60 @@
1+// SPDX-License-Identifier: Apache-2.0
2+// SPDX-FileCopyrightText: 2024 The Ebitengine Authors
3+
4+package main
5+
6+import (
7+ "runtime"
8+
9+ "github.com/ebitengine/purego"
10+ "github.com/ebitengine/purego/objc"
11+)
12+
13+const (
14+ NSApplicationActivationPolicyRegular = 0
15+ NSWindowStyleMaskTitled = 1 << 0
16+ NSBackingStoreBuffered = 2
17+)
18+
19+type NSPoint struct {
20+ X, Y float64
21+}
22+
23+type NSSize struct {
24+ Width, Height float64
25+}
26+
27+type NSRect struct {
28+ Origin NSPoint
29+ Size NSSize
30+}
31+
32+func init() {
33+ runtime.LockOSThread()
34+}
35+
36+func main() {
37+ if _, err := purego.Dlopen("/System/Library/Frameworks/Cocoa.framework/Cocoa", purego.RTLD_GLOBAL|purego.RTLD_LAZY); err != nil {
38+ panic(err)
39+ }
40+ nsApp := objc.ID(objc.GetClass("NSApplication")).Send(objc.RegisterName("sharedApplication"))
41+ nsApp.Send(objc.RegisterName("setActivationPolicy:"), NSApplicationActivationPolicyRegular)
42+ wnd := objc.ID(objc.GetClass("NSWindow")).Send(objc.RegisterName("alloc"))
43+ wnd = wnd.Send(objc.RegisterName("initWithContentRect:styleMask:backing:defer:"),
44+ NSMakeRect(0, 0, 320, 240),
45+ NSWindowStyleMaskTitled,
46+ NSBackingStoreBuffered,
47+ false,
48+ )
49+
50+ title := objc.ID(objc.GetClass("NSString")).Send(objc.RegisterName("stringWithUTF8String:"), "My Title")
51+ wnd.Send(objc.RegisterName("setTitle:"), title)
52+ wnd.Send(objc.RegisterName("makeKeyAndOrderFront:"), objc.ID(0))
53+ wnd.Send(objc.RegisterName("center"))
54+ nsApp.Send(objc.RegisterName("activateIgnoringOtherApps:"), true)
55+ nsApp.Send(objc.RegisterName("run"))
56+}
57+
58+func NSMakeRect(x, y, width, height float64) NSRect {
59+ return NSRect{Origin: NSPoint{x, y}, Size: NSSize{width, height}}
60+}
@@ -0,0 +1,60 @@
1+// SPDX-License-Identifier: Apache-2.0
2+// SPDX-FileCopyrightText: 2024 The Ebitengine Authors
3+
4+package main
5+
6+import (
7+ "runtime"
8+
9+ "github.com/ebitengine/purego"
10+ "github.com/ebitengine/purego/objc"
11+)
12+
13+const (
14+ NSApplicationActivationPolicyRegular = 0
15+ NSWindowStyleMaskTitled = 1 << 0
16+ NSBackingStoreBuffered = 2
17+)
18+
19+type NSPoint struct {
20+ X, Y float64
21+}
22+
23+type NSSize struct {
24+ Width, Height float64
25+}
26+
27+type NSRect struct {
28+ Origin NSPoint
29+ Size NSSize
30+}
31+
32+func init() {
33+ runtime.LockOSThread()
34+}
35+
36+func main() {
37+ if _, err := purego.Dlopen("/System/Library/Frameworks/Cocoa.framework/Cocoa", purego.RTLD_GLOBAL|purego.RTLD_LAZY); err != nil {
38+ panic(err)
39+ }
40+ nsApp := objc.ID(objc.GetClass("NSApplication")).Send(objc.RegisterName("sharedApplication"))
41+ nsApp.Send(objc.RegisterName("setActivationPolicy:"), NSApplicationActivationPolicyRegular)
42+ wnd := objc.ID(objc.GetClass("NSWindow")).Send(objc.RegisterName("alloc"))
43+ wnd = wnd.Send(objc.RegisterName("initWithContentRect:styleMask:backing:defer:"),
44+ NSMakeRect(0, 0, 320, 240),
45+ NSWindowStyleMaskTitled,
46+ NSBackingStoreBuffered,
47+ false,
48+ )
49+
50+ title := objc.ID(objc.GetClass("NSString")).Send(objc.RegisterName("stringWithUTF8String:"), "My Title")
51+ wnd.Send(objc.RegisterName("setTitle:"), title)
52+ wnd.Send(objc.RegisterName("makeKeyAndOrderFront:"), objc.ID(0))
53+ wnd.Send(objc.RegisterName("center"))
54+ nsApp.Send(objc.RegisterName("activateIgnoringOtherApps:"), true)
55+ nsApp.Send(objc.RegisterName("run"))
56+}
57+
58+func NSMakeRect(x, y, width, height float64) NSRect {
59+ return NSRect{Origin: NSPoint{x, y}, Size: NSSize{width, height}}
60+}
A examples/window/main_windows.go +144 -0
@@ -0,0 +1,144 @@
1+// SPDX-License-Identifier: Apache-2.0
2+// SPDX-FileCopyrightText: 2024 The Ebitengine Authors
3+
4+package main
5+
6+import (
7+ "runtime"
8+ "unsafe"
9+
10+ "golang.org/x/sys/windows"
11+
12+ "github.com/ebitengine/purego"
13+)
14+
15+const (
16+ WS_OVERLAPPEDWINDOW = 0x00000000 | 0x00C00000 | 0x00080000 | 0x00040000 | 0x00020000 | 0x00010000
17+ CW_USEDEFAULT = ^0x7fffffff
18+ SW_SHOW = 5
19+ WM_DESTROY = 2
20+)
21+
22+type (
23+ ATOM uint16
24+ HANDLE uintptr
25+ HINSTANCE HANDLE
26+ HICON HANDLE
27+ HCURSOR HANDLE
28+ HBRUSH HANDLE
29+ HWND HANDLE
30+ HMENU HANDLE
31+)
32+
33+type WNDCLASSEX struct {
34+ Size uint32
35+ Style uint32
36+ WndProc uintptr
37+ ClsExtra int32
38+ WndExtra int32
39+ Instance HINSTANCE
40+ Icon HICON
41+ Cursor HCURSOR
42+ Background HBRUSH
43+ MenuName *uint16
44+ ClassName *uint16
45+ IconSm HICON
46+}
47+
48+type RECT struct {
49+ Left, Top, Right, Bottom int32
50+}
51+
52+type POINT struct {
53+ X, Y int32
54+}
55+
56+type MSG struct {
57+ Hwnd HWND
58+ Message uint32
59+ WParam uintptr
60+ LParam uintptr
61+ Time uint32
62+ Pt POINT
63+}
64+
65+var (
66+ GetModuleHandle func(modulename *uint16) HINSTANCE
67+ RegisterClassEx func(w *WNDCLASSEX) ATOM
68+ CreateWindowEx func(exStyle uint, className, windowName *uint16,
69+ style uint, x, y, width, height int, parent HWND, menu HMENU,
70+ instance HINSTANCE, param unsafe.Pointer) HWND
71+ AdjustWindowRect func(rect *RECT, style uint, menu bool) bool
72+ ShowWindow func(hwnd HWND, cmdshow int) bool
73+ GetMessage func(msg *MSG, hwnd HWND, msgFilterMin, msgFilterMax uint32) int
74+ TranslateMessage func(msg *MSG) bool
75+ DispatchMessage func(msg *MSG) uintptr
76+ DefWindowProc func(hwnd HWND, msg uint32, wParam, lParam uintptr) uintptr
77+ PostQuitMessage func(exitCode int)
78+)
79+
80+func init() {
81+ kernel32 := windows.NewLazySystemDLL("kernel32.dll").Handle()
82+ purego.RegisterLibFunc(&GetModuleHandle, kernel32, "GetModuleHandleW")
83+
84+ user32 := windows.NewLazySystemDLL("user32.dll").Handle()
85+ purego.RegisterLibFunc(&RegisterClassEx, user32, "RegisterClassExW")
86+ purego.RegisterLibFunc(&CreateWindowEx, user32, "CreateWindowExW")
87+ purego.RegisterLibFunc(&AdjustWindowRect, user32, "AdjustWindowRect")
88+ purego.RegisterLibFunc(&ShowWindow, user32, "ShowWindow")
89+ purego.RegisterLibFunc(&GetMessage, user32, "GetMessageW")
90+ purego.RegisterLibFunc(&TranslateMessage, user32, "TranslateMessage")
91+ purego.RegisterLibFunc(&DispatchMessage, user32, "DispatchMessageW")
92+ purego.RegisterLibFunc(&DefWindowProc, user32, "DefWindowProcW")
93+ purego.RegisterLibFunc(&PostQuitMessage, user32, "PostQuitMessage")
94+
95+ runtime.LockOSThread()
96+}
97+
98+func main() {
99+ className := windows.StringToUTF16Ptr("Sample Window Class")
100+ inst := GetModuleHandle(className)
101+
102+ wc := WNDCLASSEX{
103+ Size: uint32(unsafe.Sizeof(WNDCLASSEX{})),
104+ WndProc: windows.NewCallback(wndProc),
105+ Instance: inst,
106+ ClassName: className,
107+ }
108+
109+ RegisterClassEx(&wc)
110+
111+ wr := RECT{
112+ Left: 0,
113+ Top: 0,
114+ Right: 320,
115+ Bottom: 240,
116+ }
117+ AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, false)
118+ hwnd := CreateWindowEx(
119+ 0, className,
120+ windows.StringToUTF16Ptr("My Title"),
121+ WS_OVERLAPPEDWINDOW,
122+ CW_USEDEFAULT, CW_USEDEFAULT, int(wr.Right-wr.Left), int(wr.Bottom-wr.Top),
123+ 0, 0, inst, nil,
124+ )
125+ if hwnd == 0 {
126+ panic(windows.GetLastError())
127+ }
128+
129+ ShowWindow(hwnd, SW_SHOW)
130+
131+ var msg MSG
132+ for GetMessage(&msg, 0, 0, 0) != 0 {
133+ TranslateMessage(&msg)
134+ DispatchMessage(&msg)
135+ }
136+}
137+
138+func wndProc(hwnd HWND, msg uint32, wparam, lparam uintptr) uintptr {
139+ switch msg {
140+ case WM_DESTROY:
141+ PostQuitMessage(0)
142+ }
143+ return DefWindowProc(hwnd, msg, wparam, lparam)
144+}
@@ -0,0 +1,144 @@
1+// SPDX-License-Identifier: Apache-2.0
2+// SPDX-FileCopyrightText: 2024 The Ebitengine Authors
3+
4+package main
5+
6+import (
7+ "runtime"
8+ "unsafe"
9+
10+ "golang.org/x/sys/windows"
11+
12+ "github.com/ebitengine/purego"
13+)
14+
15+const (
16+ WS_OVERLAPPEDWINDOW = 0x00000000 | 0x00C00000 | 0x00080000 | 0x00040000 | 0x00020000 | 0x00010000
17+ CW_USEDEFAULT = ^0x7fffffff
18+ SW_SHOW = 5
19+ WM_DESTROY = 2
20+)
21+
22+type (
23+ ATOM uint16
24+ HANDLE uintptr
25+ HINSTANCE HANDLE
26+ HICON HANDLE
27+ HCURSOR HANDLE
28+ HBRUSH HANDLE
29+ HWND HANDLE
30+ HMENU HANDLE
31+)
32+
33+type WNDCLASSEX struct {
34+ Size uint32
35+ Style uint32
36+ WndProc uintptr
37+ ClsExtra int32
38+ WndExtra int32
39+ Instance HINSTANCE
40+ Icon HICON
41+ Cursor HCURSOR
42+ Background HBRUSH
43+ MenuName *uint16
44+ ClassName *uint16
45+ IconSm HICON
46+}
47+
48+type RECT struct {
49+ Left, Top, Right, Bottom int32
50+}
51+
52+type POINT struct {
53+ X, Y int32
54+}
55+
56+type MSG struct {
57+ Hwnd HWND
58+ Message uint32
59+ WParam uintptr
60+ LParam uintptr
61+ Time uint32
62+ Pt POINT
63+}
64+
65+var (
66+ GetModuleHandle func(modulename *uint16) HINSTANCE
67+ RegisterClassEx func(w *WNDCLASSEX) ATOM
68+ CreateWindowEx func(exStyle uint, className, windowName *uint16,
69+ style uint, x, y, width, height int, parent HWND, menu HMENU,
70+ instance HINSTANCE, param unsafe.Pointer) HWND
71+ AdjustWindowRect func(rect *RECT, style uint, menu bool) bool
72+ ShowWindow func(hwnd HWND, cmdshow int) bool
73+ GetMessage func(msg *MSG, hwnd HWND, msgFilterMin, msgFilterMax uint32) int
74+ TranslateMessage func(msg *MSG) bool
75+ DispatchMessage func(msg *MSG) uintptr
76+ DefWindowProc func(hwnd HWND, msg uint32, wParam, lParam uintptr) uintptr
77+ PostQuitMessage func(exitCode int)
78+)
79+
80+func init() {
81+ kernel32 := windows.NewLazySystemDLL("kernel32.dll").Handle()
82+ purego.RegisterLibFunc(&GetModuleHandle, kernel32, "GetModuleHandleW")
83+
84+ user32 := windows.NewLazySystemDLL("user32.dll").Handle()
85+ purego.RegisterLibFunc(&RegisterClassEx, user32, "RegisterClassExW")
86+ purego.RegisterLibFunc(&CreateWindowEx, user32, "CreateWindowExW")
87+ purego.RegisterLibFunc(&AdjustWindowRect, user32, "AdjustWindowRect")
88+ purego.RegisterLibFunc(&ShowWindow, user32, "ShowWindow")
89+ purego.RegisterLibFunc(&GetMessage, user32, "GetMessageW")
90+ purego.RegisterLibFunc(&TranslateMessage, user32, "TranslateMessage")
91+ purego.RegisterLibFunc(&DispatchMessage, user32, "DispatchMessageW")
92+ purego.RegisterLibFunc(&DefWindowProc, user32, "DefWindowProcW")
93+ purego.RegisterLibFunc(&PostQuitMessage, user32, "PostQuitMessage")
94+
95+ runtime.LockOSThread()
96+}
97+
98+func main() {
99+ className := windows.StringToUTF16Ptr("Sample Window Class")
100+ inst := GetModuleHandle(className)
101+
102+ wc := WNDCLASSEX{
103+ Size: uint32(unsafe.Sizeof(WNDCLASSEX{})),
104+ WndProc: windows.NewCallback(wndProc),
105+ Instance: inst,
106+ ClassName: className,
107+ }
108+
109+ RegisterClassEx(&wc)
110+
111+ wr := RECT{
112+ Left: 0,
113+ Top: 0,
114+ Right: 320,
115+ Bottom: 240,
116+ }
117+ AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, false)
118+ hwnd := CreateWindowEx(
119+ 0, className,
120+ windows.StringToUTF16Ptr("My Title"),
121+ WS_OVERLAPPEDWINDOW,
122+ CW_USEDEFAULT, CW_USEDEFAULT, int(wr.Right-wr.Left), int(wr.Bottom-wr.Top),
123+ 0, 0, inst, nil,
124+ )
125+ if hwnd == 0 {
126+ panic(windows.GetLastError())
127+ }
128+
129+ ShowWindow(hwnd, SW_SHOW)
130+
131+ var msg MSG
132+ for GetMessage(&msg, 0, 0, 0) != 0 {
133+ TranslateMessage(&msg)
134+ DispatchMessage(&msg)
135+ }
136+}
137+
138+func wndProc(hwnd HWND, msg uint32, wparam, lparam uintptr) uintptr {
139+ switch msg {
140+ case WM_DESTROY:
141+ PostQuitMessage(0)
142+ }
143+ return DefWindowProc(hwnd, msg, wparam, lparam)
144+}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 102

↑↓ moveenter openesc close