diff --git a/Mac/About/AboutWindowController.swift b/Mac/About/AboutWindowController.swift index 828a42e9f..73962233a 100644 --- a/Mac/About/AboutWindowController.swift +++ b/Mac/About/AboutWindowController.swift @@ -8,14 +8,14 @@ import Cocoa -class AboutWindowController: NSWindowController { +final class AboutWindowController: NSWindowController { @IBOutlet var appIconImageView: NSImageView! @IBOutlet var appTitleLabel: NSTextField! @IBOutlet var versionLabel: NSTextField! @IBOutlet var copyrightLabel: NSTextField! @IBOutlet var websiteLabel: LinkLabel! - @IBOutlet var creditsTextView: NSTextView! + @IBOutlet var creditsTextView: LinksTextView! override func windowDidLoad() { super.windowDidLoad() diff --git a/Mac/About/AboutWindowController.xib b/Mac/About/AboutWindowController.xib index 7bb89ff5f..45e3d6b87 100644 --- a/Mac/About/AboutWindowController.xib +++ b/Mac/About/AboutWindowController.xib @@ -59,7 +59,7 @@ - + diff --git a/Mac/About/LinksTextView.swift b/Mac/About/LinksTextView.swift new file mode 100644 index 000000000..c0aae9fbc --- /dev/null +++ b/Mac/About/LinksTextView.swift @@ -0,0 +1,110 @@ +// +// LinksTextView.swift +// NetNewsWire +// +// Created by Brent Simmons on 01/04/2026. +// Copyright © 2026 Ranchero Software. All rights reserved. +// + +import AppKit +import RSWeb + +final class LinksTextView: NSTextView { + private var clickedLinkRange: NSRange? + private var clickedLinkURL: URL? + private var originalLinkAttributes: [NSAttributedString.Key: Any]? + private var isShowingHighlight = false + + override func mouseDown(with event: NSEvent) { + let point = convert(event.locationInWindow, from: nil) + let characterIndex = characterIndexForInsertion(at: point) + + guard characterIndex < textStorage?.length ?? 0 else { + super.mouseDown(with: event) + return + } + + var effectiveRange = NSRange() + if let url = textStorage?.attribute(.link, at: characterIndex, effectiveRange: &effectiveRange) as? URL { + clickedLinkRange = effectiveRange + clickedLinkURL = url + + // Save original attributes + originalLinkAttributes = textStorage?.attributes(at: characterIndex, effectiveRange: nil) + + highlight() + } else { + super.mouseDown(with: event) + } + } + + override func mouseDragged(with event: NSEvent) { + guard let range = clickedLinkRange else { + super.mouseDragged(with: event) + return + } + + let point = convert(event.locationInWindow, from: nil) + let characterIndex = characterIndexForInsertion(at: point) + let isOverLink = characterIndex >= range.location && characterIndex < range.location + range.length + + if isOverLink && !isShowingHighlight { + highlight() + } else if !isOverLink && isShowingHighlight { + unhighlight() + } + } + + override func mouseUp(with event: NSEvent) { + guard let range = clickedLinkRange, let url = clickedLinkURL else { + super.mouseUp(with: event) + return + } + + // Restore original attributes + unhighlight() + + // Check if mouse is still over the link + let point = convert(event.locationInWindow, from: nil) + let characterIndex = characterIndexForInsertion(at: point) + + if characterIndex >= range.location && characterIndex < range.location + range.length { + MacWebBrowser.openURL(url, inBackground: false) + } + + clickedLinkRange = nil + clickedLinkURL = nil + originalLinkAttributes = nil + } +} + +private extension LinksTextView { + func highlight() { + guard let range = clickedLinkRange, let textStorage else { + return + } + + textStorage.removeAttribute(.link, range: range) + textStorage.addAttribute(.foregroundColor, value: NSColor.systemOrange, range: range) + textStorage.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: range) + textStorage.addAttribute(.underlineColor, value: NSColor.systemOrange, range: range) + + isShowingHighlight = true + } + + func unhighlight() { + isShowingHighlight = false + + guard let range = clickedLinkRange, let originalLinkAttributes, let textStorage else { + return + } + + // Remove added attributes + textStorage.removeAttribute(.foregroundColor, range: range) + textStorage.removeAttribute(.underlineStyle, range: range) + textStorage.removeAttribute(.underlineColor, range: range) + + // Restore original attributes + textStorage.addAttributes(originalLinkAttributes, range: range) + } +}