aboutsummaryrefslogtreecommitdiff
path: root/Liquid Glass Bezel/BBObjectCell.m
blob: a0605988a620fc0d43e0b9d35908c7164cbb0a22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#import "BBObjectCell.h"

@implementation BBObjectCell

- (NSCellImagePosition)preferredImagePosition {
    return NSImageAbove;
}

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {  
  BOOL isFirstResponder = [[controlView window] firstResponder] == controlView && ![controlView isKindOfClass:[NSTableView class]];
  BOOL dropTarget = ([self isHighlighted] && ([self highlightsBy] & NSChangeBackgroundCellMask) && ![self isBezeled]);

	NSColor *fillColor;
	NSColor *strokeColor = [NSColor clearColor];

  if (isFirstResponder) {
    // Use QSAppearance1A (accent color) from UserDefaults instead of selectedTextBackgroundColor
    fillColor = [[NSUserDefaults standardUserDefaults] colorForKey:@"QSAppearance1A"];
    if (!fillColor) {
      // Fallback to system color if preference not set
      fillColor = [NSColor selectedTextBackgroundColor];
    }
  } else {
    fillColor = [NSColor clearColor];
  }

  if (dropTarget) {
    // Use QSAppearance1A (accent color) from UserDefaults instead of controlAccentColor
    NSColor *userAccentColor = [[NSUserDefaults standardUserDefaults] colorForKey:@"QSAppearance1A"];
    fillColor = userAccentColor ? userAccentColor : [NSColor controlAccentColor];
  }

  [fillColor setFill];
	[strokeColor setStroke];

  NSBezierPath *roundRect = [NSBezierPath bezierPath];
  [roundRect appendBezierPathWithRoundedRectangle:cellFrame withRadius:NSHeight(cellFrame)/cellRadiusFactor];
  [roundRect fill];

	[self drawInteriorWithFrame:[self drawingRectForBounds:cellFrame] inView:controlView];
}

- (NSRect)titleRectForBounds:(NSRect)theRect
{
  NSRect rect = theRect;
  rect = NSOffsetRect(rect, 0, -4);
  return [super titleRectForBounds: rect];
}

- (void)drawTextForObject:(QSObject *)drawObject withFrame:(NSRect)cellFrame inView:(NSView *)controlView {
  if ([self imagePosition] == NSImageOnly) return;

  NSString *abbrString = nil;
  if ([controlView respondsToSelector:@selector(matchedString)])
    abbrString = [(QSSearchObjectView *)controlView matchedString];

  NSString *nameString = nil;
  NSIndexSet *hitMask = nil;

  id ranker = [drawObject ranker];
  if (ranker && abbrString)
    nameString = [ranker matchedStringForAbbreviation:abbrString hitmask:&hitMask inContext:nil];

  if (!nameString)
    nameString = [drawObject displayName];

  BOOL rankedStringIsName = [nameString isEqualToString:[drawObject displayName]] || nameString == nil;
  if (!nameString) {
    // fall back to the identifier if no reasonable name can be found
    nameString = [drawObject identifier];
  }
  if (!nameString) {
    // Couldn't find anything sensible to use for the name, fallback to avoid a crash
    nameString = @"Unknown";
  }

  BOOL useAlternateColor = [controlView isKindOfClass:[NSTableView class]] && [(NSTableView *)controlView isRowSelected:[(NSTableView *)controlView rowAtPoint:cellFrame.origin]];
  NSColor *mainColor;
  
    BOOL shouldApplyTint = [[NSUserDefaults standardUserDefaults] boolForKey:@"QSLiquidGlassBezelApplyTint"];
    NSColor *userTextColor = shouldApplyTint ? [[NSUserDefaults standardUserDefaults] colorForKey:@"QSAppearance1T"] : [NSColor textColor];
    if (userTextColor) {
      mainColor = userTextColor;
    } else {
      // Fallback to system colors if QSAppearance1T preference not set
      mainColor = useAlternateColor ? [NSColor alternateSelectedControlTextColor] : [NSColor controlTextColor];
    }
  NSColor *fadedColor = [mainColor colorWithAlphaComponent:0.50];
  NSRect textDrawRect = [self titleRectForBounds:cellFrame];

  NSMutableAttributedString *titleString = [[[NSMutableAttributedString alloc] initWithString:nameString] autorelease];
  [titleString setAttributes:rankedStringIsName ? nameAttributes : detailsAttributes range:NSMakeRange(0, [titleString length])];

  // Bring out the matched letters
  if (abbrString && ![abbrString hasPrefix:@"QSActionMnemonic"]) {
    [titleString addAttribute:NSForegroundColorAttributeName value:rankedStringIsName ? fadedColor : [fadedColor colorWithAlphaComponent:0.8] range:NSMakeRange(0, [titleString length])];

    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])];
  }
