Skip to content

Commit

Permalink
Add method for defining default style for a class
Browse files Browse the repository at this point in the history
See samples/sample56.rb for a test
  • Loading branch information
wasnotrice committed Oct 23, 2013
1 parent 78164a4 commit 7746754
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 10 deletions.
1 change: 1 addition & 0 deletions lib/shoes/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def create_execution_block(blk)
def set_initial_attributes
@app = self
@style = default_styles
@element_styles = {}
@contents = []
@unslotted_elements = []
@mouse_motion = []
Expand Down
39 changes: 37 additions & 2 deletions lib/shoes/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ class Shoes
#
# Including classes must provide:
#
# @style - a hash of styles
# @style: a hash of styles
# @element_styles: a hash of {Class => styles}, where styles is
# a hash of default styles for elements of Class,
module DSL
include Shoes::Common::Style

Expand All @@ -31,6 +33,33 @@ def pattern(*args)
end
end

# Set default style for elements of a particular class, or for all
# elements, or return the current defaults for all elements
#
# @overload style(klass, styles)
# Set default style for elements of a particular class
# @param [Class] klass a Shoes element class
# @param [Hash] styles default styles for elements of klass
# @example
# style Para, :text_size => 42, :stroke => green
#
# @overload style(styles)
# Set default style for all elements
# @param [Hash] styles default style for all elements
# @example
# style :stroke => alicewhite, :fill => black
#
# @overload style()
# @return [Hash] the default style for all elements
def style(klass_or_styles = nil, styles = {})
if klass_or_styles.kind_of? Class
klass = klass_or_styles
@element_styles[klass] = styles
else
super(klass_or_styles)
end
end

private

def pop_style(opts)
Expand All @@ -45,6 +74,11 @@ def normalize_style(orig_style)
orig_style.merge(normalized_style)
end

# Default styles for elements of klass
def style_for_element(klass, styles = {})
@element_styles.fetch(klass, {}).merge(styles)
end

def create(element, *args, &blk)
element.new(@app, current_slot, *args, &blk)
end
Expand Down Expand Up @@ -406,7 +440,8 @@ def cap line_cap
opts = text.last.class == Hash ? text.pop : {}
opts[:text_styles] = gather_text_styles text
text = text.map(&:to_s).join
create Shoes.const_get(m.capitalize), text, FONT_SIZES[m.to_sym], opts
klass = Shoes.const_get(m.capitalize)
create klass, text, FONT_SIZES[m.to_sym], style_for_element(klass, opts)
end
end

Expand Down
17 changes: 9 additions & 8 deletions lib/shoes/slot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ def initialize(app, parent, opts={}, &blk)
end

def init_attributes(app, parent, opts, blk)
@app = app
@parent = parent
@contents = SlotContents.new
@style = {}
@blk = blk
@dimensions = Dimensions.new parent, opts
@fixed_height = height || false
@prepending = false
@app = app
@parent = parent
@contents = SlotContents.new
@style = {}
@element_styles = {}
@blk = blk
@dimensions = Dimensions.new parent, opts
@fixed_height = height || false
@prepending = false
set_default_dimension_values

init_values_from_options(opts)
Expand Down
10 changes: 10 additions & 0 deletions samples/sample56.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# sample56.rb
Para = Shoes::Para
Shoes.app :width => 200, :height => 160 do
style Para, :align => 'center', :stroke => pink, :size => 30
stack do
para "hello"
para "hello", :size => 10, :stroke => red
para "hello"
end
end
1 change: 1 addition & 0 deletions spec/shoes/shared_examples/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
shape
stroke
strokewidth
style
].each do |method|
include_examples "#{method} DSL method"
end
Expand Down
32 changes: 32 additions & 0 deletions spec/shoes/shared_examples/dsl/style.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
shared_examples_for "style DSL method" do
describe "setting new defaults for text block" do
let(:stroke) { Shoes::COLORS[:chartreuse] }
let(:size) { 42 }
let(:fill) { Shoes::COLORS[:peru] }
let(:font ) { "SOME FONT" }
let(:style) { {:stroke => stroke, :size => size, :fill => fill, :font => font} }

%w(Banner Title Subtitle Tagline Caption Para Inscription).each do |text_block|
describe text_block do
let(:element) { dsl.public_send text_block.downcase, "Hello!" }
let(:klass) { Shoes.const_get(text_block) }

before :each do
dsl.style klass, style
end

it "creates element with appropriate class" do
element.class.should eq(klass)
end

it "sets size" do
element.font_size.should eq(size)
end

it "sets font" do
element.font.should eq(font)
end
end
end
end
end

0 comments on commit 7746754

Please sign in to comment.