Skip to content

Instantly share code, notes, and snippets.

@nek023
Last active April 5, 2022 11:34
Show Gist options
  • Save nek023/3244601c8dc779484076 to your computer and use it in GitHub Desktop.
Save nek023/3244601c8dc779484076 to your computer and use it in GitHub Desktop.
Keep NSStatusBarButton highlighted
#import "NSStatusBarButtonCell.h"
@interface NSStatusBarButtonCell (ForciblyHighlighted)
@property (nonatomic, assign, getter=isForciblyHighlighted) BOOL forciblyHighlighted;
@end
#import "NSStatusBarButtonCell+ForciblyHighlighted.h"
#import <objc/runtime.h>
static char kQBForciblyHighlightedKey;
static void QBExchangeImplementations(Class cls, SEL srcSel, SEL dstSel)
{
Method srcMethod = class_getInstanceMethod(cls, srcSel);
Method dstMethod = class_getInstanceMethod(cls, dstSel);
if (class_addMethod(cls, srcSel, method_getImplementation(dstMethod), method_getTypeEncoding(dstMethod))) {
class_replaceMethod(cls, dstSel, method_getImplementation(srcMethod), method_getTypeEncoding(srcMethod));
} else {
method_exchangeImplementations(srcMethod, dstMethod);
}
}
@implementation NSStatusBarButtonCell (ForciblyHighlighted)
+ (void)load
{
QBExchangeImplementations(self, @selector(isHighlighted), @selector(qb_isHighlighted));
}
#pragma mark - Accessors
- (void)setForciblyHighlighted:(BOOL)forciblyHighlighted
{
objc_setAssociatedObject(self, &kQBForciblyHighlightedKey, @(forciblyHighlighted), OBJC_ASSOCIATION_ASSIGN);
// Redraw control to apply highlight state
[self.controlView setNeedsDisplay:YES];
}
- (BOOL)isForciblyHighlighted
{
return [objc_getAssociatedObject(self, &kQBForciblyHighlightedKey) boolValue];
}
#pragma mark - Getting Highlight State
- (BOOL)qb_isHighlighted
{
return ([self isForciblyHighlighted]) ? YES : [self qb_isHighlighted];
}
@end
#import <AppKit/NSButtonCell.h>
@class NSMenu, NSStatusBar;
@interface NSStatusBarButtonCell : NSButtonCell
{
NSStatusBar *_fStatusBar;
NSMenu *_fStatusMenu;
BOOL _fHighlightMode;
BOOL _fDoubleClick;
SEL _fDoubleAction;
}
+ (void)popupStatusBarMenu:(id)arg1 inRect:(struct CGRect)arg2 ofView:(id)arg3 withEvent:(id)arg4;
- (BOOL)_sendActionFrom:(id)arg1;
- (void)dismiss;
- (void)performClick:(id)arg1;
- (BOOL)trackMouse:(id)arg1 inRect:(struct CGRect)arg2 ofView:(id)arg3 untilMouseUp:(BOOL)arg4;
- (void)_fillBackground:(struct CGRect)arg1 withAlternateColor:(BOOL)arg2;
- (void)drawWithFrame:(struct CGRect)arg1 inView:(id)arg2;
- (long long)_stateForDrawing;
- (BOOL)_isExitFullScreenButton;
- (struct CGRect)drawTitle:(id)arg1 withFrame:(struct CGRect)arg2 inView:(id)arg3;
- (long long)interiorBackgroundStyle;
- (BOOL)acceptsFirstResponder;
- (void)setDoubleAction:(SEL)arg1;
- (SEL)doubleAction;
- (void)setHighlightMode:(BOOL)arg1;
- (BOOL)highlightMode;
- (void)setStatusMenu:(id)arg1;
- (id)statusMenu;
- (id)statusBar;
- (void)setStatusBar:(id)arg1;
- (void)dealloc;
- (id)init;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment