diff options
| author | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-09-04 11:11:37 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-09-04 11:11:37 +0200 |
| commit | bb5daa2e90623691a5259700b9d388d2d21f46d3 (patch) | |
| tree | f5629e81bedc6cdd35ee9a79628997c5e14ffa7c /Liquid Glass Bezel/LiquidGlassBezelObjectCell.swift | |
| parent | 3dfeb86c5c3b72acc59919c7a198fcf76a54c35c (diff) | |
Rename to Liquid Glass Bezel, add preferences
Diffstat (limited to 'Liquid Glass Bezel/LiquidGlassBezelObjectCell.swift')
| -rw-r--r-- | Liquid Glass Bezel/LiquidGlassBezelObjectCell.swift | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/Liquid Glass Bezel/LiquidGlassBezelObjectCell.swift b/Liquid Glass Bezel/LiquidGlassBezelObjectCell.swift new file mode 100644 index 0000000..dfffce6 --- /dev/null +++ b/Liquid Glass Bezel/LiquidGlassBezelObjectCell.swift @@ -0,0 +1,164 @@ +/* +import Foundation + +class LiquidGlassBezelObjectCell: QSObjectCell { + override func preferredImagePosition() -> NSControl.ImagePosition { + return .imageAbove + } + + override func draw(withFrame cellFrame: NSRect, in controlView: NSView?) { + if let controlView { + + let isFirstResponder = controlView.window?.firstResponder == controlView && !(controlView is NSTableView) + let dropTarget = isHighlighted && ((highlightsBy.rawValue & NSCell.StyleMask.changeBackgroundCellMask.rawValue) != 0) && !isBezeled + + var fillColor: NSColor? + let strokeColor = NSColor.clear + + if isFirstResponder { + fillColor = self.highlightColor() + } else { + fillColor = self.backgroundColor + } + + if dropTarget { + fillColor = NSColor(red: 0.77, green: 0.91, blue: 0.96, alpha: 1) + } + + fillColor?.setFill() + strokeColor.setStroke() + + let roundRect = NSBezierPath() + roundRect.append(withRoundedRectangle: cellFrame, withRadius: cellRadiusFactor()) + roundRect.fill() + + drawInterior(withFrame: drawingRect(forBounds: cellFrame), in: controlView) + } + } + + override func titleRect(forBounds _rect: NSRect) -> NSRect { + var rect = _rect + rect = NSOffsetRect(rect, 0, -4) + return super.titleRect(forBounds: rect) + } + + override func drawText(for drawObject: QSObject!, withFrame cellFrame: NSRect, in controlView: NSView!) { + if imagePosition == .imageOnly { + return + } + + var abbrString: String? = nil + + if controlView.responds(to: #selector(QSSearchObjectView.matchedString)) { + abbrString = (controlView as! QSSearchObjectView).matchedString() + } + + var nameString: String? = nil + var hitMask: NSIndexSet? = nil + + let ranker = drawObject.ranker() + if ranker != nil && abbrString != nil { + nameString = ranker?.matchedString(forAbbreviation: abbrString, hitmask: &hitMask, inContext: nil) + } + + if (nameString == nil) { + nameString = drawObject.displayName() + } + + let rankedStringIsName = nameString == drawObject.displayName() || nameString == nil + + if nameString == nil { + nameString = drawObject.identifier() ?? "Unknown" + } + + let useAlternateColor = controlView is NSTableView && (controlView as! NSTableView).isRowSelected((controlView as! NSTableView).row(at: cellFrame.origin)) + let mainColor = textColor() ?? (useAlternateColor ? .alternateSelectedControlTextColor : .controlTextColor) + let fadedColor = mainColor.withAlphaComponent(0.50) + let textDrawRect = titleRect(forBounds: cellFrame) + + + let titleString = NSMutableAttributedString(string: nameString!) + let nameAttributes = (value(forKey: "nameAttributes") as? [NSAttributedString.Key : Any]) + let detailsAttributes = (value(forKey: "detailsAttributes") as? [NSAttributedString.Key : Any]) + titleString.setAttributes(rankedStringIsName ? nameAttributes : detailsAttributes, range: NSMakeRange(0, titleString.length)) + + if abbrString != nil && abbrString!.hasPrefix("QSActionMnemonic") { + titleString.addAttribute(.foregroundColor, value: rankedStringIsName ? fadedColor : fadedColor.withAlphaComponent(0.8), range: NSMakeRange(0, titleString.length)) + + var hits: Int = 0 + let count = hitMask?.getIndexes(&hits, maxCount: titleString.length, inIndexRange: nil) + for i in 0..<(count ?? 0) { + for j in 1.. { + } + } + } +} + + NSUInteger i = 0; + NSUInteger j = 0; + NSUInteger hits[[titleString length]]; + NSUInteger count = [hitMask getIndexes:(NSUInteger *)&hits maxCount:[titleString length] inIndexRange:nil]; + NSDictionary *attributes = @{ + NSForegroundColorAttributeName: rankedStringIsName ? mainColor : fadedColor + }; + for(i = 0; i<count; i += j) { + for (j = 1; i+j<count && hits[i+j-1] +1 == hits[i+j]; j++); + [titleString addAttributes:attributes range:NSMakeRange(hits[i], j)]; + } + } else { + [titleString addAttribute:NSBaselineOffsetAttributeName value:[NSNumber numberWithDouble:-1.0] range:NSMakeRange(0, [titleString length])]; + } + + if (showDetails) { + NSString *detailsString = [drawObject details]; + + NSRange returnRange = [detailsString rangeOfString:@"\n"]; + if (returnRange.location != NSNotFound) { + detailsString = [detailsString substringToIndex:returnRange.location]; + } + + detailsAttributes = [detailsAttributes mutableCopy]; + [detailsAttributes setValue:[NSColor grayColor] forKey:NSForegroundColorAttributeName]; + + if (detailsString && detailsString.length && ![detailsString isEqualToString:nameString]) { + [titleString appendAttributedString:[[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"\n%@",detailsString] attributes:detailsAttributes] autorelease]]; + } + } + + NSRect centerRect = rectFromSize([titleString size]); + centerRect.size.width = NSWidth(textDrawRect); + centerRect.size.height = MIN(NSHeight(textDrawRect), centerRect.size.height); + [titleString drawInRect:centerRectInRect(centerRect, textDrawRect)]; +} + +- (void)drawSearchPlaceholderWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { + NSString *defaultText = NSLocalizedStringWithDefaultValue(@"Type to search", nil, [NSBundle mainBundle], @"Type to search", @"Hint that appears in the first pane of the QS interface when it's empty."); + NSSize textSize = [defaultText sizeWithAttributes:nameAttributes]; + NSRect textRect = centerRectInRect(rectFromSize(textSize), cellFrame); + BOOL isFirstResponder = [[controlView window] firstResponder] == controlView && ![controlView isKindOfClass:[NSTableView class]]; + + if (isFirstResponder && [controlView isKindOfClass:[QSSearchObjectView class]]) { + NSImage *find = [NSImage imageWithSystemSymbolName:@"magnifyingglass.circle.fill" accessibilityDescription:nil]; + + + [find setSize:QSSize16]; + NSRect findImageRect = expelRectFromRectOnEdge(centerRectInRect(rectFromSize([find size]), cellFrame), textRect, NSRectEdgeMinX, -2); + + + NSGraphicsContext *graphicsContext = [NSGraphicsContext currentContext]; + [graphicsContext saveGraphicsState]; + CGContextRef context = [graphicsContext CGContext]; + CGContextBeginTransparencyLayerWithRect(context, findImageRect, nil); + CGContextSetBlendMode(context, kCGBlendModeNormal); + [find drawInRect:findImageRect fromRect:rectFromSize([find size]) operation:NSCompositingOperationSourceOver fraction:1]; + CGContextSetBlendMode(context, kCGBlendModeSourceIn); + CGContextSetFillColorWithColor(context, [[NSColor textColor] CGColor]); + CGContextFillRect(context, findImageRect); + CGContextEndTransparencyLayer(context); + + [defaultText drawInRect:textRect withAttributes:nameAttributes]; + } +} + +@end +*/ |