Create and use LinksTextView in the Mac about window. It highlights links on mouseDown.

This commit is contained in:
Brent Simmons committed 2026-01-04 21:50:39 -08:00
1 parent f48e64eb9a
commit 18ce0e9fa7
3 files changed
+113 -3

No files matched your search

+2 -2
View File
@@ -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()
+1 -1
View File
@@ -59,7 +59,7 @@
<rect key="frame" x="0.0" y="0.0" width="255" height="198"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textView wantsLayer="YES" editable="NO" drawsBackground="NO" importsGraphics="NO" verticallyResizable="YES" allowsCharacterPickerTouchBarItem="NO" textCompletion="NO" spellingCorrection="YES" smartInsertDelete="YES" id="hXV-nv-VJf">
<textView wantsLayer="YES" editable="NO" drawsBackground="NO" importsGraphics="NO" verticallyResizable="YES" allowsCharacterPickerTouchBarItem="NO" textCompletion="NO" spellingCorrection="YES" smartInsertDelete="YES" id="hXV-nv-VJf" customClass="LinksTextView" customModule="NetNewsWire" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="255" height="198"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="textColor" name="textColor" catalog="System" colorSpace="catalog"/>
+110
View File
@@ -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)
}
}