//
//  // Ranked string and nameString aren't the same. Show 'nameString  ⟷ rankedString' in the UI
//  if (!rankedStringIsName && [drawObject displayName].length) {
//    [titleString addAttribute:NSFontAttributeName value:detailsFont range:NSMakeRange(0,[titleString length])];
//    NSMutableAttributedString *attributedNameString = [[NSMutableAttributedString alloc] initWithString:[drawObject displayName]];
//    [attributedNameString setAttributes:nameAttributes range:NSMakeRange(0, [[drawObject displayName] length])];
//
//    [attributedNameString appendAttributedString:[[[NSAttributedString alloc] initWithString:@" ⟷ " attributes:rankedNameAttributes] autorelease]];
//    // the replaceCharacters... method inserts the new string into the receiver at the start of the work (range.location and range.length are 0)
//    [titleString replaceCharactersInRange:NSMakeRange(0,0) withAttributedString:attributedNameString];
//    [attributedNameString release];
//  }

  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);
    // Use QSAppearance1T (text color) from UserDefaults if tinting is enabled, otherwise use textColor
    BOOL shouldApplyTint = [[NSUserDefaults standardUserDefaults] boolForKey:@"QSLiquidGlassBezelApplyTint"];
    NSColor *userTextColor = shouldApplyTint ? [[NSUserDefaults standardUserDefaults] colorForKey:@"QSAppearance1T"] : [NSColor textColor];
    NSColor *iconTextColor;
    iconTextColor = userTextColor ? userTextColor : [NSColor textColor];
    CGContextSetFillColorWithColor(context, [iconTextColor CGColor]);
    CGContextFillRect(context, findImageRect);
    CGContextEndTransparencyLayer(context);

    [defaultText drawInRect:textRect withAttributes:nameAttributes];
  }
}

- (void)drawIconForObject:(QSObject *)object withFrame:(NSRect)cellFrame inView:(NSView *)controlView {
  NSImage *image = [object icon];
  NSString *iconName = nil;
  NSImageSymbolConfiguration *iconConfig = nil;
  if ([[image name] isEqualToString: @"defaultAction"]) {
    iconName = @"gearshape.circle";
  }
  else if ([[image name] isEqualToString: @"Find"]) {
    iconName = @"magnifyingglass";
  }
  else if ([[image name] isEqualToString: @"Object"]) {
    iconName = @"cube.transparent";
    // Use QSAppearance1A (accent color) from UserDefaults instead of controlAccentColor
    NSColor *userAccentColor = [[NSUserDefaults standardUserDefaults] colorForKey:@"QSAppearance1A"];
    NSColor *iconAccentColor = userAccentColor ? userAccentColor : [NSColor controlAccentColor];
    iconConfig = [NSImageSymbolConfiguration configurationWithHierarchicalColor:iconAccentColor];
  }
  else if ([[image name] isEqualToString: @"ContactAddress"]) {
    iconName = @"mappin.circle";
  }
  else if ([[image name] isEqualToString: @"ContactEmail"]) {
    iconName = @"envelope.circle";
  }
  else if ([[image name] isEqualToString: @"ContactPhone"]) {
    iconName = @"phone.circle";
  }
  else if ([[image name] isEqualToString: @"DefaultBookmarkIcon"]) {
    iconName = @"link.circle";
  }
  else if ([[image name] isEqualToString: @"Catalog"]) {
    iconName = @"sparkles.rectangle.stack";
  }
  else if ([[image name] isEqualToString: @"Triggers"]) {
    iconName = @"bolt.fill";
  }

  
  if (iconName != nil) {
    NSImage *newIcon = [NSImage imageWithSystemSymbolName:iconName accessibilityDescription:[image name]];
    
    if (iconConfig == nil) {
      // Use QSAppearance1T (text color) and QSAppearance1A (accent color) from UserDefaults
      BOOL shouldApplyTint = [[NSUserDefaults standardUserDefaults] boolForKey:@"QSLiquidGlassBezelApplyTint"];

      NSColor *userTextColor = shouldApplyTint ? [[NSUserDefaults standardUserDefaults] colorForKey:@"QSAppearance1T"] : [NSColor textColor];
      NSColor *userAccentColor = [[NSUserDefaults standardUserDefaults] colorForKey:@"QSAppearance1A"];
      
      NSColor *iconTextColor = userTextColor ? userTextColor : [NSColor textColor];
      NSColor *iconAccentColor = userAccentColor ? userAccentColor : [NSColor controlAccentColor];
      
      NSArray *colors = [NSArray arrayWithObjects:iconTextColor, iconAccentColor, nil];
      iconConfig = [NSImageSymbolConfiguration configurationWithPaletteColors:colors];
    }

    QSObject *newObject = [QSObject objectWithName:[object name]];
    [newObject setIcon:[newIcon imageWithSymbolConfiguration:iconConfig]];
    [newObject setIconLoaded:[object iconLoaded]];
    return [super drawIconForObject: newObject withFrame:cellFrame inView:controlView];
  }
  
  [super drawIconForObject: object withFrame:cellFrame inView:controlView];
}

@end