Skip to content
Browse files

Merge pull request #49 from davorb/master

Added support for background color in app.rb and solved #41
  • Loading branch information...
2 parents eb24e40 + 62008eb commit ad1e1fdf0c727892a5bd1285f7c541cf9a0eff13 @ashbb ashbb committed Jun 11, 2012
View
3 Rakefile
@@ -50,9 +50,6 @@ task :spec, [:module] => "spec:all" do
end
namespace :spec do
-
-
-
desc "Run All Specs / All Modules"
task :default => ["spec:all"]
View
15 lib/shoes/app.rb
@@ -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)
@@ -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
@@ -44,7 +46,8 @@ def default_options
:width => 600,
:height => 500,
:title => "Shoooes!",
- :resizable => true
+ :resizable => true,
+ :background => white
}
end
@@ -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
View
20 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
@@ -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)
@@ -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
@@ -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|
View
83 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
View
17 spec/shoes/app_spec.rb
@@ -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
View
24 spec/swt_shoes/app_spec.rb
@@ -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)
View
161 spec/swt_shoes/background_painter_spec.rb
@@ -0,0 +1,161 @@
+require 'swt_shoes/spec_helper'
+APP_WIDTH = 400
+APP_HEIGHT = 200
+
+# Helper method
+def create_painter(options = {})
+ # the "color" and nil parameters should not be accessed
+ Shoes::Swt::BackgroundPainter.new ["color", options] , nil
+end
+
+# For the given options we expect set_width to return expected
+def test_set_width(expected, options)
+ painter = create_painter(options)
+ painter.set_width(paintEvent).should eq expected
+end
+
+# For the given options we expect set_heidht to return expected
+def test_set_height(expected, options)
+ painter = create_painter(options)
+ painter.set_height(paintEvent).should eq expected
+end
+
+def test_set_x(expected, options)
+ painter = create_painter(options)
+ width = painter.set_width(paintEvent)
+ painter.set_x(paintEvent, width).should eq expected
+end
+
+def test_set_y(expected, options)
+ painter = create_painter(options)
+ height = painter.set_height(paintEvent)
+ painter.set_y(paintEvent, height).should eq expected
+end
+
+def test_calculate_coordinates(expected, options)
+ painter = create_painter(options)
+ painter.calculate_coords(paintEvent).should eq expected
+end
+
+describe Shoes::Swt::BackgroundPainter do
+
+ describe "calculating coordines" do
+ let(:paintEvent) do
+ paintEvent = double "paintEvent"
+ paintEvent.stub(height: APP_HEIGHT, width: APP_WIDTH)
+ paintEvent
+ end
+
+ describe "set_width" do
+ it "sets the width to options[:width] if supplied" do
+ test_set_width(50, width: 50)
+ end
+
+ it "sets the width to twice the radius if supplied" do
+ test_set_width(2*25, radius: 25)
+ end
+
+ it "sets the width to the width option even if radius is supplied" do
+ test_set_width(50, width: 50, radius: 70)
+ end
+
+ it "sets the width correctly if right and left are supplied" do
+ # the purpose is that left and right are the offsets from the borders
+ # the method then computes the distance between those 2 offsets
+ test_set_width(APP_WIDTH - (30 + 20), left: 20, right: 30)
+ end
+
+ it "doest not use left and right if width is also supplied" do
+ test_set_width(70, left: 20, right: 30, width: 70)
+ end
+
+ it "sets the width to the app width if no width parameters are supplied" do
+ test_set_width(APP_WIDTH, {})
+ end
+ end
+
+ describe "set_height" do
+ it "sets height to height if supplied" do
+ test_set_height(50, height: 50)
+ end
+
+ it "sets height to twice the radius if supplied" do
+ test_set_height(2 * 40, radius: 40)
+ end
+
+ it "sets height corectly if bottom and top are supplied" do
+ test_set_height(APP_HEIGHT - ( 30 + 50), bottom: 30, top: 50)
+ end
+
+ it "doesn't use bottom and top if heifht is specified" do
+ test_set_height(150, bottom: 30, top: 25, height: 150)
+ end
+
+ it "Sets height to the app height if no options are supplied" do
+ test_set_height(APP_HEIGHT, {})
+ end
+
+ end
+
+ describe "set_x" do
+ it "sets x to :left if supplied" do
+ test_set_x(25, left: 25)
+ end
+
+ it "sets x to 0 when just right is supplied" do
+ test_set_x(0, right: 70)
+ end
+
+ it "sets x accordingly if right and width are supplied" do
+ test_set_x(APP_WIDTH - (50 + 30), width: 50, right: 30)
+ end
+
+ it "sets x to 0 if nothing is supplied" do
+ test_set_x(0, {})
+ end
+ end
+
+ describe "set_y" do
+ it "sets y to :top if supplied" do
+ test_set_y(35, top: 35)
+ end
+
+ it "sets y to 0 when just :bottom is supplied" do
+ test_set_y(0, bottom: 55)
+ end
+
+ it "sets y accordingly if bottom and height are supplied" do
+ test_set_y(APP_HEIGHT - (75 + 100), height: 75, bottom: 100)
+ end
+
+ it "sets y to 0 if nothing is supplied" do
+ test_set_y(0, {})
+ end
+ end
+
+ describe "calculate_coords" do
+ it "sets the values correctly with basic values supplied" do
+ options = {left: 30, width: 70, top: 20, height: 85}
+ test_calculate_coordinates({x: options[:left],
+ width: options[:width],
+ y: options[:top],
+ height: options[:height]},
+ options)
+ end
+
+ it "sets the values correctly with more complex values" do
+ options = {left: 30, right: 45, top: 70, bottom: 15}
+ width = APP_WIDTH - (options[:right] + options[:left])
+ height = APP_HEIGHT - (options[:bottom] + options[:top])
+ test_calculate_coordinates({x: options[:left],
+ width: width,
+ y: options[:top],
+ height: height},
+ options)
+
+ end
+
+ end
+
+ end
+end
View
5,658 static/manual-ja.txt
2,829 additions, 2,829 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
4 testing/background.rb
@@ -0,0 +1,4 @@
+Shoes.app width: 400, height: 400 do
+ options = {left: 30, right: 45, top: 70, bottom: 15}
+ background green, options
+end
View
1 testing/blank-window.rb
@@ -3,3 +3,4 @@
end
end
+

0 comments on commit ad1e1fd

Please sign in to comment.
Something went wrong with that request. Please try again.