Class: Teacup::Appearance

Inherits:
Stylesheet show all
Defined in:
lib/teacup-ios/appearance.rb

Overview

An interface to style views using the UIAppearance protocol.

Work similarly as the Stylesheet class.

Teacup::Appearance.new do

style UINavigationBar,
  tintColor: UIColor.colorWithRed(0.886, green:0.635, blue:0, alpha: 1)

end

Constant Summary collapse

TeacupAppearanceApplyNotification =
'TeacupAppearanceApplyNotification'

Instance Attribute Summary

Attributes inherited from Stylesheet

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Stylesheet

[], []=, #app_size, #autoresize, #constrain, #constrain_above, #constrain_below, #constrain_bottom, #constrain_center_x, #constrain_center_y, #constrain_height, #constrain_left, #constrain_right, #constrain_size, #constrain_to_left, #constrain_to_right, #constrain_top, #constrain_width, #constrain_xy, #device, #device_is?, #extends_style?, #flexible_bottom, #flexible_height, #flexible_left, #flexible_right, #flexible_top, #flexible_width, #flip, #get_stylesheet_cache, #iPad, #iPadRetina, #iPhone, #iPhone35, #iPhone4, #iPhone5, #iPhoneRetina, #identity, #import, #import_instance_vars, #imported_stylesheets, #inspect, #pi, #query, #rotate, #run_block, #screen_size, #set_stylesheet_cache, #spin, #stylesheet_cache, stylesheets, #transform_layer, #transform_view, #twist

Constructor Details

#initialize(&block) ⇒ Appearance

disable the stylesheet ‘name’ parameter, and assign the “super secret” stylesheet name



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/teacup-ios/appearance.rb', line 29

def initialize(&block)
  NSNotificationCenter.defaultCenter.addObserver(self,
          selector: :'apply_appearance:',
          name: UIApplicationDidFinishLaunchingNotification,
          object: nil)
  NSNotificationCenter.defaultCenter.addObserver(self,
          selector: :'apply_appearance:',
          name: TeacupAppearanceApplyNotification,
          object: nil)

  super(&block)
end

Class Method Details

.applyObject



17
18
19
# File 'lib/teacup-ios/appearance.rb', line 17

def self.apply
  NSNotificationCenter.defaultCenter.postNotificationName(TeacupAppearanceApplyNotification, object:nil)
end

Instance Method Details

#apply_appearance(notification = nil) ⇒ Object

This block is only run once, and the properties object from when_contained_in is a copy (via Teacup::Style.new), so we remove the when_contained_in property using ‘delete`



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/teacup-ios/appearance.rb', line 75

def apply_appearance(notification=nil)
  return unless run_block
  NSNotificationCenter.defaultCenter.removeObserver(self, name:UIApplicationDidFinishLaunchingNotification, object:nil)
  NSNotificationCenter.defaultCenter.removeObserver(self, name:TeacupAppearanceApplyNotification, object:nil)

  when_contained_in.each do |klass, properties|
    contained_in = properties.delete(:when_contained_in)
    contained_in = [contained_in] unless contained_in.is_a?(NSArray)
    # make a copy and add nil to the end
    contained_in += [nil]
    appearance = klass.send(:'appearanceWhenContainedIn:', *contained_in)
    Teacup.apply_hash appearance, properties.build, klass
  end
  styles.each do |klass, properties|
    appearance = klass.appearance
    Teacup.apply_hash appearance, properties.build, klass
  end
end

#exclude_instance_varsObject



42
43
44
# File 'lib/teacup-ios/appearance.rb', line 42

def exclude_instance_vars
  @exclude_instance_vars ||= super + [:@when_contained_in]
end

#style(*queries) ⇒ Object

Styles that have the ‘when_contained_in` setting need to be kept separate



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/teacup-ios/appearance.rb', line 47

def style(*queries)
  # do not modify queries, it gets passed to `super`
  if queries[-1].is_a? Hash
    properties = queries[-1]
  else
    # empty style declarations are allowed, but accomplish nothing.
    return
  end

  if properties.include?(:when_contained_in)
    # okay NOW modify queries
    queries.pop
    queries.each do |stylename|
      style = Style.new
      style.stylename = stylename
      style.stylesheet = self
      style.merge!(properties)

      when_contained_in << [stylename, style]
    end
  else
    super
  end
end

#when_contained_inObject

Contains a list of styles associated with “containers”. These do not get merged like the usual ‘style` declarations.



23
24
25
# File 'lib/teacup-ios/appearance.rb', line 23

def when_contained_in
  @when_contained_in ||= []
end