sugarcube 0.20.25 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. data/Gemfile +3 -1
  2. data/Gemfile.lock +1 -1
  3. data/README.md +897 -1306
  4. data/Rakefile +3 -7
  5. data/app/uicontrol_controller.rb +2 -2
  6. data/lib/sugarcube-568.rb +1 -9
  7. data/lib/sugarcube-all.rb +18 -0
  8. data/lib/sugarcube-animations.rb +15 -0
  9. data/lib/{sugarcube → sugarcube-animations}/animation_chain.rb +0 -0
  10. data/lib/{sugarcube → sugarcube-animations}/uiview.rb +41 -111
  11. data/lib/sugarcube-anonymous.rb +1 -9
  12. data/lib/sugarcube-attributedstring.rb +1 -9
  13. data/lib/sugarcube-attributedstring/nsattributedstring.rb +2 -11
  14. data/lib/sugarcube-awesome.rb +15 -0
  15. data/lib/sugarcube-awesome/awesome_exts.rb +22 -0
  16. data/lib/sugarcube-classic.rb +26 -0
  17. data/lib/sugarcube-color.rb +15 -0
  18. data/lib/sugarcube-color/fixnum.rb +18 -0
  19. data/lib/sugarcube-color/nsarray.rb +11 -0
  20. data/lib/sugarcube-color/nsstring.rb +16 -0
  21. data/lib/{sugarcube/symbol/symbol_uicolor.rb → sugarcube-color/symbol.rb} +12 -4
  22. data/lib/{sugarcube → sugarcube-color}/uicolor.rb +16 -2
  23. data/lib/sugarcube-color/uiimage.rb +13 -0
  24. data/lib/sugarcube-common.rb +22 -0
  25. data/lib/sugarcube-constants.rb +15 -0
  26. data/lib/sugarcube-constants/symbol.rb +759 -0
  27. data/lib/sugarcube-coregraphics.rb +15 -0
  28. data/lib/{sugarcube → sugarcube-coregraphics}/core_graphics.rb +0 -0
  29. data/lib/sugarcube-corelocation.rb +17 -0
  30. data/lib/{sugarcube → sugarcube-corelocation}/core_location.rb +0 -0
  31. data/lib/sugarcube-events.rb +15 -0
  32. data/lib/{sugarcube → sugarcube-events}/uicontrol.rb +3 -3
  33. data/lib/{sugarcube → sugarcube-events}/uitextview.rb +0 -0
  34. data/lib/sugarcube-factories.rb +15 -0
  35. data/lib/{sugarcube → sugarcube-factories}/nserror.rb +0 -0
  36. data/lib/{sugarcube → sugarcube-factories}/uiactionsheet.rb +3 -1
  37. data/lib/sugarcube-factories/uiactivityindicator.rb +17 -0
  38. data/lib/{sugarcube → sugarcube-factories}/uialertview.rb +3 -3
  39. data/lib/{sugarcube → sugarcube-factories}/uibarbuttonitem.rb +19 -3
  40. data/lib/{sugarcube → sugarcube-factories}/uibutton.rb +10 -10
  41. data/lib/{sugarcube → sugarcube-factories}/uisegmentedcontrol.rb +4 -4
  42. data/lib/{sugarcube → sugarcube-factories}/uitableview.rb +0 -0
  43. data/lib/{sugarcube → sugarcube-factories}/uitableviewcell.rb +3 -7
  44. data/lib/sugarcube-files.rb +15 -0
  45. data/lib/{sugarcube/nsstring_files.rb → sugarcube-files/nsstring.rb} +0 -0
  46. data/lib/sugarcube-foundation.rb +15 -0
  47. data/lib/{sugarcube → sugarcube-foundation}/nsarray.rb +0 -18
  48. data/lib/sugarcube-foundation/nsindexpath.rb +13 -0
  49. data/lib/{sugarcube → sugarcube-foundation}/nsindexset.rb +0 -0
  50. data/lib/sugarcube-foundation/nsstring.rb +31 -0
  51. data/lib/{sugarcube → sugarcube-foundation}/nsurl.rb +0 -4
  52. data/lib/sugarcube-gestures.rb +1 -9
  53. data/lib/sugarcube-image.rb +15 -0
  54. data/lib/sugarcube-image/cicolor.rb +7 -0
  55. data/lib/sugarcube-image/cifilter.rb +772 -0
  56. data/lib/sugarcube-image/ciimage.rb +22 -0
  57. data/lib/sugarcube-image/uicolor.rb +7 -0
  58. data/lib/{sugarcube → sugarcube-image}/uiimage.rb +38 -41
  59. data/lib/sugarcube-indexpath.rb +15 -0
  60. data/lib/{sugarcube/nsindexpath.rb → sugarcube-indexpath/indexpath.rb} +12 -21
  61. data/lib/sugarcube-legacy.rb +15 -0
  62. data/lib/{sugarcube → sugarcube-legacy}/activesupport.rb +0 -0
  63. data/lib/sugarcube-legacy/log.rb +45 -0
  64. data/lib/sugarcube-legacy/symbol.rb +219 -0
  65. data/lib/sugarcube-localized.rb +15 -0
  66. data/lib/sugarcube-localized/nserror.rb +8 -0
  67. data/lib/sugarcube-localized/nsstring.rb +10 -0
  68. data/lib/sugarcube-modal.rb +15 -0
  69. data/lib/{sugarcube → sugarcube-modal}/modal.rb +0 -0
  70. data/lib/sugarcube-notifications.rb +15 -0
  71. data/lib/{sugarcube → sugarcube-notifications}/notifications.rb +0 -0
  72. data/lib/sugarcube-nscoder.rb +15 -0
  73. data/lib/{sugarcube → sugarcube-nscoder}/nscoder.rb +0 -0
  74. data/lib/sugarcube-nsdata.rb +15 -0
  75. data/lib/{sugarcube → sugarcube-nsdata}/nsdata.rb +0 -0
  76. data/lib/sugarcube-nsdata/nsstring.rb +9 -0
  77. data/lib/sugarcube-nsdata/nsurl.rb +7 -0
  78. data/lib/sugarcube-nsdata/uiimage.rb +8 -0
  79. data/lib/sugarcube-nsdate.rb +15 -0
  80. data/lib/{sugarcube → sugarcube-nsdate}/date_parser.rb +0 -0
  81. data/lib/sugarcube-nsdate/fixnum.rb +23 -0
  82. data/lib/{sugarcube → sugarcube-nsdate}/nsdate.rb +6 -1
  83. data/lib/{sugarcube → sugarcube-nsdate}/nsdate_delta.rb +0 -0
  84. data/lib/sugarcube-nsdate/nsstring.rb +16 -0
  85. data/lib/sugarcube-nsuserdefaults.rb +15 -0
  86. data/lib/{sugarcube → sugarcube-nsuserdefaults}/nsuserdefaults.rb +0 -0
  87. data/lib/sugarcube-numbers.rb +15 -0
  88. data/lib/sugarcube-numbers/fixnum.rb +25 -0
  89. data/lib/sugarcube-numbers/nsstring.rb +13 -0
  90. data/lib/{sugarcube → sugarcube-numbers}/numeric.rb +44 -4
  91. data/lib/sugarcube-osx/adjust.rb +373 -0
  92. data/lib/sugarcube-pipes.rb +15 -0
  93. data/lib/sugarcube-pipes/pipes.rb +78 -0
  94. data/lib/sugarcube-pointer.rb +15 -0
  95. data/lib/sugarcube-pointer/nsarray.rb +13 -0
  96. data/lib/sugarcube-repl.rb +1 -9
  97. data/lib/sugarcube-resources.rb +15 -0
  98. data/lib/sugarcube-timer.rb +15 -0
  99. data/lib/{sugarcube → sugarcube-timer}/timer.rb +0 -0
  100. data/lib/sugarcube-to_s.rb +15 -0
  101. data/lib/{sugarcube/to_s → sugarcube-to_s}/nserror.rb +1 -4
  102. data/lib/sugarcube-to_s/nsindexpath.rb +7 -0
  103. data/lib/{sugarcube/to_s → sugarcube-to_s}/nslayoutconstraint.rb +0 -0
  104. data/lib/{sugarcube/to_s → sugarcube-to_s}/nsnotification.rb +0 -0
  105. data/lib/{sugarcube/to_s → sugarcube-to_s}/nsset.rb +0 -0
  106. data/lib/{sugarcube/to_s → sugarcube-to_s}/nsurl.rb +0 -0
  107. data/lib/{sugarcube/to_s → sugarcube-to_s}/uicolor.rb +0 -0
  108. data/lib/{sugarcube/to_s → sugarcube-to_s}/uievent.rb +0 -0
  109. data/lib/{sugarcube/to_s → sugarcube-to_s}/uilabel.rb +0 -0
  110. data/lib/{sugarcube/to_s → sugarcube-to_s}/uitextfield.rb +0 -0
  111. data/lib/{sugarcube/to_s → sugarcube-to_s}/uitouch.rb +0 -0
  112. data/lib/{sugarcube/to_s → sugarcube-to_s}/uiview.rb +0 -0
  113. data/lib/{sugarcube/to_s → sugarcube-to_s}/uiviewcontroller.rb +0 -0
  114. data/lib/sugarcube-uicolor.rb +1 -0
  115. data/lib/sugarcube-uifont.rb +15 -0
  116. data/lib/sugarcube-uiimage.rb +1 -0
  117. data/lib/sugarcube-uikit.rb +15 -0
  118. data/lib/{sugarcube → sugarcube-uikit}/calayer.rb +0 -0
  119. data/lib/sugarcube-uikit/nsattributedstring.rb +12 -0
  120. data/lib/sugarcube-uikit/nsstring.rb +38 -0
  121. data/lib/sugarcube-uikit/symbol.rb +67 -0
  122. data/lib/{sugarcube → sugarcube-uikit}/uifont.rb +1 -4
  123. data/lib/sugarcube-uikit/uiimage.rb +9 -0
  124. data/lib/{sugarcube → sugarcube-uikit}/uilabel.rb +0 -0
  125. data/lib/{sugarcube → sugarcube-uikit}/uipickerview.rb +0 -0
  126. data/lib/sugarcube-uikit/uiview.rb +100 -0
  127. data/lib/{sugarcube → sugarcube-uikit}/uiviewcontroller.rb +0 -0
  128. data/lib/{sugarcube → sugarcube-uikit}/uiwebview.rb +0 -0
  129. data/lib/sugarcube-unholy.rb +1 -9
  130. data/lib/sugarcube.rb +4 -9
  131. data/lib/sugarcube/adjust.rb +1 -0
  132. data/lib/sugarcube/look_in.rb +22 -0
  133. data/lib/sugarcube/version.rb +1 -1
  134. data/runtests +1 -1
  135. data/spec/{uicolor_components_spec.rb → color_components_spec.rb} +0 -0
  136. data/spec/{uicolor_other_representations_spec.rb → color_other_representations_spec.rb} +0 -0
  137. data/spec/{uicolor_spec.rb → color_spec.rb} +0 -0
  138. data/spec/image_cifilters_spec.rb +549 -0
  139. data/spec/{uiimage_color_at_spec.rb → image_color_at_spec.rb} +0 -0
  140. data/spec/{uiimage_scale_spec.rb → image_scale_spec.rb} +0 -0
  141. data/spec/image_uiimage_filters_spec.rb +47 -0
  142. data/spec/nsdate_spec.rb +11 -0
  143. data/spec/nsurl_spec.rb +1 -1
  144. data/spec/numeric_spec.rb +10 -0
  145. data/spec/pipes_spec.rb +135 -0
  146. data/spec/sugarcube_legacy.rb +59 -0
  147. data/spec/symbol_constants_spec.rb +809 -0
  148. data/spec/symbol_uicolor_spec.rb +20 -4
  149. data/spec/symbol_uifont_spec.rb +30 -0
  150. data/spec/uiactionsheet_spec.rb +1 -1
  151. data/spec/uialertview_spec.rb +1 -1
  152. data/spec/uibarbuttonitem_spec.rb +46 -46
  153. data/sugarcube.gemspec +1 -1
  154. metadata +142 -74
  155. data/lib/sugarcube/exceptions.rb +0 -2
  156. data/lib/sugarcube/fixnum.rb +0 -59
  157. data/lib/sugarcube/nsstring.rb +0 -107
  158. data/lib/sugarcube/symbol.rb +0 -766
  159. data/lib/sugarcube/uiactivityindicator.rb +0 -17
  160. data/lib/sugarcube/uuid.rb +0 -10
  161. data/spec/activesupport_spec.rb +0 -96
  162. data/spec/symbol_spec.rb +0 -32
