DVUI

columnLayoutProportional

Size columns widths using ratios.

Positive widths are treated as fixed widths and are not modified. Negative widths are treated as ratios and are replaced by a calculated width. Results are returned in col_widths, which will always be positive (or zero) values. If content_width is larger than the grid's visible area, horizontal scrolling should be enabled via the grid's init_opts.

Examples: To lay out three columns with equal widths, use the same negative ratio for each column: { -1, -1, -1 } or { -0.33, -0.33, -0.33 } To make the second column with twice the width of the first, use a negative ratio twice as large. {-1, -2 } or { -50, -100 } To lay out a fixed column width with all other columns sharing the remaining, use a positive width for the fixed column and the same negative ratio for the variable columns. { -1, 50, -1 }.

Parameters

#
ratio_widths:[]const f32
[]const f32
col_widths:[]f32
[]f32
content_width:f32
f32

Source

Implementation

#
pub fn columnLayoutProportional(ratio_widths: []const f32, col_widths: []f32, content_width: f32) void {
    const scroll_bar_w: f32 = GridWidget.scrollbar_padding_defaults.w;
    std.debug.assert(ratio_widths.len == col_widths.len); // input and output slices must be the same length

    // Count all of the positive widths as reserved widths.
    // Total all of the negative widths.
    const reserved_w, const ratio_w_total: f32 = blk: {
        var res_width: f32 = 0;
        var total_ratio_w: f32 = 0;
        for (ratio_widths) |w| {
            if (w <= 0) {
                total_ratio_w += -w;
            } else {
                res_width += w;
            }
        }
        break :blk .{ res_width, total_ratio_w };
    };
    const available_w = content_width - reserved_w - scroll_bar_w;

    // For each negative width, replace it width a positive calculated width.
    for (col_widths, ratio_widths) |*col_w, ratio_w| {
        if (ratio_w <= 0) {
            col_w.* = -ratio_w / ratio_w_total * available_w;
        } else {
            col_w.* = ratio_w;
        }
    }
}