p0deje / Maccy

Lightweight clipboard manager for macOS
https://maccy.app
MIT License
12.76k stars 540 forks source link

[V2] Make use of `NSAttributedString.TextHighlightStyle` with next version of macOS #830

Closed weisJ closed 1 month ago

weisJ commented 2 months ago

The next version of macOS adds text highlighting capabilities similar to how search results are displayed in Safari, Notes etc. Maybe this could be added as an option for the search result highlighting in Maccy also.

https://developer.apple.com/documentation/foundation/nsattributedstring/texthighlightstyle

p0deje commented 2 months ago

I think it should be possible to implement it even on Sonoma by using NSColor.findHighlightColor. The only caveat is that the selected item color is white which makes unreadable on the yellow background. Here is a quick example:

diff --git a/Maccy/Observables/HistoryItemDecorator.swift b/Maccy/Observables/HistoryItemDecorator.swift
index 5fd012c..adcb58f 100644
--- a/Maccy/Observables/HistoryItemDecorator.swift
+++ b/Maccy/Observables/HistoryItemDecorator.swift
@@ -96,14 +96,15 @@ class HistoryItemDecorator: Identifiable, Hashable {

     var attributedString = AttributedString(title.shortened(to: 500))
     for range in attributedString.ranges(of: query, options: .caseInsensitive) {
-      switch Defaults[.highlightMatch] {
-      case .italic:
-        attributedString[range].font = .italic(.body)()
-      case .underline:
-        attributedString[range].underlineStyle = .single
-      default:
-        attributedString[range].font = .bold(.body)()
-      }
+      attributedString[range].backgroundColor = NSColor.findHighlightColor
+//      switch Defaults[.highlightMatch] {
+//      case .italic:
+//        attributedString[range].font = .italic(.body)()
+//      case .underline:
+//        attributedString[range].underlineStyle = .single
+//      default:
+//        attributedString[range].font = .bold(.body)()
+//      }
     }
Screenshot 2024-08-16 at 11 58 00