Skip to content

Commit

Permalink
make GridDir an enum
Browse files Browse the repository at this point in the history
  • Loading branch information
jkrumbiegel committed Jan 17, 2022
1 parent 270e5cd commit 412e16d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 78 deletions.
101 changes: 47 additions & 54 deletions src/gridlayout.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
GridLayout(; kwargs...) = GridLayout(1, 1; kwargs...)
GridLayout(; @nospecialize(kwargs...)) = GridLayout(1, 1; kwargs...)

observablify(x::Observable) = x
observablify(x, type=Any) = Observable{type}(x)
Expand Down Expand Up @@ -72,7 +72,7 @@ function GridLayout(nrows::Int, ncols::Int;

content = GridContent[]

alignmode = observablify(alignmode, AlignMode)
alignmode_obs = convert(Observable{AlignMode}, alignmode)
width_obs = convert(Observable{SizeAttribute}, width)
height_obs = convert(Observable{SizeAttribute}, height)
tellwidth_obs = convert(Observable{Bool}, tellwidth)
Expand Down Expand Up @@ -101,7 +101,7 @@ function GridLayout(nrows::Int, ncols::Int;
colsizes,
addedrowgaps,
addedcolgaps,
alignmode,
alignmode_obs,
equalprotrusiongaps,
layoutobservables,
width_obs,
Expand All @@ -124,8 +124,8 @@ end
function update!(gl::GridLayout)
gl.block_updates && return

w = determinedirsize(gl, Col())
h = determinedirsize(gl, Row())
w = determinedirsize(gl, Col)
h = determinedirsize(gl, Row)

new_autosize = (w, h)
new_protrusions = RectSides{Float32}(
Expand Down Expand Up @@ -311,7 +311,7 @@ function prependrows!(gl::GridLayout, n::Int; rowsizes=nothing, addedrowgaps=not

with_updates_suspended(gl, update = update) do
set_nrows!(gl, nrows(gl) + n)
set_rowoffset!(gl, offset(gl, Row()) - n)
set_rowoffset!(gl, offset(gl, Row) - n)
prepend!(gl.rowsizes, rowsizes)
prepend!(gl.addedrowgaps, addedrowgaps)
end
Expand All @@ -324,7 +324,7 @@ function prependcols!(gl::GridLayout, n::Int; colsizes=nothing, addedcolgaps=not

with_updates_suspended(gl, update = update) do
set_ncols!(gl, ncols(gl) + n)
set_coloffset!(gl, offset(gl, Col()) - n)
set_coloffset!(gl, offset(gl, Col) - n)
prepend!(gl.colsizes, colsizes)
prepend!(gl.addedcolgaps, addedcolgaps)
end
Expand Down Expand Up @@ -451,8 +451,8 @@ function deleterow!(gl::GridLayout, irow::Int)
update!(gl)
end

rowoffset(gl) = offset(gl, Row())
coloffset(gl) = offset(gl, Col())
rowoffset(gl) = offset(gl, Row)
coloffset(gl) = offset(gl, Col)

function deletecol!(gl::GridLayout, icol::Int)
if !(firstcol(gl) <= icol <= lastcol(gl))
Expand Down Expand Up @@ -497,15 +497,15 @@ end

function Base.isempty(gl::GridLayout, dir::GridDir, i::Int)
!any(gl.content) do c
span = dir isa Row ? c.span.rows : c.span.cols
span = dir == Row ? c.span.rows : c.span.cols
i in span
end
end

function trim!(gl::GridLayout)
irow = firstrow(gl)
while irow <= lastrow(gl) && nrows(gl) > 1
if isempty(gl, Row(), irow)
if isempty(gl, Row, irow)
deleterow!(gl, irow)
else
irow += 1
Expand All @@ -514,7 +514,7 @@ function trim!(gl::GridLayout)

icol = firstcol(gl)
while icol <= lastcol(gl) && ncols(gl) > 1
if isempty(gl, Col(), icol)
if isempty(gl, Col, icol)
deletecol!(gl, icol)
else
icol += 1
Expand Down Expand Up @@ -603,7 +603,7 @@ function colsize!(gl::GridLayout, i::Int, s::ContentSize)
if !(firstcol(gl) <= i <= lastcol(gl))
error("Can't set size of invalid column $i.")
end
i_unoffset = unoffset(gl, i, Col())
i_unoffset = unoffset(gl, i, Col)
gl.colsizes[i_unoffset] = s
update!(gl)
end
Expand All @@ -614,7 +614,7 @@ function rowsize!(gl::GridLayout, i::Int, s::ContentSize)
if !(firstrow(gl) <= i <= lastrow(gl))
error("Can't set size of invalid row $i.")
end
i_unoffset = unoffset(gl, i, Row())
i_unoffset = unoffset(gl, i, Row)
gl.rowsizes[i_unoffset] = s
update!(gl)
end
Expand Down Expand Up @@ -696,8 +696,8 @@ function _compute_maxgrid(gl)
maxgrid
end

sideoffset(gl, ::Union{Right, Left}) = offset(gl, Col())
sideoffset(gl, ::Union{Top, Bottom}) = offset(gl, Row())
sideoffset(gl, ::Union{Right, Left}) = offset(gl, Col)
sideoffset(gl, ::Union{Top, Bottom}) = offset(gl, Row)

function _compute_remaining_horizontal_space(content_bbox, sumcolgaps, leftprot, rightprot, alignmode::Inside)::Float32
width(content_bbox) - sumcolgaps
Expand Down Expand Up @@ -977,8 +977,7 @@ function align_to_bbox!(gl::GridLayout, suggestedbbox::Rect2f)
nothing
end

dirlength(gl::GridLayout, c::Col) = ncols(gl)
dirlength(gl::GridLayout, r::Row) = nrows(gl)
dirlength(gl::GridLayout, g::GridDir) = g == Row ? nrows(gl) : ncols(gl)

function dirgaps(gl::GridLayout, dir::GridDir)
starts = zeros(Float32, dirlength(gl, dir))
Expand All @@ -993,8 +992,7 @@ function dirgaps(gl::GridLayout, dir::GridDir)
starts, stops
end

dirsizes(gl::GridLayout, c::Col) = gl.colsizes
dirsizes(gl::GridLayout, r::Row) = gl.rowsizes
dirsizes(gl::GridLayout, g::GridDir) = g == Row ? gl.rowsizes : gl.colsizes

"""
Determine the size of a grid layout along one of its dimensions.
Expand Down Expand Up @@ -1022,7 +1020,7 @@ function determinedirsize(gl::GridLayout, gdir::GridDir)

dirgapsstart, dirgapsstop = dirgaps(gl, gdir)

forceequalprotrusiongaps = gl.equalprotrusiongaps[gdir isa Row ? 1 : 2]
forceequalprotrusiongaps = gl.equalprotrusiongaps[gdir == Row ? 1 : 2]

dirgapsizes = if forceequalprotrusiongaps
innergaps = dirgapsstart[2:end] .+ dirgapsstop[1:end-1]
Expand All @@ -1034,7 +1032,7 @@ function determinedirsize(gl::GridLayout, gdir::GridDir)

inner_gapsizes = dirlength(gl, gdir) > 1 ? sum(dirgapsizes) : 0

addeddirgapsizes = gdir isa Row ? gl.addedrowgaps : gl.addedcolgaps
addeddirgapsizes = gdir == Row ? gl.addedrowgaps : gl.addedcolgaps

addeddirgaps = dirlength(gl, gdir) == 1 ? 0 : sum(addeddirgapsizes) do c
if c isa Fixed
Expand All @@ -1048,7 +1046,7 @@ function determinedirsize(gl::GridLayout, gdir::GridDir)
return if gl.alignmode[] isa Inside
inner_size_combined
elseif gl.alignmode[] isa Outside
paddings = if gdir isa Row
paddings = if gdir == Row
gl.alignmode[].padding.top + gl.alignmode[].padding.bottom
else
gl.alignmode[].padding.left + gl.alignmode[].padding.right
Expand Down Expand Up @@ -1149,16 +1147,16 @@ function compute_col_row_sizes(spaceforcolumns, spaceforrows, gl)::Tuple{Vector{

# then determinable auto sizes
filterenum(Auto, gl.colsizes) do (i, auto)
i_offset = offset(gl, i, Col())
size = determinedirsize(i_offset, gl, Col())
i_offset = offset(gl, i, Col)
size = determinedirsize(i_offset, gl, Col)
if !isnothing(size)
colwidths[i] = size
determinedcols[i] = true
end
end
filterenum(Auto, gl.rowsizes) do (i, auto)
i_offset = offset(gl, i, Row())
size = determinedirsize(i_offset, gl, Row())
i_offset = offset(gl, i, Row)
size = determinedirsize(i_offset, gl, Row)
if !isnothing(size)
rowheights[i] = size
determinedrows[i] = true
Expand All @@ -1167,14 +1165,14 @@ function compute_col_row_sizes(spaceforcolumns, spaceforrows, gl)::Tuple{Vector{

# now aspect sizes that refer to already determined counterparts
filterenum(Aspect, gl.colsizes) do (i, aspect)
aspectindex_unoffset = unoffset(gl, aspect.index, Col())
aspectindex_unoffset = unoffset(gl, aspect.index, Col)
if determinedrows[aspectindex_unoffset]
colwidths[i] = aspect.ratio * rowheights[aspectindex_unoffset]
determinedcols[i] = true
end
end
filterenum(Aspect, gl.rowsizes) do (i, aspect)
aspectindex_unoffset = unoffset(gl, aspect.index, Row())
aspectindex_unoffset = unoffset(gl, aspect.index, Row)
if determinedcols[aspectindex_unoffset]
rowheights[i] = aspect.ratio * colwidths[aspectindex_unoffset]
determinedrows[i] = true
Expand Down Expand Up @@ -1239,7 +1237,7 @@ function compute_col_row_sizes(spaceforcolumns, spaceforrows, gl)::Tuple{Vector{
# now if either columns or rows had no aspects left, they should have all sizes determined
# we run over the aspects again
filterenum(Aspect, gl.colsizes) do (i, aspect)
aspectindex_unoffset = unoffset(gl, aspect.index, Col())
aspectindex_unoffset = unoffset(gl, aspect.index, Col)
if determinedrows[aspectindex_unoffset]
colwidths[i] = aspect.ratio * rowheights[aspectindex_unoffset]
determinedcols[i] = true
Expand All @@ -1248,7 +1246,7 @@ function compute_col_row_sizes(spaceforcolumns, spaceforrows, gl)::Tuple{Vector{
end
end
filterenum(Aspect, gl.rowsizes) do (i, aspect)
aspectindex_unoffset = unoffset(gl, aspect.index, Row())
aspectindex_unoffset = unoffset(gl, aspect.index, Row)
if determinedcols[aspectindex_unoffset]
rowheights[i] = aspect.ratio * colwidths[aspectindex_unoffset]
determinedrows[i] = true
Expand Down Expand Up @@ -1593,10 +1591,10 @@ Indices of the rows / cols for each side
"""
function side_indices(gl, c::GridContent)
return RowCols(
c.span.cols.start - offset(gl, Col()),
c.span.cols.stop - offset(gl, Col()),
c.span.rows.start - offset(gl, Row()),
c.span.rows.stop - offset(gl, Row()),
c.span.cols.start - offset(gl, Col),
c.span.cols.stop - offset(gl, Col),
c.span.rows.start - offset(gl, Row),
c.span.rows.stop - offset(gl, Row),
)
end

Expand Down Expand Up @@ -1631,45 +1629,45 @@ function protrusion(gc::GridContent, side::Side)
# elseif gc.side isa Outer; BBox(l - pl, r + pr, b - pb, t + pt)
elseif gc.side isa Union{Left, Right}
if side isa typeof(gc.side)
determinedirsize(gc.content, Col(), gc.side)
determinedirsize(gc.content, Col, gc.side)
else
0.0
end
elseif gc.side isa Union{Top, Bottom}
if side isa typeof(gc.side)
determinedirsize(gc.content, Row(), gc.side)
determinedirsize(gc.content, Row, gc.side)
else
0.0
end
elseif gc.side isa TopLeft
if side isa Top
determinedirsize(gc.content, Row(), gc.side)
determinedirsize(gc.content, Row, gc.side)
elseif side isa Left
determinedirsize(gc.content, Col(), gc.side)
determinedirsize(gc.content, Col, gc.side)
else
0.0
end
elseif gc.side isa TopRight
if side isa Top
determinedirsize(gc.content, Row(), gc.side)
determinedirsize(gc.content, Row, gc.side)
elseif side isa Right
determinedirsize(gc.content, Col(), gc.side)
determinedirsize(gc.content, Col, gc.side)
else
0.0
end
elseif gc.side isa BottomLeft
if side isa Bottom
determinedirsize(gc.content, Row(), gc.side)
determinedirsize(gc.content, Row, gc.side)
elseif side isa Left
determinedirsize(gc.content, Col(), gc.side)
determinedirsize(gc.content, Col, gc.side)
else
0.0
end
elseif gc.side isa BottomRight
if side isa Bottom
determinedirsize(gc.content, Row(), gc.side)
determinedirsize(gc.content, Row, gc.side)
elseif side isa Right
determinedirsize(gc.content, Col(), gc.side)
determinedirsize(gc.content, Col, gc.side)
else
0.0
end
Expand Down Expand Up @@ -1753,15 +1751,10 @@ function bbox_for_solving_from_side(maxgrid::RowCols, bbox_cell::Rect2f, idx_rec
end
end

startside(c::Col) = Left()
stopside(c::Col) = Right()
startside(r::Row) = Top()
stopside(r::Row) = Bottom()


getspan(gc::GridContent, dir::Col) = gc.span.cols
getspan(gc::GridContent, dir::Row) = gc.span.rows
startside(g::GridDir) = g == Col ? Left() : Top()
stopside(g::GridDir) = g == Col ? Right() : Bottom()

getspan(gc::GridContent, g::GridDir) = g == Col ? gc.span.cols : gc.span.rows


"""
Expand All @@ -1773,7 +1766,7 @@ an element placed in the left protrusion loses its ability to influence height.
"""
function determinedirsize(content, gdir::GridDir, side::Side)
reportedsize = reportedsizeobservable(content)
if gdir isa Row
if gdir == Row
if side isa Union{Inner, Top, Bottom, TopLeft, TopRight, BottomLeft, BottomRight}
# TODO: is reportedsize the correct thing to return? or plus protrusions depending on the side
ifnothing(reportedsize[][2], nothing)
Expand Down
17 changes: 8 additions & 9 deletions src/helpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,13 @@ width(rect::Rect{2}) = right(rect) - left(rect)
height(rect::Rect{2}) = top(rect) - bottom(rect)


function BBox(left::Number, right::Number, bottom::Number, top::Number)
function BBox(left::Float32, right::Float32, bottom::Float32, top::Float32)
mini = (left, bottom)
maxi = (right, top)
return Rect2f(mini, maxi .- mini)
end
BBox(left::Number, right::Number, bottom::Number, top::Number) =
BBox(Float32(left)::Float32, Float32(right)::Float32, Float32(bottom)::Float32, Float32(top)::Float32)


function RowCols(ncols::Int, nrows::Int)
Expand Down Expand Up @@ -174,21 +176,18 @@ function set_ncols!(gl, x)
end

function set_rowoffset!(gl, x)
gl.offsets = (x, offset(gl, Col()))
gl.offsets = (x, offset(gl, Col))
end
function set_coloffset!(gl, x)
gl.offsets = (offset(gl, Row()), x)
gl.offsets = (offset(gl, Row), x)
end

offset(gl, ::Row) = offsets(gl)[1]
offset(gl, ::Col) = offsets(gl)[2]
offset(gl, g::GridDir) = offsets(gl)[g == Row ? 1 : 2]

# convert an index into an array from 1:nrow or 1:ncol
# into the respective column / row number that can also be negative if offset
offset(gl, i, ::Row) = i + offset(gl, Row())
offset(gl, i, ::Col) = i + offset(gl, Col())
offset(gl, i, g::GridDir) = i + offset(gl, g)

# convert a column / row number that can also be negative if offset
# to an index from 1:nrow or 1:ncol
unoffset(gl, i, ::Row) = i - offset(gl, Row())
unoffset(gl, i, ::Col) = i - offset(gl, Col())
unoffset(gl, i, g::GridDir) = i - offset(gl, g)
4 changes: 1 addition & 3 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ struct BottomRight <: Side end
struct Inner <: Side end
struct Outer <: Side end

abstract type GridDir end
struct Col <: GridDir end
struct Row <: GridDir end
@enum GridDir Row Col

struct RowCols{T <: Union{Number, Vector{Float32}}}
lefts::T
Expand Down
Loading

0 comments on commit 412e16d

Please sign in to comment.