sugarcube 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/
2
+ *.gem
3
+ .DS_Store
data/README.md ADDED
@@ -0,0 +1,127 @@
1
+ sugarcube
2
+ =========
3
+
4
+ Some sugar for your cocoa, or your tea.
5
+
6
+ About
7
+ -----
8
+
9
+ CocoaTouch/iOS is a *verbose* framework. These extensions hope to make
10
+ development in rubymotion more enjoyable by tacking "UI" methods onto the base
11
+ classes (String, Fixnum, Float). With sugarcube, you can create a color from an
12
+ integer or symbol, or create a UIFont or UIImage from a string.
13
+
14
+ Some UI classes are opened up as well, like adding the '<<' operator to a UIView
15
+ instance, instead of view.addSubview(subview), you can use the more idiomatic:
16
+ view << subview.
17
+
18
+ examples
19
+ ========
20
+
21
+ Fixnum
22
+ --------
23
+
24
+ ```ruby
25
+ # create a UIColor from a hex value
26
+ 0xffffff.uicolor # => UIColor.colorWithRed(1.0, green:1.0, blue:1.0, alpha:1.0)
27
+ 0xffffff.uicolor(0.5) # => UIColor.colorWithRed(1.0, green:1.0, blue:1.0, alpha:0.5)
28
+
29
+ # create a percentage
30
+ 100.percent # => 1.00
31
+ 55.percent # => 0.55
32
+ ```
33
+
34
+ Float
35
+ -------
36
+
37
+ ```ruby
38
+ # create a percentage
39
+ 100.0.percent # => 1.00
40
+ 55.0.percent # => 0.55
41
+ ```
42
+
43
+ String
44
+ --------
45
+
46
+ ```ruby
47
+ # UIImage from name
48
+ "my_image".uiimage # => UIImage.imageNamed("my_image")
49
+ # (if you have an image named "blue", use "blue".uiimage.uicolor)
50
+
51
+ # UIFont from name
52
+ "my_font".uifont # => UIFont.fontWithName("my_font", size:UIFont.systemFontSize)
53
+ "my_font".uifont(20) # => UIFont.fontWithName("my_font", size:20)
54
+
55
+ # UIColor from color name OR image name OR hex code
56
+ "blue".uicolor == :blue.uicolor # => UIColor.blueColor
57
+ "#ff00ff".uicolor == :fuchsia.uicolor == 0xff00ff.uicolor # => UIColor.colorWithRed(1.0, green:0.0, blue:1.0, alpha:1.0)
58
+ "#f0f".uicolor(0.5) == :fuchsia.uicolor(0.5) == 0xff00ff.uicolor(0.5) # => UIColor.colorWithRed(1.0, green:1.0, blue:1.0, alpha:0.5)
59
+ # note: 0xf0f.uicolor == 0x00f0f.uicolor. There's no way to tell the difference
60
+ # at run time between those two Fixnum literals.
61
+ "my_image".uicolor == "my_image".uiimage.uicolor # => UIColor.colorWithPatternImage(UIImage.imageNamed("my_image"))
62
+
63
+ # NSLocalizedString from string
64
+ "hello".localized # => NSBundle.mainBundle.localizedStringForKey("hello", value:nil, table:nil)
65
+ "hello".localized('Hello!', 'hello_table') # => ...("hello", value:'Hello!', table:'hello_table')
66
+ ```
67
+
68
+ Symbol
69
+ --------
70
+
71
+ This is the "big daddy". Lots of sugar here...
72
+
73
+ ```ruby
74
+ :center.uialignment # => UITextAlignmentCenter
75
+ :upside_down.uiorientation # => UIDeviceOrientationPortraitUpsideDown
76
+ :rounded.uibuttontype # => UIButtonTypeRoundedRect
77
+ :highlighted.uicontrolstate # => UIControlStateHighlighted
78
+ :touch.uicontrolevent # => UIControlEventTouchUpInside
79
+ :all.uicontrolevent # => UIControlEventAllEvents
80
+ :blue.uicolor # UIColor.blueColor
81
+ # all CSS colors are supported, and alpha
82
+ # (no "grey"s, only "gray"s, consistent with UIKit, which only provides "grayColor")
83
+ :firebrick.uicolor(0.25) # => 0xb22222.uicolor(0.25)
84
+ :bold.uifont # UIFont.boldSystemFontOfSize(UIFont.systemFontSize)
85
+ :bold.uifont(10) # UIFont.boldSystemFontOfSize(10)
86
+ :small.uifontsize # => UIFont.smallSystemFontSize
87
+ :small.uifont # => UIFont.systemFontOfSize(:small.uifontsize)
88
+ :bold.uifont(:small) # UIFont.boldSystemFontOfSize(:small.uifontsize)
89
+ ```
90
+
91
+ UIImage
92
+ ---------
93
+
94
+ ```ruby
95
+ image = "my_image".uiimage
96
+ image.uicolor # => UIColor.colorWithPatternImage(image)
97
+ ```
98
+
99
+ UIView
100
+ --------
101
+
102
+ ```ruby
103
+ self.view << subview # => self.view.addSubview(subview)
104
+ ```
105
+
106
+ UIControl
107
+ -----------
108
+
109
+ Inspired by BubbleWrap's `when` method, but I prefer jQuery-style verbs and
110
+ sugarcube symbols.
111
+
112
+ ```ruby
113
+ button = UIButton.alloc.initWithFrame([0, 0, 10, 10])
114
+
115
+ button.on(:touch) { my_code }
116
+ button.on(:touchupoutside, :touchcancel) { |event|
117
+ puts event.inspect
118
+ # my_code...
119
+ }
120
+
121
+ # remove handlers
122
+ button.off(:touch, :touchupoutside, :touchcancel)
123
+ button.off(:all)
124
+ ```
125
+
126
+ You can only remove handlers by "type", not by the action. e.g. If you bind
127
+ three `:touch` events, calling `button.off(:touch)` will remove all three.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/lib/sugarcube.rb ADDED
@@ -0,0 +1,10 @@
1
+ unless defined?(Motion::Project::Config)
2
+ raise "This file must be required within a RubyMotion project Rakefile."
3
+ end
4
+
5
+
6
+ Motion::Project::App.setup do |app|
7
+ Dir.glob(File.join(File.dirname(__FILE__), 'sugarcube/*.rb')).each do |file|
8
+ app.files.unshift(file)
9
+ end
10
+ end
@@ -0,0 +1,2 @@
1
+ class SugarNotFoundException < Exception
2
+ end
@@ -0,0 +1,24 @@
1
+
2
+
3
+ class Fixnum
4
+
5
+ # 0xffffff.uicolor
6
+ # 0xffffff.uicolor(0.33)
7
+ # =>
8
+ # UIColor.colorWithRed(1.0, green:1.0, red: 1.0, alpha:1.0)
9
+ # UIColor.colorWithRed(1.0, green:1.0, red: 1.0, alpha:0.33)
10
+ def uicolor(alpha=nil)
11
+ alpha = 1.0 if alpha.nil?
12
+
13
+ red = ((self & 0xFF0000) >> 16).to_f / 255.0
14
+ green = ((self & 0xFF00) >> 8).to_f / 255.0
15
+ blue = (self & 0xFF).to_f / 255.0
16
+
17
+ UIColor.colorWithRed(red, green:green, blue:blue, alpha:alpha.to_f)
18
+ end
19
+
20
+ def percent
21
+ self.to_f.percent
22
+ end
23
+
24
+ end
@@ -0,0 +1,9 @@
1
+
2
+
3
+ class Float
4
+
5
+ def percent
6
+ self / 100.0
7
+ end
8
+
9
+ end
@@ -0,0 +1,35 @@
1
+ class String
2
+
3
+ def uiimage
4
+ UIImage.imageNamed(self)
5
+ end
6
+
7
+ def uifont(size=UIFont.systemFontSize)
8
+ UIFont.fontWithName(self, size:size)
9
+ end
10
+
11
+ def uicolor(alpha=nil)
12
+ if self[0,1] == '#'
13
+ # #fff
14
+ if self.length == 4
15
+ return (self[1] * 2 + self[2] * 2 + self[3] * 2).to_i(16).uicolor(alpha)
16
+ end
17
+ # else
18
+ return self[1..-1].to_i(16).uicolor(alpha)
19
+ end
20
+
21
+ begin
22
+ self.to_sym.uicolor(alpha)
23
+ rescue SugarNotFoundException
24
+ self.uiimage.uicolor(alpha)
25
+ end
26
+ end
27
+
28
+ # This can be called as `"Hello".localized` or `"Hello"._`. The `str._`
29
+ # syntax is meant to be reminiscent of gettext-style `_(str)`.
30
+ def localized(value=nil, table=nil)
31
+ NSBundle.mainBundle.localizedStringForKey(self, value:value, table:table)
32
+ end
33
+ alias :_ :localized
34
+
35
+ end
@@ -0,0 +1,190 @@
1
+ =begin
2
+ Adds some UI classes to the Symbol class. These methods are prefixed with `ui`
3
+ to make their intent clear, and to provide a little bit of "namespacing"
4
+
5
+ # alignment
6
+ :left.uialignment => UITextAlignmentLeft
7
+
8
+ # uicolors
9
+ :black.uicolor => UIColor.blackColor
10
+
11
+ :darkturquoise.uicolor => UIColor.blackColor
12
+
13
+ # fonts
14
+ :system.uifont => UIFont.systemFontOfSize(UIFont.systemFontSize)
15
+ :label.uifont => UIFont.systemFontOfSize(UIFont.labelFontSize)
16
+
17
+ You can extend the defaults by adding entries:
18
+
19
+ Symbol.css_colors[:my_color] = 0x123456
20
+ Symbol.font_sizes[:big] = 40
21
+
22
+ :my_color.uicolor => UIColor
23
+ :big.uifont => UIFont
24
+ =end
25
+ class Symbol
26
+ class << self
27
+ attr_accessor :devices
28
+ attr_accessor :alignments
29
+ attr_accessor :orientations
30
+ attr_accessor :buttontypes
31
+ attr_accessor :bordertypes
32
+ attr_accessor :controlstates
33
+ attr_accessor :controlevents
34
+ attr_accessor :system_fonts
35
+ attr_accessor :font_sizes
36
+ end
37
+
38
+ @devices = {
39
+ iphone: UIUserInterfaceIdiomPhone,
40
+ ipad: UIUserInterfaceIdiomPad,
41
+ }
42
+
43
+ @alignments = {
44
+ left: UITextAlignmentLeft,
45
+ right: UITextAlignmentRight,
46
+ center: UITextAlignmentCenter,
47
+ }
48
+
49
+ @orientations = {
50
+ portrait: UIInterfaceOrientationPortrait,
51
+ upside_down: UIInterfaceOrientationPortraitUpsideDown,
52
+ left: UIInterfaceOrientationLandscapeLeft,
53
+ right: UIInterfaceOrientationLandscapeRight,
54
+ }
55
+
56
+ @buttontypes = {
57
+ custom: UIButtonTypeCustom,
58
+ rounded: UIButtonTypeRoundedRect,
59
+ rounded_rect: UIButtonTypeRoundedRect,
60
+ detail: UIButtonTypeDetailDisclosure,
61
+ detail_disclosure: UIButtonTypeDetailDisclosure,
62
+ info: UIButtonTypeInfoLight,
63
+ info_light: UIButtonTypeInfoLight,
64
+ info_dark: UIButtonTypeInfoDark,
65
+ contact: UIButtonTypeContactAdd,
66
+ contact_add: UIButtonTypeContactAdd,
67
+ }
68
+
69
+ @bordertypes = {
70
+ none: UITextBorderStyleNone,
71
+ line: UITextBorderStyleLine,
72
+ bezel: UITextBorderStyleBezel,
73
+ rounded: UITextBorderStyleRoundedRect,
74
+ rounded_rect: UITextBorderStyleRoundedRect,
75
+ }
76
+
77
+ @controlstates = {
78
+ normal: UIControlStateNormal,
79
+ highlighted: UIControlStateHighlighted,
80
+ disabled: UIControlStateDisabled,
81
+ selected: UIControlStateSelected,
82
+ application: UIControlStateApplication,
83
+ }
84
+
85
+ @controlevents = {
86
+ touch: UIControlEventTouchUpInside,
87
+ touch_down: UIControlEventTouchDown,
88
+
89
+ touch_down_repeat: UIControlEventTouchDownRepeat,
90
+ touch_drag_inside: UIControlEventTouchDragInside,
91
+ touch_drag_outside: UIControlEventTouchDragOutside,
92
+ touch_drag_enter: UIControlEventTouchDragEnter,
93
+ touch_drag_exit: UIControlEventTouchDragExit,
94
+ touch_up_inside: UIControlEventTouchUpInside,
95
+ touch_up_outside: UIControlEventTouchUpOutside,
96
+ touch_cancel: UIControlEventTouchCancel,
97
+
98
+ value_changed: UIControlEventValueChanged,
99
+
100
+ editing_did_begin: UIControlEventEditingDidBegin,
101
+ editing_changed: UIControlEventEditingChanged,
102
+ editing_did_end: UIControlEventEditingDidEnd,
103
+ editing_did_endonexit: UIControlEventEditingDidEndOnExit,
104
+
105
+ all_touch: UIControlEventAllTouchEvents,
106
+ all_editing: UIControlEventAllEditingEvents,
107
+ application: UIControlEventApplicationReserved,
108
+ system: UIControlEventSystemReserved,
109
+ all: UIControlEventAllEvents,
110
+ }
111
+
112
+ @system_fonts = {
113
+ system: :"systemFontOfSize:",
114
+ bold: :"boldSystemFontOfSize:",
115
+ italic: :"italicSystemFontOfSize:",
116
+ }
117
+
118
+ @font_sizes = {
119
+ label: :labelFontSize,
120
+ button: :buttonFontSize,
121
+ small: :smallSystemFontSize,
122
+ system: :systemFontSize,
123
+ }
124
+
125
+ private
126
+ def look_in(here)
127
+ return here[self] if here.has_key? self
128
+ raise SugarNotFoundException.new(self)
129
+ end
130
+
131
+ public
132
+ def uidevice
133
+ look_in(Symbol.devices)
134
+ if Symbol.devices[self]
135
+ return Symbol.devices[self]
136
+ end
137
+ raise SugarNotFoundException.new(self)
138
+ end
139
+
140
+ def uialignment
141
+ look_in(Symbol.alignments)
142
+ end
143
+
144
+ def uiorientation
145
+ look_in(Symbol.orientations)
146
+ end
147
+
148
+ def uibuttontype
149
+ look_in(Symbol.buttontypes)
150
+ end
151
+
152
+ def uibordertype
153
+ look_in(Symbol.bordertypes)
154
+ end
155
+
156
+ def uicontrolstate
157
+ look_in(Symbol.controlstates)
158
+ end
159
+
160
+ def uicontrolevent
161
+ look_in(Symbol.controlevents)
162
+ end
163
+
164
+ def uifont(size=UIFont.systemFontSize)
165
+ # system fonts
166
+ begin
167
+ font = look_in(Symbol.system_fonts)
168
+ if Symbol === size
169
+ size = Symbol.font_sizes.fetch(size).uifontsize
170
+ end
171
+ return UIFont.send(font, size)
172
+ rescue SugarNotFoundException
173
+ size = look_in(font_sizes).uifontsize
174
+ return UIFont.systemFontOfSize(size)
175
+ end
176
+ end
177
+
178
+ def uifontsize
179
+ size = look_in(Symbol.system_fonts)
180
+ if Symbol === size
181
+ return UIFont.send(Symbol.font_sizes[self])
182
+ end
183
+ return size.to_f
184
+ end
185
+
186
+ def to_teacup_stylesheet
187
+ Teacup::Stylesheet[self]
188
+ end
189
+
190
+ end
@@ -0,0 +1,172 @@
1
+ class Symbol
2
+ class << self
3
+ attr_accessor :uicolors
4
+ attr_accessor :css_colors
5
+ end
6
+
7
+ def uicolor(alpha=nil)
8
+ begin
9
+ # iOS colors
10
+ color = UIColor.send(look_in(Symbol.uicolors))
11
+
12
+ if not alpha.nil?
13
+ color = color.colorWithAlphaComponent(alpha.to_f)
14
+ end
15
+ rescue SugarNotFoundException
16
+ # css colors
17
+ color = look_in(Symbol.css_colors).uicolor(alpha)
18
+ end
19
+
20
+ color
21
+ end
22
+
23
+ @uicolors = {
24
+ black: :blackColor,
25
+ blue: :blueColor,
26
+ brown: :brownColor,
27
+ cyan: :cyanColor,
28
+ darkgray: :darkGrayColor,
29
+ gray: :grayColor,
30
+ green: :greenColor,
31
+ lightgray: :lightGrayColor,
32
+ magenta: :magentaColor,
33
+ orange: :orangeColor,
34
+ purple: :purpleColor,
35
+ red: :redColor,
36
+ yellow: :yellowColor,
37
+ white: :whiteColor,
38
+ clear: :clearColor,
39
+
40
+ table_view: :groupTableViewBackgroundColor,
41
+ }
42
+
43
+ @css_colors = {
44
+ aliceblue: 0xf0f8ff,
45
+ antiquewhite: 0xfaebd7,
46
+ aqua: 0x00ffff,
47
+ aquamarine: 0x7fffd4,
48
+ azure: 0xf0ffff,
49
+ beige: 0xf5f5dc,
50
+ bisque: 0xffe4c4,
51
+ blanchedalmond: 0xffebcd,
52
+ blueviolet: 0x8a2be2,
53
+ burlywood: 0xdeb887,
54
+ cadetblue: 0x5f9ea0,
55
+ chartreuse: 0x7fff00,
56
+ chocolate: 0xd2691e,
57
+ coral: 0xff7f50,
58
+ cornflowerblue: 0x6495ed,
59
+ cornsilk: 0xfff8dc,
60
+ crimson: 0xdc143c,
61
+ darkblue: 0x00008b,
62
+ darkcyan: 0x008b8b,
63
+ darkgoldenrod: 0xb8860b,
64
+ darkgreen: 0x006400,
65
+ darkkhaki: 0xbdb76b,
66
+ darkmagenta: 0x8b008b,
67
+ darkolivegreen: 0x556b2f,
68
+ darkorange: 0xff8c00,
69
+ darkorchid: 0x9932cc,
70
+ darkred: 0x8b0000,
71
+ darksalmon: 0xe9967a,
72
+ darkseagreen: 0x8fbc8f,
73
+ darkslateblue: 0x483d8b,
74
+ darkslategray: 0x2f4f4f,
75
+ darkturquoise: 0x00ced1,
76
+ darkviolet: 0x9400d3,
77
+ deeppink: 0xff1493,
78
+ deepskyblue: 0x00bfff,
79
+ dimgray: 0x696969,
80
+ dodgerblue: 0x1e90ff,
81
+ firebrick: 0xb22222,
82
+ floralwhite: 0xfffaf0,
83
+ forestgreen: 0x228b22,
84
+ fuchsia: 0xff00ff,
85
+ gainsboro: 0xdcdcdc,
86
+ ghostwhite: 0xf8f8ff,
87
+ gold: 0xffd700,
88
+ goldenrod: 0xdaa520,
89
+ greenyellow: 0xadff2f,
90
+ honeydew: 0xf0fff0,
91
+ hotpink: 0xff69b4,
92
+ indianred: 0xcd5c5c,
93
+ indigo: 0x4b0082,
94
+ ivory: 0xfffff0,
95
+ khaki: 0xf0e68c,
96
+ lavender: 0xe6e6fa,
97
+ lavenderblush: 0xfff0f5,
98
+ lawngreen: 0x7cfc00,
99
+ lemonchiffon: 0xfffacd,
100
+ lightblue: 0xadd8e6,
101
+ lightcoral: 0xf08080,
102
+ lightcyan: 0xe0ffff,
103
+ lightgoldenrodyellow: 0xfafad2,
104
+ lightgreen: 0x90ee90,
105
+ lightpink: 0xffb6c1,
106
+ lightsalmon: 0xffa07a,
107
+ lightseagreen: 0x20b2aa,
108
+ lightskyblue: 0x87cefa,
109
+ lightslategray: 0x778899,
110
+ lightsteelblue: 0xb0c4de,
111
+ lightyellow: 0xffffe0,
112
+ lime: 0x00ff00,
113
+ limegreen: 0x32cd32,
114
+ linen: 0xfaf0e6,
115
+ maroon: 0x800000,
116
+ mediumaquamarine: 0x66cdaa,
117
+ mediumblue: 0x0000cd,
118
+ mediumorchid: 0xba55d3,
119
+ mediumpurple: 0x9370d8,
120
+ mediumseagreen: 0x3cb371,
121
+ mediumslateblue: 0x7b68ee,
122
+ mediumspringgreen: 0x00fa9a,
123
+ mediumturquoise: 0x48d1cc,
124
+ mediumvioletred: 0xc71585,
125
+ midnightblue: 0x191970,
126
+ mintcream: 0xf5fffa,
127
+ mistyrose: 0xffe4e1,
128
+ moccasin: 0xffe4b5,
129
+ navajowhite: 0xffdead,
130
+ navy: 0x000080,
131
+ oldlace: 0xfdf5e6,
132
+ olive: 0x808000,
133
+ olivedrab: 0x6b8e23,
134
+ orangered: 0xff4500,
135
+ orchid: 0xda70d6,
136
+ palegoldenrod: 0xeee8aa,
137
+ palegreen: 0x98fb98,
138
+ paleturquoise: 0xafeeee,
139
+ palevioletred: 0xd87093,
140
+ papayawhip: 0xffefd5,
141
+ peachpuff: 0xffdab9,
142
+ peru: 0xcd853f,
143
+ pink: 0xffc0cb,
144
+ plum: 0xdda0dd,
145
+ powderblue: 0xb0e0e6,
146
+ rosybrown: 0xbc8f8f,
147
+ royalblue: 0x4169e1,
148
+ saddlebrown: 0x8b4513,
149
+ salmon: 0xfa8072,
150
+ sandybrown: 0xf4a460,
151
+ seagreen: 0x2e8b57,
152
+ seashell: 0xfff5ee,
153
+ sienna: 0xa0522d,
154
+ silver: 0xc0c0c0,
155
+ skyblue: 0x87ceeb,
156
+ slateblue: 0x6a5acd,
157
+ slategray: 0x708090,
158
+ snow: 0xfffafa,
159
+ springgreen: 0x00ff7f,
160
+ steelblue: 0x4682b4,
161
+ tan: 0xd2b48c,
162
+ teal: 0x008080,
163
+ thistle: 0xd8bfd8,
164
+ tomato: 0xff6347,
165
+ turquoise: 0x40e0d0,
166
+ violet: 0xee82ee,
167
+ wheat: 0xf5deb3,
168
+ whitesmoke: 0xf5f5f5,
169
+ yellowgreen: 0x9acd32,
170
+ }
171
+
172
+ end
@@ -0,0 +1,59 @@
1
+
2
+ class UIColor
3
+
4
+ def to_s
5
+ alpha = self.alpha.to_s
6
+
7
+ Symbol.uicolors.each_pair do |color, method|
8
+ if UIColor.send(method) == self
9
+ return "UIColor.#{method}(#{alpha})"
10
+ end
11
+ end
12
+
13
+ red = (self.red * 255).to_i << 16
14
+ green = (self.green * 255).to_i << 8
15
+ blue = (self.blue * 255).to_i
16
+ my_color = red + green + blue
17
+ Symbol.css_colors.each_pair do |color, hex|
18
+ if hex == my_color
19
+ return "UIColor.color(#{color.inspect}, #{alpha})"
20
+ end
21
+ end
22
+ return "UIColor.color(#{red}, #{green}, #{blue}, #{alpha})"
23
+ end
24
+
25
+ def color
26
+ if not @color
27
+ red = Pointer.new(:float)
28
+ green = Pointer.new(:float)
29
+ blue = Pointer.new(:float)
30
+ alpha = Pointer.new(:float)
31
+ self.getRed(red, green:green, blue:blue, alpha:alpha)
32
+ @color = {
33
+ red: red[0],
34
+ green: green[0],
35
+ blue: blue[0],
36
+ alpha: alpha[0],
37
+ }
38
+ end
39
+ @color
40
+ end
41
+
42
+ def red
43
+ self.color[:red]
44
+ end
45
+
46
+ def green
47
+ self.color[:green]
48
+ end
49
+
50
+ def blue
51
+ self.color[:blue]
52
+ end
53
+
54
+ def alpha
55
+ self.color[:alpha]
56
+ end
57
+
58
+
59
+ end
@@ -0,0 +1,46 @@
1
+
2
+
3
+ class UIControl
4
+
5
+ # event blocks need to be retained, and the addTarget method explicitly does
6
+ # *not* retain `target`. This makes sure that callbacks are retained by
7
+ # pushing the block onto a stack.
8
+ def sugarcube_callbacks
9
+ @sugarcube_callbacks ||= Hash.new { |hash, key| hash[key] = [] }
10
+ end
11
+
12
+ # Add event handlers to UIControls
13
+ #
14
+ # @example
15
+ # button = UIButton.alloc.initWithFrame([0, 0, 10, 10])
16
+ # button.on(:touch) { my_code }
17
+ # button.on(:touchupoutside, :touchcancel) { my_code }
18
+ def on(*events, &block)
19
+ events.each do |event|
20
+ event = event.uicontrolevent unless Fixnum === event
21
+
22
+ sugarcube_callbacks[event].push block
23
+ addTarget(block, action: :call, forControlEvents:event)
24
+ end
25
+
26
+ self
27
+ end
28
+
29
+ # Removes all events that were bound with `on`.
30
+ #
31
+ # @example
32
+ # button.off(:touch)
33
+ # button.off(:touchupoutside, :touchcancel)
34
+ def off(*events)
35
+ events.each do |event|
36
+ event = event.uicontrolevent unless Fixnum === event
37
+
38
+ sugarcube_callbacks[event].each do |block|
39
+ self.removeTarget(block, action: :call, forControlEvents:event)
40
+ end
41
+ sugarcube_callbacks.delete(event)
42
+ end
43
+ self
44
+ end
45
+
46
+ end
@@ -0,0 +1,14 @@
1
+
2
+
3
+ class UIImage
4
+
5
+ def uicolor(alpha=nil)
6
+ color = UIColor.colorWithPatternImage(self)
7
+ if not alpha.nil?
8
+ color = color.colorWithAlphaComponent(alpha.to_f)
9
+ end
10
+
11
+ color
12
+ end
13
+
14
+ end
@@ -0,0 +1,24 @@
1
+
2
+
3
+ class UIView
4
+
5
+ def <<(view)
6
+ self.addSubview view
7
+ end
8
+
9
+ # i like this idea, but view[0] = view doesn't *look* like an insertion,
10
+ # it lookes like a replacement... it should be written as
11
+ # view[0] += sub_view
12
+ # or
13
+ # view[0] << sub_view
14
+ # def []=(index, view)
15
+ # self.insertSubview(view, atIndex:index)
16
+ # end
17
+
18
+ def to_s
19
+ "{#{self.class.name} @ x: #{self.frame.origin.x} y:#{self.frame.origin.y}, "\
20
+ "#{self.frame.size.width}×#{self.frame.size.height}}"\
21
+ "#{self.superview ? ' child of ' + self.superview.class.name : ''}"
22
+ end
23
+
24
+ end
@@ -0,0 +1,3 @@
1
+ module SugarCube
2
+ Version = '0.1.1'
3
+ end
data/sugarcube.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/sugarcube/version.rb', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = 'sugarcube'
6
+ gem.version = SugarCube::Version
7
+
8
+ gem.authors = ['Colin Thomas-Arnold', 'Chris Clarke']
9
+ gem.email = ['colin@fusionbox.com']
10
+ gem.summary = %{Extensions for Ruby to make Rubymotion development more enjoyable, and hopefully more rubyesque!}
11
+ gem.description = <<-DESC
12
+ CocoaTouch/iOS is a *verbose* framework. These extensions hope to make
13
+ development in rubymotion more enjoyable by tacking "UI" methods onto the
14
+ base classes (String, Fixnum, Float). With sugarcube, you can create a
15
+ color from an integer or symbol, or create a UIFont or UIImage from a
16
+ string.
17
+
18
+ Some UI classes are opened up as well, like adding the '<<' operator to a
19
+ UIView instance, instead of view.addSubview(subview), you can use the more
20
+ idiomatic: view << subview.
21
+ DESC
22
+
23
+ gem.homepage = 'https://github.com/fusionbox/sugarcube'
24
+
25
+ gem.files = `git ls-files`.split($\)
26
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
27
+ gem.test_files = gem.files.grep(%r{^spec/})
28
+
29
+ gem.require_paths = ['lib']
30
+
31
+ gem.add_dependency 'rake'
32
+ gem.add_development_dependency 'rspec'
33
+
34
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sugarcube
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,19 +11,70 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
  date: 2012-07-10 00:00:00.000000000 Z
