ruby_motion_query 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +15 -0
  2. data/LICENSE +21 -0
  3. data/README.md +964 -0
  4. data/lib/ruby_motion_query.rb +8 -0
  5. data/motion/ext.rb +34 -0
  6. data/motion/ruby_motion_query/actions.rb +44 -0
  7. data/motion/ruby_motion_query/animations.rb +125 -0
  8. data/motion/ruby_motion_query/app.rb +71 -0
  9. data/motion/ruby_motion_query/base.rb +161 -0
  10. data/motion/ruby_motion_query/color.rb +80 -0
  11. data/motion/ruby_motion_query/data.rb +32 -0
  12. data/motion/ruby_motion_query/device.rb +73 -0
  13. data/motion/ruby_motion_query/enumerablish.rb +74 -0
  14. data/motion/ruby_motion_query/event.rb +125 -0
  15. data/motion/ruby_motion_query/events.rb +65 -0
  16. data/motion/ruby_motion_query/factory.rb +32 -0
  17. data/motion/ruby_motion_query/font.rb +66 -0
  18. data/motion/ruby_motion_query/format.rb +54 -0
  19. data/motion/ruby_motion_query/image.rb +79 -0
  20. data/motion/ruby_motion_query/position.rb +45 -0
  21. data/motion/ruby_motion_query/selectors.rb +56 -0
  22. data/motion/ruby_motion_query/stylers/ui_button_styler.rb +32 -0
  23. data/motion/ruby_motion_query/stylers/ui_control_styler.rb +8 -0
  24. data/motion/ruby_motion_query/stylers/ui_date_picker_styler.rb +8 -0
  25. data/motion/ruby_motion_query/stylers/ui_image_view_styler.rb +14 -0
  26. data/motion/ruby_motion_query/stylers/ui_label_styler.rb +36 -0
  27. data/motion/ruby_motion_query/stylers/ui_navigation_bar_styler.rb +8 -0
  28. data/motion/ruby_motion_query/stylers/ui_page_control_styler.rb +8 -0
  29. data/motion/ruby_motion_query/stylers/ui_refresh_control_styler.rb +8 -0
  30. data/motion/ruby_motion_query/stylers/ui_scroll_view_styler.rb +10 -0
  31. data/motion/ruby_motion_query/stylers/ui_segmented_control_styler.rb +8 -0
  32. data/motion/ruby_motion_query/stylers/ui_slider_styler.rb +8 -0
  33. data/motion/ruby_motion_query/stylers/ui_stepper_styler.rb +8 -0
  34. data/motion/ruby_motion_query/stylers/ui_switch_styler.rb +10 -0
  35. data/motion/ruby_motion_query/stylers/ui_tab_bar_styler.rb +8 -0
  36. data/motion/ruby_motion_query/stylers/ui_table_view_cell_styler.rb +8 -0
  37. data/motion/ruby_motion_query/stylers/ui_table_view_styler.rb +8 -0
  38. data/motion/ruby_motion_query/stylers/ui_text_field_styler.rb +8 -0
  39. data/motion/ruby_motion_query/stylers/ui_text_view_styler.rb +8 -0
  40. data/motion/ruby_motion_query/stylers/ui_view_styler.rb +243 -0
  41. data/motion/ruby_motion_query/stylesheet.rb +185 -0
  42. data/motion/ruby_motion_query/subviews.rb +52 -0
  43. data/motion/ruby_motion_query/tags.rb +14 -0
  44. data/motion/ruby_motion_query/traverse.rb +183 -0
  45. data/motion/ruby_motion_query/utils.rb +53 -0
  46. metadata +89 -0
