164 行
16 KiB
Plaintext
164 行
16 KiB
Plaintext
#import "DYTheme.h"
|
|
|
|
NSString *const DYThemeDidChange=@"DYThemeDidChange";
|
|
static NSString *const preferenceDomain=@"com.dayu.color.preferences";
|
|
static NSString *const distributedChange=@"com.dayu.color.theme.changed";
|
|
static NSColor *rgb(double r,double g,double b){return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:1];}
|
|
@interface DYThemeStore : NSObject
|
|
@property(nonatomic,retain) NSUserDefaults *defaults;
|
|
@end
|
|
@implementation DYThemeStore
|
|
- (instancetype)init {
|
|
if((self=[super init])) {
|
|
_defaults=[[NSUserDefaults alloc]initWithSuiteName:preferenceDomain];
|
|
[_defaults registerDefaults:@{@"themeIndex":@7}];
|
|
[[NSDistributedNotificationCenter defaultCenter]addObserver:self selector:@selector(externalChange:) name:distributedChange object:preferenceDomain suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];
|
|
}
|
|
return self;
|
|
}
|
|
- (void)dealloc {[[NSDistributedNotificationCenter defaultCenter]removeObserver:self];[_defaults release];[super dealloc];}
|
|
- (void)externalChange:(NSNotification *)note {
|
|
[_defaults synchronize];
|
|
dispatch_async(dispatch_get_main_queue(),^{[[NSNotificationCenter defaultCenter]postNotificationName:DYThemeDidChange object:nil];});
|
|
}
|
|
@end
|
|
static DYThemeStore *store(void){static DYThemeStore *s;static dispatch_once_t once;dispatch_once(&once,^{s=[DYThemeStore new];});return s;}
|
|
NSArray<NSString *> *DYThemeNames(void){return @[@"深空灰",@"冰川蓝",@"薰衣草紫",@"翡翠绿",@"琥珀橙",@"黑金渐变",@"霓虹炫彩",@"冰蓝柔光",@"曜石柔光"];}
|
|
NSInteger DYThemeIndex(void){NSInteger i=[store().defaults integerForKey:@"themeIndex"];return i>=0&&i<9?i:7;}
|
|
static const double paletteRGB[9][17][3]={
|
|
{{0.11,0.12,0.14},{0.075,0.08,0.095},{0.17,0.18,0.21},{0.12,0.13,0.15},{0.27,0.29,0.33},{0.2,0.22,0.25},{0.36,0.39,0.44},{0.25,0.28,0.32},{0.21,0.23,0.26},{0.15,0.17,0.2},{0.78,0.83,0.9},{0.55,0.61,0.7},{1.0,1.0,1.0},{0.78,0.82,0.88},{0.96,0.97,0.98},{0.86,0.89,0.92},{0.67,0.72,0.77}},
|
|
{{0.031,0.086,0.148},{0.025,0.075,0.13},{0.07,0.155,0.235},{0.045,0.12,0.19},{0.115,0.22,0.33},{0.07,0.15,0.24},{0.15,0.33,0.5},{0.08,0.24,0.36},{0.23,0.32,0.41},{0.18,0.26,0.35},{0.38,0.76,1.0},{0.23,0.64,0.97},{0.96,0.98,1.0},{0.62,0.7,0.76},{0.88,0.96,1.0},{0.82,0.9,0.96},{0.58,0.7,0.78}},
|
|
{{0.11,0.09,0.17},{0.07,0.06,0.12},{0.18,0.14,0.28},{0.13,0.1,0.21},{0.27,0.22,0.42},{0.2,0.16,0.31},{0.43,0.34,0.68},{0.31,0.23,0.52},{0.22,0.18,0.31},{0.15,0.12,0.23},{0.78,0.71,1.0},{0.5,0.42,0.93},{0.95,0.9,1.0},{0.63,0.5,0.95},{0.98,0.95,1.0},{0.9,0.86,0.97},{0.7,0.66,0.82}},
|
|
{{0.08,0.17,0.1},{0.05,0.12,0.07},{0.12,0.27,0.15},{0.09,0.2,0.11},{0.16,0.35,0.2},{0.12,0.27,0.15},{0.21,0.57,0.31},{0.14,0.4,0.21},{0.14,0.27,0.16},{0.1,0.2,0.11},{0.54,0.96,0.64},{0.22,0.75,0.35},{0.91,1.0,0.93},{0.28,0.8,0.42},{0.95,1.0,0.96},{0.86,0.96,0.88},{0.66,0.8,0.68}},
|
|
{{0.23,0.15,0.08},{0.16,0.1,0.05},{0.35,0.22,0.11},{0.26,0.16,0.08},{0.45,0.28,0.14},{0.36,0.22,0.1},{0.72,0.44,0.18},{0.56,0.32,0.12},{0.29,0.18,0.09},{0.2,0.12,0.06},{0.98,0.67,0.31},{0.86,0.47,0.17},{1.0,0.86,0.57},{0.93,0.59,0.22},{1.0,0.96,0.9},{0.97,0.9,0.82},{0.8,0.7,0.58}},
|
|
{{.065,.060,.050},{.012,.012,.012},{.12,.105,.075},{.045,.042,.035},{.105,.092,.065},{.15,.125,.075},{.17,.14,.085},{.075,.065,.045},{.23,.20,.14},{.10,.09,.065},{.98,.82,.42},{.72,.51,.18},{1,.90,.65},{.88,.74,.43},{1,.86,.48},{.96,.82,.49},{.79,.70,.48}},
|
|
{{.11,.10,.13},{.045,.040,.055},{.16,.14,.20},{.083,.073,.103},{.115,.104,.139},{.15,.135,.18},{.16,.145,.19},{.105,.10,.13},{.24,.22,.28},{.14,.125,.17},{.90,.20,.83},{.02,.79,.94},{.93,.97,1},{.75,.72,.82},{.95,.97,1},{.86,.86,.91},{.67,.64,.73}},
|
|
{{.89,.94,.97},{.84,.91,.95},{.94,.98,1},{.90,.95,.98},{.83,.90,.95},{.91,.96,.99},{.95,.985,1},{.83,.91,.96},{.74,.84,.90},{.88,.94,.97},{.52,.61,.89},{.29,.74,.80},{.96,.99,1},{.40,.55,.65},{.12,.28,.34},{.17,.32,.39},{.20,.33,.41}},
|
|
{{.10,.105,.12},{.055,.056,.065},{.17,.18,.20},{.115,.119,.135},{.065,.073,.09},{.13,.14,.16},{.18,.19,.22},{.105,.11,.13},{.25,.27,.31},{.10,.11,.13},{.62,.55,.91},{.25,.75,.82},{.88,.94,.98},{.55,.64,.72},{.94,.96,.99},{.85,.89,.94},{.65,.70,.77}}
|
|
};
|
|
NSColor *DYThemeSwatch(NSInteger index){static const double values[9][3]={{.30,.33,.38},{.28,.72,1},{.58,.52,1},{.30,.86,.52},{1,.61,.28},{.96,.77,.36},{.65,.38,1},{.82,.93,.98},{.27,.35,.42}};index=MAX(0,MIN(8,index));return rgb(values[index][0],values[index][1],values[index][2]);}
|
|
void DYSetThemeIndex(NSInteger index){if(index<0||index>8)return;[store().defaults setInteger:index forKey:@"themeIndex"];[store().defaults synchronize];[[NSNotificationCenter defaultCenter]postNotificationName:DYThemeDidChange object:nil];[[NSDistributedNotificationCenter defaultCenter]postNotificationName:distributedChange object:preferenceDomain userInfo:nil deliverImmediately:YES];}
|
|
NSDictionary<NSString *,NSColor *> *DYThemePalette(void){
|
|
static NSArray *palettes;static dispatch_once_t once;dispatch_once(&once,^{
|
|
NSMutableArray *all=[NSMutableArray array];
|
|
for(NSInteger index=0;index<9;index++){
|
|
const double (*p)[3]=paletteRGB[index];NSMutableDictionary *d=[NSMutableDictionary dictionary];
|
|
for(int i=0;i<17;i++)d[[NSString stringWithFormat:@"slot%d",i]]=rgb(p[i][0],p[i][1],p[i][2]);
|
|
NSColor *accent=index==0?rgb(.72,.77,.84):index==1?rgb(.22,.67,1):index==6?rgb(.78,.75,.87):index==7?rgb(.20,.35,.44):index==8?rgb(.62,.80,.88):DYThemeSwatch(index);
|
|
[d addEntriesFromDictionary:@{@"background":d[@"slot1"],@"card":d[@"slot3"],@"border":[accent colorWithAlphaComponent:.38],@"text":d[@"slot14"],@"secondary":d[@"slot16"],@"accent":accent,@"field":d[@"slot4"],@"headerTop":d[@"slot6"],@"headerBottom":d[@"slot7"],@"track":d[@"slot8"],@"activeTop":d[@"slot10"],@"activeBottom":d[@"slot11"],@"knob":d[@"slot12"]}];d[@"backgroundEnd"]=index==5?rgb(.085,.078,.060):d[@"background"];d[@"cardEnd"]=index==5?rgb(.085,.075,.052):d[@"card"];if(index==6){d[@"backgroundEnd"]=rgb(.065,.060,.080);d[@"cardEnd"]=rgb(.093,.090,.110);d[@"border"]=[rgb(.36,.33,.43)colorWithAlphaComponent:.40];} if(index==7){d[@"backgroundEnd"]=rgb(.90,.95,.98);d[@"cardEnd"]=rgb(.86,.93,.97);d[@"border"]=rgb(.55,.70,.79);}
|
|
if(index==8){d[@"backgroundEnd"]=rgb(.085,.09,.10);d[@"cardEnd"]=rgb(.15,.16,.18);d[@"border"]=rgb(.39,.56,.65);}
|
|
[all addObject:[[d copy]autorelease]];
|
|
}palettes=[all copy];
|
|
});return palettes[DYThemeIndex()];
|
|
}
|
|
NSColor *DYThemeColor(NSString *role){
|
|
static NSDictionary *colors;static dispatch_once_t once;dispatch_once(&once,^{
|
|
NSMutableDictionary *all=[NSMutableDictionary dictionary];for(NSString *key in DYThemePalette())all[key]=[NSColor colorWithName:[@"Dayu." stringByAppendingString:key] dynamicProvider:^NSColor *(NSAppearance *appearance){return DYThemePalette()[key];}];colors=[all copy];
|
|
});return colors[role]?:NSColor.whiteColor;
|
|
}
|
|
|
|
NSShadow *DYThemeTextShadow(void) {
|
|
NSWorkspace *workspace=NSWorkspace.sharedWorkspace;
|
|
if(DYThemeIndex()!=5||workspace.accessibilityDisplayShouldIncreaseContrast||workspace.accessibilityDisplayShouldReduceTransparency)return nil;
|
|
NSShadow *shadow=[[[NSShadow alloc]init]autorelease];shadow.shadowColor=[DYThemeColor(@"accent")colorWithAlphaComponent:.30];shadow.shadowBlurRadius=1.8;shadow.shadowOffset=NSZeroSize;return shadow;
|
|
}
|
|
NSDictionary *DYThemeTextAttributes(NSFont *font,NSColor *color) {
|
|
NSMutableDictionary *attributes=[NSMutableDictionary dictionaryWithDictionary:@{NSFontAttributeName:font,NSForegroundColorAttributeName:color}];
|
|
NSShadow *shadow=DYThemeTextShadow();if(shadow)attributes[NSShadowAttributeName]=shadow;return attributes;
|
|
}
|
|
void DYDrawThemeSurface(NSBezierPath *path,BOOL card) {
|
|
NSDictionary *palette=DYThemePalette();NSString *role=card?@"card":@"background";
|
|
if(DYThemeIndex()>=5){NSGradient *gradient=[[[NSGradient alloc]initWithStartingColor:palette[role] endingColor:palette[card?@"cardEnd":@"backgroundEnd"]]autorelease];[gradient drawInBezierPath:path angle:35];}
|
|
else {[palette[role]setFill];[path fill];}
|
|
}
|
|
BOOL DYThemeIsLight(void) {return DYThemeIndex()==7;}
|
|
BOOL DYThemeUsesRelief(void) {return DYThemeIndex()==7||DYThemeIndex()==8;}
|
|
// Draw the rim as a filled ring, so the highlight follows the entire rounded
|
|
// outline rather than glowing around text or requiring additional view layers.
|
|
static void softGradient(NSBezierPath *path,NSArray *colors) {
|
|
NSRect r=path.bounds;BOOL flipped=NSGraphicsContext.currentContext.isFlipped;
|
|
NSGradient *gradient=[[[NSGradient alloc]initWithColors:colors]autorelease];
|
|
[NSGraphicsContext saveGraphicsState];[path addClip];
|
|
[gradient drawFromPoint:NSMakePoint(NSMinX(r),flipped?NSMinY(r):NSMaxY(r)) toPoint:NSMakePoint(NSMaxX(r),flipped?NSMaxY(r):NSMinY(r)) options:NSGradientDrawsBeforeStartingLocation|NSGradientDrawsAfterEndingLocation];
|
|
[NSGraphicsContext restoreGraphicsState];
|
|
}
|
|
void DYDrawSoftControl(NSRect rect,CGFloat radius,BOOL recessed,BOOL emphasized,BOOL pressed) {
|
|
BOOL strong=NSWorkspace.sharedWorkspace.accessibilityDisplayShouldIncreaseContrast;
|
|
BOOL quiet=strong||NSWorkspace.sharedWorkspace.accessibilityDisplayShouldReduceTransparency;
|
|
if(DYThemeIndex()==8){
|
|
NSBezierPath *shape=[NSBezierPath bezierPathWithRoundedRect:rect xRadius:radius yRadius:radius];
|
|
NSColor *top=recessed||pressed?rgb(.045,.05,.065):rgb(.17,.18,.205);
|
|
NSColor *bottom=recessed||pressed?rgb(.095,.11,.13):rgb(.10,.105,.12);
|
|
if(emphasized){top=pressed?rgb(.25,.22,.43):rgb(.35,.27,.62);bottom=pressed?rgb(.10,.29,.35):rgb(.12,.40,.48);}
|
|
if(!quiet&&!recessed&&!pressed){
|
|
[NSGraphicsContext saveGraphicsState];NSShadow *glow=[[[NSShadow alloc]init]autorelease];
|
|
glow.shadowColor=[rgb(.36,.72,.83)colorWithAlphaComponent:emphasized?.42:.21];glow.shadowOffset=NSZeroSize;glow.shadowBlurRadius=emphasized?4:3;[glow set];[top setFill];[shape fill];[NSGraphicsContext restoreGraphicsState];
|
|
}
|
|
softGradient(shape,@[top,bottom]);
|
|
NSBezierPath *rim=[NSBezierPath bezierPathWithRoundedRect:rect xRadius:radius yRadius:radius];CGFloat width=strong?1.3:1;
|
|
[rim appendBezierPath:[NSBezierPath bezierPathWithRoundedRect:NSInsetRect(rect,width,width) xRadius:MAX(0,radius-width) yRadius:MAX(0,radius-width)]];rim.windingRule=NSWindingRuleEvenOdd;
|
|
if(strong){[rgb(.65,.79,.88)setFill];[rim fill];}
|
|
else softGradient(rim,recessed||pressed?@[rgb(.15,.22,.29),rgb(.41,.59,.69)]:@[rgb(.70,.82,.91),rgb(.28,.40,.51),rgb(.16,.24,.29),rgb(.38,.53,.61)]);
|
|
return;
|
|
}
|
|
NSBezierPath *shape=[NSBezierPath bezierPathWithRoundedRect:rect xRadius:radius yRadius:radius];
|
|
NSColor *top=recessed||pressed?rgb(.79,.87,.93):rgb(.96,.985,1);
|
|
NSColor *bottom=recessed||pressed?rgb(.91,.96,.99):rgb(.85,.92,.97);
|
|
if(emphasized){top=pressed?rgb(.66,.73,.90):rgb(.76,.77,.96);bottom=pressed?rgb(.50,.77,.83):rgb(.55,.85,.90);}
|
|
if(!quiet&&!recessed&&!pressed){
|
|
BOOL flipped=NSGraphicsContext.currentContext.isFlipped;
|
|
for(int light=0;light<2;light++){
|
|
[NSGraphicsContext saveGraphicsState];NSShadow *shadow=[[[NSShadow alloc]init]autorelease];
|
|
shadow.shadowColor=light?[NSColor colorWithWhite:1 alpha:.90]:[rgb(.37,.55,.66)colorWithAlphaComponent:.28];
|
|
CGFloat d=light?-1.5:2;shadow.shadowOffset=NSMakeSize(d,flipped?d:-d);shadow.shadowBlurRadius=light?2.5:3.5;[shadow set];[top setFill];[shape fill];[NSGraphicsContext restoreGraphicsState];
|
|
}
|
|
}
|
|
softGradient(shape,@[top,bottom]);
|
|
NSBezierPath *rim=[NSBezierPath bezierPathWithRoundedRect:rect xRadius:radius yRadius:radius];
|
|
CGFloat width=strong?1.3:1;[rim appendBezierPath:[NSBezierPath bezierPathWithRoundedRect:NSInsetRect(rect,width,width) xRadius:MAX(0,radius-width) yRadius:MAX(0,radius-width)]];rim.windingRule=NSWindingRuleEvenOdd;
|
|
if(strong){[rgb(.33,.49,.60)setFill];[rim fill];}
|
|
else softGradient(rim,recessed||pressed?@[rgb(.56,.71,.81),rgb(.98,1,1)]:@[NSColor.whiteColor,rgb(.80,.89,.95),rgb(.55,.72,.83),rgb(.96,.99,1)]);
|
|
// The inner hairline gives a visible lower edge even with reduced effects.
|
|
NSBezierPath *inner=[NSBezierPath bezierPathWithRoundedRect:NSInsetRect(rect,1.5,1.5) xRadius:MAX(0,radius-1.5) yRadius:MAX(0,radius-1.5)];
|
|
[[rgb(.40,.60,.73)colorWithAlphaComponent:strong?.35:.12]setStroke];inner.lineWidth=.5;[inner stroke];
|
|
}
|
|
@implementation DYThemeLabel
|
|
- (void)drawRect:(NSRect)rect {[NSGraphicsContext saveGraphicsState];[DYThemeTextShadow()set];[super drawRect:rect];[NSGraphicsContext restoreGraphicsState];}
|
|
@end
|
|
@interface DYThemeButtonCell : NSButtonCell @end
|
|
@implementation DYThemeButtonCell
|
|
- (NSRect)drawTitle:(NSAttributedString*)title withFrame:(NSRect)frame inView:(NSView*)view {
|
|
if(DYThemeIndex()<5)return [super drawTitle:title withFrame:frame inView:view];
|
|
NSMutableAttributedString *gold=[[title mutableCopy]autorelease];[gold addAttributes:DYThemeTextAttributes(self.font?:[NSFont systemFontOfSize:11],[DYThemeColor(@"text")colorWithAlphaComponent:self.enabled?1:.45]) range:NSMakeRange(0,gold.length)];return [super drawTitle:gold withFrame:frame inView:view];
|
|
}
|
|
- (void)drawBezelWithFrame:(NSRect)frame inView:(NSView*)view {
|
|
if(DYThemeIndex()<5||!self.bordered){[super drawBezelWithFrame:frame inView:view];return;}
|
|
if(DYThemeUsesRelief()){DYDrawSoftControl(NSInsetRect(frame,2,3),7,NO,[view.identifier isEqual:@"primaryAction"],self.highlighted);return;}
|
|
NSBezierPath *path=[NSBezierPath bezierPathWithRoundedRect:NSInsetRect(frame,1,2) xRadius:6 yRadius:6];if(DYThemeIndex()==6&&[view.identifier isEqual:@"primaryAction"]){NSGradient *g=[[[NSGradient alloc]initWithColors:@[DYThemePalette()[@"activeTop"],DYThemePalette()[@"activeBottom"]]]autorelease];[g drawInBezierPath:path angle:0];}else DYDrawThemeSurface(path,YES);[[DYThemeColor(@"accent")colorWithAlphaComponent:self.highlighted?.8:.32]setStroke];path.lineWidth=self.highlighted?1.5:1;[path stroke];
|
|
}
|
|
@end
|
|
@implementation DYThemeButton
|
|
+ (Class)cellClass {return DYThemeButtonCell.class;}
|
|
- (void)drawRect:(NSRect)rect {
|
|
if(DYThemeUsesRelief()&&[self.identifier isEqual:@"layerAction"])DYDrawSoftControl(NSInsetRect(self.bounds,2,2),6,NO,NO,self.cell.highlighted);
|
|
if(DYThemeIndex()==6&&[self.identifier isEqual:@"layerAction"]){
|
|
NSBezierPath *plate=[NSBezierPath bezierPathWithRoundedRect:NSInsetRect(self.bounds,1,1) xRadius:5 yRadius:5];
|
|
[[NSColor colorWithWhite:1 alpha:self.cell.highlighted?.12:.055]setFill];[plate fill];
|
|
[[NSColor colorWithWhite:1 alpha:self.cell.highlighted?.30:.14]setStroke];plate.lineWidth=.7;[plate stroke];
|
|
}
|
|
[super drawRect:rect];
|
|
}
|
|
@end
|
|
@implementation DYThemePopUpButton
|
|
- (void)drawRect:(NSRect)rect {
|
|
if(DYThemeIndex()<5){[super drawRect:rect];return;}
|
|
NSRect bounds=NSInsetRect(self.bounds,1,2);NSBezierPath *path=[NSBezierPath bezierPathWithRoundedRect:bounds xRadius:6 yRadius:6];if(DYThemeUsesRelief())DYDrawSoftControl(NSInsetRect(self.bounds,2,3),6,NO,NO,self.cell.highlighted);else DYDrawThemeSurface(path,YES);
|
|
if(!DYThemeUsesRelief()){[[DYThemeColor(@"accent")colorWithAlphaComponent:self.enabled?.38:.18]setStroke];path.lineWidth=1;[path stroke];}
|
|
NSMutableDictionary *attributes=[NSMutableDictionary dictionaryWithDictionary:DYThemeTextAttributes(self.font?:[NSFont systemFontOfSize:11],[DYThemeColor(@"text")colorWithAlphaComponent:self.enabled?1:.45])];NSMutableParagraphStyle *paragraph=[[[NSMutableParagraphStyle alloc]init]autorelease];paragraph.lineBreakMode=NSLineBreakByTruncatingTail;attributes[NSParagraphStyleAttributeName]=paragraph;
|
|
CGFloat y=NSMidY(self.bounds)-(self.font?:[NSFont systemFontOfSize:11]).pointSize*.62;
|
|
[(self.pullsDown?[self itemAtIndex:0].title:(self.selectedItem.title?:@""))drawInRect:NSMakeRect(9,y,MAX(0,self.bounds.size.width-29),18) withAttributes:attributes];
|
|
[@"⌄" drawInRect:NSMakeRect(self.bounds.size.width-20,y,13,18) withAttributes:attributes];
|
|
if(self.window.firstResponder==self){[DYThemeColor(@"accent")setStroke];path.lineWidth=2;[path stroke];}
|
|
}
|
|
@end
|