Files
NetNewsWire/Mac/MainWindow/Detail/DetailWebView.swift
Stuart Breckenridge e2eeed8f99 Target macOS 13
• `xcconfig` `MACOSX_DEPLOYMENT_TARGET` updated to 13.0
• Removed `@available` annotations for macOS < 13.0
• Removed for Big Sur fixes.

This has been built and doesn’t trigger any build settings should be `xcconfig` options.
2023-05-30 09:15:08 +08:00

103 lines
2.6 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// DetailWebView.swift
// NetNewsWire
//
// Created by Brent Simmons on 2/10/18.
// Copyright © 2018 Ranchero Software. All rights reserved.
//
import AppKit
import WebKit
import RSCore
@MainActor final class DetailWebView: WKWebView {
weak var keyboardDelegate: KeyboardDelegate?
override func accessibilityLabel() -> String? {
return NSLocalizedString("label.text.article", comment: "Article")
}
// MARK: - NSResponder
override func keyDown(with event: NSEvent) {
if keyboardDelegate?.keydown(event, in: self) ?? false {
return
}
super.keyDown(with: event)
}
// MARK: NSView
override func willOpenMenu(_ menu: NSMenu, with event: NSEvent) {
// Theres no API for affecting a WKWebViews contextual menu.
// (WebView had API for this.)
//
// This a minor hack. It hides unwanted menu items.
// The menu item identifiers are not documented anywhere;
// they could change, and this code would need updating.
for menuItem in menu.items {
if shouldHideMenuItem(menuItem) {
menuItem.isHidden = true
}
}
super.willOpenMenu(menu, with: event)
}
override func viewWillStartLiveResize() {
super.viewWillStartLiveResize()
evaluateJavaScript("document.body.style.overflow = 'hidden';", completionHandler: nil)
}
override func viewDidEndLiveResize() {
super.viewDidEndLiveResize()
evaluateJavaScript("document.body.style.overflow = 'visible';", completionHandler: nil)
}
override func setFrameSize(_ newSize: NSSize) {
super.setFrameSize(newSize)
}
// MARK: NSTextFinderClient
// Returning false here prevents the "Replace" checkbox from appearing in the find bar
override var isEditable: Bool { return false }
}
// MARK: - Private
private extension NSUserInterfaceItemIdentifier {
static let DetailMenuItemIdentifierReload = NSUserInterfaceItemIdentifier(rawValue: "WKMenuItemIdentifierReload")
static let DetailMenuItemIdentifierOpenLink = NSUserInterfaceItemIdentifier(rawValue: "WKMenuItemIdentifierOpenLink")
}
private extension DetailWebView {
static let menuItemIdentifiersToHide: [NSUserInterfaceItemIdentifier] = [.DetailMenuItemIdentifierReload]
static let menuItemIdentifierMatchStrings = ["newwindow", "download"]
func shouldHideMenuItem(_ menuItem: NSMenuItem) -> Bool {
guard let identifier = menuItem.identifier else {
return false
}
if DetailWebView.menuItemIdentifiersToHide.contains(identifier) {
return true
}
let lowerIdentifier = identifier.rawValue.lowercased()
for matchString in DetailWebView.menuItemIdentifierMatchStrings {
if lowerIdentifier.contains(matchString) {
return true
}
}
return false
}
}