@@ -0,0 +1,15 @@
1
+ unless defined?(Motion::Project::Config)
2
+ raise "The sugarcube gem must be required within a RubyMotion project Rakefile."
3
+ end
4
+
5
+
6
+ Motion::Project::App.setup do |app|
7
+ # scans app.files until it finds app/ (the default)
8
+ # if found, it inserts just before those files, otherwise it will insert to
9
+ # the end of the list
10
+ insert_point = app.files.find_index { |file| file =~ /^(?:\.\/)?app\// } || 0
11
+
12
+ Dir.glob(File.join(File.dirname(__FILE__), 'sugarcube-localized/**/*.rb')).reverse.each do |file|
13
+ app.files.insert(insert_point, file)
14
+ end
15
+ end
@@ -0,0 +1,8 @@
1
+ class NSError
2
+
3
+ def localized
4
+ localizedDescription
5
+ end
6
+ alias _ localized
7
+
8
+ end
@@ -0,0 +1,10 @@
1
+ class NSString
2
+
3
+ # This can be called as `"Hello".localized` or `"Hello"._`. The `str._`
4
+ # syntax is meant to be reminiscent of gettext-style `_(str)`.
5
+ def localized(value=nil, table=nil)
6
+ @localized = NSBundle.mainBundle.localizedStringForKey(self, value:value, table:table)
7
+ end
8
+ alias _ localized
9
+
10
+ end
@@ -0,0 +1,15 @@
1
+ unless defined?(Motion::Project::Config)
2
+ raise "The sugarcube gem must be required within a RubyMotion project Rakefile."
3
+ end
4
+
5
+
6
+ Motion::Project::App.setup do |app|
7
+ # scans app.files until it finds app/ (the default)
8
+ # if found, it inserts just before those files, otherwise it will insert to
9
+ # the end of the list
10
+ insert_point = app.files.find_index { |file| file =~ /^(?:\.\/)?app\// } || 0
11
+
12
+ Dir.glob(File.join(File.dirname(__FILE__), 'sugarcube-modal/**/*.rb')).reverse.each do |file|
13
+ app.files.insert(insert_point, file)
14
+ end
15
+ end
File without changes
@@ -0,0 +1,15 @@
1
+ unless defined?(Motion::Project::Config)
2
+ raise "The sugarcube gem must be required within a RubyMotion project Rakefile."
3
+ end
4
+
5
+
6
+ Motion::Project::App.setup do |app|
7
+ # scans app.files until it finds app/ (the default)
8
+ # if found, it inserts just before those files, otherwise it will insert to
9
+ # the end of the list
10
+ insert_point = app.files.find_index { |file| file =~ /^(?:\.\/)?app\// } || 0
11
+
12
+ Dir.glob(File.join(File.dirname(__FILE__), 'sugarcube-notifications/**/*.rb')).reverse.each do |file|
13
+ app.files.insert(insert_point, file)
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ unless defined?(Motion::Project::Config)
2
+ raise "The sugarcube gem must be required within a RubyMotion project Rakefile."
3
+ end
4
+
5
+
6
+ Motion::Project::App.setup do |app|
7
+ # scans app.files until it finds app/ (the default)
8
+ # if found, it inserts just before those files, otherwise it will insert to
9
+ # the end of the list
10
+ insert_point = app.files.find_index { |file| file =~ /^(?:\.\/)?app\// } || 0
11
+
12
+ Dir.glob(File.join(File.dirname(__FILE__), 'sugarcube-nscoder/**/*.rb')).reverse.each do |file|
13
+ app.files.insert(insert_point, file)
14
+ end
15
+ end
File without changes
@@ -0,0 +1,15 @@
1
+ unless defined?(Motion::Project::Config)
2
+ raise "The sugarcube gem must be required within a RubyMotion project Rakefile."
3
+ end
4
+
5
+
6
+ Motion::Project::App.setup do |app|
7
+ # scans app.files until it finds app/ (the default)
8
+ # if found, it inserts just before those files, otherwise it will insert to
9
+ # the end of the list
10
+ insert_point = app.files.find_index { |file| file =~ /^(?:\.\/)?app\// } || 0
11
+
12
+ Dir.glob(File.join(File.dirname(__FILE__), 'sugarcube-nsdata/**/*.rb')).reverse.each do |file|
13
+ app.files.insert(insert_point, file)
14
+ end
15
+ end
File without changes
@@ -0,0 +1,9 @@
1
+ class NSString
2
+
3
+ # @return [NSData] NSData representation encoded using UTF8, or a specified
4
+ # encoding
5
+ def nsdata(encoding=NSUTF8StringEncoding)
6
+ dataUsingEncoding(encoding)
7
+ end
8
+
9
+ end
@@ -0,0 +1,7 @@
1
+ class NSURL
2
+
3
+ def nsdata
4
+ NSData.dataWithContentsOfURL(self)
5
+ end
6
+
7
+ end
@@ -0,0 +1,8 @@
1
+ class UIImage
2
+
3
+ # @return [NSData] an NSData object in PNG format
4
+ def nsdata
5
+ UIImagePNGRepresentation(self)
6
+ end
7
+
8
+ end
@@ -0,0 +1,15 @@
1
+ unless defined?(Motion::Project::Config)
2
+ raise "The sugarcube gem must be required within a RubyMotion project Rakefile."
3
+ end
4
+
5
+
6
+ Motion::Project::App.setup do |app|
7
+ # scans app.files until it finds app/ (the default)
8
+ # if found, it inserts just before those files, otherwise it will insert to
9
+ # the end of the list
10
+ insert_point = app.files.find_index { |file| file =~ /^(?:\.\/)?app\// } || 0
11
+
12
+ Dir.glob(File.join(File.dirname(__FILE__), 'sugarcube-nsdate/**/*.rb')).reverse.each do |file|
13
+ app.files.insert(insert_point, file)
14
+ end
15
+ end
File without changes
@@ -0,0 +1,23 @@
1
+ class Fixnum
2
+
3
+ def nstimezone
4
+ NSTimeZone.timeZoneForSecondsFromGMT(self)
5
+ end
6
+
7
+ def before(date)
8
+ date - self
9
+ end
10
+
11
+ def ago
12
+ self.before(NSDate.new)
13
+ end
14
+
15
+ def after(date)
16
+ date + self
17
+ end
18
+
19
+ def hence
20
+ self.after(NSDate.new)
21
+ end
22
+
23
+ end
@@ -17,6 +17,11 @@ class NSDate
17
17
  return calendar.dateFromComponents(date_components)
18
18
  end
19
19
 
20
+ # Time.now is defined, but not NSDate.now.
21
+ def self.now
22
+ NSDate.new
23
+ end
24
+
20
25
  def self.today
21
26
  NSDate.new.start_of_day
22
27
  end
@@ -44,7 +49,7 @@ class NSDate
44
49
  # See
45
50
  # <https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1>
46
51
  # and
47
- # <http://www.unicode.org/reports/tr35/tr35-19.html#Date_Format_Patterns>
52
+ # <http://www.unicode.org/reports/tr35/tr35-19.html#Date_Field_Symbol_Table>
48
53
  # for more information about date format strings.
49
54
  def string_with_format(format)
50
55
  if format.is_a?(Symbol)
File without changes
@@ -0,0 +1,16 @@
1
+ class NSString
2
+
3
+ def nstimezone
4
+ case self
5
+ when /([+-]?\d{4})/
6
+ sec = $1[-4,2].to_i * 3600
7
+ NSTimeZone.timeZoneForSecondsFromGMT(sec)
8
+ when /(GMT|UTC)([+-]\d{1,2})?/
9
+ sec = $2 ? $2.to_i * 3600 : 0
10
+ NSTimeZone.timeZoneForSecondsFromGMT(sec)
11
+ else
12
+ NSTimeZone.timeZoneWithName(self)
13
+ end
14
+ end
15
+
16
+ end
@@ -0,0 +1,15 @@
1
+ unless defined?(Motion::Project::Config)
2
+ raise "The sugarcube gem must be required within a RubyMotion project Rakefile."
3
+ end
4
+
5
+
6
+ Motion::Project::App.setup do |app|
7
+ # scans app.files until it finds app/ (the default)
8
+ # if found, it inserts just before those files, otherwise it will insert to
9
+ # the end of the list
10
+ insert_point = app.files.find_index { |file| file =~ /^(?:\.\/)?app\// } || 0
11
+
12
+ Dir.glob(File.join(File.dirname(__FILE__), 'sugarcube-nsuserdefaults/**/*.rb')).reverse.each do |file|
13
+ app.files.insert(insert_point, file)
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ unless defined?(Motion::Project::Config)
2
+ raise "The sugarcube gem must be required within a RubyMotion project Rakefile."
3
+ end
4
+
5
+
6
+ Motion::Project::App.setup do |app|
7
+ # scans app.files until it finds app/ (the default)
8
+ # if found, it inserts just before those files, otherwise it will insert to
9
+ # the end of the list
10
+ insert_point = app.files.find_index { |file| file =~ /^(?:\.\/)?app\// } || 0
11
+
12
+ Dir.glob(File.join(File.dirname(__FILE__), 'sugarcube-numbers/**/*.rb')).reverse.each do |file|
13
+ app.files.insert(insert_point, file)
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ class Fixnum
2
+
3
+ def nth
4
+ # if the first two digits of rank are between 11 and 20, it's an
5
+ # 'up-teenth' kinda number
6
+ modulo_100 = self % 100
7
+ if modulo_100 < 10 || modulo_100 > 20
8
+ case self % 10
9
+ when 1
10
+ return "st"
11
+ when 2
12
+ return "nd"
13
+ when 3
14
+ return "rd"
15
+ end
16
+ end
17
+
18
+ return "th"
19
+ end
20
+
21
+ def ordinalize
22
+ return "#{self}#{nth}"
23
+ end
24
+
25
+ end
@@ -0,0 +1,13 @@
1
+ class NSString
2
+
3
+ # @return boolean
4
+ def nan?
5
+ self.to_number.nil?
6
+ end
7
+
8
+ def to_number
9
+ number_formatter = NSNumberFormatter.new
10
+ number_formatter.numberFromString(self)
11
+ end
12
+
13
+ end
@@ -1,9 +1,21 @@
1
1
  class Numeric
2
2
 
3
+ ##|
4
+ ##| MISC
5
+ ##|
6
+
3
7
  def percent
4
8
  self / 100.0
5
9
  end
6
10
 
11
+ def g
12
+ self * 9.78033
13
+ end
14
+
15
+ def in_g
16
+ self / 9.78033
17
+ end
18
+
7
19
  def radians
8
20
  self
9
21
  end
@@ -36,7 +48,7 @@ class Numeric
36
48
  end
37
49
 
38
50
  def kilometers
39
- self * 1000.0
51
+ self * 1000.0 # 1000 m / km
40
52
  end
41
53
  alias kilometer kilometers
42
54
 
@@ -45,7 +57,7 @@ class Numeric
45
57
  end
46
58
 
47
59
  def miles
48
- self * 1609.344
60
+ self * 1609.344 # 1609.344 m / mi
49
61
  end
50
62
  alias mile miles
51
63
 
@@ -54,7 +66,7 @@ class Numeric
54
66
  end
55
67
 
56
68
  def feet
57
- self / 3.28084
69
+ self / 3.28084 # 3.28084 m / ft
58
70
  end
59
71
  alias foot feet
60
72
 
@@ -97,8 +109,36 @@ class Numeric
97
109
  end
98
110
  alias exabyte exabytes
99
111
 
112
+ def in_bytes
113
+ self
114
+ end
115
+
116
+ def in_kilobytes
117
+ self / 1024**1
118
+ end
119
+
120
+ def in_megabytes
121
+ self / 1024**2
122
+ end
123
+
124
+ def in_gigabytes
125
+ self / 1024**3
126
+ end
127
+
128
+ def in_terabytes
129
+ self / 1024**4
130
+ end
131
+
132
+ def in_petabytes
133
+ self / 1024**5
134
+ end
135
+
136
+ def in_exabytes
137
+ self / 1024**6
138
+ end
139
+
100
140
  def string_with_style(style=NSNumberFormatterDecimalStyle)
101
- if style.is_a? Symbol
141
+ if style.respond_to?(:nsnumberstyle)
102
142
  style = style.nsnumberstyle
103
143
  end
104
144
  NSNumberFormatter.localizedStringFromNumber(self, numberStyle:style)
@@ -0,0 +1,373 @@
1
+ $sugarcube_view = nil
2
+ $sugarcube_items = nil
3
+ $sugarcube_restore = nil
4
+
5
+ module SugarCube
6
+ module Adjust
7
+ module_function
8
+
9
+ def repl_format(format=nil)
10
+ if format
11
+ @@repl_format = format
12
+ else
13
+ # if adjust has not been called, the instance variable has not yet been set
14
+ @@repl_format ||= :ruby
15
+ end
16
+ @@repl_format
17
+ end
18
+
19
+ def adjust(view=nil, format=nil)
20
+ SugarCube::Adjust::repl_format(format.to_sym) if format
21
+ return $sugarcube_view if not view
22
+
23
+ if view.is_a? Fixnum
24
+ window = NSApplication.sharedApplication.keyWindow
25
+ window ||= NSApplication.sharedApplication.mainWindow
26
+ window ||= NSApplication.sharedApplication.windows[0]
27
+ $sugarcube_items ||= SugarCube::Adjust::build_tree(window, :subviews)
28
+ view = $sugarcube_items[view]
29
+ end
30
+
31
+ # a/adjust will return this object
32
+ $sugarcube_view = view
33
+
34
+ if view.is_a?(NSView)
35
+ $sugarcube_restore = {
36
+ frame: SugarCube::Adjust.frame,
37
+ }
38
+
39
+ if format
40
+ puts format_frame view.frame
41
+ end
42
+ end
43
+
44
+ view
45
+ end
46
+ alias a adjust
47
+
48
+ ##| FRAME
49
+ def frame f=nil
50
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view
51
+
52
+ return $sugarcube_view.frame if not f
53
+
54
+ f = SugarCube::CoreGraphics::Rect(f)
55
+ $sugarcube_view.frame = f
56
+ puts format_frame(f)
57
+
58
+ $sugarcube_view
59
+ end
60
+ alias f frame
61
+
62
+ ##| ORIGIN
63
+ def left val=1
64
+ SugarCube::Adjust::right -val
65
+ end
66
+ alias l left
67
+
68
+ def right val=1
69
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view
70
+
71
+ f = $sugarcube_view.frame
72
+ f.origin.x += val
73
+ $sugarcube_view.frame = f
74
+ puts format_frame(f)
75
+
76
+ $sugarcube_view
77
+ end
78
+ alias r right
79
+
80
+ def up val=1
81
+ SugarCube::Adjust::down -val
82
+ end
83
+ alias u up
84
+
85
+ def down val=1
86
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view
87
+
88
+ f = $sugarcube_view.frame
89
+ f.origin.y += val
90
+ $sugarcube_view.frame = f
91
+ puts format_frame(f)
92
+
93
+ $sugarcube_view
94
+ end
95
+ alias d down
96
+
97
+ def origin x=nil, y=nil
98
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view
99
+
100
+ f = $sugarcube_view.frame
101
+ return f.origin if not x
102
+
103
+ if y
104
+ f.origin.x = x
105
+ f.origin.y = y
106
+ else
107
+ f.origin = SugarCube::CoreGraphics::Point(x)
108
+ end
109
+ $sugarcube_view.frame = f
110
+ puts format_frame(f)
111
+
112
+ $sugarcube_view
113
+ end
114
+ alias o origin
115
+
116
+ ##| SIZE
117
+ def thinner val=1
118
+ SugarCube::Adjust::wider -val
119
+ end
120
+ alias n thinner
121
+
122
+ def wider val=1
123
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view
124
+
125
+ f = $sugarcube_view.frame
126
+ f.size.width += val
127
+ $sugarcube_view.frame = f
128
+ puts format_frame(f)
129
+
130
+ $sugarcube_view
131
+ end
132
+ alias w wider
133
+
134
+ def shorter val=1
135
+ SugarCube::Adjust::taller -val
136
+ end
137
+ alias s shorter
138
+
139
+ def taller val=1
140
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view
141
+
142
+ f = $sugarcube_view.frame
143
+ f.size.height += val
144
+ $sugarcube_view.frame = f
145
+ puts format_frame(f)
146
+
147
+ $sugarcube_view
148
+ end
149
+ alias t taller
150
+
151
+ def size w=nil, h=nil
152
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view
153
+
154
+ f = $sugarcube_view.frame
155
+ return f.size if not w
156
+
157
+ if h
158
+ f.size.width = w
159
+ f.size.height = h
160
+ else
161
+ f.size = SugarCube::CoreGraphics::Size(w)
162
+ end
163
+ $sugarcube_view.frame = f
164
+ puts format_frame(f)
165
+
166
+ $sugarcube_view
167
+ end
168
+ alias z size
169
+
170
+ def center(*args)
171
+ raise "no view has been assigned to SugarCube::Adjust::adjust" unless $sugarcube_view
172
+
173
+ element = nil
174
+ total = nil
175
+ direction = 'h'
176
+ args.each do |option|
177
+ case option
178
+ when String, Symbol # accept string or symbol
179
+ direction = option.to_s
180
+ when Numeric
181
+ if not total
182
+ total = option
183
+ elsif not element
184
+ element = option
185
+ else
186
+ raise "I don't know what to do with #{option.inspect}"
187
+ end
188
+ else
189
+ raise "I don't know what to do with #{option.inspect}"
190
+ end
191
+ end
192
+ element = 1 unless element
193
+ total = 1 unless total
194
+
195
+ view = $sugarcube_view
196
+
197
+ left = view.origin.x
198
+ top = view.origin.y
199
+
200
+ if /h|x/.match(direction.downcase)
201
+ swidth = view.frame.width
202
+ pwidth = view.superview.frame.width / total
203
+ left = (pwidth - swidth) / 2 + pwidth * (element - 1)
204
+ end
205
+ if /v|y/.match(direction.downcase)
206
+ sheight = view.frame.height
207
+ pheight = view.superview.frame.height / total
208
+ top = (pheight - sheight) / 2 + pheight * (element - 1)
209
+ end
210
+
211
+ self.origin left, top
212
+ end
213
+ alias c center
214
+
215
+ # @param item this can be a tree-like item (UIView, UIViewController,
216
+ # CALayer) or an integer, in which case it will select that window from
217
+ # UIApplication.sharedApplication.window. Defalt is to display the
218
+ # keyWindow
219
+ # @param selector If you pass an unsupported object to tree, you will need
220
+ # to pass a selector as well - this method should return an array of
221
+ # items which are passed recursively to tree
222
+ # @block sel_blk If a block is passed, it will be used to build the array of
223
+ # items that are called recursively
224
+ def tree(item=nil, selector=nil, &sel_blk)
225
+ if item.is_a?(Fixnum)
226
+ item = NSApplication.sharedApplication.windows[item]
227
+ elsif ! item
228
+ item = NSApplication.sharedApplication.keyWindow || NSApplication.sharedApplication.mainWindow || NSApplication.sharedApplication.windows[0]
229
+ end
230
+ unless item
231
+ puts 'View is nil (no window, view, or controller to display)'
232
+ return
233
+ end
234
+
235
+ if sel_blk
236
+ if selector
237
+ raise "You can't hand me a block AND a selector. I don't know what to do with them!"
238
+ end
239
+ if sel_blk.arity != 1
240
+ raise "The block you passed should accept one and only one object"
241
+ end
242
+ selector = sel_blk
243
+ elsif ! selector
244
+ if item.is_a? NSView
245
+ selector = :subviews
246
+ elsif item.is_a? NSWindow
247
+ item = item.contentView
248
+ selector = :subviews
249
+ elsif item.is_a? NSViewController
250
+ selector = lambda { |ctlr|
251
+ ret = Array.new ctlr.childViewControllers
252
+ if ctlr.presentedViewController && ctlr.presentedViewController.presentingViewController == ctlr
253
+ ret << ctlr.presentedViewController
254
+ end
255
+ ret
256
+ }
257
+ elsif item.is_a? CALayer
258
+ selector = :sublayers
259
+ else
260
+ raise "Unable to determine a SugarCube::Adjust::tree selector for #{item.class.name}"
261
+ end
262
+ end
263
+
264
+ $sugarcube_items = SugarCube::Adjust::build_tree(item, selector)
265
+ if self.respond_to? :draw_tree
266
+ draw_tree(item, selector)
267
+ else
268
+ SugarCube::Adjust::draw_tree(item, selector)
269
+ end
270
+ puts ''
271
+
272
+ return item
273
+ end
274
+
275
+ # Draws the tree items
276
+ def draw_tree(item, selector, tab=nil, is_last=true, items_index=0)
277
+ space = ' '
278
+ if items_index < 10
279
+ print ' '
280
+ elsif items_index < 100
281
+ print ' '
282
+ elsif items_index > 999 # good god, man!
283
+ space = ''
284
+ end
285
+ print items_index.to_s + ":" + space
286
+
287
+ if tab
288
+ print tab
289
+ if is_last
290
+ print '`-- '
291
+ tab += ' '
292
+ else
293
+ print '+-- '
294
+ tab += '| '
295
+ end
296
+ else
297
+ print '. '
298
+ tab = ''
299
+ end
300
+
301
+ if self == item || $sugarcube_view == item
302
+ print "\033[1m"
303
+ end
304
+ if item.is_a? NSView
305
+ puts "#{item} frame: [[#{item.frame.origin.x}, #{item.frame.origin.y}], [#{item.frame.size.width}, #{item.frame.size.height}]]"
306
+ else
307
+ puts item.to_s
308
+ end
309
+ if self == item || $sugarcube_view == item
310
+ print "\033[0m"
311
+ end
312
+
313
+ if selector.is_a? Proc
314
+ items = selector.call(item)
315
+ else
316
+ items = item.send(selector)
317
+ end
318
+ items ||= []
319
+
320
+ items.each_with_index { |subview, index|
321
+ items_index += 1
322
+ if self.respond_to? :draw_tree
323
+ items_index = draw_tree(subview, selector, tab, index == items.length - 1, items_index)
324
+ else
325
+ items_index = SugarCube::Adjust::draw_tree(subview, selector, tab, index == items.length - 1, items_index)
326
+ end
327
+ }
328
+
329
+ return items_index
330
+ end
331
+
332
+ def build_tree(item, selector)
333
+ if selector.is_a? Proc
334
+ items = selector.call(item)
335
+ else
336
+ items = item.send(selector)
337
+ end
338
+ items ||= []
339
+
340
+ ret = [item]
341
+ items.each_with_index { |subview, index|
342
+ ret.concat SugarCube::Adjust::build_tree(subview, selector)
343
+ }
344
+ ret
345
+ end
346
+
347
+ def root
348
+ (NSApplication.sharedApplication.keyWindow || NSApplication.sharedApplication.windows[0]).windowController
349
+ end
350
+
351
+ ##| RESTORE
352
+ def restore
353
+ raise 'no view has been assigned to SugarCube::Adjust::adjust' unless $sugarcube_view
354
+
355
+ $sugarcube_restore.each do |msg, value|
356
+ SugarCube::Adjust.send(msg, value)
357
+ end
358
+ end
359
+
360
+ def format_frame(frame)
361
+ case SugarCube::Adjust::repl_format
362
+ when :json then
363
+ "{x: #{frame.origin.x}, y: #{frame.origin.y}, width: #{frame.size.width}, height: #{frame.size.height}}"
364
+ when :ruby then
365
+ "[[#{frame.origin.x}, #{frame.origin.y}], [#{frame.size.width}, #{frame.size.height}]]"
366
+ when :objc
367
+ frame.to_s
368
+ else
369
+ raise "Unknown repl_format #{SugarCube::Adjust::repl_format.inspect}"
370
+ end
371
+ end
372
+ end
373
+ end