145 行
11 KiB
Plaintext
145 行
11 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;}
|
|
// Appearance IDs 7/8 preserve the approved light/dark preferences. Legacy
|
|
// colorful IDs are intentionally unavailable and fall back to dark on read.
|
|
NSArray<NSString *> *DYThemeNames(void){return @[@"浅色",@"深色"];}
|
|
NSInteger DYThemeIndex(void){NSInteger i=[store().defaults integerForKey:@"themeIndex"];return i==7||i==8?i:8;}
|
|
void DYSetThemeIndex(NSInteger index){if(index!=7&&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];}
|
|
static const double paletteRGB[2][17][3]={
|
|
{{.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}}
|
|
};
|
|
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<2;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(.20,.35,.44):rgb(.62,.80,.88);
|
|
[d addEntriesFromDictionary:@{@"background":d[@"slot1"],@"card":d[@"slot3"],@"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==0?rgb(.90,.95,.98):rgb(.085,.09,.10);
|
|
d[@"cardEnd"]=index==0?rgb(.86,.93,.97):rgb(.15,.16,.18);
|
|
d[@"border"]=index==0?rgb(.55,.70,.79):rgb(.39,.56,.65);
|
|
[all addObject:[[d copy]autorelease]];
|
|
}palettes=[all copy];
|
|
});return palettes[DYThemeIndex()==7?0:1];
|
|
}
|
|
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) {return nil;}
|
|
NSDictionary *DYThemeTextAttributes(NSFont *font,NSColor *color) {return @{NSFontAttributeName:font,NSForegroundColorAttributeName:color};}
|
|
void DYDrawThemeSurface(NSBezierPath *path,BOOL card) {
|
|
NSDictionary *palette=DYThemePalette();NSGradient *g=[[[NSGradient alloc]initWithStartingColor:palette[card?@"card":@"background"] endingColor:palette[card?@"cardEnd":@"backgroundEnd"]]autorelease];[g drawInBezierPath:path angle:35];
|
|
}
|
|
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 {[super drawRect:rect];}
|
|
@end
|
|
@interface DYThemeButtonCell : NSButtonCell @end
|
|
@implementation DYThemeButtonCell
|
|
- (NSRect)drawTitle:(NSAttributedString*)title withFrame:(NSRect)frame inView:(NSView*)view {
|
|
BOOL selected=[view.identifier isEqual:@"appearanceToggle"]&&self.state==NSControlStateValueOn;
|
|
NSMutableAttributedString *styled=[[title mutableCopy]autorelease];
|
|
NSFont *font=selected?[NSFont boldSystemFontOfSize:11]:(self.font?:[NSFont systemFontOfSize:11]);
|
|
[styled addAttributes:DYThemeTextAttributes(font,[DYThemeColor(@"text")colorWithAlphaComponent:self.enabled?1:.45]) range:NSMakeRange(0,styled.length)];return [super drawTitle:styled withFrame:frame inView:view];
|
|
}
|
|
- (void)drawBezelWithFrame:(NSRect)frame inView:(NSView*)view {
|
|
if(!self.bordered){[super drawBezelWithFrame:frame inView:view];return;}
|
|
BOOL selected=[view.identifier isEqual:@"appearanceToggle"]&&self.state==NSControlStateValueOn;
|
|
DYDrawSoftControl(NSInsetRect(frame,2,3),7,selected,[view.identifier isEqual:@"primaryAction"],self.highlighted);
|
|
}
|
|
@end
|
|
@implementation DYThemeButton
|
|
+ (Class)cellClass {return DYThemeButtonCell.class;}
|
|
- (void)drawRect:(NSRect)rect {
|
|
if([self.identifier isEqual:@"layerAction"])DYDrawSoftControl(NSInsetRect(self.bounds,2,2),6,NO,NO,self.cell.highlighted);
|
|
[super drawRect:rect];
|
|
}
|
|
@end
|
|
@implementation DYThemePopUpButton
|
|
- (void)drawRect:(NSRect)rect {
|
|
NSRect bounds=NSInsetRect(self.bounds,2,3);NSBezierPath *path=[NSBezierPath bezierPathWithRoundedRect:bounds xRadius:6 yRadius:6];DYDrawSoftControl(bounds,6,NO,NO,self.cell.highlighted);
|
|
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
|