Skip to content

Commit

Permalink
Add Shoes::Swt::Color, refactor fill
Browse files Browse the repository at this point in the history
  • Loading branch information
wasnotrice committed Nov 19, 2012
1 parent 2d0df9c commit e0effc4
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 18 deletions.
7 changes: 4 additions & 3 deletions lib/shoes/swt/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ class App
def initialize dsl
@dsl = dsl
::Swt::Widgets::Display.app_name = @dsl.app_title
@background = Color.new(@dsl.opts[:background])
@real = ::Swt::Widgets::Shell.new(::Swt.display, main_window_style).tap do |shell|
shell.set_image ::Swt::Graphics::Image.new(::Swt.display, SHOES_ICON)
shell.setText(@dsl.app_title)
shell.image = ::Swt::Graphics::Image.new(::Swt.display, SHOES_ICON)
shell.text = (@dsl.app_title)
shell.background_mode = ::Swt::SWT::INHERIT_DEFAULT
shell.setBackground @dsl.opts[:background].to_native
shell.background = @background.real
end
@shell = @real

Expand Down
25 changes: 21 additions & 4 deletions lib/shoes/swt/color.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
module Shoes
module Swt
module Color
def to_native
::Swt::Graphics::Color.new(Shoes.display, @red, @green, @blue)
class Color
# @param [Shoes::Color] color the DSL representation of this color
def initialize(color)
@dsl = color
@real = ::Swt::Graphics::Color.new(Shoes.display, @dsl.red, @dsl.green, @dsl.blue)
@alpha = color.alpha
end

attr_reader :alpha, :dsl, :real

def apply_as_background(gc)
gc.set_background real
gc.set_alpha alpha
end

# TODO: Remove
def red; @real.red; end
def green; @real.green; end
def blue; @real.blue; end
end
end
end

module Shoes
class Color
include Shoes::Swt::Color
def to_native
::Shoes::Swt::Color.new(self)
end
end
end
7 changes: 7 additions & 0 deletions lib/shoes/swt/common/fill.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Common
#
# @note Including classes must provide `#dsl`
module Fill
DEFAULT_COLOR =
# This object's fill color
#
# @return [Swt::Graphics::Color] The Swt representation of this object's fill color
Expand All @@ -19,6 +20,12 @@ def fill_alpha
dsl.fill.alpha if dsl.fill
end

# @param [Swt::Graphics::GC] gc the graphics context in which to apply fill
def apply_fill(gc)
fill.apply_as_background(gc)
#gc.background = self.fill
#gc.alpha = self.fill_alpha
end
end
end
end
Expand Down
12 changes: 5 additions & 7 deletions lib/shoes/swt/common/painter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@ def paint_control(event)
unless @obj.dsl.hidden
gc.set_antialias ::Swt::SWT::ON
gc.set_transform(@obj.transform)
fill gc if fill_setup gc
draw gc if draw_setup gc
fill gc if fill_setup(gc)
draw gc if draw_setup(gc)
end
end

# Override in subclass if not using fill
def fill_setup(gc)
if @obj.fill
gc.set_background @obj.fill
gc.set_alpha @obj.fill_alpha
true
end
@obj.apply_fill(gc)#gc.set_background @obj.fill
#@obj.apply_fill_alpha(gc)#gc.set_alpha @obj.fill_alpha
true
end

# Implement in subclass
Expand Down
4 changes: 2 additions & 2 deletions spec/swt_shoes/color_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'swt_shoes/spec_helper'

describe Shoes::Swt::Color do
subject { Shoes::COLORS[:salmon].to_native }
subject { Shoes::Swt::Color.new(Shoes::COLORS[:salmon]) }

its(:class) { should eq(Java::OrgEclipseSwtGraphics::Color) }
its(:class) { should eq(Shoes::Swt::Color) }
its(:red) { should eq(250) }
its(:green) { should eq(128) }
its(:blue) { should eq(114) }
Expand Down
4 changes: 2 additions & 2 deletions spec/swt_shoes/shared_examples/painter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
shared_context "painter context" do
let(:event) { double("event", :gc => gc) }
let(:gc) { double("gc", :get_line_width => sw).as_null_object }
let(:fill) { Shoes::COLORS[:chartreuse].to_native }
let(:fill) { Shoes::Swt::Color.new(Shoes::Color.new(11, 12, 13, fill_alpha)) }
let(:stroke) { Shoes::COLORS[:papayawhip].to_native }
let(:fill_alpha) { 70 }
let(:stroke_alpha) { 110 }
Expand Down Expand Up @@ -63,7 +63,7 @@
shared_examples_for "fill painter" do
describe "sets fill" do
specify "color" do
gc.should_receive(:set_background).with(fill)
gc.should_receive(:set_background).with(fill.real)
subject.paint_control(event)
end

Expand Down

0 comments on commit e0effc4

Please sign in to comment.