motion-kit 0.0.1 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +839 -0
  3. data/lib/motion-kit-cocoa/cocoa_util.rb +59 -0
  4. data/lib/motion-kit-cocoa/constraints/constraint.rb +765 -0
  5. data/lib/motion-kit-cocoa/constraints/constraint_placeholder.rb +22 -0
  6. data/lib/motion-kit-cocoa/constraints/constraints_layout.rb +536 -0
  7. data/lib/motion-kit-cocoa/constraints/constraints_target.rb +52 -0
  8. data/lib/motion-kit-cocoa/layouts/cagradientlayer_layout.rb +13 -0
  9. data/lib/motion-kit-cocoa/layouts/calayer_layout.rb +27 -0
  10. data/lib/motion-kit-cocoa/layouts/sugarcube_compat.rb +38 -0
  11. data/lib/motion-kit-ios/dummy.rb +93 -0
  12. data/lib/motion-kit-ios/ios_util.rb +20 -0
  13. data/lib/motion-kit-ios/layouts/constraints_layout.rb +22 -0
  14. data/lib/motion-kit-ios/layouts/layout_device.rb +32 -0
  15. data/lib/motion-kit-ios/layouts/layout_orientation.rb +47 -0
  16. data/lib/motion-kit-ios/layouts/uibutton_layout.rb +52 -0
  17. data/lib/motion-kit-ios/layouts/uiview_layout.rb +45 -0
  18. data/lib/motion-kit-ios/layouts/uiview_layout_autoresizing.rb +75 -0
  19. data/lib/motion-kit-ios/layouts/uiview_layout_constraints.rb +36 -0
  20. data/lib/motion-kit-ios/layouts/uiview_layout_frame.rb +307 -0
  21. data/lib/motion-kit-ios/layouts/uiview_layout_gradient.rb +20 -0
  22. data/lib/motion-kit-osx/dummy.rb +89 -0
  23. data/lib/motion-kit-osx/layouts/constraints_layout.rb +42 -0
  24. data/lib/motion-kit-osx/layouts/nsmenu_extensions.rb +186 -0
  25. data/lib/motion-kit-osx/layouts/nsmenu_layout.rb +109 -0
  26. data/lib/motion-kit-osx/layouts/nsmenuitem_extensions.rb +45 -0
  27. data/lib/motion-kit-osx/layouts/nstablecolumn_layout.rb +12 -0
  28. data/lib/motion-kit-osx/layouts/nstableview_layout.rb +20 -0
  29. data/lib/motion-kit-osx/layouts/nsview_layout.rb +47 -0
  30. data/lib/motion-kit-osx/layouts/nsview_layout_autoresizing.rb +75 -0
  31. data/lib/motion-kit-osx/layouts/nsview_layout_constraints.rb +34 -0
  32. data/lib/motion-kit-osx/layouts/nsview_layout_frame.rb +375 -0
  33. data/lib/motion-kit-osx/layouts/nswindow_frame.rb +14 -0
  34. data/lib/motion-kit-osx/layouts/nswindow_layout.rb +77 -0
  35. data/lib/motion-kit-osx/osx_util.rb +16 -0
  36. data/lib/motion-kit.rb +33 -0
  37. data/lib/motion-kit/calculate.rb +263 -0
  38. data/lib/motion-kit/layouts/base_layout.rb +299 -0
  39. data/lib/motion-kit/layouts/base_layout_class_methods.rb +43 -0
  40. data/lib/motion-kit/layouts/parent.rb +68 -0
  41. data/lib/motion-kit/layouts/view_layout.rb +327 -0
  42. data/lib/motion-kit/motion-kit.rb +18 -0
  43. data/lib/motion-kit/object.rb +16 -0
  44. data/lib/motion-kit/util.rb +20 -0
  45. data/lib/motion-kit/version.rb +1 -1
  46. data/spec/ios/apply_styles_spec.rb +21 -0
  47. data/spec/ios/autoresizing_helper_spec.rb +224 -0
  48. data/spec/ios/calculate_spec.rb +322 -0
  49. data/spec/ios/calculator_spec.rb +31 -0
  50. data/spec/ios/constraints_helpers/attribute_lookup_spec.rb +27 -0
  51. data/spec/ios/constraints_helpers/axis_lookup_spec.rb +13 -0
  52. data/spec/ios/constraints_helpers/center_constraints_spec.rb +419 -0
  53. data/spec/ios/constraints_helpers/constraint_placeholder_spec.rb +72 -0
  54. data/spec/ios/constraints_helpers/priority_lookup_spec.rb +19 -0
  55. data/spec/ios/constraints_helpers/relationship_lookup_spec.rb +27 -0
  56. data/spec/ios/constraints_helpers/relative_corners_spec.rb +274 -0
  57. data/spec/ios/constraints_helpers/relative_location_spec.rb +111 -0
  58. data/spec/ios/constraints_helpers/simple_constraints_spec.rb +2763 -0
  59. data/spec/ios/constraints_helpers/size_constraints_spec.rb +422 -0
  60. data/spec/ios/constraints_helpers/view_lookup_constraints_spec.rb +93 -0
  61. data/spec/ios/create_layout_spec.rb +40 -0
  62. data/spec/ios/custom_layout_spec.rb +13 -0
  63. data/spec/ios/deferred_spec.rb +89 -0
  64. data/spec/ios/device_helpers_spec.rb +51 -0
  65. data/spec/ios/frame_helper_spec.rb +1150 -0
  66. data/spec/ios/layer_layout_spec.rb +36 -0
  67. data/spec/ios/layout_extensions_spec.rb +70 -0
  68. data/spec/ios/layout_spec.rb +74 -0
  69. data/spec/ios/layout_state_spec.rb +27 -0
  70. data/spec/ios/motionkit_util_spec.rb +102 -0
  71. data/spec/ios/objc_selectors_spec.rb +10 -0
  72. data/spec/ios/orientation_helper_specs.rb +67 -0
  73. data/spec/ios/parent_layout_spec.rb +19 -0
  74. data/spec/ios/parent_spec.rb +45 -0
  75. data/spec/ios/remove_layout_spec.rb +25 -0
  76. data/spec/ios/root_layout_spec.rb +53 -0
  77. data/spec/ios/setters_spec.rb +63 -0
  78. data/spec/ios/uibutton_layout_spec.rb +24 -0
  79. data/spec/ios/uitextfield_spec.rb +14 -0
  80. data/spec/ios/view_attr_spec.rb +25 -0
  81. data/spec/osx/autoresizing_helper_spec.rb +224 -0
  82. data/spec/osx/constraints_helper_spec.rb +0 -0
  83. data/spec/osx/constraints_helpers/orientation_lookup_spec.rb +13 -0
  84. data/spec/osx/constraints_helpers/simple_constraints_spec.rb +2095 -0
  85. data/spec/osx/constraints_helpers/size_constraints_spec.rb +362 -0
  86. data/spec/osx/create_menu_spec.rb +14 -0
  87. data/spec/osx/create_via_extensions_spec.rb +63 -0
  88. data/spec/osx/deferred_spec.rb +85 -0
  89. data/spec/osx/frame_helper_spec.rb +1881 -0
  90. data/spec/osx/menu_extensions_spec.rb +376 -0
  91. data/spec/osx/menu_layout_spec.rb +157 -0
  92. data/spec/osx/menu_spec.rb +70 -0
  93. data/spec/osx/root_menu_spec.rb +15 -0
  94. metadata +166 -14