14
- dependencies: []
15
- description: ! " CocoaTouch/iOS is a *verbose* framework. These extensions hope
16
- to make\n development in rubymotion more enjoyable by tacking \"UI\" methods
17
- onto the\n base classes (String, Fixnum, Float). With sugarcube, you can create
18
- a\n color from an integer or symbol, or create a UIFont or UIImage from a\n
19
- \ string.\n\n Some UI classes are opened up as well, like adding the '<<'
20
- operator to a\n UIView instance, instead of view.addSubview(subview), you can
21
- use the more\n idiomatic: view << subview.\n"
22
- email: colin@fusionbox.com
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rake
17
+ requirement: &70117501802540 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *70117501802540
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: &70117501801900 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *70117501801900
37
+ description: ! 'CocoaTouch/iOS is a *verbose* framework. These extensions hope to
38
+ make
39
+
40
+ development in rubymotion more enjoyable by tacking "UI" methods onto the
41
+
42
+ base classes (String, Fixnum, Float). With sugarcube, you can create a
43
+
44
+ color from an integer or symbol, or create a UIFont or UIImage from a
45
+
46
+ string.
47
+
48
+
49
+ Some UI classes are opened up as well, like adding the ''<<'' operator to a
50
+
51
+ UIView instance, instead of view.addSubview(subview), you can use the more
52
+
53
+ idiomatic: view << subview.
54
+
55
+ '
56
+ email:
57
+ - colin@fusionbox.com
23
58
  executables: []
24
59
  extensions: []
25
60
  extra_rdoc_files: []
26
- files: []
61
+ files:
62
+ - .gitignore
63
+ - README.md
64
+ - Rakefile
65
+ - lib/sugarcube.rb
66
+ - lib/sugarcube/exceptions.rb
67
+ - lib/sugarcube/fixnum.rb
68
+ - lib/sugarcube/float.rb
69
+ - lib/sugarcube/string.rb
70
+ - lib/sugarcube/symbol.rb
71
+ - lib/sugarcube/symbol/symbol_uicolor.rb
72
+ - lib/sugarcube/uicolor.rb
73
+ - lib/sugarcube/uicontrol.rb
74
+ - lib/sugarcube/uiimage.rb
75
+ - lib/sugarcube/uiview.rb
76
+ - lib/sugarcube/version.rb
77
+ - sugarcube.gemspec
27
78
  homepage: https://github.com/fusionbox/sugarcube
28
79
  licenses: []
29
80
  post_install_message: