Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Four Little Problems

Four Little Problems

Andy Lee talks about four Cocoa programming obstacles he encountered, and how resolved them.

CocoaHeadsNYC

March 14, 2013
Tweet

More Decks by CocoaHeadsNYC

Other Decks in Programming

Transcript

  1. Four Little Problems
    Andy Lee
    for CocoaHeadsNYC, 2013-03-14

    View Slide

  2. Better solutions?

    View Slide

  3. Problem
    Make 3 NSMatrix controls behave as one.

    View Slide

  4. Problem
    Make 3 NSMatrix controls behave as one.

    View Slide

  5. Solution
    Make 3 NSMatrix controls behave as one.
    AKMultiRadioView
    delegate
    target
    Custom superview,
    plus delegation.

    View Slide

  6. Solution
    Make 3 NSMatrix controls behave as one.
    AKMultiRadioView
    delegate
    doRadioAction:
    multiRadioViewDidMakeSelection:
    Your action code
    target

    View Slide

  7. API
    Make 3 NSMatrix controls behave as one.
    @interface AKMultiRadioView : NSView
    @protocol AKMultiRadioViewDelegate

    View Slide

  8. API
    Make 3 NSMatrix controls behave as one.
    @interface AKMultiRadioView : NSView
    - id delegate;
    - (NSInteger)selectedTag;
    - (BOOL)selectCellWithTag:(NSInteger)tag;
    - (IBAction)doRadioAction:(id)sender;
    @protocol AKMultiRadioViewDelegate
    - multiRadioViewDidMakeSelection:
    (AKMultiRadioView *)mrv

    View Slide

  9. How to use
    Make 3 NSMatrix controls behave as one.
    • Set the superview class in IB.
    • Drop in the NSMatrix instances.
    • Use unique cell tags.
    • Connect targets and delegate.
    • Put action code in the delegate method.

    View Slide

  10. Problem
    Set text color for disabled buttons.
    Apple’s colors
    The colors I want
    (Those triangles are actually characters.)
    (Yeah, I know the difference is subtle.)

    View Slide

  11. Solution
    Set text color for disabled buttons.
    @interface AKButtonCell : NSButtonCell
    // AppKit passes in an all-gray string.
    // Override to change the string color.
    // Then call super.
    - (NSRect)drawTitle:(NSAttributedString *)title
    withFrame:(NSRect)frame
    inView:(NSView *)controlView;

    View Slide

  12. How to use
    Set text color for disabled buttons.
    • Select your button’s cell in IB.
    • Set its class to AKButtonCell.
    • Set custom colors if you want.

    View Slide

  13. Credit
    Set text color for disabled buttons.
    http://stackoverflow.com/questions/
    6370500/nsbutton-set-text-color-in-
    disabled-mode/10632311#10632311

    View Slide

  14. Problem
    Focus rings where and when I want them.
    Standard focus ring bleeds.
    WebViews have no focus ring.
    I don’t like standard look.

    View Slide

  15. Solution
    Focus rings where and when I want them.
    AKFocusView
    Wrap views in custom superview.
    AKFocusView
    table view web view

    View Slide

  16. How to use
    Focus rings where and when I want them.
    • Embed any view in an AKFocusView.
    • No code required.
    • Redraws automatically as needed.
    • Draws focus ring as solid border.
    • Suppresses normal AppKit focus ring.

    View Slide

  17. Problem
    Control over key view loop.

    View Slide

  18. Problem
    Control over key view loop.
    • NSBrowser not included by default.
    • Views in drawers not included.
    • Sometimes trapped in toolbar.

    View Slide

  19. Solution
    Control over key view loop.
    @interface AKWindow : NSWindow
    - (void)setTabChain:(NSArray *)views;
    - (BOOL)handlePossibleTabChainEvent:(NSEvent *)e;
    //In NSApplication subclass, override:
    - (void)sendEvent:(NSEvent *)anEvent;

    View Slide

  20. Solution
    Control over key view loop.
    • I currently hack toolbar buttons in.
    • Should go into a separate class.
    • Note that [self window] may be a drawer.
    • Note Full Keyboard Access.

    View Slide

  21. All code is on github:
    aglee/appkido

    View Slide

  22. Demo

    View Slide

  23. That’s all, thanks!

    View Slide