From 768e492ea13d8c0f62e0334c4c626db4596a66d8 Mon Sep 17 00:00:00 2001 From: Ruben Beltran del Rio Date: Sat, 11 Feb 2023 13:37:20 +0100 Subject: Port more files to swift --- Flat Bezel.xcodeproj/project.pbxproj | 24 ++- Flat Bezel/BBFlatBezelInterface.xib | 6 +- Flat Bezel/BBObjectCell.h | 4 + Flat Bezel/BBObjectCell.m | 160 ++++++++++++++++ Flat Bezel/BBSearchObjectView.h | 10 - Flat Bezel/BBSearchObjectView.m | 201 --------------------- Flat Bezel/BackgroundView.swift | 12 +- Flat Bezel/FlatBezel-Bridging-Header.h | 2 +- .../FlatBezelCollectingSearchObjectView.swift | 38 ++++ Flat Bezel/FlatBezelObjectCell.swift | 164 +++++++++++++++++ Flat Bezel/FlatBezelSearchObjectView.swift | 6 + Info.plist | 4 +- 12 files changed, 398 insertions(+), 233 deletions(-) create mode 100644 Flat Bezel/BBObjectCell.h create mode 100644 Flat Bezel/BBObjectCell.m delete mode 100644 Flat Bezel/BBSearchObjectView.h delete mode 100644 Flat Bezel/BBSearchObjectView.m create mode 100644 Flat Bezel/FlatBezelCollectingSearchObjectView.swift create mode 100644 Flat Bezel/FlatBezelObjectCell.swift create mode 100644 Flat Bezel/FlatBezelSearchObjectView.swift diff --git a/Flat Bezel.xcodeproj/project.pbxproj b/Flat Bezel.xcodeproj/project.pbxproj index 4ad4677..9b2a193 100644 --- a/Flat Bezel.xcodeproj/project.pbxproj +++ b/Flat Bezel.xcodeproj/project.pbxproj @@ -7,20 +7,26 @@ objects = { /* Begin PBXBuildFile section */ - 42447D7D19407DA00077B4B2 /* BBSearchObjectView.m in Sources */ = {isa = PBXBuildFile; fileRef = 42447D7C19407DA00077B4B2 /* BBSearchObjectView.m */; }; + 42447D7D19407DA00077B4B2 /* BBObjectCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 42447D7C19407DA00077B4B2 /* BBObjectCell.m */; }; 42663058193CC1FA00C0DD31 /* BBFlatBezelInterface.xib in Resources */ = {isa = PBXBuildFile; fileRef = 42663057193CC1FA00C0DD31 /* BBFlatBezelInterface.xib */; }; B576D2F52993FBD400A8B645 /* BackgroundView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B576D2F42993FBD400A8B645 /* BackgroundView.swift */; }; + B59AD27F2997AC2F006DA797 /* FlatBezelSearchObjectView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59AD27E2997AC2F006DA797 /* FlatBezelSearchObjectView.swift */; }; + B59AD2812997AE4A006DA797 /* FlatBezelCollectingSearchObjectView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59AD2802997AE4A006DA797 /* FlatBezelCollectingSearchObjectView.swift */; }; + B59AD2852997B860006DA797 /* FlatBezelObjectCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = B59AD2842997B860006DA797 /* FlatBezelObjectCell.swift */; }; E470B7E31709F3A0000169F2 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E470B7E21709F3A0000169F2 /* Cocoa.framework */; }; E470B7E91709F3A0000169F2 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = E470B7E71709F3A0000169F2 /* InfoPlist.strings */; }; E470B7F81709F3A0000169F2 /* BBFlatBezelInterface.m in Sources */ = {isa = PBXBuildFile; fileRef = E470B7F71709F3A0000169F2 /* BBFlatBezelInterface.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ - 42447D7B19407DA00077B4B2 /* BBSearchObjectView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BBSearchObjectView.h; sourceTree = ""; }; - 42447D7C19407DA00077B4B2 /* BBSearchObjectView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BBSearchObjectView.m; sourceTree = ""; }; + 42447D7B19407DA00077B4B2 /* BBObjectCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BBObjectCell.h; sourceTree = ""; }; + 42447D7C19407DA00077B4B2 /* BBObjectCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BBObjectCell.m; sourceTree = ""; }; 42663057193CC1FA00C0DD31 /* BBFlatBezelInterface.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = BBFlatBezelInterface.xib; sourceTree = ""; }; B576D2F42993FBD400A8B645 /* BackgroundView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackgroundView.swift; sourceTree = ""; }; B576D2F62993FD7500A8B645 /* FlatBezel-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "FlatBezel-Bridging-Header.h"; sourceTree = ""; }; + B59AD27E2997AC2F006DA797 /* FlatBezelSearchObjectView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlatBezelSearchObjectView.swift; sourceTree = ""; }; + B59AD2802997AE4A006DA797 /* FlatBezelCollectingSearchObjectView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlatBezelCollectingSearchObjectView.swift; sourceTree = ""; }; + B59AD2842997B860006DA797 /* FlatBezelObjectCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlatBezelObjectCell.swift; sourceTree = ""; }; E470B7DF1709F3A0000169F2 /* Flat Bezel.qsplugin */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Flat Bezel.qsplugin"; sourceTree = BUILT_PRODUCTS_DIR; }; E470B7E21709F3A0000169F2 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; }; E470B7E61709F3A0000169F2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = SOURCE_ROOT; }; @@ -77,12 +83,15 @@ children = ( E470B7EA1709F3A0000169F2 /* Configuration */, E470B7E51709F3A0000169F2 /* Supporting Files */, - 42447D7B19407DA00077B4B2 /* BBSearchObjectView.h */, - 42447D7C19407DA00077B4B2 /* BBSearchObjectView.m */, + 42447D7B19407DA00077B4B2 /* BBObjectCell.h */, + 42447D7C19407DA00077B4B2 /* BBObjectCell.m */, E470B7F61709F3A0000169F2 /* BBFlatBezelInterface.h */, E470B7F71709F3A0000169F2 /* BBFlatBezelInterface.m */, 42663057193CC1FA00C0DD31 /* BBFlatBezelInterface.xib */, B576D2F42993FBD400A8B645 /* BackgroundView.swift */, + B59AD27E2997AC2F006DA797 /* FlatBezelSearchObjectView.swift */, + B59AD2802997AE4A006DA797 /* FlatBezelCollectingSearchObjectView.swift */, + B59AD2842997B860006DA797 /* FlatBezelObjectCell.swift */, ); path = "Flat Bezel"; sourceTree = ""; @@ -198,9 +207,12 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + B59AD2812997AE4A006DA797 /* FlatBezelCollectingSearchObjectView.swift in Sources */, B576D2F52993FBD400A8B645 /* BackgroundView.swift in Sources */, - 42447D7D19407DA00077B4B2 /* BBSearchObjectView.m in Sources */, + 42447D7D19407DA00077B4B2 /* BBObjectCell.m in Sources */, E470B7F81709F3A0000169F2 /* BBFlatBezelInterface.m in Sources */, + B59AD2852997B860006DA797 /* FlatBezelObjectCell.swift in Sources */, + B59AD27F2997AC2F006DA797 /* FlatBezelSearchObjectView.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Flat Bezel/BBFlatBezelInterface.xib b/Flat Bezel/BBFlatBezelInterface.xib index b3039c6..b6fd59e 100644 --- a/Flat Bezel/BBFlatBezelInterface.xib +++ b/Flat Bezel/BBFlatBezelInterface.xib @@ -28,7 +28,7 @@ - + @@ -38,7 +38,7 @@ - + @@ -48,7 +48,7 @@ - + diff --git a/Flat Bezel/BBObjectCell.h b/Flat Bezel/BBObjectCell.h new file mode 100644 index 0000000..3fa67c6 --- /dev/null +++ b/Flat Bezel/BBObjectCell.h @@ -0,0 +1,4 @@ +#import + +@interface BBObjectCell : QSObjectCell +@end diff --git a/Flat Bezel/BBObjectCell.m b/Flat Bezel/BBObjectCell.m new file mode 100644 index 0000000..28e79d2 --- /dev/null +++ b/Flat Bezel/BBObjectCell.m @@ -0,0 +1,160 @@ +#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) { + fillColor = [self highlightColor]; + } else { + fillColor = [self backgroundColor]; + } + + if (dropTarget) { + fillColor = [NSColor colorWithRed:0.77 green:0.91 blue:0.96 alpha:1]; + } + + [fillColor setFill]; + [strokeColor setStroke]; + + NSBezierPath *roundRect = [NSBezierPath bezierPath]; + [roundRect appendBezierPathWithRoundedRectangle:cellFrame withRadius: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 = (textColor ? textColor : (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 - -@interface BBSearchObjectView : QSSearchObjectView -@end - -@interface BBCollectingSearchObjectView : QSCollectingSearchObjectView -@end - -@interface BBObjectCell : QSObjectCell -@end diff --git a/Flat Bezel/BBSearchObjectView.m b/Flat Bezel/BBSearchObjectView.m deleted file mode 100644 index 0722e0c..0000000 --- a/Flat Bezel/BBSearchObjectView.m +++ /dev/null @@ -1,201 +0,0 @@ -#import "BBSearchObjectView.h" - -@implementation BBSearchObjectView -+ (Class)cellClass { return [BBObjectCell class]; } - -@end - -@implementation BBCollectingSearchObjectView -+ (Class)cellClass { return [BBObjectCell class]; } - -- (void)drawRect:(NSRect)rect { - NSRect frame = [self frame]; - NSInteger count = [collection count]; - if (![self currentEditor] && count) { - frame.origin = NSZeroPoint; - [[self cell] drawWithFrame:frame inView:self]; - NSInteger i; - CGFloat iconSize = collectionSpace?collectionSpace:16; - CGFloat opacity = collecting?1.0:0.75; - QSObject *object; - CGFloat totalWidth = iconSize + 2; - for (i = 0; i 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; iCFBundlePackageType BNDL CFBundleShortVersionString - 2.0 + 1.0.0 CFBundleVersion - 6 + 1 QSRequirements minHostVersion -- cgit