From 860b98eb5d929529edc74c37640ff2bf54829a26 Mon Sep 17 00:00:00 2001 From: Cecil Date: Sun, 25 Nov 2018 22:00:46 -0700 Subject: [PATCH] for #410 - move grid.rb to lib/layout/ --- Tests/layout/grid.rb | 63 +------------------------------------------- lib/layout/grid.rb | 63 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 62 deletions(-) create mode 100644 lib/layout/grid.rb diff --git a/Tests/layout/grid.rb b/Tests/layout/grid.rb index 93585464..3c29bdc2 100644 --- a/Tests/layout/grid.rb +++ b/Tests/layout/grid.rb @@ -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 diff --git a/lib/layout/grid.rb b/lib/layout/grid.rb new file mode 100644 index 00000000..d7147812 --- /dev/null +++ b/lib/layout/grid.rb @@ -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