Class: Teacup::Style

Inherits:
Hash
  • Object
show all
Defined in:
lib/teacup-ios/style.rb,
lib/teacup-osx/style.rb

Overview

The Style class is where the precedence rules are applied. A Style can query the Stylesheet that created it to look up other styles (for ‘extends:`) and to import other Stylesheets.

Constant Summary collapse

Orientations =
[:portrait, :upside_up, :upside_down, :landscape, :landscape_left, :landscape_right]
Overrides =
{
  0 => [:portrait, :upside_up],
  UIInterfaceOrientationPortrait => [:portrait, :upside_up],
  UIInterfaceOrientationPortraitUpsideDown => [:portrait, :upside_down],
  UIInterfaceOrientationLandscapeLeft => [:landscape, :landscape_left],
  UIInterfaceOrientationLandscapeRight => [:landscape, :landscape_right],
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#stylenameObject

Returns the value of attribute stylename.



8
9
10
# File 'lib/teacup-ios/style.rb', line 8

def stylename
  @stylename
end

#stylesheetObject

Returns the value of attribute stylesheet.



9
10
11
# File 'lib/teacup-ios/style.rb', line 9

def stylesheet
  @stylesheet
end

Instance Method Details

#build(target = nil, rotation_orientation = nil, seen = {}) ⇒ Object

orientation is completely ignored on OS X, but the Teacup code was written to support them, and it was easier to ignore the orientation system than refactor it out of the shared code base.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/teacup-osx/style.rb', line 13

def build(target_class=nil, rotation_orientation=nil, seen={})
  properties = Style.new
  properties.stylename = self.stylename
  properties.stylesheet = self.stylesheet

  # if we have an orientation, only apply those styles.  otherwise apply the
  # entire style, including the current orientation.
  if rotation_orientation
    # in order to preserve the "local-first" override, we need to *delete*
    # the keys in imported_stylesheets and extended_properties that are
    # present in this style - even though we don't ultimately *apply* the
    # styles
    delete_keys = self.keys
    orientation = rotation_orientation
  else
    delete_keys = []
    properties.update(self)
    orientation = UIApplication.sharedApplication.statusBarOrientation
  end

  # first, move orientation settings into properties "base" level.
  Overrides[orientation].each do |orientation_key|
    if override = self[orientation_key]
      # override is first, so it takes precedence
      if override.is_a? Hash
        Teacup::merge_defaults override, properties, properties
      end
    end
  end

  # delete all of them from `properties`
  Orientations.each do |orientation_key|
    if self[orientation_key]
      properties.supports[orientation_key] = true
    end
    properties.delete(orientation_key)
  end

  # now we can merge extends, and imported_stylesheets.  before merging,
  # these will go through the same process that we just finished on the
  # local style
  if stylesheet && stylesheet.is_a?(Teacup::Stylesheet)
    stylesheet.imported_stylesheets.reverse.each do |stylesheet|
      imported_properties = stylesheet.query(self.stylename, target_class, rotation_orientation, seen)
      delete_keys.each do |key|
        imported_properties.delete(key)
      end
      Teacup::merge_defaults! properties, imported_properties
    end

    if also_includes = self[:extends]
      also_includes = [also_includes] unless also_includes.is_a? Array

      # turn style names into Hashes by querying them on the stylesheet
      # (this does not pass `seen`, because this is a new query)
      also_includes.each do |also_include|
        extended_properties = stylesheet.query(also_include, target_class, rotation_orientation)
        delete_keys.each do |key|
          extended_properties.delete(key)
        end
        Teacup::merge_defaults! properties, extended_properties
      end
    end
    properties.delete(:extends)

    # if we know the class of the target, we can apply styles via class
    # inheritance.
    if target_class
      target_class.class.ancestors.each do |ancestor|
        extended_properties = stylesheet.query(ancestor, nil, rotation_orientation)
        Teacup::merge_defaults!(properties, extended_properties)
      end
    end
  end

  return properties
end

#supportsObject

A hash of orientation => true/false. true means the orientation is supported.



22
23
24
# File 'lib/teacup-ios/style.rb', line 22

def supports
  @supports ||= {}
end

#supports?(orientation_key) ⇒ Boolean

returns the value - ‘nil` has special meaning when querying :portrait or :upside_up

Returns:

  • (Boolean)


27
28
29
# File 'lib/teacup-ios/style.rb', line 27

def supports? orientation_key
  supports[orientation_key]
end