소스 검색

UI: Add status overlay for macOS dock icon

Adds an implementation for the dock icon status overlay on macOS (which
doesn't rely on QtMacExtras, which means it also works on Qt6).
gxalpha 3 년 전
부모
커밋
eea2fd2f3f
1개의 변경된 파일50개의 추가작업 그리고 2개의 파일을 삭제
  1. 50 2
      UI/platform-osx.mm

+ 50 - 2
UI/platform-osx.mm

@@ -202,9 +202,57 @@ void EnableOSXDockIcon(bool enable)
 				NSApplicationActivationPolicyProhibited];
 }
 
-// Not implemented yet
+@interface DockView : NSView {
+@private
+	QIcon icon;
+}
+@end
+
+@implementation DockView
+- (id)initWithIcon:(QIcon)icon
+{
+	self = [super init];
+	self->icon = icon;
+	return self;
+}
+- (void)drawRect:(NSRect)dirtyRect
+{
+	CGSize size = dirtyRect.size;
+
+	/* Draw regular app icon */
+	NSImage *appIcon = [[NSWorkspace sharedWorkspace]
+		iconForFile:[[NSBundle mainBundle] bundlePath]];
+	[appIcon drawInRect:CGRectMake(0, 0, size.width, size.height)];
+
+	/* Draw small icon on top */
+	float iconSize = 0.45;
+	CGImageRef image =
+		icon.pixmap(size.width, size.height).toImage().toCGImage();
+	CGContextRef context = [[NSGraphicsContext currentContext] CGContext];
+	CGContextDrawImage(context,
+			   CGRectMake(size.width * (1 - iconSize), 0,
+				      size.width * iconSize,
+				      size.height * iconSize),
+			   image);
+	CGImageRelease(image);
+}
+@end
+
 void TaskbarOverlayInit() {}
-void TaskbarOverlaySetStatus(TaskbarOverlayStatus) {}
+void TaskbarOverlaySetStatus(TaskbarOverlayStatus status)
+{
+	QIcon icon;
+	if (status == TaskbarOverlayStatusActive)
+		icon = QIcon::fromTheme("obs-active",
+					QIcon(":/res/images/active_mac.png"));
+	else if (status == TaskbarOverlayStatusPaused)
+		icon = QIcon::fromTheme("obs-paused",
+					QIcon(":/res/images/paused_mac.png"));
+
+	NSDockTile *tile = [NSApp dockTile];
+	[tile setContentView:[[DockView alloc] initWithIcon:icon]];
+	[tile display];
+}
 
 /*
  * This custom NSApplication subclass makes the app compatible with CEF. Qt