mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2025-08-12 06:26:36 +00:00
Implement macOS web view mouse over events
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
import SwiftUI
|
||||
import Articles
|
||||
|
||||
final class ArticleView: NSViewControllerRepresentable {
|
||||
struct ArticleView: NSViewControllerRepresentable {
|
||||
|
||||
var sceneModel: SceneModel
|
||||
var articleModel: ArticleModel
|
||||
|
||||
102
Multiplatform/macOS/Article/WebStatusBarView.swift
Normal file
102
Multiplatform/macOS/Article/WebStatusBarView.swift
Normal file
@@ -0,0 +1,102 @@
|
||||
//
|
||||
// WebStatusBarView.swift
|
||||
// Multiplatform macOS
|
||||
//
|
||||
// Created by Maurice Parker on 7/8/20.
|
||||
// Copyright © 2020 Ranchero Software. All rights reserved.
|
||||
//
|
||||
|
||||
import AppKit
|
||||
import Articles
|
||||
|
||||
final class WebStatusBarView: NSView {
|
||||
|
||||
var urlLabel = NSTextField(labelWithString: "")
|
||||
|
||||
var mouseoverLink: String? {
|
||||
didSet {
|
||||
updateLinkForDisplay()
|
||||
}
|
||||
}
|
||||
|
||||
private var linkForDisplay: String? {
|
||||
didSet {
|
||||
needsLayout = true
|
||||
if let link = linkForDisplay {
|
||||
urlLabel.stringValue = link
|
||||
self.isHidden = false
|
||||
}
|
||||
else {
|
||||
urlLabel.stringValue = ""
|
||||
self.isHidden = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var didConfigureLayerRadius = false
|
||||
|
||||
override var isOpaque: Bool {
|
||||
return false
|
||||
}
|
||||
|
||||
override var isFlipped: Bool {
|
||||
return true
|
||||
}
|
||||
|
||||
override var wantsUpdateLayer: Bool {
|
||||
return true
|
||||
}
|
||||
|
||||
override init(frame frameRect: NSRect) {
|
||||
super.init(frame: frameRect)
|
||||
commonInit()
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
super.init(coder: coder)
|
||||
commonInit()
|
||||
}
|
||||
|
||||
override func updateLayer() {
|
||||
guard let layer = layer else {
|
||||
return
|
||||
}
|
||||
if !didConfigureLayerRadius {
|
||||
layer.cornerRadius = 4.0
|
||||
didConfigureLayerRadius = true
|
||||
}
|
||||
|
||||
let color = self.effectiveAppearance.isDarkMode ? NSColor.textBackgroundColor : AppAssets.webStatusBarBackground
|
||||
layer.backgroundColor = color.cgColor
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private extension WebStatusBarView {
|
||||
|
||||
func commonInit() {
|
||||
self.isHidden = true
|
||||
urlLabel.translatesAutoresizingMaskIntoConstraints = false
|
||||
urlLabel.lineBreakMode = .byTruncatingMiddle
|
||||
urlLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
|
||||
|
||||
addSubview(urlLabel)
|
||||
NSLayoutConstraint.activate([
|
||||
leadingAnchor.constraint(equalTo: urlLabel.leadingAnchor, constant: -6),
|
||||
trailingAnchor.constraint(equalTo: urlLabel.trailingAnchor, constant: 6),
|
||||
centerYAnchor.constraint(equalTo: urlLabel.centerYAnchor)
|
||||
])
|
||||
}
|
||||
|
||||
func updateLinkForDisplay() {
|
||||
if let mouseoverLink = mouseoverLink, !mouseoverLink.isEmpty {
|
||||
linkForDisplay = mouseoverLink.strippingHTTPOrHTTPSScheme
|
||||
}
|
||||
else {
|
||||
linkForDisplay = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,9 +27,13 @@ class WebViewController: NSViewController {
|
||||
private struct MessageName {
|
||||
static let imageWasClicked = "imageWasClicked"
|
||||
static let imageWasShown = "imageWasShown"
|
||||
static let mouseDidEnter = "mouseDidEnter"
|
||||
static let mouseDidExit = "mouseDidExit"
|
||||
static let showFeedInspector = "showFeedInspector"
|
||||
}
|
||||
|
||||
var statusBarView: WebStatusBarView!
|
||||
|
||||
private var webView: PreloadedWebView?
|
||||
|
||||
private var articleExtractor: ArticleExtractor? = nil
|
||||
@@ -60,6 +64,16 @@ class WebViewController: NSViewController {
|
||||
DistributedNotificationCenter.default().addObserver(self, selector: #selector(appleColorPreferencesChanged(_:)), name: .appleColorPreferencesChangedNotification, object: nil)
|
||||
DistributedNotificationCenter.default().addObserver(self, selector: #selector(appleInterfaceThemeChanged(_:)), name: .appleInterfaceThemeChangedNotification, object: nil)
|
||||
|
||||
statusBarView = WebStatusBarView()
|
||||
statusBarView.translatesAutoresizingMaskIntoConstraints = false
|
||||
self.view.addSubview(statusBarView)
|
||||
NSLayoutConstraint.activate([
|
||||
self.view.leadingAnchor.constraint(equalTo: statusBarView.leadingAnchor, constant: -6),
|
||||
self.view.trailingAnchor.constraint(greaterThanOrEqualTo: statusBarView.trailingAnchor, constant: 6),
|
||||
self.view.bottomAnchor.constraint(equalTo: statusBarView.bottomAnchor, constant: 2),
|
||||
statusBarView.heightAnchor.constraint(equalToConstant: 20)
|
||||
])
|
||||
|
||||
loadWebView()
|
||||
}
|
||||
|
||||
@@ -178,6 +192,12 @@ extension WebViewController: WKScriptMessageHandler {
|
||||
return
|
||||
case MessageName.imageWasClicked:
|
||||
return
|
||||
case MessageName.mouseDidEnter:
|
||||
if let link = message.body as? String {
|
||||
statusBarView.mouseoverLink = link
|
||||
}
|
||||
case MessageName.mouseDidExit:
|
||||
statusBarView.mouseoverLink = nil
|
||||
case MessageName.showFeedInspector:
|
||||
return
|
||||
default:
|
||||
@@ -201,7 +221,7 @@ private extension WebViewController {
|
||||
|
||||
// Add the webview
|
||||
webView.translatesAutoresizingMaskIntoConstraints = false
|
||||
self.view.addSubview(webView)
|
||||
self.view.addSubview(webView, positioned: .below, relativeTo: self.statusBarView)
|
||||
NSLayoutConstraint.activate([
|
||||
self.view.leadingAnchor.constraint(equalTo: webView.leadingAnchor),
|
||||
self.view.trailingAnchor.constraint(equalTo: webView.trailingAnchor),
|
||||
@@ -211,6 +231,8 @@ private extension WebViewController {
|
||||
|
||||
webView.configuration.userContentController.add(WrapperScriptMessageHandler(self), name: MessageName.imageWasClicked)
|
||||
webView.configuration.userContentController.add(WrapperScriptMessageHandler(self), name: MessageName.imageWasShown)
|
||||
webView.configuration.userContentController.add(WrapperScriptMessageHandler(self), name: MessageName.mouseDidEnter)
|
||||
webView.configuration.userContentController.add(WrapperScriptMessageHandler(self), name: MessageName.mouseDidExit)
|
||||
webView.configuration.userContentController.add(WrapperScriptMessageHandler(self), name: MessageName.showFeedInspector)
|
||||
|
||||
self.renderPage(webView)
|
||||
|
||||
Reference in New Issue
Block a user