git.y1.nz

SameBoy

Accurate GB/GBC emulator
download: https://git.y1.nz/archives/sameboy.tar.gz
README | Files | Log | Refs | LICENSE

Cocoa/SolariumFixer.m

      1 #import <Cocoa/Cocoa.h>
      2 #import <objc/runtime.h>
      3 
      4 // Comment out to debug
      5 #define NSLog(...)
      6 
      7 // Solarium has weird proportions, we need to fix them.
      8 @implementation NSControl (SolariumFixer)
      9 
     10 - (void)awakeFromNib
     11 {
     12     if (@available(macOS 26.0, *)) {
     13         if ([self.superview isKindOfClass:objc_getClass("NSToolbarItemViewer")]) return;
     14         
     15         if ([self isKindOfClass:[NSStepper class]]) {
     16             NSLog(@"Stepper needs adjustment: %s in window %@", sel_getName(self.action), self.window.title);
     17             CGRect frame = self.frame;
     18             frame.origin.y += 1;
     19             self.frame = frame;
     20             return;
     21         }
     22         if (self.controlSize != NSControlSizeRegular) return;
     23         
     24         if ([self isKindOfClass:[NSPopUpButton class]] ) {
     25             NSLog(@"Popup button needs adjustment: %@ in window %@", ((NSButton *)self).title, self.window.title);
     26             CGRect frame = self.frame;
     27             if (frame.size.height != 25) {
     28                 NSLog(@"%@ in window %@ has the wrong height!", ((NSButton *)self).title, self.window.title);
     29                 return;
     30             }
     31             frame.size.width -= 2 + 5; // Remove 5 from the right and 2 from the left
     32             frame.origin.x += 2;
     33             frame.origin.y += 2;
     34             self.frame = frame;
     35         }
     36         else if ([self isKindOfClass:[NSSegmentedControl class]] ) {
     37             NSLog(@"Segmented button needs adjustment: %s in window %@", sel_getName(self.action), self.window.title);
     38             CGRect frame = self.frame;
     39             if (frame.size.height != 25) {
     40                 NSLog(@"%s in window %@ has the wrong height!", sel_getName(self.action), self.window.title);
     41                 return;
     42             }
     43             frame.origin.x += 8;
     44             frame.origin.y += 1;
     45             self.frame = frame;
     46         }
     47         else if ([self isKindOfClass:[NSTextField class]]) {
     48             NSTextField *field = (id)self;
     49             if (![field isBezeled]) return;
     50             NSLog(@"Text field needs adjustment: %@ in window %@", ((NSTextField *)self).placeholderString, self.window.title);
     51             CGRect frame = self.frame;
     52             if (frame.size.height == 21) {
     53                 frame.size.height = 24;
     54             }
     55             else {
     56                 NSLog(@"%@ in window %@ has the wrong height!", ((NSTextField *)self).placeholderString, self.window.title);
     57                 return;
     58             }
     59             frame.size.width -= 1 + 1; // Remove 1 from the right and 1 from the left
     60             frame.origin.x += 1;
     61             frame.origin.y -= 1;
     62 
     63             self.frame = frame;
     64         }
     65         else if ([self isKindOfClass:[NSButton class]]) {
     66             NSLog(@"Button: %@ in window %@", @(sel_getName(self.action)), self.window.title);
     67             NSButton *button = (id)self;
     68             if (!button.isBordered) return;
     69             if (button.bezelStyle == NSRoundedBezelStyle) {
     70                 NSLog(@"Button needs adjustment: %@ in window %@", @(sel_getName(self.action)), self.window.title);
     71                 CGRect frame = self.frame;
     72                 frame.size.width -= 7 + 7; // Remove 7 from the right and 7 from the left
     73                 frame.origin.x += 7;
     74                 frame.origin.y += 5;
     75                 if (frame.size.height == 32) {
     76                     frame.size.height = 25;
     77                 }
     78                 else {
     79                     NSLog(@"%@ in window %@ has the wrong height!", @(sel_getName(self.action)), self.window.title);
     80                 }                self.frame = frame;
     81             }
     82             else if (button.bezelStyle == NSBezelStyleRegularSquare) {
     83                 CGRect frame = self.frame;
     84                 if (frame.size.height != 18) return;
     85                 NSLog(@"Check/Radio needs adjustment: %@ in window %@", ((NSButton *)self).title, self.window.title);
     86                 frame.size.width -= 2;
     87                 frame.origin.x += 2;
     88                 frame.origin.y += 1;
     89                 frame.size.height = 16;
     90                 self.frame = frame;
     91             }
     92             else if (button.bezelStyle == NSBezelStyleHelpButton) {
     93                 CGRect frame = self.frame;
     94                 NSLog(@"Help button needs adjustment: %@ in window %@", ((NSButton *)self).title, self.window.title);
     95                 frame.origin.y += 2;
     96                 self.frame = frame;
     97             }
     98         }
     99         else if ([self isKindOfClass:[NSSlider class]]) {
    100             NSLog(@"Slider needs adjustment: %s in window %@", sel_getName(self.action), self.window.title);
    101             CGRect frame = self.frame;
    102             frame.origin.y += 3;
    103             self.frame = frame;
    104         }
    105     }
    106 }
    107 
    108 @end
    109 
    110 @implementation NSToolbarItem (SolariumFixer)
    111 
    112 static CGSize minSizeHook(id self, SEL _cmd)
    113 {
    114     return CGSizeMake(8, 0);
    115 }
    116 
    117 - (void)awakeFromNib
    118 {
    119     if (@available(macOS 26.0, *)) {
    120         NSLog(@"Toolbar item %@ has view %@", self.label, self.view);
    121         if ([self.view isKindOfClass:[NSTextField class]]) {
    122             NSLog(@"Handling (Text field)");
    123             self.bordered = true;
    124             
    125             NSSize maxSize = self.maxSize;
    126             maxSize.height = 36;
    127             self.maxSize = maxSize;
    128             
    129             NSSize minSize = self.minSize;
    130             minSize.height = 36;
    131             self.minSize = minSize;
    132             
    133             ((NSTextField *)self.view).backgroundColor = [NSColor clearColor];
    134             ((NSTextField *)self.view).bezeled = false;
    135             ((NSTextField *)self.view).bordered = true;
    136 
    137             // Work around even more AppKit bugs
    138             self.toolbar.displayMode++;
    139             self.toolbar.displayMode--;
    140         }
    141         else if ([self.view isKindOfClass:[NSPopUpButton class]]) {
    142             NSLog(@"Handling (Pop up button)");
    143             self.bordered = true;
    144             
    145             NSSize maxSize = self.maxSize;
    146             maxSize.height = 28;
    147             self.maxSize = maxSize;
    148             
    149             NSSize minSize = self.minSize;
    150             minSize.height = 28;
    151             self.minSize = minSize;
    152         }
    153     }
    154     else if (@available(macOS 11.0, *)) { // While at it, make macOS 11-15 a bit more consistent
    155         if ([self.view isKindOfClass:[NSTextField class]]) {
    156             ((NSTextField *)self.view).bezelStyle = NSTextFieldRoundedBezel;
    157         }
    158     }
    159 }
    160 
    161 + (void)load
    162 {
    163     if (@available(macOS 26.0, *)) {
    164         method_setImplementation(class_getInstanceMethod(objc_getClass("NSToolbarFlexibleSpaceItem"), @selector(minSize)),
    165                                  (void *)minSizeHook);
    166     }
    167 }
    168 @end

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.