@@ -0,0 +1,8 @@
1
+ module RubyMotionQuery
2
+ module Stylers
3
+
4
+ class UIStepperStyler < UIControlStyler
5
+ end
6
+
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ module RubyMotionQuery
2
+ module Stylers
3
+
4
+ class UISwitchStyler < UIControlStyler
5
+ def on=(value) ; @view.setOn(value) ; end
6
+ def on ; @view.isOn ; end
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,8 @@
1
+ module RubyMotionQuery
2
+ module Stylers
3
+
4
+ class UITabBarStyler < UIViewStyler
5
+ end
6
+
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module RubyMotionQuery
2
+ module Stylers
3
+
4
+ class UITableViewCellStyler < UIViewStyler
5
+ end
6
+
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module RubyMotionQuery
2
+ module Stylers
3
+
4
+ class UITableViewStyler < UIScrollViewStyler
5
+ end
6
+
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module RubyMotionQuery
2
+ module Stylers
3
+
4
+ class UITextFieldStyler < UIControlStyler
5
+ end
6
+
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module RubyMotionQuery
2
+ module Stylers
3
+
4
+ class UITextViewStyler < UIViewStyler
5
+ end
6
+
7
+ end
8
+ end
@@ -0,0 +1,243 @@
1
+ module RubyMotionQuery
2
+ module Stylers
3
+
4
+ # When you create a styler, always inherit UIViewStyler
5
+ class UIViewStyler
6
+ def initialize(view)
7
+ @view = view
8
+ end
9
+
10
+ # If a view is reset, all state should be reset as well
11
+ def view=(value)
12
+ @view = value
13
+ end
14
+ def view
15
+ @view
16
+ end
17
+
18
+ def view_has_been_styled?
19
+ !@view.rmq_data.style_name.nil?
20
+ end
21
+
22
+ def superview
23
+ @view.superview || rmq(@view).view_controller.view || rmq.window
24
+ end
25
+ alias :parent :superview
26
+
27
+ def tag(tags)
28
+ rmq(@view).tag(tags)
29
+ end
30
+
31
+ def frame=(value)
32
+ if value == :full # Thanks teacup for the name
33
+ value = self.superview.bounds
34
+ elsif value.is_a?(Hash)
35
+ h = value
36
+ h[:l] ||= (h[:left] || 0)
37
+ h[:t] ||= (h[:top] || 0)
38
+ h[:w] ||= (h[:width] || 0)
39
+ h[:h] ||= (h[:height] || 0)
40
+
41
+ value = [[h[:l], h[:t]], [h[:w], h[:h]]]
42
+ end
43
+
44
+ @view.frame = value
45
+ end
46
+ def frame
47
+ @view.frame
48
+ end
49
+
50
+ def padded=(value)
51
+ #st.padded = {l: 10, t: 10, b:10, r: 10}
52
+
53
+ if value.is_a?(Hash)
54
+ h = value
55
+ h[:l] ||= (h[:left] || 0)
56
+ h[:t] ||= (h[:top] || 0)
57
+ h[:r] ||= (h[:right] || 0)
58
+ h[:b] ||= (h[:bottom] || 0)
59
+
60
+ sbounds = self.superview.bounds
61
+
62
+ value = [
63
+ [h[:l], h[:t]],
64
+ [
65
+ sbounds.size.width - h[:l] - h[:r],
66
+ sbounds.size.height - h[:t] - h[:b]
67
+ ]]
68
+
69
+ @view.frame = value
70
+ end
71
+
72
+ end
73
+
74
+ def left=(value)
75
+ f = @view.frame
76
+ f.origin.x = value
77
+ @view.frame = f
78
+ end
79
+ def left
80
+ @view.origin.x
81
+ end
82
+ alias :x :left
83
+
84
+ def top=(value)
85
+ f = @view.frame
86
+ f.origin.y = value
87
+ @view.frame = f
88
+ end
89
+ def top
90
+ @view.origin.y
91
+ end
92
+ alias :y :top
93
+
94
+ def width=(value)
95
+ f = @view.frame
96
+ f.size.width = value
97
+ @view.frame = f
98
+ end
99
+ def width
100
+ @view.size.width
101
+ end
102
+
103
+ def height=(value)
104
+ f = @view.frame
105
+ f.size.height = value
106
+ @view.frame = f
107
+ end
108
+ def height
109
+ @view.size.height
110
+ end
111
+
112
+ def bottom=(value)
113
+ self.top = value - self.height
114
+ end
115
+ def bottom
116
+ self.top + self.height
117
+ end
118
+
119
+ def from_bottom=(value)
120
+ if superview = @view.superview
121
+ self.top = superview.bounds.size.height - self.height - value
122
+ end
123
+ end
124
+ def from_bottom
125
+ if superview = @view.superview
126
+ superview.bounds.size.height - self.top
127
+ end
128
+ end
129
+
130
+ def right=(value)
131
+ self.left = value - self.width
132
+ end
133
+ def right
134
+ self.left + self.width
135
+ end
136
+
137
+ def from_right=(value)
138
+ if superview = @view.superview
139
+ self.left = superview.bounds.size.width - self.width - value
140
+ end
141
+ end
142
+ def from_right
143
+ if superview = @view.superview
144
+ superview.bounds.size.width - self.left
145
+ end
146
+ end
147
+
148
+ def center=(value)
149
+ @view.center = value
150
+ end
151
+ def center
152
+ @view.center
153
+ end
154
+
155
+ def center_x=(value)
156
+ c = @view.center
157
+ c.x = value
158
+ @view.center = c
159
+ end
160
+ def center_x
161
+ @view.center.x
162
+ end
163
+
164
+ def center_y=(value)
165
+ c = @view.center
166
+ c.y = value
167
+ @view.center = c
168
+ end
169
+ def center_y
170
+ @view.center.y
171
+ end
172
+
173
+ # param can be :horizontal, :vertical, :both
174
+ def centered=(option)
175
+ if parent = @view.superview
176
+ case option
177
+ when :horizontal
178
+ # Not using parent.center.x here for orientation
179
+ self.center_x = parent.bounds.size.width / 2
180
+ when :vertical
181
+ self.center_y = parent.bounds.size.height / 2
182
+ else
183
+ @view.center = [parent.bounds.size.width / 2, parent.bounds.size.height / 2]
184
+ end
185
+ end
186
+ end
187
+
188
+ def background_color=(value)
189
+ @view.backgroundColor = value
190
+ end
191
+ def background_color
192
+ @view.backgroundColor
193
+ end
194
+
195
+ # TODO add background_image_tiled
196
+
197
+ def z_position=(index)
198
+ @view.layer.zPosition = index
199
+ end
200
+ def z_position
201
+ @view.layer.zPosition
202
+ end
203
+
204
+ def opaque=(value)
205
+ @view.layer.opaque = value
206
+ end
207
+ def opaque
208
+ @view.layer.opaque
209
+ end
210
+
211
+ def hidden=(value)
212
+ @view.hidden = value
213
+ end
214
+ def hidden
215
+ @view.hidden
216
+ end
217
+
218
+ def enabled=(value)
219
+ @view.enabled = value
220
+ end
221
+ def enabled
222
+ @view.enabled
223
+ end
224
+
225
+ def scale=(amount)
226
+ if amount == 1.0
227
+ @view.transform = CGAffineTransformIdentity
228
+ else
229
+ if amount.is_a?(NSArray)
230
+ width = amount[0]
231
+ height = amount[1]
232
+ else
233
+ height = amount
234
+ width = amount
235
+ end
236
+
237
+ @view.transform = CGAffineTransformMakeScale(width, height)
238
+ end
239
+ end
240
+
241
+ end
242
+ end
243
+ end
@@ -0,0 +1,185 @@
1
+ module RubyMotionQuery
2
+ class RMQ
3
+ def stylesheet=(value)
4
+ controller = view_controller
5
+ unless value.is_a?(RubyMotionQuery::Stylesheet)
6
+ value = value.new(controller)
7
+ end
8
+ controller.rmq_data.stylesheet = value
9
+ self
10
+ end
11
+ def stylesheet
12
+ @_stylesheet ||= begin
13
+ self.view_controller.rmq_data.stylesheet if self.view_controller
14
+ end
15
+ end
16
+
17
+ def apply_style(style_name)
18
+ selected.each do |selected_view|
19
+ apply_style_to_view selected_view, style_name
20
+ end
21
+ self
22
+ end
23
+
24
+ def style()
25
+ selected.each do |view|
26
+ yield(styler_for(view))
27
+ end
28
+ self
29
+ end
30
+
31
+ def reapply_styles
32
+ selected.each do |selected_view|
33
+ if style_name = selected_view.rmq_data.style_name
34
+ apply_style_to_view selected_view, style_name
35
+ end
36
+ end
37
+ end
38
+
39
+ def styler_for(view)
40
+ # TODO should have a pool of stylers to reuse, or just assume single threaded and
41
+ # memoize this, however if you do that, make sure the dev doesn't retain them in a var
42
+ custom_stylers(view) || begin
43
+ case view
44
+ # TODO, rename all the stylers to Styler::UILabel, etc
45
+ when UILabel then Stylers::UILabelStyler.new(view)
46
+ when UIButton then Stylers::UIButtonStyler.new(view)
47
+ when UIImageView then Stylers::UIImageViewStyler.new(view)
48
+ when UIScrollView then Stylers::UIScrollViewStyler.new(view)
49
+ when UISwitch then Stylers::UISwitchStyler.new(view)
50
+ when UIDatePicker then Stylers::UIDatePickerStyler.new(view)
51
+ when UISegmentedControl then Stylers::UISegmentedControlStyler.new(view)
52
+ when UIRefreshControl then Stylers::UIRefreshControlStyler.new(view)
53
+ when UIPageControl then Stylers::UIPageControlStyler.new(view)
54
+ when UISlider then Stylers::UISliderStyler.new(view)
55
+ when UIStepper then Stylers::UIStepperStyler.new(view)
56
+ when UITabBar then Stylers::UITabBarStyler.new(view)
57
+ when UITableView then Stylers::UITableViewStyler.new(view)
58
+ when UITableViewCell then Stylers::UITableViewCellStyler.new(view)
59
+ when UITextView then Stylers::UITextViewStyler.new(view)
60
+ when UITextField then Stylers::UITextFieldStyler.new(view)
61
+ when UINavigationBar then Stylers::UINavigationBarStyler.new(view)
62
+ # TODO, all the controls are done, but missing some views, add
63
+ when UIControl then Stylers::UIControlStyler.new(view)
64
+ else
65
+ Stylers::UIViewStyler.new(view)
66
+ end
67
+ end
68
+ end
69
+
70
+ protected
71
+
72
+ # Override to set your own stylers, or just open up the styler classes
73
+ def custom_stylers(view)
74
+ end
75
+
76
+ def apply_style_to_view(view, style_name)
77
+ begin
78
+ stylesheet.__send__(style_name, styler_for(view))
79
+ view.rmq_data.style_name = style_name
80
+ rescue NoMethodError => e
81
+ if e.message =~ /.*#{style_name.to_s}.*/
82
+ puts "\n[RMQ ERROR] style_name :#{style_name} doesn't exist for a #{view.class.name}. Add 'def #{style_name}(sv)' to #{stylesheet.class.name} class\n\n"
83
+ else
84
+ raise e
85
+ end
86
+ end
87
+ end
88
+
89
+ end
90
+
91
+ class Stylesheet
92
+ attr_reader :controller
93
+
94
+ def initialize(controller)
95
+ @controller = WeakRef.new(controller)
96
+
97
+ unless Stylesheet.application_was_setup
98
+ Stylesheet.application_was_setup = true
99
+ application_setup
100
+ end
101
+ setup
102
+ end
103
+
104
+ def application_setup
105
+ # Override to do your overall setup for your applications. This
106
+ # is where you want to add your custom fonts and colors
107
+ # This only gets called once
108
+ end
109
+
110
+ def setup
111
+ # Override if you need to do setup in your specific stylesheet
112
+ end
113
+
114
+ class << self
115
+ attr_accessor :application_was_setup
116
+ end
117
+
118
+ # Convenience methods -------------------
119
+ def rmq
120
+ if @controller
121
+ @controller.rmq
122
+ else
123
+ RMQ
124
+ end
125
+ end
126
+
127
+ def device
128
+ RMQ.device
129
+ end
130
+
131
+ def landscape?
132
+ device.landscape?
133
+ end
134
+ def portrait?
135
+ device.portrait?
136
+ end
137
+
138
+ def iphone?
139
+ device.iphone?
140
+ end
141
+ def ipad?
142
+ device.ipad?
143
+ end
144
+
145
+ def four_inch?
146
+ RMQ.device.four_inch?
147
+ end
148
+
149
+ def retina?
150
+ RMQ.device.retina?
151
+ end
152
+
153
+ def window
154
+ RMQ.app.window
155
+ end
156
+
157
+ def app_width
158
+ app_size.width
159
+ end
160
+
161
+ def app_height
162
+ app_size.height
163
+ end
164
+
165
+ def app_size
166
+ device.screen.applicationFrame.size
167
+ end
168
+
169
+ def screen_size
170
+ device.screen.bounds.size
171
+ end
172
+
173
+ def image
174
+ RMQ.image
175
+ end
176
+
177
+ def color
178
+ RMQ.color
179
+ end
180
+
181
+ def font
182
+ RMQ.font
183
+ end
184
+ end
185
+ end
@@ -0,0 +1,52 @@
1
+ # See traversing.rb and selectors.rb for finding and traversing subviews
2
+ module RubyMotionQuery
3
+ class RMQ
4
+
5
+ def remove
6
+ selected.each { |view| view.removeFromSuperview }
7
+ self
8
+ end
9
+
10
+ def append(view_or_constant, style = nil)
11
+ subviews_added = []
12
+ selected.each do |selected_view|
13
+ if view_or_constant.is_a?(UIView)
14
+ new_view = view_or_constant
15
+ else
16
+ new_view = create_view(view_or_constant)
17
+ end
18
+
19
+ subviews_added << new_view
20
+ selected_view.addSubview(new_view)
21
+
22
+ if self.stylesheet
23
+ apply_style_to_view(new_view, style) if style
24
+ end
25
+ end
26
+
27
+ RMQ.create_with_array_and_selectors(subviews_added, selectors, @context)
28
+ end
29
+ alias :add_subview :append
30
+
31
+ def insert(view, at_index, style = nil)
32
+ # TODO
33
+ end
34
+
35
+ protected
36
+
37
+ def create_view(klass)
38
+ if klass == UIButton
39
+ klass.buttonWithType(UIButtonTypeCustom).tap do |o|
40
+ o.hidden = false
41
+ o.opaque = true
42
+ end
43
+ else
44
+ klass.alloc.initWithFrame(CGRectZero).tap do |o|
45
+ o.hidden = false
46
+ o.opaque = true
47
+ end
48
+ end
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,14 @@
1
+ module RubyMotionQuery
2
+ class RMQ
3
+
4
+ def tag(*tag_or_tags)
5
+ selected.each do |view|
6
+ view.rmq_data.tag(tag_or_tags)
7
+ end
8
+ self
9
+ end
10
+
11
+ # See /lib/data.rb for the rest of the tag stuff
12
+
13
+ end
14
+ end