FLEXArgumentInputViewFactory.m 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //
  2. // FLEXArgumentInputViewFactory.m
  3. // FLEXInjected
  4. //
  5. // Created by Ryan Olson on 6/15/14.
  6. //
  7. //
  8. #import "FLEXArgumentInputViewFactory.h"
  9. #import "FLEXArgumentInputView.h"
  10. #import "FLEXArgumentInputObjectView.h"
  11. #import "FLEXArgumentInputNumberView.h"
  12. #import "FLEXArgumentInputSwitchView.h"
  13. #import "FLEXArgumentInputStructView.h"
  14. #import "FLEXArgumentInputNotSupportedView.h"
  15. #import "FLEXArgumentInputStringView.h"
  16. #import "FLEXArgumentInputFontView.h"
  17. #import "FLEXArgumentInputColorView.h"
  18. #import "FLEXArgumentInputDateView.h"
  19. #import "FLEXRuntimeUtility.h"
  20. @implementation FLEXArgumentInputViewFactory
  21. + (FLEXArgumentInputView *)argumentInputViewForTypeEncoding:(const char *)typeEncoding {
  22. return [self argumentInputViewForTypeEncoding:typeEncoding currentValue:nil];
  23. }
  24. + (FLEXArgumentInputView *)argumentInputViewForTypeEncoding:(const char *)typeEncoding currentValue:(id)currentValue {
  25. Class subclass = [self argumentInputViewSubclassForTypeEncoding:typeEncoding currentValue:currentValue];
  26. if (!subclass) {
  27. // Fall back to a FLEXArgumentInputNotSupportedView if we can't find a subclass that fits the type encoding.
  28. // The unsupported view shows "nil" and does not allow user input.
  29. subclass = [FLEXArgumentInputNotSupportedView class];
  30. }
  31. // Remove the field name if there is any (e.g. \"width\"d -> d)
  32. const NSUInteger fieldNameOffset = [FLEXRuntimeUtility fieldNameOffsetForTypeEncoding:typeEncoding];
  33. return [[subclass alloc] initWithArgumentTypeEncoding:typeEncoding + fieldNameOffset];
  34. }
  35. + (Class)argumentInputViewSubclassForTypeEncoding:(const char *)typeEncoding currentValue:(id)currentValue {
  36. // Remove the field name if there is any (e.g. \"width\"d -> d)
  37. const NSUInteger fieldNameOffset = [FLEXRuntimeUtility fieldNameOffsetForTypeEncoding:typeEncoding];
  38. Class argumentInputViewSubclass = nil;
  39. NSArray<Class> *inputViewClasses = @[[FLEXArgumentInputColorView class],
  40. [FLEXArgumentInputFontView class],
  41. [FLEXArgumentInputStringView class],
  42. [FLEXArgumentInputStructView class],
  43. [FLEXArgumentInputSwitchView class],
  44. [FLEXArgumentInputDateView class],
  45. [FLEXArgumentInputNumberView class],
  46. [FLEXArgumentInputObjectView class]];
  47. // Note that order is important here since multiple subclasses may support the same type.
  48. // An example is the number subclass and the bool subclass for the type @encode(BOOL).
  49. // Both work, but we'd prefer to use the bool subclass.
  50. for (Class inputViewClass in inputViewClasses) {
  51. if ([inputViewClass supportsObjCType:typeEncoding + fieldNameOffset withCurrentValue:currentValue]) {
  52. argumentInputViewSubclass = inputViewClass;
  53. break;
  54. }
  55. }
  56. return argumentInputViewSubclass;
  57. }
  58. + (BOOL)canEditFieldWithTypeEncoding:(const char *)typeEncoding currentValue:(id)currentValue {
  59. return [self argumentInputViewSubclassForTypeEncoding:typeEncoding currentValue:currentValue] != nil;
  60. }
  61. @end