Files
rmq/motion/ruby_motion_query/stylesheet.rb
2013-07-29 15:37:44 -07:00

186 lines
4.7 KiB
Ruby

module RubyMotionQuery
class RMQ
def stylesheet=(value)
controller = view_controller
unless value.is_a?(RubyMotionQuery::Stylesheet)
value = value.new(controller)
end
controller.rmq_data.stylesheet = value
self
end
def stylesheet
@_stylesheet ||= begin
self.view_controller.rmq_data.stylesheet if self.view_controller
end
end
def apply_style(style_name)
selected.each do |selected_view|
apply_style_to_view selected_view, style_name
end
self
end
def style()
selected.each do |view|
yield(styler_for(view))
end
self
end
def reapply_styles
selected.each do |selected_view|
if style_name = selected_view.rmq_data.style_name
apply_style_to_view selected_view, style_name
end
end
end
def styler_for(view)
# TODO should have a pool of stylers to reuse, or just assume single threaded and
# memoize this, however if you do that, make sure the dev doesn't retain them in a var
custom_stylers(view) || begin
case view
# TODO, rename all the stylers to Styler::UILabel, etc
when UILabel then Stylers::UILabelStyler.new(view)
when UIButton then Stylers::UIButtonStyler.new(view)
when UIImageView then Stylers::UIImageViewStyler.new(view)
when UIScrollView then Stylers::UIScrollViewStyler.new(view)
when UISwitch then Stylers::UISwitchStyler.new(view)
when UIDatePicker then Stylers::UIDatePickerStyler.new(view)
when UISegmentedControl then Stylers::UISegmentedControlStyler.new(view)
when UIRefreshControl then Stylers::UIRefreshControlStyler.new(view)
when UIPageControl then Stylers::UIPageControlStyler.new(view)
when UISlider then Stylers::UISliderStyler.new(view)
when UIStepper then Stylers::UIStepperStyler.new(view)
when UITabBar then Stylers::UITabBarStyler.new(view)
when UITableView then Stylers::UITableViewStyler.new(view)
when UITableViewCell then Stylers::UITableViewCellStyler.new(view)
when UITextView then Stylers::UITextViewStyler.new(view)
when UITextField then Stylers::UITextFieldStyler.new(view)
when UINavigationBar then Stylers::UINavigationBarStyler.new(view)
# TODO, all the controls are done, but missing some views, add
when UIControl then Stylers::UIControlStyler.new(view)
else
Stylers::UIViewStyler.new(view)
end
end
end
protected
# Override to set your own stylers, or just open up the styler classes
def custom_stylers(view)
end
def apply_style_to_view(view, style_name)
begin
stylesheet.__send__(style_name, styler_for(view))
view.rmq_data.style_name = style_name
rescue NoMethodError => e
if e.message =~ /.*#{style_name.to_s}.*/
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"
else
raise e
end
end
end
end
class Stylesheet
attr_reader :controller
def initialize(controller)
@controller = WeakRef.new(controller)
unless Stylesheet.application_was_setup
Stylesheet.application_was_setup = true
application_setup
end
setup
end
def application_setup
# Override to do your overall setup for your applications. This
# is where you want to add your custom fonts and colors
# This only gets called once
end
def setup
# Override if you need to do setup in your specific stylesheet
end
class << self
attr_accessor :application_was_setup
end
# Convenience methods -------------------
def rmq
if @controller
@controller.rmq
else
RMQ
end
end
def device
RMQ.device
end
def landscape?
device.landscape?
end
def portrait?
device.portrait?
end
def iphone?
device.iphone?
end
def ipad?
device.ipad?
end
def four_inch?
RMQ.device.four_inch?
end
def retina?
RMQ.device.retina?
end
def window
RMQ.app.window
end
def app_width
app_size.width
end
def app_height
app_size.height
end
def app_size
device.screen.applicationFrame.size
end
def screen_size
device.screen.bounds.size
end
def image
RMQ.image
end
def color
RMQ.color
end
def font
RMQ.font
end
end
end