Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
xcuserdata
.build/
.DS_Store
20 changes: 15 additions & 5 deletions Sources/MGScopeBar/MGScopeBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,30 @@
#import "MGRecessedPopUpButtonCell.h"


static NSColor *MGScopeBarWhite(CGFloat lightWhite, CGFloat darkWhite)
{
return [NSColor colorWithName:nil dynamicProvider:^NSColor *(NSAppearance *appearance) {
NSAppearanceName name = [appearance bestMatchFromAppearancesWithNames:@[NSAppearanceNameAqua, NSAppearanceNameDarkAqua]];
CGFloat white = [name isEqualToString:NSAppearanceNameDarkAqua] ? darkWhite : lightWhite;
return [NSColor colorWithCalibratedWhite:white alpha:1.0];
}];
}


#define SCOPE_BAR_H_INSET 8.0 // inset on left and right
#define SCOPE_BAR_HEIGHT 25.0 // used in -sizeToFit
#define SCOPE_BAR_START_COLOR_GRAY [NSColor colorWithCalibratedWhite:0.75 alpha:1.0] // bottom color of gray gradient
#define SCOPE_BAR_END_COLOR_GRAY [NSColor colorWithCalibratedWhite:0.90 alpha:1.0] // top color of gray gradient
#define SCOPE_BAR_START_COLOR_GRAY MGScopeBarWhite(0.75, 0.19) // bottom color of gray gradient
#define SCOPE_BAR_END_COLOR_GRAY MGScopeBarWhite(0.90, 0.25) // top color of gray gradient
#define SCOPE_BAR_START_COLOR_BLUE [NSColor colorWithCalibratedRed:0.71 green:0.75 blue:0.81 alpha:1.0] // bottom color of blue gradient
#define SCOPE_BAR_END_COLOR_BLUE [NSColor colorWithCalibratedRed:0.80 green:0.82 blue:0.87 alpha:1.0] // top color of blue gradient
#define SCOPE_BAR_BORDER_COLOR [NSColor colorWithCalibratedWhite:0.69 alpha:1.0] // bottom line's color
#define SCOPE_BAR_BORDER_COLOR MGScopeBarWhite(0.69, 0.10) // bottom line's color
#define SCOPE_BAR_BORDER_WIDTH 1.0 // bottom line's width

#define SCOPE_BAR_SEPARATOR_COLOR [NSColor colorWithCalibratedWhite:0.52 alpha:1.0] // color of vertical-line separators between groups
#define SCOPE_BAR_SEPARATOR_COLOR MGScopeBarWhite(0.52, 0.40) // color of vertical-line separators between groups
#define SCOPE_BAR_SEPARATOR_WIDTH 1.0 // width of vertical-line separators between groups
#define SCOPE_BAR_SEPARATOR_HEIGHT 16.0 // separators are vertically centered in the bar

#define SCOPE_BAR_LABEL_COLOR [NSColor colorWithCalibratedWhite:0.45 alpha:1.0] // color of groups' labels
#define SCOPE_BAR_LABEL_COLOR MGScopeBarWhite(0.45, 0.62) // color of groups' labels
#define SCOPE_BAR_FONTSIZE 12.0 // font-size of labels and buttons
#define SCOPE_BAR_ITEM_SPACING 6.0 // spacing between buttons/separators/labels
#define SCOPE_BAR_BUTTON_IMAGE_SIZE 16.0 // size of buttons' images (width and height)
Expand Down
17 changes: 17 additions & 0 deletions Tests/MGScopeBarTests/MGScopeBarTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,21 @@ final class MGScopeBarTests: XCTestCase {
// Use XCTAssert and related functions to verify your tests produce the correct
// results.
}

private func backgroundBrightness(of appearanceName: NSAppearance.Name) throws -> CGFloat {
let bar = MGScopeBar(frame: NSRect(x: 0, y: 0, width: 200, height: 25))
bar.appearance = NSAppearance(named: appearanceName)
let rep = try XCTUnwrap(bar.bitmapImageRepForCachingDisplay(in: bar.bounds))
bar.cacheDisplay(in: bar.bounds, to: rep)
let color = try XCTUnwrap(rep.colorAt(x: rep.pixelsWide / 2, y: rep.pixelsHigh / 2))
return try XCTUnwrap(color.usingColorSpace(.genericGray)).whiteComponent
}

func testBackgroundIsLightInAquaAppearance() throws {
XCTAssertEqual(try backgroundBrightness(of: .aqua), 0.825, accuracy: 0.03)
}

func testBackgroundIsDarkInDarkAquaAppearance() throws {
XCTAssertLessThan(try backgroundBrightness(of: .darkAqua), 0.35)
}
}
Loading