git.y1.nz

SameBoy

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

Cocoa/NSTextFieldCell+Inset.m

      1 #import "NSTextFieldCell+Inset.h"
      2 #import <AppKit/AppKit.h>
      3 #import <objc/runtime.h>
      4 
      5 @interface NSTextFieldCell ()
      6 @property NSSize textInset;
      7 - (bool)_isEditingInView:(NSView *)view;
      8 @end
      9 
     10 @implementation NSTextFieldCell (Inset)
     11 
     12 - (void)setTextInset:(NSSize)textInset
     13 {
     14     objc_setAssociatedObject(self, @selector(textInset), @(textInset), OBJC_ASSOCIATION_RETAIN);
     15 }
     16 
     17 - (NSSize)textInset
     18 {
     19     return [objc_getAssociatedObject(self, _cmd) sizeValue];
     20 }
     21 
     22 - (void)drawWithFrameHook:(NSRect)cellFrame inView:(NSView *)controlView
     23 {
     24     NSSize inset = self.textInset;
     25     if (self.drawsBackground) {
     26         [self.backgroundColor setFill];
     27         if ([self _isEditingInView:controlView]) {
     28             NSRectFill(cellFrame);
     29         }
     30         else {
     31             NSRectFill(NSMakeRect(cellFrame.origin.x, cellFrame.origin.y,
     32                                   cellFrame.size.width, inset.height));
     33             NSRectFill(NSMakeRect(cellFrame.origin.x, cellFrame.origin.y + cellFrame.size.height - inset.height,
     34                                   cellFrame.size.width, inset.height));
     35             
     36             NSRectFill(NSMakeRect(cellFrame.origin.x, cellFrame.origin.y + inset.height,
     37                                   inset.width, cellFrame.size.height - inset.height * 2));
     38             NSRectFill(NSMakeRect(cellFrame.origin.x + cellFrame.size.width - inset.width, cellFrame.origin.y + inset.height,
     39                                   inset.width, cellFrame.size.height - inset.height * 2));
     40         }
     41     }
     42     cellFrame.origin.x += inset.width;
     43     cellFrame.origin.y += inset.height;
     44     cellFrame.size.width -= inset.width * 2;
     45     cellFrame.size.height -= inset.height * 2;
     46     [self drawWithFrameHook:cellFrame inView:controlView];
     47 }
     48 
     49 + (void)load
     50 {
     51     method_exchangeImplementations(class_getInstanceMethod(self, @selector(drawWithFrame:inView:)),
     52                                    class_getInstanceMethod(self, @selector(drawWithFrameHook:inView:)));
     53 }
     54 
     55 @end
     56 
     57 
     58 @implementation NSTextField (Inset)
     59 
     60 - (bool)wantsUpdateLayerHook
     61 {
     62     CGSize inset = ((NSTextFieldCell *)self.cell).textInset;
     63     if (inset.width || inset.height) return false;
     64     return [self wantsUpdateLayerHook];
     65 }
     66 
     67 + (void)load
     68 {
     69     Method method = class_getInstanceMethod(self, @selector(wantsUpdateLayer));
     70     if (class_addMethod(self, @selector(wantsUpdateLayer), method_getImplementation(method), method_getTypeEncoding(method))) {
     71         method = class_getInstanceMethod(self, @selector(wantsUpdateLayer));
     72     }
     73     method_exchangeImplementations(method,
     74                                    class_getInstanceMethod(self, @selector(wantsUpdateLayerHook)));
     75 }
     76 
     77 @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.