lib/teacup/layout.rb in teacup-0.3.12 vs lib/teacup/layout.rb in teacup-1.0.0

- old
+ new

@@ -9,21 +9,62 @@ # In order to use layout() in a UIViewController most effectively you will want # to define a stylesheet method that returns a stylesheet. # # @example # class MyViewController < UIViewController + # stylesheet :logo + # # layout(:my_view) do # layout UIImage, :logo # end - # - # def stylesheet - # Teacup::Stylesheet[:logo] - # end # end module Layout - attr_accessor :stylesheet + # Assign a Stylesheet or Stylesheet name (Symbol) + # + # @return val + # + # @example + # + # stylesheet = Stylesheet.new do + # style :root, backgroundColor: UIColor.blueColor + # end + # controller.stylesheet = stylesheet + # # or use a stylename + # view.stylesheet = :stylesheet_name + # + def stylesheet= val + @stylesheet = val + end + # Returns a stylesheet to use to style the contents of this controller's + # view. You can also assign a stylesheet to {stylesheet=}, which will in + # turn call {restyle!}. + # + # This method will be queried each time {restyle!} is called, and also + # implicitly whenever Teacup needs to draw your layout (currently only at + # view load time). + # + # @return Teacup::Stylesheet + # + # @example + # + # def stylesheet + # if [UIInterfaceOrientationLandscapeLeft, + # UIInterfaceOrientationLandscapeRight].include?(UIInterface.currentDevice.orientation) + # Teacup::Stylesheet[:ipad] + # else + # Teacup::Stylesheet[:ipadvertical] + # end + # end + def stylesheet + if @stylesheet.is_a? Symbol + @stylesheet = Teacup::Stylesheet[@stylesheet] + end + + @stylesheet + end + # Alter the layout of a view # # @param instance The first parameter is the view that you want to # layout. # @@ -63,21 +104,23 @@ view = to_instance(view_or_class) name = nil properties = properties_or_nil - if Hash === name_or_properties + if name_or_properties.is_a? Hash name = nil properties = name_or_properties elsif name_or_properties name = name_or_properties.to_sym end # prevents the calling of restyle! until we return to this method should_restyle = Teacup.should_restyle_and_block - view.stylesheet = stylesheet + unless view.stylesheet + view.stylesheet = stylesheet + end view.stylename = name if properties view.style(properties) if properties end @@ -86,13 +129,12 @@ instance_exec(view, &block) if block_given? superview_chain.pop end if should_restyle - view.restyle! - # restore to whatever it was, either nil or true Teacup.should_restyle! + view.restyle! end view end # Add a new subview to the view heirarchy. @@ -163,7 +205,8 @@ # The view at the end of the stack is the one into which subviews # are currently being attached. def superview_chain @superview_chain ||= [] end + end end