motion-kit 0.0.1 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
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,42 @@
1
+ # @requires MotionKit::ConstraintsLayout
2
+ module MotionKit
3
+ class ConstraintsLayout
4
+
5
+ def content_compression_resistance_priority(value, for_orientation: orientation)
6
+ orientation = Constraint.orientation_lookup(orientation)
7
+ end
8
+
9
+ # consistency with iOS methods:
10
+ def content_compression_resistance_priority(value, for_axis: orientation)
11
+ content_compression_resistance_priority(value, for_orientation: orientation)
12
+ end
13
+
14
+ def compression_priority(value, for_orientation: orientation)
15
+ content_compression_resistance_priority(value, for_orientation: orientation)
16
+ end
17
+
18
+ # consistency with iOS methods:
19
+ def compression_priority(value, for_axis: orientation)
20
+ content_compression_resistance_priority(value, for_orientation: orientation)
21
+ end
22
+
23
+ def content_hugging_priority(value, for_orientation: orientation)
24
+ orientation = Constraint.orientation_lookup(orientation)
25
+ end
26
+
27
+ # consistency with iOS methods:
28
+ def content_hugging_priority(value, for_axis: orientation)
29
+ content_hugging_priority(value, for_orientation: orientation)
30
+ end
31
+
32
+ def hugging_priority(value, for_orientation: orientation)
33
+ content_hugging_priority(value, for_orientation: orientation)
34
+ end
35
+
36
+ # consistency with iOS methods:
37
+ def hugging_priority(value, for_axis: orientation)
38
+ content_hugging_priority(value, for_orientation: orientation)
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,186 @@
1
+ # @requires MotionKit::NSMenuLayout
2
+ module MotionKit
3
+ class NSMenuLayout
4
+
5
+ # useful when writing menus
6
+ def app_name
7
+ NSBundle.mainBundle.infoDictionary['CFBundleName']
8
+ end
9
+
10
+ def _menu_title_and_options(title, options, default_title=nil, default_options={})
11
+ if title.is_a?(NSDictionary)
12
+ options = title
13
+ title = options[:title]
14
+ end
15
+ title ||= default_title
16
+ return title, default_options.merge(options)
17
+ end
18
+
19
+ def app_menu(title=nil, options={})
20
+ title, options = _menu_title_and_options(title, options, app_name)
21
+
22
+ exclude = Array(options.fetch(:exclude, []))
23
+ return create(title) do
24
+ unless exclude.include?(:about)
25
+ add about_item
26
+ add separator_item
27
+ end
28
+ unless exclude.include?(:prefs)
29
+ add preferences_item
30
+ add separator_item
31
+ end
32
+ unless exclude.include?(:services)
33
+ add services_item
34
+ add separator_item
35
+ end
36
+ unless exclude.include?(:hide)
37
+ add hide_item
38
+ add hide_others_item
39
+ end
40
+ add show_all_item unless exclude.include?(:show)
41
+ add quit_item unless exclude.include?(:quit)
42
+ end
43
+ end
44
+
45
+ def file_menu(title=nil, options={})
46
+ title, options = _menu_title_and_options(title, options, 'File')
47
+
48
+ exclude = Array(options.fetch(:exclude, []))
49
+ return create(title) do
50
+ add new_item unless exclude.include?(:new)
51
+ add open_item unless exclude.include?(:open)
52
+ add separator_item unless [:new, :open].all? { |menu_name| exclude.include?(menu_name) }
53
+
54
+ add close_item unless exclude.include?(:close)
55
+ add save_item unless exclude.include?(:save)
56
+ add revert_to_save_item unless exclude.include?(:revert)
57
+ add separator_item unless [:close, :save, :revert].all? { |menu_name| exclude.include?(menu_name) }
58
+
59
+ add page_setup_item unless exclude.include?(:page_setup)
60
+ add print_item unless exclude.include?(:print)
61
+ end
62
+ end
63
+
64
+ def window_menu(title=nil, options={})
65
+ title, options = _menu_title_and_options(title, options, 'Window')
66
+
67
+ exclude = Array(options.fetch(:exclude, []))
68
+ return create(title) do
69
+ add minimize_item unless exclude.include?(:minimize)
70
+ add zoom_item unless exclude.include?(:zoom)
71
+ add separator_item unless [:minimize, :zoom].all? { |menu_name| exclude.include?(menu_name) }
72
+
73
+ add bring_all_to_front_item unless exclude.include?(:bring_all_to_front)
74
+ end
75
+ end
76
+
77
+ def help_menu(title=nil, options={})
78
+ title, options = _menu_title_and_options(title, options, 'Help')
79
+
80
+ exclude = Array(options.fetch(:exclude, []))
81
+ return create(title) do
82
+ add help_item
83
+ end
84
+ end
85
+
86
+ def separator_item
87
+ NSMenuItem.separatorItem
88
+ end
89
+
90
+ def about_item(title=nil, options={})
91
+ title, options = _menu_title_and_options(title, options, "About #{app_name}", { action: 'orderFrontStandardAboutPanel:' })
92
+ return self.item(title, options)
93
+ end
94
+
95
+ def preferences_item(title=nil, options={})
96
+ title, options = _menu_title_and_options(title, options, 'Preferences', { key: ',', action: 'openPreferences:' })
97
+ return self.item(title, options)
98
+ end
99
+
100
+ def services_item(title=nil, options={})
101
+ title, options = _menu_title_and_options(title, options, 'Services')
102
+ return self.item(title, options)
103
+ end
104
+
105
+ def hide_item(title=nil, options={})
106
+ title, options = _menu_title_and_options(title, options, "Hide #{app_name}", { key: 'h', action: 'hide:' })
107
+ return self.item(title, options)
108
+ end
109
+
110
+ def hide_others_item(title=nil, options={})
111
+ title, options = _menu_title_and_options(title, options, 'Hide Others', { key: 'h', action: 'hideOtherApplications:', mask: NSCommandKeyMask | NSAlternateKeyMask })
112
+ return self.item(title, options)
113
+ end
114
+
115
+ def show_all_item(title=nil, options={})
116
+ title, options = _menu_title_and_options(title, options, 'Show All', { action: 'unhideAllApplications:' })
117
+ return self.item(title, options)
118
+ end
119
+
120
+ def quit_item(title=nil, options={})
121
+ title, options = _menu_title_and_options(title, options, "Quit #{app_name}", { key: 'q', action: 'terminate:' })
122
+ return self.item(title, options)
123
+ end
124
+
125
+ def new_item(title=nil, options={})
126
+ title, options = _menu_title_and_options(title, options, 'New', { key: 'n', action: 'newDocument:' })
127
+ return self.item(title, options)
128
+ end
129
+
130
+ def open_item(title=nil, options={})
131
+ title, options = _menu_title_and_options(title, options, 'Open…', { key: 'o', action: 'openDocument:' })
132
+ return self.item(title, options)
133
+ end
134
+
135
+ def close_item(title=nil, options={})
136
+ title, options = _menu_title_and_options(title, options, 'Close', { key: 'w', action: 'performClose:' })
137
+ return self.item(title, options)
138
+ end
139
+
140
+ def save_item(title=nil, options={})
141
+ title, options = _menu_title_and_options(title, options, 'Save…', { key: 's', action: 'saveDocument:' })
142
+ return self.item(title, options)
143
+ end
144
+
145
+ def save_as_item(title=nil, options={})
146
+ title, options = _menu_title_and_options(title, options, 'Save as…', { key: 'S', action: 'saveDocumentAs:' })
147
+ return self.item(title, options)
148
+ end
149
+
150
+ def revert_to_save_item(title=nil, options={})
151
+ title, options = _menu_title_and_options(title, options, 'Revert to Saved', { action: 'revertDocumentToSaved:' })
152
+ return self.item(title, options)
153
+ end
154
+
155
+ def page_setup_item(title=nil, options={})
156
+ title, options = _menu_title_and_options(title, options, 'Page Setup…', { key: 'P', action: 'runPageLayout:' })
157
+ return self.item(title, options)
158
+ end
159
+
160
+ def print_item(title=nil, options={})
161
+ title, options = _menu_title_and_options(title, options, 'Print…', { key: 'p', action: 'printDocument:' })
162
+ return self.item(title, options)
163
+ end
164
+
165
+ def minimize_item(title=nil, options={})
166
+ title, options = _menu_title_and_options(title, options, 'Minimize', { key: 'm', action: 'performMiniaturize:' })
167
+ return self.item(title, options)
168
+ end
169
+
170
+ def zoom_item(title=nil, options={})
171
+ title, options = _menu_title_and_options(title, options, 'Zoom', { action: 'performMiniaturize:' })
172
+ return self.item(title, options)
173
+ end
174
+
175
+ def bring_all_to_front_item(title=nil, options={})
176
+ title, options = _menu_title_and_options(title, options, 'Bring All To Front', { action: 'arrangeInFront:' })
177
+ return self.item(title, options)
178
+ end
179
+
180
+ def help_item(title=nil, options={})
181
+ title, options = _menu_title_and_options(title, options, "#{app_name} Help", { key: '?', action: 'showHelp:' })
182
+ return self.item("#{app_name} Help", options)
183
+ end
184
+
185
+ end
186
+ end
@@ -0,0 +1,109 @@
1
+ # @provides MotionKit::MenuLayout
2
+ # @provides MotionKit::NSMenuLayout
3
+ # @requires MotionKit::ViewLayout
4
+ module MotionKit
5
+ class MenuLayout < ViewLayout
6
+
7
+ # A more sensible name for the menu that is created.
8
+ def menu
9
+ self.view
10
+ end
11
+
12
+ # platform specific default root view
13
+ def default_root
14
+ # child WindowLayout classes can return *their* NSView subclass from self.nsview_class
15
+ menu_class = self.class.targets || NSMenu
16
+ menu_class.alloc.init
17
+ end
18
+
19
+ def add_child(submenu)
20
+ target.addItem(submenu)
21
+ end
22
+
23
+ def remove_child(submenu)
24
+ target.removeItem(submenu)
25
+ end
26
+
27
+ # override root to allow a menu title for the top level menu
28
+ def root(element, element_id=nil, &block)
29
+ if element && element.is_a?(NSString)
30
+ element = NSMenu.alloc.initWithTitle(element)
31
+ end
32
+ super(element, element_id, &block)
33
+ end
34
+
35
+ # override 'add'; menus are just a horse of a different color.
36
+ def add(title_or_item, element_id=nil, options={}, &block)
37
+ if element_id.is_a?(NSDictionary)
38
+ options = element_id
39
+ element_id = nil
40
+ end
41
+
42
+ if title_or_item.is_a?(NSMenuItem)
43
+ item = title_or_item
44
+ menu = nil
45
+ retval = item
46
+ elsif title_or_item.is_a?(NSMenu)
47
+ menu = title_or_item
48
+ item = self.item(menu.title, options)
49
+ item.submenu = menu
50
+ retval = menu
51
+ else
52
+ title = title_or_item
53
+ item = self.item(title, options)
54
+
55
+ if block
56
+ menu = create(title)
57
+ item.submenu = menu
58
+ retval = menu
59
+ else
60
+ retval = item
61
+ end
62
+ end
63
+
64
+ self.apply(:add_child, item)
65
+
66
+ if menu && block
67
+ menuitem_was = @menu_item
68
+ @menu_item = item
69
+ context(menu, &block)
70
+ @menu_item = menuitem_was
71
+ end
72
+
73
+ if element_id
74
+ create(retval, element_id)
75
+ end
76
+
77
+ return retval
78
+ end
79
+
80
+ def create(title_or_item, element_id=nil, &block)
81
+ if title_or_item.is_a?(NSString)
82
+ item = NSMenu.alloc.initWithTitle(title_or_item)
83
+ else
84
+ item = title_or_item
85
+ end
86
+
87
+ return super(item, element_id, &block)
88
+ end
89
+
90
+ def item(title, options={})
91
+ action = options.fetch(:action, nil)
92
+ key = options.fetch(:keyEquivalent, options.fetch(:key, ''))
93
+ key ||= '' # key must never be nil
94
+ item = NSMenuItem.alloc.initWithTitle(title, action: action, keyEquivalent: key)
95
+
96
+ mask = options.fetch(:keyEquivalentModifierMask, options.fetch(:mask, nil))
97
+ unless mask.nil?
98
+ item.keyEquivalentModifierMask = mask
99
+ end
100
+
101
+ return item
102
+ end
103
+
104
+ end
105
+
106
+ class NSMenuLayout < MenuLayout
107
+ targets NSMenu
108
+ end
109
+ end
@@ -0,0 +1,45 @@
1
+ # @requires MotionKit::NSMenuLayout
2
+ module MotionKit
3
+ class NSMenuLayout
4
+
5
+ ##|
6
+ ##| These methods are meant to be called on the parent menu item, but
7
+ ##| there's no way to reference an NSMenu's parent *item* (only its parent
8
+ ##| *menu*). So the @menu_item is stored in an ivar before the menu block
9
+ ##| is created, and these methods access that menu item.
10
+ ##|
11
+
12
+ # sets the title of the current NSMenu AND it's parent NSMenuItem
13
+ def title(value)
14
+ target.title = value
15
+ @menu_item.title = value
16
+ end
17
+
18
+ def attributedTitle(value)
19
+ target.title = value.to_s
20
+ @menu_item.attributedTitle = value
21
+ end
22
+
23
+ def attributed_title(value)
24
+ target.title = value.to_s
25
+ @menu_item.attributedTitle = value
26
+ end
27
+
28
+ def state(value)
29
+ @menu_item.state = value
30
+ end
31
+
32
+ def tag(value)
33
+ @menu_item.tag = value
34
+ end
35
+
36
+ def keyEquivalentModifierMask(value)
37
+ @menu_item.keyEquivalentModifierMask = value
38
+ end
39
+
40
+ def key_equivalent_modifier_mask(value)
41
+ @menu_item.keyEquivalentModifierMask = value
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,12 @@
1
+ # @provides MotionKit::NSTableColumnLayout
2
+ # @requires MotionKit::BaseLayout
3
+ module MotionKit
4
+ class NSTableColumnLayout < BaseLayout
5
+ targets NSTableColumn
6
+
7
+ def title(value)
8
+ target.headerCell.stringValue = value
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,20 @@
1
+ # @provides MotionKit::NSTableViewLayout
2
+ # @requires MotionKit::NSViewLayout
3
+ module MotionKit
4
+ class NSTableViewLayout < NSViewLayout
5
+ targets NSTableView
6
+
7
+ def add_column(column_or_identifier, &block)
8
+ if column_or_identifier.is_a?(NSTableColumn)
9
+ column = column_or_identifier
10
+ else
11
+ column = NSTableColumn.alloc.initWithIdentifier(column_or_identifier)
12
+ column.headerCell.stringValue = column_or_identifier
13
+ end
14
+ target.addTableColumn(column)
15
+ context(column, &block)
16
+ end
17
+ alias add_table_column add_column
18
+
19
+ end
20
+ end
@@ -0,0 +1,47 @@
1
+ # @provides MotionKit::Layout
2
+ # @provides MotionKit::NSViewLayout
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* NSView subclass from self.targets
10
+ view_class = self.class.targets || MotionKit.default_view_class
11
+ view_class.alloc.initWithFrame([[0, 0], [0, 0]])
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
+ # NSViews 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
+ if root.layer
33
+ reapply!(root.layer)
34
+ end
35
+ super(root)
36
+ end
37
+
38
+ return self
39
+ end
40
+
41
+ end
42
+
43
+ class NSViewLayout < Layout
44
+ targets NSView
45
+ end
46
+
47
+ end