From b80d60db575ad88931e8dae0b2494de87d7f0e8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Boris=20Du=C5=A1ek?= Date: Mon, 31 Mar 2014 21:41:34 +0200 Subject: [PATCH] Add OakSetAccessibilityLabel function This function makes it easy to set accessibility "labels" for UI elements. It is flexible - takes care of NSView vs. NSControl case, labels can be specified directly as a string or as a user interface element. --- .../src/OakUIConstructionFunctions.h | 1 + .../src/OakUIConstructionFunctions.mm | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/Frameworks/OakAppKit/src/OakUIConstructionFunctions.h b/Frameworks/OakAppKit/src/OakUIConstructionFunctions.h index af60fd34..8862221b 100644 --- a/Frameworks/OakAppKit/src/OakUIConstructionFunctions.h +++ b/Frameworks/OakAppKit/src/OakUIConstructionFunctions.h @@ -16,3 +16,4 @@ PUBLIC NSImageView* OakCreateDividerImageView (); PUBLIC NSBox* OakCreateViewWithColor (NSColor* color = nil, NSColor* secondaryColor = nil); PUBLIC NSBox* OakCreateVerticalLine (NSColor* primaryColor, NSColor* secondaryColor = nil); PUBLIC NSBox* OakCreateHorizontalLine (NSColor* primaryColor, NSColor* secondaryColor = nil); +PUBLIC BOOL OakSetAccessibilityLabel (NSObject* element, NSObject* label); diff --git a/Frameworks/OakAppKit/src/OakUIConstructionFunctions.mm b/Frameworks/OakAppKit/src/OakUIConstructionFunctions.mm index 7ea5741c..ef632a84 100644 --- a/Frameworks/OakAppKit/src/OakUIConstructionFunctions.mm +++ b/Frameworks/OakAppKit/src/OakUIConstructionFunctions.mm @@ -211,3 +211,23 @@ NSImageView* OakCreateDividerImageView () [res setContentCompressionResistancePriority:NSLayoutPriorityDefaultLow forOrientation:NSLayoutConstraintOrientationVertical]; return res; } + +BOOL OakSetAccessibilityLabel (NSObject* element, NSObject* label) +{ + if([element isKindOfClass:[NSControl class]]) + element = [(NSControl*)element cell] ?: element; // e.g. NSTableView is an NSControl with a nil cell + else if(!([element isKindOfClass:[NSView class]] || [element isKindOfClass:[NSCell class]])) + return NO; + + NSString* attribute = NSAccessibilityDescriptionAttribute; + if([label isKindOfClass:[NSView class]] || [label isKindOfClass:[NSCell class]]) + { + attribute = NSAccessibilityTitleUIElementAttribute; + if([label isKindOfClass:[NSControl class]]) + label = [(NSControl*)label cell] ?: label; + } + else if(![label isKindOfClass:[NSString class]]) + return NO; + + return [element accessibilitySetOverrideValue:label forAttribute:attribute]; +}