Skip to content

Commit

Permalink
Merge pull request #49 from davorb/master
Browse files Browse the repository at this point in the history
Added support for background color in app.rb and solved #41
  • Loading branch information
ashbb committed Jun 11, 2012
2 parents eb24e40 + 62008eb commit ad1e1fd
Show file tree
Hide file tree
Showing 10 changed files with 3,149 additions and 2,837 deletions.
3 changes: 0 additions & 3 deletions Rakefile
Expand Up @@ -50,9 +50,6 @@ task :spec, [:module] => "spec:all" do
end

namespace :spec do



desc "Run All Specs / All Modules"
task :default => ["spec:all"]

Expand Down
15 changes: 14 additions & 1 deletion lib/shoes/app.rb
Expand Up @@ -19,6 +19,7 @@ class App
attr_accessor :opts, :blk

attr_accessor :width, :height, :title, :resizable
attr_writer :background, :width, :height

def initialize(opts={}, &blk)
opts = default_options.merge(opts)
Expand All @@ -27,6 +28,7 @@ def initialize(opts={}, &blk)
self.height = opts[:height]
self.title = opts[:title]
self.resizable = opts[:resizable]
self.background = opts[:background]
self.opts = opts

@app = self
Expand All @@ -44,7 +46,8 @@ def default_options
:width => 600,
:height => 500,
:title => "Shoooes!",
:resizable => true
:resizable => true,
:background => white
}
end

Expand All @@ -54,5 +57,15 @@ def default_styles
:strokewidth => 1
}
end

# If background is called without any options this
# will simply return it's value. Otherwise it will
# interpret the call as the user wanting to set the
# background, in which case it will call gui_background
def background(*opts)
return @background if opts.empty?
@gui.gui_background opts
end

end
end
20 changes: 18 additions & 2 deletions lib/shoes/swt/app.rb
@@ -1,6 +1,5 @@
require 'swt'

#require 'shoes/framework_adapters/swt_shoes/window'
require 'shoes/swt/background_painter'

module Shoes
module Swt
Expand All @@ -10,6 +9,8 @@ module Swt
#
class App

attr_reader :background

def initialize app
@app = app
@app.gui_container = container = ::Swt::Widgets::Shell.new(::Swt.display, main_window_style)
Expand All @@ -18,6 +19,7 @@ def initialize app

container.setSize(app.width, app.height)
container.setText(app.title)
gui_background [@app.background]

container.addListener(::Swt::SWT::Close, main_window_on_close)
end
Expand All @@ -31,6 +33,20 @@ def open
Shoes.logger.debug "::Swt.display disposed... exiting Shoes::App.new"
end

# gui_background will set the background to the
# value passed to it through opts.
# It will accept either a Shoes::Color object or
# a Shoes::Color object along with some additional
# options.
def gui_background(opts)
if opts.size == 1
@app.gui_container.setBackground(opts[0].to_native)
@app.background = opts[0]
else
@app.gui_container.addPaintListener(BackgroundPainter.new(opts, @app))
end
end

private
def main_window_on_close
lambda { |event|
Expand Down
83 changes: 83 additions & 0 deletions lib/shoes/swt/background_painter.rb
@@ -0,0 +1,83 @@
module Shoes
module Swt
# BackgroundPainter paints and repaints Shoes.app's background(s).
# It is only used if the user supplies any parameters other than the color,
# such as , :right, :left, :width or :height.
class BackgroundPainter
include org.eclipse.swt.events.PaintListener
attr_accessor :options, :color

def initialize(*opts, app)
@app = app
self.options = opts[0][1]
self.color = opts[0][0]
self.color = options[:fill] if options.has_key? :fill
end

def paintControl(paintEvent)
coords = calculate_coords paintEvent
paintEvent.gc.setBackground(color.to_native)
paintEvent.gc.fillRectangle(coords[:x], coords[:y], coords[:width], coords[:height])
end

def calculate_coords(paintEvent)
coords = Hash.new

coords[:width] = set_width(paintEvent)
coords[:height] = set_height(paintEvent)
coords[:x] = set_x(paintEvent, coords[:width])
coords[:y] = set_y(paintEvent, coords[:height])

coords
end

def set_width(paintEvent)
if options.has_key? :width
options[:width]
elsif options.has_key? :radius
2*options[:radius]
elsif options.has_key?(:left) && options.has_key?(:right)
paintEvent.width - (options[:right] + options[:left])
else
paintEvent.width
end
end

def set_height(paintEvent)
if options.has_key? :height
height = options[:height]
elsif options.has_key? :radius
2*options[:radius]
elsif options.has_key?(:top) && options.has_key?(:bottom)
paintEvent.height - (options[:top] + options[:bottom])
else
paintEvent.height
end
end

def set_x(paintEvent, width)
if options.has_key? :left
options[:left]
# if width is != paintEvent.width then it was altered in some way and we
# have to adjust. Otherwise, 0 is a valid answer.
elsif options.has_key?(:right) && (width != paintEvent.width)
paintEvent.width - (width + options[:right])
else
0
end
end

def set_y(paintEvent, height)
if options.has_key? :top
options[:top]
# height is != the paintEvent.height then it was altered and we need to adjust
elsif options.has_key?(:bottom) && (height != paintEvent.height)
paintEvent.height - (height + options[:bottom])
else
0
end
end

end
end
end
17 changes: 17 additions & 0 deletions spec/shoes/app_spec.rb
Expand Up @@ -95,4 +95,21 @@
app2.line(0, 100, 100, 0).style[:strokewidth].should_not == 10
end
end

describe "background" do
subject { Shoes::App.new }
let(:white) { Shoes::COLORS[:white] }
let(:blue) { Shoes::COLORS[:blue] }

it "should set the default background color to white" do
subject.background.should == white
end

it "should allow users to set the background from args" do
input_blk = Proc.new {}
args = { :background => blue }
app = Shoes::App.new args, &input_blk
app.background.should == blue
end
end
end
24 changes: 22 additions & 2 deletions spec/swt_shoes/app_spec.rb
Expand Up @@ -4,25 +4,45 @@

let(:mock_shell) { mock(:swt_shell,
:setSize => true, :setText => true,
:addListener => true, :setLayout => true) }
:addListener => true, :setLayout => true,
:setBackground => true, :open => true) }

before :each do
::Swt::Widgets::Shell.stub(:new) { mock_shell }
::Swt.stub(:event_loop)
end

describe Shoes::App do
subject { Shoes::App.new }
subject { Shoes::App.new }
let(:white) { Shoes::COLORS[:white] }
let(:blue) { Shoes::COLORS[:blue] }

context "shell" do
it "receives open" do
mock_shell.should_receive(:open)
subject
end

it "receives setBackground" do
mock_shell.should_receive :setBackground
subject
end
end

context "Shoes::App background" do
it "allows users to set the background from a block" do
input_blk = Proc.new { background blue }
args = { }
app = Shoes::App.new args, &input_blk
app.background.should == blue
end
end

context "Shoes::App ancestors" do
subject { Shoes::App.ancestors }

it { should include(Shoes::Swt::ElementMethods) }

it "uses Shoes::Swt::ElementMethods before Shoes::ElementMethods" do
framework_index = subject.index(Shoes::Swt::ElementMethods)
shoes_index = subject.index(Shoes::ElementMethods)
Expand Down

0 comments on commit ad1e1fd

Please sign in to comment.