From 7a85b9fc836f0f1cdce92b0351a5184c8f3aa970 Mon Sep 17 00:00:00 2001 From: Giada Toccafondi Date: Fri, 8 Mar 2024 10:21:27 +0100 Subject: [PATCH 1/2] add error messages in GridLayout to handle invalid values for heights and widths --- src/layouts.jl | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/layouts.jl b/src/layouts.jl index c671bb8ba..f034b1c8b 100644 --- a/src/layouts.jl +++ b/src/layouts.jl @@ -211,10 +211,28 @@ grid(args...; kw...) = GridLayout(args...; kw...) function GridLayout( dims...; parent = RootLayout(), - widths = zeros(dims[2]), - heights = zeros(dims[1]), + widths = nothing, + heights = nothing, kw..., ) +# Check the values for heights and widths if values are provided +if heights !== nothing && widths !== nothing + if sum(heights) != 1 + error("The sum of heights must be 1!") +end +if sum(widths) != 1 + error("The sum of widths must be 1!") +end +if all(x -> 0 < x < 1, widths) == false + error("Values for widths must be in the range (0, 1)!") +end +if all(x -> 0 < x < 1, heights) == false + error("Values for heights must be in the range (0, 1)!") +end +else + heights = zeros(dims[1]) + widths = zeros(dims[2]) +end grid = Matrix{AbstractLayout}(undef, dims...) layout = GridLayout( parent, @@ -227,12 +245,13 @@ function GridLayout( # convert(Vector{Float64}, heights), KW(kw), ) - for i in eachindex(grid) + for i in eachindex(grid) grid[i] = EmptyLayout(layout) end layout end + Base.size(layout::GridLayout) = size(layout.grid) Base.length(layout::GridLayout) = length(layout.grid) Base.getindex(layout::GridLayout, r::Int, c::Int) = layout.grid[r, c] From f5d7e87750b43887110278630f5356e01fdbc22c Mon Sep 17 00:00:00 2001 From: Giada Toccafondi Date: Fri, 8 Mar 2024 16:09:41 +0100 Subject: [PATCH 2/2] Used JuliaFormatter --- src/layouts.jl | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/layouts.jl b/src/layouts.jl index f034b1c8b..c21f325d4 100644 --- a/src/layouts.jl +++ b/src/layouts.jl @@ -215,24 +215,24 @@ function GridLayout( heights = nothing, kw..., ) -# Check the values for heights and widths if values are provided -if heights !== nothing && widths !== nothing - if sum(heights) != 1 - error("The sum of heights must be 1!") -end -if sum(widths) != 1 - error("The sum of widths must be 1!") -end -if all(x -> 0 < x < 1, widths) == false - error("Values for widths must be in the range (0, 1)!") -end -if all(x -> 0 < x < 1, heights) == false - error("Values for heights must be in the range (0, 1)!") -end -else - heights = zeros(dims[1]) - widths = zeros(dims[2]) -end + # Check the values for heights and widths if values are provided + if heights !== nothing && widths !== nothing + if sum(heights) != 1 + error("The sum of heights must be 1!") + end + if sum(widths) != 1 + error("The sum of widths must be 1!") + end + if all(x -> 0 < x < 1, widths) == false + error("Values for widths must be in the range (0, 1)!") + end + if all(x -> 0 < x < 1, heights) == false + error("Values for heights must be in the range (0, 1)!") + end + else + heights = zeros(dims[1]) + widths = zeros(dims[2]) + end grid = Matrix{AbstractLayout}(undef, dims...) layout = GridLayout( parent, @@ -245,13 +245,12 @@ end # convert(Vector{Float64}, heights), KW(kw), ) - for i in eachindex(grid) + for i in eachindex(grid) grid[i] = EmptyLayout(layout) end layout end - Base.size(layout::GridLayout) = size(layout.grid) Base.length(layout::GridLayout) = length(layout.grid) Base.getindex(layout::GridLayout, r::Int, c::Int) = layout.grid[r, c]