@@ -0,0 +1,52 @@
1
+ module MotionKit
2
+ class ConstraintsTarget
3
+ attr :view
4
+
5
+ def initialize(view)
6
+ @view = view
7
+ @constraints = []
8
+ end
9
+
10
+ def add_constraints(constraints)
11
+ @constraints.concat(constraints)
12
+ end
13
+
14
+ def apply_all_constraints(layout, target)
15
+ @constraints.map do |mk_constraint|
16
+ mk_constraint.resolve_all(layout, target).map do |constraint|
17
+ base_view_class = MotionKit.base_view_class
18
+
19
+ if constraint.firstItem.is_a?(base_view_class) && constraint.secondItem.is_a?(base_view_class)
20
+ common_ancestor = nil
21
+
22
+ ancestors = [constraint.firstItem]
23
+ parent_view = constraint.firstItem
24
+ while parent_view = parent_view.superview
25
+ ancestors << parent_view
26
+ end
27
+
28
+ current_view = constraint.secondItem
29
+ while current_view
30
+ if ancestors.include? current_view
31
+ common_ancestor = current_view
32
+ break
33
+ end
34
+ current_view = current_view.superview
35
+ end
36
+
37
+ unless common_ancestor
38
+ raise NoCommonAncestorError.new("No common ancestors between #{constraint.firstItem} and #{constraint.secondItem}")
39
+ end
40
+ else
41
+ common_ancestor = constraint.firstItem
42
+ end
43
+
44
+ common_ancestor.addConstraint(constraint)
45
+ constraint
46
+ end
47
+ end.flatten
48
+ end
49
+
50
+ end
51
+
52
+ end
@@ -0,0 +1,13 @@
1
+ # @provides MotionKit::CAGradientLayerLayout
2
+ # @requires MotionKit::CALayerLayout
3
+ # @requires MotionKit::ViewLayout
4
+ module MotionKit
5
+ class CAGradientLayerLayout < CALayerLayout
6
+ targets CAGradientLayer
7
+
8
+ def colors(values)
9
+ target.colors = values.map { |color| color.is_a?(UIColor) ? color.CGColor : color }
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,27 @@
1
+ # @provides MotionKit::CALayerLayout
2
+ # @requires MotionKit::ViewLayout
3
+ module MotionKit
4
+ class CALayerLayout < ViewLayout
5
+ targets CALayer
6
+
7
+ # platform specific default root view
8
+ def default_root
9
+ self.class.targets.layer
10
+ end
11
+
12
+ def add_child(subview)
13
+ target.addSublayer(subview)
14
+ end
15
+
16
+ def remove_child(subview)
17
+ subview.removeFromSuperlayer
18
+ end
19
+
20
+ # a more appropriate name for the root layer
21
+ def layer
22
+ self.view
23
+ end
24
+
25
+ end
26
+
27
+ end
@@ -0,0 +1,38 @@
1
+ # @requires MotionKit::BaseLayout
2
+ module MotionKit
3
+ class BaseLayout
4
+
5
+ def frame(*values)
6
+ apply(:frame, *values)
7
+ end
8
+
9
+ def left(*values)
10
+ apply(:left, *values)
11
+ end
12
+
13
+ def right(*values)
14
+ apply(:right, *values)
15
+ end
16
+
17
+ def up(*values)
18
+ apply(:up, *values)
19
+ end
20
+
21
+ def down(*values)
22
+ apply(:down, *values)
23
+ end
24
+
25
+ def origin(*values)
26
+ apply(:origin, *values)
27
+ end
28
+
29
+ def size(*values)
30
+ apply(:size, *values)
31
+ end
32
+
33
+ def center(*values)
34
+ apply(:center, *values)
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,93 @@
1
+ class DummyView < UIView
2
+ private
3
+ def dummy
4
+ setFrame(nil)
5
+ setOpaque(nil)
6
+ setClipsToBounds(nil)
7
+ setUserInteractionEnabled(nil)
8
+ UIView.appearanceWhenContainedIn(UIView, nil)
9
+ UIView.appearance
10
+ end
11
+ end
12
+
13
+ class DummyTableView < UITableView
14
+ private
15
+ def dummy
16
+ setAllowSelection(value)
17
+ end
18
+ end
19
+
20
+ class DummyButton < UIButton
21
+ private
22
+ def dummy
23
+ setTitleEdgeInsets(nil)
24
+ end
25
+ end
26
+
27
+ class DummyScrollView < UIScrollView
28
+ private
29
+ def dummy
30
+ setPagingEnabled(nil)
31
+ setScrollEnabled(nil)
32
+ setShowsHorizontalScrollIndicator(nil)
33
+ setShowsVerticalScrollIndicator(nil)
34
+ end
35
+ end
36
+
37
+ class DummyActivityIndicatorView < UIActivityIndicatorView
38
+ private
39
+ def dummy
40
+ setHidesWhenStopped(nil)
41
+ end
42
+ end
43
+
44
+ class DummyLabel < UILabel
45
+ private
46
+ def dummy
47
+ setAdjustsFontSizeToFitWidth(nil)
48
+ end
49
+ end
50
+
51
+ class DummyTextField < UITextField
52
+ private
53
+ def dummy
54
+ setSecureTextEntry(nil)
55
+ setReturnKeyType(nil)
56
+ setAutocapitalizationType(nil)
57
+ setAutocorrectionType(nil)
58
+ setSpellCheckingType(nil)
59
+ end
60
+ end
61
+
62
+ class DummyPickerView < UIPickerView
63
+ private
64
+ def dummy
65
+ setShowsSelectionIndicator(nil)
66
+ end
67
+ end
68
+
69
+ class DummyLayer < CALayer
70
+ private
71
+ def dummy
72
+ setCornerRadius(nil)
73
+ setTransform(nil)
74
+ setMasksToBounds(nil)
75
+ setShadowOffset(nil)
76
+ setShadowOpacity(nil)
77
+ setShadowRadius(nil)
78
+ setShadowOffset(nil)
79
+ setShadowColor(nil)
80
+ setShadowPath(nil)
81
+ setOpaque(nil)
82
+ setTranslucent(nil)
83
+ end
84
+ end
85
+
86
+ class DummySwitch < UISwitch
87
+ private
88
+ def dummy
89
+ on?
90
+ setOn(true)
91
+ end
92
+ end
93
+
@@ -0,0 +1,20 @@
1
+ module MotionKit
2
+ module_function
3
+
4
+ def appearance_class
5
+ @appearance_klass ||= UIView.appearance.class
6
+ end
7
+
8
+ def base_view_class
9
+ UIView
10
+ end
11
+
12
+ def default_view_class
13
+ UIView
14
+ end
15
+
16
+ def no_intrinsic_metric
17
+ UIViewNoIntrinsicMetric
18
+ end
19
+
20
+ end
@@ -0,0 +1,22 @@
1
+ # @requires MotionKit::ConstraintsLayout
2
+ module MotionKit
3
+ class ConstraintsLayout
4
+
5
+ def content_compression_resistance_priority(value, for_axis: axis)
6
+ axis = Constraint.axis_lookup(axis)
7
+ end
8
+
9
+ def compression_priority(value, for_axis: axis)
10
+ content_compression_resistance_priority(value, for_axis: axis)
11
+ end
12
+
13
+ def content_hugging_priority(value, for_axis: axis)
14
+ axis = Constraint.axis_lookup(axis)
15
+ end
16
+
17
+ def hugging_priority(value, for_axis: axis)
18
+ content_hugging_priority(value, for_axis: axis)
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,32 @@
1
+ # @requires MotionKit::BaseLayout
2
+ module MotionKit
3
+ class BaseLayout
4
+
5
+ def iphone?
6
+ UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPhone
7
+ end
8
+
9
+ def iphone4?
10
+ iphone? && UIScreen.mainScreen.bounds.size.width == 320 && UIScreen.mainScreen.bounds.size.height == 568
11
+ end
12
+
13
+ def iphone35?
14
+ iphone? && UIScreen.mainScreen.bounds.size.width == 320 && UIScreen.mainScreen.bounds.size.height == 480
15
+ end
16
+
17
+ def ipad?
18
+ UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad
19
+ end
20
+
21
+ def retina?
22
+ UIScreen.mainScreen.respond_to?(:scale) && UIScreen.mainScreen.scale == 2
23
+ end
24
+
25
+ [:iphone, :iphone4, :iphone35, :ipad, :retina].each do |method_name|
26
+ define_method(method_name) do |&block|
27
+ block.call if self.send("#{method_name}?")
28
+ end
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,47 @@
1
+ # @requires MotionKit::BaseLayout
2
+ module MotionKit
3
+ class BaseLayout
4
+
5
+ # This method is used to check the orientation. On an ipad, this method
6
+ # returns true for :portrait if the device is "upside down", but it returns
7
+ # false in the same situation on an iphone.
8
+ def orientation?(value)
9
+ if target.is_a?(UIView) && target.nextResponder && target.nextResponder.is_a?(UIViewController)
10
+ interface_orientation = target.nextResponder.interfaceOrientation
11
+ else
12
+ interface_orientation = UIApplication.sharedApplication.statusBarOrientation
13
+ end
14
+
15
+ return case value
16
+ when :portrait
17
+ if ipad?
18
+ interface_orientation == UIInterfaceOrientationPortrait || interface_orientation == UIInterfaceOrientationPortraitUpsideDown
19
+ else
20
+ interface_orientation == UIInterfaceOrientationPortrait
21
+ end
22
+ when :upright, UIInterfaceOrientationPortrait
23
+ interface_orientation == UIInterfaceOrientationPortrait
24
+ when :landscape
25
+ interface_orientation == UIInterfaceOrientationLandscapeLeft || interface_orientation == UIInterfaceOrientationLandscapeRight
26
+ when :landscape_left, UIInterfaceOrientationLandscapeLeft
27
+ interface_orientation == UIInterfaceOrientationLandscapeLeft
28
+ when :landscape_right, UIInterfaceOrientationLandscapeRight
29
+ interface_orientation == UIInterfaceOrientationLandscapeRight
30
+ when :upside_down, UIInterfaceOrientationPortraitUpsideDown
31
+ interface_orientation == UIInterfaceOrientationPortraitUpsideDown
32
+ end
33
+ end
34
+
35
+ [:portrait, :upright, :upside_down, :landscape, :landscape_left, :landscape_right].each do |orientation|
36
+ define_method("#{orientation}?") do
37
+ return orientation?(orientation)
38
+ end
39
+ define_method(orientation) do |&block|
40
+ if orientation?(orientation)
41
+ yield
42
+ end
43
+ end
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,52 @@
1
+ # @provides MotionKit::UIButtonLayout
2
+ # @requires MotionKit::UIViewLayout
3
+ module MotionKit
4
+ class UIButtonLayout < UIViewLayout
5
+ targets UIButton
6
+
7
+ def title(value)
8
+ title(value, state: UIControlStateNormal)
9
+ end
10
+
11
+ def title(value, state: state)
12
+ if value.is_a?(NSAttributedString)
13
+ target.setAttributedTitle(value, forState: state)
14
+ else
15
+ target.setTitle(value, forState: state)
16
+ end
17
+ end
18
+
19
+ def title_color(value)
20
+ title_color(value, state: UIControlStateNormal)
21
+ end
22
+
23
+ def title_color(value, state: state)
24
+ target.setTitleColor(value, forState: state)
25
+ end
26
+
27
+ def title_shadow_color(value)
28
+ title_shadow_color(value, state: UIControlStateNormal)
29
+ end
30
+
31
+ def title_shadow_color(value, state: state)
32
+ target.setTitleShadowColor(value, forState: state)
33
+ end
34
+
35
+ def background_image(value)
36
+ background_image(value, state: UIControlStateNormal)
37
+ end
38
+
39
+ def background_image(value, state: state)
40
+ target.setBackgroundImage(value, forState: state)
41
+ end
42
+
43
+ def image(value)
44
+ image(value, state: UIControlStateNormal)
45
+ end
46
+
47
+ def image(value, state: state)
48
+ target.setImage(value, forState: state)
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,45 @@
1
+ # @provides MotionKit::Layout
2
+ # @provides MotionKit::UIViewLayout
3
+ # @requires MotionKit::ViewLayout
4
+ module MotionKit
5
+ class Layout < ViewLayout
6
+
7
+ # platform specific default root view
8
+ def default_root
9
+ # child Layout classes will return *their* UIView subclass from self.targets
10
+ view_class = self.class.targets || MotionKit.default_view_class
11
+ view_class.alloc.initWithFrame(UIScreen.mainScreen.applicationFrame)
12
+ end
13
+
14
+ def add_child(subview)
15
+ target.addSubview(subview)
16
+ end
17
+
18
+ def remove_child(subview)
19
+ subview.removeFromSuperview
20
+ end
21
+
22
+ # UIViews AND CALayers are updated
23
+ def reapply!(root=nil)
24
+ if root.is_a?(CALayer)
25
+ @layout_state = :reapply
26
+ MotionKit.find_all_layers(root) do |layer|
27
+ call_style_method(layer, layer.motion_kit_id) if layer.motion_kit_id
28
+ end
29
+ @layout_state = :initial
30
+ else
31
+ root ||= self.view
32
+ reapply!(root.layer)
33
+ super(root)
34
+ end
35
+
36
+ return self
37
+ end
38
+
39
+ end
40
+
41
+ class UIViewLayout < Layout
42
+ targets UIView
43
+ end
44
+
45
+ end