Skip to content

Commit

Permalink
for #410 - move grid.rb to lib/layout/
Browse files Browse the repository at this point in the history
  • Loading branch information
Cecil committed Nov 26, 2018
1 parent 943ba9d commit 860b98e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 62 deletions.
63 changes: 1 addition & 62 deletions Tests/layout/grid.rb
Original file line number Diff line number Diff line change
@@ -1,66 +1,5 @@
# Tests/layout/l4.rb - grid layout

class GridLayout

attr_accessor :ncol, :nrow, :colsz, :rowsz, :widgets, :hpad, :vpad

def initialize(attr = {})
@ncol = 0
@nrow = 0
@colsz = 0
@rowsz = 0
@widgets = []
@hpad = attr[:hpad] ? attr[:hpad] : 1
@vpad = attr[:vpad] ? attr[:vpad] : 1
end

def setup(canvas, attr)
end

def add(canvas, ele, attr)
col = attr[:col] ? attr[:col]-1 : 0
row = attr[:row] ? attr[:row]-1 : 0
rspan = attr[:rspan]
cspan = attr[:cspan]
@ncol = [@ncol, col + (cspan ? cspan : 1)].max
@nrow = [@nrow, row + (rspan ? rspan : 1)].max
widgets << {ele: ele, col: col, row: row, cspan: cspan, rspan: rspan}
end

def remove
end

def clear
end

def size (canvas, pass)
return if pass == 0
@rowsz = (canvas.height / @nrow).to_i
@colsz = (canvas.width / @ncol).to_i
@widgets.each do |entry|
x = entry[:col] * @colsz + @hpad
y = entry[:row] * @rowsz + @vpad
widget = entry[:ele]
widget.move(x, y)
if entry[:cspan]
w = entry[:cspan] * @colsz - @hpad
if widget.width != w
widget.style width: w
end
end
if entry[:rspan]
h = entry[:rspan] * @rowsz - @vpad
if widget.height != h
widget.style height: h
end
end
end
end

def finish
end

end
require 'layout/grid'

Shoes.app width: 400, height: 400 do
stack do
Expand Down
63 changes: 63 additions & 0 deletions lib/layout/grid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# grid layout

class GridLayout

attr_accessor :ncol, :nrow, :colsz, :rowsz, :widgets, :hpad, :vpad

def initialize(attr = {})
@ncol = 0
@nrow = 0
@colsz = 0
@rowsz = 0
@widgets = []
@hpad = attr[:hpad] ? attr[:hpad] : 1
@vpad = attr[:vpad] ? attr[:vpad] : 1
end

def setup(canvas, attr)
end

def add(canvas, ele, attr)
col = attr[:col] ? attr[:col]-1 : 0
row = attr[:row] ? attr[:row]-1 : 0
rspan = attr[:rspan]
cspan = attr[:cspan]
@ncol = [@ncol, col + (cspan ? cspan : 1)].max
@nrow = [@nrow, row + (rspan ? rspan : 1)].max
widgets << {ele: ele, col: col, row: row, cspan: cspan, rspan: rspan}
end

def remove
end

def clear
end

def size (canvas, pass)
return if pass == 0
@rowsz = (canvas.height / @nrow).to_i
@colsz = (canvas.width / @ncol).to_i
@widgets.each do |entry|
x = entry[:col] * @colsz + @hpad
y = entry[:row] * @rowsz + @vpad
widget = entry[:ele]
widget.move(x, y)
if entry[:cspan]
w = entry[:cspan] * @colsz - @hpad
if widget.width != w
widget.style width: w
end
end
if entry[:rspan]
h = entry[:rspan] * @rowsz - @vpad
if widget.height != h
widget.style height: h
end
end
end
end

def finish
end

end

0 comments on commit 860b98e

Please sign in to comment.