Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webshot Screenshot #201

Merged
merged 2 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,22 @@ This is all tenable for simple programs, but these are the reasons we don't *rec

## Examples

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

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

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

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

### [See all examples](https://github.com/progrium/macdriver/blob/main/macos/_examples)
### [webshot](./macos/_examples/webshot/main.go)
A webview PNG capture example in under 100 lines:

![webshot screenshot](./macos/_examples/webshot/webshot.png?raw=true)

### [See all examples](./macos/_examples)

## How it works

Expand Down
3 changes: 2 additions & 1 deletion macos/_examples/menu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ func setMainMenu(app appkit.Application) {
func setSystemBar(app appkit.Application) {
item := appkit.StatusBar_SystemStatusBar().StatusItemWithLength(appkit.VariableStatusItemLength)
objc.Retain(&item)
item.Button().SetTitle("TestTray")
img := appkit.Image_ImageWithSystemSymbolNameAccessibilityDescription("multiply.circle.fill", "A multiply symbol inside a filled circle.")
item.Button().SetImage(img)

menu := appkit.NewMenuWithTitle("main")
menu.AddItem(appkit.NewMenuItemWithAction("Hide", "h", func(sender objc.Object) { app.Hide(nil) }))
Expand Down
72 changes: 33 additions & 39 deletions macos/_examples/webshot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,71 +23,65 @@ func launched(app appkit.Application, delegate *appkit.ApplicationDelegate) {

w := appkit.NewWindowWithSize(600, 400)
objc.Retain(&w)
w.SetTitle("Test widgets")

w.SetTitle("Webshot Demo")
sv := appkit.NewVerticalStackView()
w.SetContentView(sv)

webView := webkit.NewWebView()
webView.SetTranslatesAutoresizingMaskIntoConstraints(false)
webView.LoadHTMLStringBaseURL(html, foundation.URLClass.URLWithString(url))
sv.AddViewInGravity(webView, appkit.StackViewGravityTop)

snapshotButton := appkit.NewButtonWithTitle("capture")
wv := webkit.NewWebView()
wv.SetTranslatesAutoresizingMaskIntoConstraints(false)
wv.LoadHTMLStringBaseURL(html, foundation.URL_URLWithString(url))
sv.AddViewInGravity(wv, appkit.StackViewGravityTop)

snapshotWin := appkit.NewWindowWithSize(0, 0)
objc.Retain(&snapshotWin)
snapshotWin.SetTitle("Test widgets")
sw := appkit.NewWindowWithSize(0, 0)
objc.Retain(&sw)
swv := webkit.NewWebView()
swv.SetTranslatesAutoresizingMaskIntoConstraints(false)
sw.SetContentView(swv)

snapshotWebView := webkit.NewWebView()
snapshotWebView.SetTranslatesAutoresizingMaskIntoConstraints(false)
snapshotWin.SetContentView(snapshotWebView)

navigationDelegate := &webkit.NavigationDelegate{}
navigationDelegate.SetWebViewDidFinishNavigation(func(webView webkit.WebView, navigation webkit.Navigation) {
navDelegate := &webkit.NavigationDelegate{}
navDelegate.SetWebViewDidFinishNavigation(func(webView webkit.WebView, navigation webkit.Navigation) {
dispatch.MainQueue().DispatchAsync(func() {
script := `var rect = {"width":document.body.scrollWidth, "height":document.body.scrollHeight}; rect`
webView.EvaluateJavaScriptCompletionHandler(script, func(value objc.Object, err foundation.Error) {
rect := foundation.DictToMap[string, foundation.Number](foundation.DictionaryFrom(value.Ptr()))
width := rect["width"].DoubleValue()
height := rect["height"].DoubleValue()
snapshotWin.SetFrameDisplay(foundation.Rect{Size: foundation.Size{Width: width, Height: height}}, true)
snapshotWebView.LoadHTMLStringBaseURL(html, foundation.URLClass.URLWithString(url))
sw.SetFrameDisplay(foundation.Rect{Size: foundation.Size{Width: width, Height: height}}, true)
swv.LoadHTMLStringBaseURL(html, foundation.URL_URLWithString(url))
})
})
})
webView.SetNavigationDelegate(navigationDelegate)

ssnd := &webkit.NavigationDelegate{}
ssnd.SetWebViewDidFinishNavigation(func(webView webkit.WebView, navigation webkit.Navigation) {
snapshotButton.SetEnabled(true)
})
snapshotWebView.SetNavigationDelegate(ssnd)
wv.SetNavigationDelegate(navDelegate)

action.Set(snapshotButton, func(sender objc.Object) {
snapshotWebView.TakeSnapshotWithConfigurationCompletionHandler(nil, func(image appkit.Image, err foundation.Error) {
imageRef := image.CGImageForProposedRectContextHints(nil, nil, nil)
imageRepo := appkit.NewBitmapImageRepWithCGImage(imageRef)
imageRepo.SetSize(image.Size())
pngData := imageRepo.RepresentationUsingTypeProperties(appkit.BitmapImageFileTypePNG, nil)
button := appkit.NewButtonWithTitle("capture")

if err := os.WriteFile("webview_screenshot.png", pngData, os.ModePerm); err != nil {
fmt.Println("write image to file error:", err)
snd := &webkit.NavigationDelegate{}
snd.SetWebViewDidFinishNavigation(func(webView webkit.WebView, navigation webkit.Navigation) {
button.SetEnabled(true)
})
swv.SetNavigationDelegate(snd)

action.Set(button, func(sender objc.Object) {
swv.TakeSnapshotWithConfigurationCompletionHandler(nil, func(image appkit.Image, err foundation.Error) {
imgref := image.CGImageForProposedRectContextHints(nil, nil, nil)
img := appkit.NewBitmapImageRepWithCGImage(imgref)
img.SetSize(image.Size())
png := img.RepresentationUsingTypeProperties(appkit.BitmapImageFileTypePNG, nil)
if err := os.WriteFile("webview_screenshot.png", png, os.ModePerm); err != nil {
fmt.Println("image write to file error:", err)
} else {
fmt.Println("image captured to webview_screenshot.png")
}
})
})
snapshotButton.SetEnabled(false)

sv.AddViewInGravity(snapshotButton, appkit.StackViewGravityTop)
button.SetEnabled(false)
sv.AddViewInGravity(button, appkit.StackViewGravityTop)

wd := &appkit.WindowDelegate{}
wd.SetWindowWillClose(func(notification foundation.Notification) {
snapshotWin.Close()
sw.Close()
})
w.SetDelegate(wd)

w.MakeKeyAndOrderFront(nil)
w.Center()

Expand Down
Binary file added macos/_examples/webshot/webshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading