-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebView.swift
More file actions
48 lines (37 loc) · 1.66 KB
/
WebView.swift
File metadata and controls
48 lines (37 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import SwiftUI
import WebKit
final class WebViewStore: ObservableObject {
let webView: WKWebView = WKWebView(frame: .zero)
@Published var urlString: String = "https://www.google.com"
func load() {
guard let url = URL(string: urlString.trimmingCharacters(in: .whitespacesAndNewlines)) else { return }
webView.load(URLRequest(url: url))
}
func goBack() { if webView.canGoBack { webView.goBack() } }
func goForward() { if webView.canGoForward { webView.goForward() } }
func reload() { webView.reload() }
func snapshotToDesktop() {
let config = WKSnapshotConfiguration()
config.afterScreenUpdates = true
webView.takeSnapshot(with: config) { image, error in
guard error == nil, let image = image else { return }
guard let tiff = image.tiffRepresentation,
let rep = NSBitmapImageRep(data: tiff),
let png = rep.representation(using: .png, properties: [:]) else { return }
let desktop = FileManager.default.urls(for: .desktopDirectory, in: .userDomainMask).first!
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd_HH-mm-ss"
let name = "FloatingBrowser_\(formatter.string(from: Date())).png"
let fileURL = desktop.appendingPathComponent(name)
try? png.write(to: fileURL)
NSSound(named: "Glass")?.play()
}
}
}
struct WebViewRepresentable: NSViewRepresentable {
@ObservedObject var store: WebViewStore
func makeNSView(context: Context) -> WKWebView {
store.webView
}
func updateNSView(_ nsView: WKWebView, context: Context) {}
}