Skip to content

Commit afd1efc

Browse files
authored
Webshot Screenshot (#201)
* macos: use SF symbols in menu example. cleanup webshot example and add screenshot * README: add webshot example
1 parent 881d876 commit afd1efc

File tree

4 files changed

+45
-45
lines changed

4 files changed

+45
-45
lines changed

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,22 @@ This is all tenable for simple programs, but these are the reasons we don't *rec
8080

8181
## Examples
8282

83-
### [largetype](https://github.com/progrium/macdriver/blob/main/macos/_examples/largetype/main.go)
83+
### [largetype](./macos/_examples/largetype/main.go)
8484
A Contacts/Quicksilver-style Large Type utility in under 80 lines:
8585

86-
![largetype screenshot](https://github.com/progrium/macdriver/blob/main/macos/_examples/largetype/largetype.jpeg?raw=true)
86+
![largetype screenshot](./macos/_examples/largetype/largetype.jpeg?raw=true)
8787

88-
### [pomodoro](https://github.com/progrium/macdriver/blob/main/macos/_examples/pomodoro/main.go)
88+
### [pomodoro](./macos/_examples/pomodoro/main.go)
8989
A menu bar pomodoro timer in under 80 lines:
9090

91-
![pomodoro gif](https://github.com/progrium/macdriver/blob/main/macos/_examples/pomodoro/pomodoro.gif?raw=true)
91+
![pomodoro gif](./macos/_examples/pomodoro/pomodoro.gif?raw=true)
9292

93-
### [See all examples](https://github.com/progrium/macdriver/blob/main/macos/_examples)
93+
### [webshot](./macos/_examples/webshot/main.go)
94+
A webview PNG capture example in under 100 lines:
95+
96+
![webshot screenshot](./macos/_examples/webshot/webshot.png?raw=true)
97+
98+
### [See all examples](./macos/_examples)
9499

95100
## How it works
96101

macos/_examples/menu/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ func setMainMenu(app appkit.Application) {
7575
func setSystemBar(app appkit.Application) {
7676
item := appkit.StatusBar_SystemStatusBar().StatusItemWithLength(appkit.VariableStatusItemLength)
7777
objc.Retain(&item)
78-
item.Button().SetTitle("TestTray")
78+
img := appkit.Image_ImageWithSystemSymbolNameAccessibilityDescription("multiply.circle.fill", "A multiply symbol inside a filled circle.")
79+
item.Button().SetImage(img)
7980

8081
menu := appkit.NewMenuWithTitle("main")
8182
menu.AddItem(appkit.NewMenuItemWithAction("Hide", "h", func(sender objc.Object) { app.Hide(nil) }))

macos/_examples/webshot/main.go

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,71 +23,65 @@ func launched(app appkit.Application, delegate *appkit.ApplicationDelegate) {
2323

2424
w := appkit.NewWindowWithSize(600, 400)
2525
objc.Retain(&w)
26-
w.SetTitle("Test widgets")
27-
26+
w.SetTitle("Webshot Demo")
2827
sv := appkit.NewVerticalStackView()
2928
w.SetContentView(sv)
3029

31-
webView := webkit.NewWebView()
32-
webView.SetTranslatesAutoresizingMaskIntoConstraints(false)
33-
webView.LoadHTMLStringBaseURL(html, foundation.URLClass.URLWithString(url))
34-
sv.AddViewInGravity(webView, appkit.StackViewGravityTop)
35-
36-
snapshotButton := appkit.NewButtonWithTitle("capture")
30+
wv := webkit.NewWebView()
31+
wv.SetTranslatesAutoresizingMaskIntoConstraints(false)
32+
wv.LoadHTMLStringBaseURL(html, foundation.URL_URLWithString(url))
33+
sv.AddViewInGravity(wv, appkit.StackViewGravityTop)
3734

38-
snapshotWin := appkit.NewWindowWithSize(0, 0)
39-
objc.Retain(&snapshotWin)
40-
snapshotWin.SetTitle("Test widgets")
35+
sw := appkit.NewWindowWithSize(0, 0)
36+
objc.Retain(&sw)
37+
swv := webkit.NewWebView()
38+
swv.SetTranslatesAutoresizingMaskIntoConstraints(false)
39+
sw.SetContentView(swv)
4140

42-
snapshotWebView := webkit.NewWebView()
43-
snapshotWebView.SetTranslatesAutoresizingMaskIntoConstraints(false)
44-
snapshotWin.SetContentView(snapshotWebView)
45-
46-
navigationDelegate := &webkit.NavigationDelegate{}
47-
navigationDelegate.SetWebViewDidFinishNavigation(func(webView webkit.WebView, navigation webkit.Navigation) {
41+
navDelegate := &webkit.NavigationDelegate{}
42+
navDelegate.SetWebViewDidFinishNavigation(func(webView webkit.WebView, navigation webkit.Navigation) {
4843
dispatch.MainQueue().DispatchAsync(func() {
4944
script := `var rect = {"width":document.body.scrollWidth, "height":document.body.scrollHeight}; rect`
5045
webView.EvaluateJavaScriptCompletionHandler(script, func(value objc.Object, err foundation.Error) {
5146
rect := foundation.DictToMap[string, foundation.Number](foundation.DictionaryFrom(value.Ptr()))
5247
width := rect["width"].DoubleValue()
5348
height := rect["height"].DoubleValue()
54-
snapshotWin.SetFrameDisplay(foundation.Rect{Size: foundation.Size{Width: width, Height: height}}, true)
55-
snapshotWebView.LoadHTMLStringBaseURL(html, foundation.URLClass.URLWithString(url))
49+
sw.SetFrameDisplay(foundation.Rect{Size: foundation.Size{Width: width, Height: height}}, true)
50+
swv.LoadHTMLStringBaseURL(html, foundation.URL_URLWithString(url))
5651
})
5752
})
5853
})
59-
webView.SetNavigationDelegate(navigationDelegate)
60-
61-
ssnd := &webkit.NavigationDelegate{}
62-
ssnd.SetWebViewDidFinishNavigation(func(webView webkit.WebView, navigation webkit.Navigation) {
63-
snapshotButton.SetEnabled(true)
64-
})
65-
snapshotWebView.SetNavigationDelegate(ssnd)
54+
wv.SetNavigationDelegate(navDelegate)
6655

67-
action.Set(snapshotButton, func(sender objc.Object) {
68-
snapshotWebView.TakeSnapshotWithConfigurationCompletionHandler(nil, func(image appkit.Image, err foundation.Error) {
69-
imageRef := image.CGImageForProposedRectContextHints(nil, nil, nil)
70-
imageRepo := appkit.NewBitmapImageRepWithCGImage(imageRef)
71-
imageRepo.SetSize(image.Size())
72-
pngData := imageRepo.RepresentationUsingTypeProperties(appkit.BitmapImageFileTypePNG, nil)
56+
button := appkit.NewButtonWithTitle("capture")
7357

74-
if err := os.WriteFile("webview_screenshot.png", pngData, os.ModePerm); err != nil {
75-
fmt.Println("write image to file error:", err)
58+
snd := &webkit.NavigationDelegate{}
59+
snd.SetWebViewDidFinishNavigation(func(webView webkit.WebView, navigation webkit.Navigation) {
60+
button.SetEnabled(true)
61+
})
62+
swv.SetNavigationDelegate(snd)
63+
64+
action.Set(button, func(sender objc.Object) {
65+
swv.TakeSnapshotWithConfigurationCompletionHandler(nil, func(image appkit.Image, err foundation.Error) {
66+
imgref := image.CGImageForProposedRectContextHints(nil, nil, nil)
67+
img := appkit.NewBitmapImageRepWithCGImage(imgref)
68+
img.SetSize(image.Size())
69+
png := img.RepresentationUsingTypeProperties(appkit.BitmapImageFileTypePNG, nil)
70+
if err := os.WriteFile("webview_screenshot.png", png, os.ModePerm); err != nil {
71+
fmt.Println("image write to file error:", err)
7672
} else {
7773
fmt.Println("image captured to webview_screenshot.png")
7874
}
7975
})
8076
})
81-
snapshotButton.SetEnabled(false)
82-
83-
sv.AddViewInGravity(snapshotButton, appkit.StackViewGravityTop)
77+
button.SetEnabled(false)
78+
sv.AddViewInGravity(button, appkit.StackViewGravityTop)
8479

8580
wd := &appkit.WindowDelegate{}
8681
wd.SetWindowWillClose(func(notification foundation.Notification) {
87-
snapshotWin.Close()
82+
sw.Close()
8883
})
8984
w.SetDelegate(wd)
90-
9185
w.MakeKeyAndOrderFront(nil)
9286
w.Center()
9387

520 KB
Loading

0 commit comments

Comments
 (0)