Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

offsets #29

Merged
merged 6 commits into from
Jan 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/GridLayoutBase.jl
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ export Left, Right, Top, Bottom, TopLeft, BottomLeft, TopRight, BottomRight
export grid!, hbox!, vbox!
export swap!
export protrusionsobservable, suggestedbboxobservable, reportedsizeobservable, autosizeobservable, computedbboxobservable, gridcontent
export ncols, nrows
export ncols, nrows, offsets
export contents, content
export tight_bbox

269 changes: 155 additions & 114 deletions src/gridlayout.jl

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions src/helpers.jl
Original file line number Diff line number Diff line change
@@ -165,3 +165,30 @@ function mapsides(
f(side, getindex.((first, rest...), (side,))...)
end
end

function set_nrows!(gl, x)
gl.size = (x, gl.size[2])
end
function set_ncols!(gl, x)
gl.size = (gl.size[1], x)
end

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

offset(gl, ::Row) = offsets(gl)[1]
offset(gl, ::Col) = offsets(gl)[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())

# 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())
6 changes: 3 additions & 3 deletions src/precompile.jl
Original file line number Diff line number Diff line change
@@ -27,9 +27,9 @@ function _precompile_()
@warnpcfail precompile(Core.kwfunc(GridLayout), (NamedTuple{(:bbox, :alignmode), Tuple{Rect2f, Al}}, Type{GridLayout}))
@warnpcfail precompile(Core.kwfunc(GridLayout), (NamedTuple{(:bbox, :alignmode), Tuple{Observable{Rect2f}, Al}}, Type{GridLayout}))
@warnpcfail precompile(Core.kwfunc(GridLayout), (NamedTuple{(:bbox, :alignmode), Tuple{NTuple{4,Int}, Al}}, Type{GridLayout}))
if fbody !== nothing
@warnpcfail precompile(fbody, (Nothing, Nothing, Nothing, Nothing, Nothing, Al, Tuple{Bool, Bool}, Rect2f, Auto, Auto, Bool, Bool, Symbol, Symbol, Float64, Float64, Emptykwargs, Type{GridLayout}, Int, Int))
end
# if fbody !== nothing
# @warnpcfail precompile(fbody, (Nothing, Nothing, Nothing, Nothing, Nothing, Al, Tuple{Bool, Bool}, Rect2f, Auto, Auto, Bool, Bool, Symbol, Symbol, Float64, Float64, Emptykwargs, Type{GridLayout}, Int, Int))
# end
end
@warnpcfail precompile(Core.kwfunc(GridLayout), (NamedTuple{(:colsizes, :rowsizes), Tuple{Fixed, Relative}}, Type{GridLayout}, Int, Int))
@warnpcfail precompile(Core.kwfunc(GridLayout), (NamedTuple{(:addedcolgaps,), Tuple{Vector{Fixed}}}, Type{GridLayout}, Int, Int))
8 changes: 4 additions & 4 deletions src/types.jl
Original file line number Diff line number Diff line change
@@ -160,8 +160,8 @@ mutable struct GridLayout
parent # this parent is supposed to be any kind of object where it's beneficial
# to access it through the assigned GridLayout, like a Figure in Makie
content::Vector{GridContent}
nrows::Int
ncols::Int
size::Tuple{Int, Int}
offsets::Tuple{Int, Int}
rowsizes::Vector{ContentSize}
colsizes::Vector{ContentSize}
addedrowgaps::Vector{GapSize}
@@ -183,11 +183,11 @@ mutable struct GridLayout

function GridLayout(
parent,
content, nrows, ncols, rowsizes, colsizes,
content, size, offsets, rowsizes, colsizes,
addedrowgaps, addedcolgaps, alignmode, equalprotrusiongaps, needs_update,
layoutobservables, width, height, tellwidth, tellheight, halign, valign, default_rowgap, default_colgap)

gl = new(parent, content, nrows, ncols, rowsizes, colsizes,
gl = new(parent, content, size, offsets, rowsizes, colsizes,
addedrowgaps, addedcolgaps, alignmode, equalprotrusiongaps,
needs_update, false, layoutobservables, width, height, tellwidth, tellheight,
halign, valign, default_rowgap, default_colgap, nothing)
72 changes: 65 additions & 7 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using GridLayoutBase
using GridLayoutBase: GridSubposition
using GridLayoutBase: offsets
using Test
using Observables

include("debugrect.jl")


# @testset "GridLayout Zero Outside AlignMode" begin
begin
@testset "GridLayout Zero Outside AlignMode" begin
bbox = BBox(0, 1000, 0, 1000)
layout = GridLayout(bbox = bbox, alignmode = Outside(0))
dr = layout[1, 1] = DebugRect()
@@ -24,7 +24,7 @@ begin
@test computedbboxobservable(dr)[] == BBox(100, 900, 100, 900)

dr2 = layout[1, 2] = DebugRect()
@test layout.nrows == 1 && layout.ncols == 2
@test nrows(layout) == 1 && ncols(layout) == 2
colgap!(layout, 1, Fixed(0))

@test computedbboxobservable(dr)[].widths == computedbboxobservable(dr2)[].widths == Float32[400.0, 800.0]
@@ -175,28 +175,38 @@ end

dr = layout[1, 1] = DebugRect()
@test size(layout) == (1, 1)
@test offsets(layout) == (0, 0)

layout[1, 2] = dr
@test size(layout) == (1, 2)
@test offsets(layout) == (0, 0)

layout[3, 2] = dr
@test size(layout) == (3, 2)
@test offsets(layout) == (0, 0)

layout[4, 4] = dr
@test size(layout) == (4, 4)
@test offsets(layout) == (0, 0)

layout[0, 1] = dr
@test size(layout) == (5, 4)
@test offsets(layout) == (-1, 0)

layout[1, 0] = dr
@test size(layout) == (5, 5)
@test offsets(layout) == (-1, -1)

layout[-1, -1] = dr
@test size(layout) == (7, 7)
@test size(layout) == (6, 6)
@test offsets(layout) == (-2, -2)

layout[3, 3] = dr
trim!(layout)
@test size(layout) == (1, 1)
@test offsets(layout) == (-2, -2)
# reset offsets to zero
layout.offsets = (0, 0)

layout[2:3, 4:5] = dr
@test size(layout) == (3, 5)
@@ -361,7 +371,7 @@ end

text_long = repr(MIME"text/plain"(), gl)
@test text_long == """
GridLayout[3, 5] with 2 children
GridLayout[1:3, 1:5] with 2 children
┣━ [1, 1] DebugRect
┗━ [2:3, 4:5] DebugRect
"""
@@ -373,7 +383,7 @@ end

text_longer = repr(MIME"text/plain"(), gl)
# this is actually a bit buggy with the newline space space newline at the end
@test text_longer == "GridLayout[3, 5] with 3 children\n ┣━ [1, 1] DebugRect\n ┣━ [2:3, 4:5] DebugRect\n ┗━ [1, 2] GridLayout[5, 3] with 1 children\n ┗━ [1:5, 3] DebugRect\n \n"
@test text_longer == "GridLayout[1:3, 1:5] with 3 children\n ┣━ [1, 1] DebugRect\n ┣━ [2:3, 4:5] DebugRect\n ┗━ [1, 2] GridLayout[1:5, 1:3] with 1 children\n ┗━ [1:5, 3] DebugRect\n \n"


gl3 = GridLayout()
@@ -383,7 +393,7 @@ end
text_long_downconnection = repr(MIME"text/plain"(), gl3)

# this is also a bit buggy for the same reason as above
@test text_long_downconnection == "GridLayout[2, 2] with 2 children\n ┣━ [1, 1] GridLayout[1, 1] with 1 children\n ┃ ┗━ [1, 1] DebugRect\n\n ┗━ [2, 2] DebugRect\n"
@test text_long_downconnection == "GridLayout[1:2, 1:2] with 2 children\n ┣━ [1, 1] GridLayout[1:1, 1:1] with 1 children\n ┃ ┗━ [1, 1] DebugRect\n\n ┗━ [2, 2] DebugRect\n"
end

@testset "vector and array assigning" begin
@@ -824,3 +834,51 @@ end
@test_throws ErrorException tight_bbox(gl)
end

@testset "offset row/col auto size" begin
bbox = BBox(0, 1000, 0, 1000)
gl = GridLayout(bbox = bbox, alignmode = Outside(0), halign = :center)
dr1 = gl[0, 1] = DebugRect(height = 200)
dr2 = gl[1, 0] = DebugRect(width = 100)
dr3 = gl[1, 1] = DebugRect()
colgap!(gl, 0)
rowgap!(gl, 0)

@test GridLayoutBase.determinedirsize(0, gl, GridLayoutBase.Col()) == 100
@test GridLayoutBase.determinedirsize(0, gl, GridLayoutBase.Row()) == 200

@test suggestedbboxobservable(dr1)[] == BBox(100, 1000, 800, 1000)
@test suggestedbboxobservable(dr2)[] == BBox(0, 100, 0, 800)
@test suggestedbboxobservable(dr3)[] == BBox(100, 1000, 0, 800)
end

@testset "offset aspect" begin
bbox = BBox(0, 1000, 0, 1000)
gl = GridLayout(bbox = bbox, alignmode = Outside(0), halign = :center)
# make columns and rows offset
gl[0, 0] = DebugRect()
colgap!(gl, 0)
rowgap!(gl, 0)
# aspect refers to row 0
colsize!(gl, 1, Aspect(0, 1.5))

maxgrid, gridboxes = GridLayoutBase.compute_rowcols(gl, suggestedbboxobservable(gl)[])
@test gridboxes.lefts == [0, 250]
@test gridboxes.rights == [250, 1000]
@test gridboxes.tops == [1000, 500]
@test gridboxes.bottoms == [500, 0]

bbox = BBox(0, 1000, 0, 1000)
gl = GridLayout(bbox = bbox, alignmode = Outside(0), halign = :center)
# make columns and rows offset
gl[0, 0] = DebugRect()
colgap!(gl, 0)
rowgap!(gl, 0)
# aspect refers to column 0
rowsize!(gl, 1, Aspect(0, 1.5))

maxgrid, gridboxes = GridLayoutBase.compute_rowcols(gl, suggestedbboxobservable(gl)[])
@test gridboxes.lefts == [0, 500]
@test gridboxes.rights == [500, 1000]
@test gridboxes.tops == [1000, 750]
@test gridboxes.bottoms == [750, 0]
end