Initial take on webview popover #261

This commit is contained in:
Alex Mazanov 2022-01-13 18:46:35 -08:00
parent d1e4fe6714
commit 447bf60567
No known key found for this signature in database
GPG Key ID: FD35C3C7C1D34AB4
4 changed files with 66 additions and 1 deletions

View File

@ -114,6 +114,7 @@
FA57F2A9272A309600817A79 /* Intents.intentdefinition in Sources */ = {isa = PBXBuildFile; fileRef = FAA14A0D2728C65D0052FDB8 /* Intents.intentdefinition */; };
FA64C9DF25CF691D00C4E5C5 /* GeneralPreferencesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA64C9DE25CF691D00C4E5C5 /* GeneralPreferencesView.swift */; };
FA64C9E225CF696D00C4E5C5 /* PluginsPreferencesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA64C9E125CF696D00C4E5C5 /* PluginsPreferencesView.swift */; };
FA69F5BC2791145E00D6DDC6 /* WebView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA69F5BB2791145E00D6DDC6 /* WebView.swift */; };
FA8518CC258DAAAB008AD21C /* Binding+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA8518CB258DAAAB008AD21C /* Binding+Extension.swift */; };
FA8518F52598E786008AD21C /* SwifCron in Frameworks */ = {isa = PBXBuildFile; productRef = FA8518F42598E786008AD21C /* SwifCron */; };
FAA14A0E2728C65D0052FDB8 /* Intents.intentdefinition in Sources */ = {isa = PBXBuildFile; fileRef = FAA14A0D2728C65D0052FDB8 /* Intents.intentdefinition */; };
@ -205,6 +206,7 @@
FA60506726B593FA001A939A /* ru */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ru; path = ru.lproj/Localizable.strings; sourceTree = "<group>"; };
FA64C9DE25CF691D00C4E5C5 /* GeneralPreferencesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeneralPreferencesView.swift; sourceTree = "<group>"; };
FA64C9E125CF696D00C4E5C5 /* PluginsPreferencesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PluginsPreferencesView.swift; sourceTree = "<group>"; };
FA69F5BB2791145E00D6DDC6 /* WebView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebView.swift; sourceTree = "<group>"; };
FA8518CB258DAAAB008AD21C /* Binding+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Binding+Extension.swift"; sourceTree = "<group>"; };
FAA14A0D2728C65D0052FDB8 /* Intents.intentdefinition */ = {isa = PBXFileReference; lastKnownFileType = file.intentdefinition; path = Intents.intentdefinition; sourceTree = "<group>"; };
FAA14A102728C6E90052FDB8 /* EnablePluginIntentHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnablePluginIntentHandler.swift; sourceTree = "<group>"; };
@ -326,6 +328,7 @@
3962572D2569949200341A19 /* Plugin Repository */,
39641EA0254DD32100713DAF /* AboutPluginView.swift */,
398DAE7925537DAF00747D90 /* PluginErrorView.swift */,
FA69F5BB2791145E00D6DDC6 /* WebView.swift */,
);
path = UI;
sourceTree = "<group>";
@ -746,6 +749,7 @@
39224DD225F3F19B00BABF21 /* AppDelegate+Toolbar.swift in Sources */,
39641EAD254E0E3400713DAF /* ImageLoader.swift in Sources */,
3990DCE12602BB5B002CDCD5 /* PluginDebugInfo.swift in Sources */,
FA69F5BC2791145E00D6DDC6 /* WebView.swift in Sources */,
39B64C3F26152F5E00B7FA63 /* XbarAPI.swift in Sources */,
FAD571A325A0071700623B57 /* StreamablePlugin.swift in Sources */,
39641EA5254E096200713DAF /* PreferencesView.swift in Sources */,

View File

@ -36,6 +36,7 @@ class MenubarItem: NSObject {
private var aboutPopover = NSPopover()
private var errorPopover = NSPopover()
private var webPopover = NSPopover()
private var popoverDismissMonitor: Any?
private let popoverDismissEventMask: NSEvent.EventTypeMask = [.leftMouseDown, .rightMouseDown]
@ -350,6 +351,21 @@ extension MenubarItem {
stopPopupMonitor()
}
func showWebPopover(url: URL, widht: CGFloat, height: CGFloat) {
let urlRequest = URLRequest(url: url)
webPopover.behavior = .transient
webPopover.contentViewController = NSHostingController(rootView: WebPanelView(request: urlRequest))
webPopover.contentSize = NSSize(width: widht, height: height)
webPopover.show(relativeTo: barItem.button!.bounds, of: barItem.button!, preferredEdge: .minY)
webPopover.contentViewController?.view.window?.becomeKey()
startPopupMonitor()
}
func hideWebPopover(_ sender: AnyObject?) {
webPopover.performClose(sender)
stopPopupMonitor()
}
func popoverHideHandler(_ event: NSEvent?) {
if aboutPopover.isShown {
hideAboutPopover(event)
@ -358,6 +374,10 @@ extension MenubarItem {
if errorPopover.isShown {
hideErrorPopover(event)
}
if webPopover.isShown {
hideWebPopover(event)
}
}
@objc func aboutSwiftBar() {
@ -676,7 +696,12 @@ extension MenubarItem {
}
if let href = params.href, let url = URL(string: href) {
NSWorkspace.shared.open(url)
if params.webView {
showWebPopover(url: url, widht: params.webViewWidth, height: params.webViewHeight)
} else {
NSWorkspace.shared.open(url)
}
out = true
return out
}

View File

@ -180,6 +180,20 @@ struct MenuLineParameters {
params["tooltip"]
}
var webView: Bool {
params["webview"] == "true"
}
var webViewHeight: CGFloat {
guard let sizeStr = params["webviewh"], let pSize = Int(sizeStr) else { return 400 }
return CGFloat(pSize)
}
var webViewWidth: CGFloat {
guard let sizeStr = params["webvieww"], let pSize = Int(sizeStr) else { return 500 }
return CGFloat(pSize)
}
var shortcut: KeyCombo? {
guard let shortcut = params["shortcut"] else { return nil }
var modifiers: NSEvent.ModifierFlags = []

22
SwiftBar/UI/WebView.swift Normal file
View File

@ -0,0 +1,22 @@
import SwiftUI
import WebKit
struct WebView: NSViewRepresentable {
let request: URLRequest
func makeNSView(context _: Context) -> WKWebView {
WKWebView()
}
func updateNSView(_ uiView: WKWebView, context _: Context) {
uiView.load(request)
}
}
struct WebPanelView: View {
let request: URLRequest
var body: some View {
VStack {
WebView(request: request)
}
}
}