文件
dayu-color/大鱼调色/Plugin/DYCanvasStatusToast.h
T

35 行
2.7 KiB
Objective-C

#pragma once
#import <Cocoa/Cocoa.h>
// A native, click-through HUD has a real wall-clock lifetime even when FCP
// caches a paused OSC texture. It does not need parameter writes to erase it.
@interface DYCanvasStatusToast : NSObject
- (void)showMessage:(NSString*)message;
- (void)close;
@end
@implementation DYCanvasStatusToast {
NSPanel *_panel;NSTextField *_label;uint64_t _generation;
}
- (void)dealloc{[_panel orderOut:nil];[_panel release];[super dealloc];}
- (void)close{++_generation;[_panel orderOut:nil];}
- (void)showMessage:(NSString*)message {
NSCAssert(NSThread.isMainThread,@"Status HUD requires the main queue");
if(!message.length||!NSApp.isRunning)return;
if(!_panel){
_panel=[[NSPanel alloc]initWithContentRect:NSZeroRect styleMask:NSWindowStyleMaskBorderless|NSWindowStyleMaskNonactivatingPanel backing:NSBackingStoreBuffered defer:NO];
_panel.releasedWhenClosed=NO;_panel.opaque=NO;_panel.backgroundColor=NSColor.clearColor;_panel.hasShadow=NO;_panel.ignoresMouseEvents=YES;_panel.hidesOnDeactivate=YES;_panel.level=NSFloatingWindowLevel;
_panel.collectionBehavior=NSWindowCollectionBehaviorTransient|NSWindowCollectionBehaviorFullScreenAuxiliary;
_panel.contentView.wantsLayer=YES;_panel.contentView.layer.cornerRadius=8;_panel.contentView.layer.backgroundColor=[NSColor colorWithWhite:.08 alpha:.62].CGColor;
_label=[NSTextField wrappingLabelWithString:@""];_label.font=[NSFont systemFontOfSize:12 weight:NSFontWeightMedium];_label.textColor=NSColor.whiteColor;_label.maximumNumberOfLines=2;_label.lineBreakMode=NSLineBreakByTruncatingTail;[_panel.contentView addSubview:_label];
}
NSPoint mouse=NSEvent.mouseLocation;NSScreen *screen=NSScreen.mainScreen;for(NSScreen *candidate in NSScreen.screens)if(NSPointInRect(mouse,candidate.frame)){screen=candidate;break;}
NSRect bounds=screen.visibleFrame;CGFloat width=MIN(420.,MAX(180.,[message sizeWithAttributes:@{NSFontAttributeName:_label.font}].width+24));width=MIN(width,NSWidth(bounds)-24);
CGFloat height=[message boundingRectWithSize:NSMakeSize(width-24,40) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:_label.font}].size.height+18;height=MIN(54.,MAX(34.,height));
// Place below the pointer, with a gap, and keep the entire compact HUD on screen.
CGFloat x=MAX(NSMinX(bounds)+12,MIN(NSMaxX(bounds)-width-12,mouse.x-width/2));CGFloat y=MAX(NSMinY(bounds)+12,MIN(NSMaxY(bounds)-height-12,mouse.y-height-24));
[_panel setFrame:NSMakeRect(x,y,width,height) display:NO];_label.frame=NSMakeRect(12,8,width-24,height-16);_label.stringValue=message;[_panel orderFrontRegardless];
uint64_t generation=++_generation;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,2*NSEC_PER_SEC),dispatch_get_main_queue(),^{if(self->_generation==generation)[self close];});
}
@end