From f48e64eb9a5ff7ecd833dd499e746fe75293ff99 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sun, 4 Jan 2026 21:50:16 -0800 Subject: [PATCH] Make LinkLabel highlight the link on mouseDown. (This is used in the about window.) --- Mac/About/LinkLabel.swift | 74 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 4 deletions(-) diff --git a/Mac/About/LinkLabel.swift b/Mac/About/LinkLabel.swift index fd35f7a2c..310065e57 100644 --- a/Mac/About/LinkLabel.swift +++ b/Mac/About/LinkLabel.swift @@ -6,10 +6,76 @@ // Copyright © 2025 Ranchero Software. All rights reserved. // -final class LinkLabel: NSTextField { +import AppKit +import RSWeb - /// Les pièces de résistance — keeping it a Mac-assed Mac app. +final class LinkLabel: NSTextField { + private var linkURL: URL? + private var originalAttributedString: NSAttributedString? + private var isShowingHighlight = false + + /// La pièce de résistance — keeping it a Mac-assed Mac app. override func resetCursorRects() { - addCursorRect(bounds, cursor: .pointingHand) - } + addCursorRect(bounds, cursor: .pointingHand) + } + + override func mouseDown(with event: NSEvent) { + if linkURL == nil { + linkURL = attributedStringValue.attribute(.link, at: 0, effectiveRange: nil) as? URL + } + if originalAttributedString == nil { + originalAttributedString = attributedStringValue + } + highlight() + } + + override func mouseDragged(with event: NSEvent) { + let locationInView = convert(event.locationInWindow, from: nil) + let isInside = bounds.contains(locationInView) + + if isInside && !isShowingHighlight { + highlight() + } else if !isInside && isShowingHighlight { + unhighlight() + } + } + + override func mouseUp(with event: NSEvent) { + let locationInView = convert(event.locationInWindow, from: nil) + let isInside = bounds.contains(locationInView) + + unhighlight() + + if isInside, let linkURL { + MacWebBrowser.openURL(linkURL, inBackground: false) + } + } +} + +private extension LinkLabel { + func highlight() { + changeColor(.systemOrange) + isShowingHighlight = true + } + + func unhighlight() { + if let originalAttributedString { + attributedStringValue = originalAttributedString + } + isShowingHighlight = false + } + + func changeColor(_ color: NSColor) { + let styledString = NSMutableAttributedString(string: stringValue) + let fullRange = NSRange(location: 0, length: styledString.length) + + if let font { + styledString.addAttribute(.font, value: font, range: fullRange) + } + styledString.addAttribute(.foregroundColor, value: color, range: fullRange) + styledString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: fullRange) + styledString.addAttribute(.underlineColor, value: color, range: fullRange) + + attributedStringValue = styledString + } }