DVUI

init

Parameters

#
self:*GridWidget
*GridWidget
src:std.builtin.SourceLocation
std.builtin.SourceLocation
cols:WidthsOrNum
WidthsOrNum
init_opts:InitOpts
InitOpts
opts:Options
Options

Source

Implementation

#
pub fn init(self: *GridWidget, src: std.builtin.SourceLocation, cols: WidthsOrNum, init_opts: InitOpts, opts: Options) void {
    self.* = .{
        .init_opts = init_opts,
        .cols = cols,
        // SAFETY: Set bellow
        .col_widths = undefined,

        // SAFETY: Set bellow
        .bsi = undefined,
        // SAFETY: Set bellow based on bsi
        .frame_viewport = undefined,

        // SAFETY: Widgets set bellow
        .vbox = undefined,
        .header_group = undefined,
        .body_group = undefined,
        .scroll = undefined,
    };

    self.vbox.init(src, .{ .dir = .vertical }, defaults.override(opts));
    self.vbox.drawBackground();

    if (dvui.dataGet(null, self.data().id, "_resizing", bool)) |resizing| {
        self.resizing = resizing;
    }
    if (dvui.dataGet(null, self.data().id, "_header_height", f32)) |header_height| {
        self.header_height = header_height;
    }
    if (dvui.dataGet(null, self.data().id, "_row_height", f32)) |row_height| {
        self.last_row_height = row_height;
        self.row_height = row_height;
    }
    if (dvui.dataGet(null, self.data().id, "_sort_col", usize)) |sort_col| {
        self.sort_col_number = sort_col;
    }
    if (dvui.dataGet(null, self.data().id, "_sort_direction", SortDirection)) |sort_direction| {
        self.sort_direction = sort_direction;
    }
    if (dvui.dataGet(null, self.data().id, "_hsi", ScrollInfo)) |hsi| {
        self.hsi = hsi;
    }
    if (dvui.dataGet(null, self.data().id, "_default_si", ScrollInfo)) |default_si| {
        self.default_scroll_info = default_si;
    }

    // Ensure resize on first initialization.
    if (dvui.firstFrame(self.data().id)) {
        self.resizing = true;
    }

    self.last_header_height = self.header_height;
    if (init_opts.resize_rows or self.resizing) {
        self.row_height = 0;
        self.header_height = 0;
    }
    // Set the self.col_widths slice to point to the user-supplied col_widths or the
    // internally stored col_widths.
    switch (self.cols) {
        .num_cols => |num_cols| {
            self.col_widths = blk: {
                if (dvui.dataGetSlice(null, self.data().id, "_col_widths", []f32)) |col_widths| {
                    if (col_widths.len == num_cols) {
                        break :blk col_widths;
                    }
                }
                dvui.dataSetSliceCopies(null, self.data().id, "_col_widths", &[1]f32{0}, num_cols);
                if (dvui.dataGetSlice(null, self.data().id, "_col_widths", []f32)) |col_widths| {
                    break :blk col_widths;
                } else {
                    dvui.log.debug("GridWidget: {x} could not allocate column widths", .{self.data().id});
                    break :blk &default_col_widths;
                }
            };

            if (self.init_opts.resize_cols) {
                @memset(self.col_widths, 0);
            }

            // If the grid is keep track of col widths then keep a copy of the starting col widths.
            self.starting_col_widths = dvui.currentWindow().arena().alloc(f32, self.col_widths.len) catch |err| default: {
                dvui.logError(@src(), err, "GridWidget {x} could not allocate column widths", .{self.data().id});
                dvui.Debug.errorOutline(self.data().rectScale().r);
                break :default null;
            };
            if (self.starting_col_widths) |starting| {
                @memcpy(starting, self.col_widths);
            }
        },
        .col_widths => |col_widths| {
            self.col_widths = col_widths;
        },
    }

    if (self.init_opts.scroll_opts) |*scroll_opts| {
        if (scroll_opts.scroll_info) |scroll_info| {
            self.bsi = scroll_info;
        } else {
            self.bsi = &self.default_scroll_info;
            scroll_opts.scroll_info = self.bsi;
            // Move the .horizontal and .vertical settings from scroll_opts to scroll_info
            if (scroll_opts.horizontal) |mode| self.bsi.horizontal = mode;
            if (scroll_opts.vertical) |mode| self.bsi.vertical = mode;
            scroll_opts.horizontal = null;
            scroll_opts.vertical = null;
        }
    } else {
        self.bsi = &self.default_scroll_info;
    }

    self.frame_viewport = self.bsi.viewport.topLeft();

    var scroll_opts: ScrollAreaWidget.InitOpts = self.init_opts.scroll_opts orelse .{ .frame_viewport = self.frame_viewport, .scroll_info = self.bsi };
    scroll_opts.container = false;
    self.scroll.init(
        @src(),
        scroll_opts,
        .{
            .name = "GridWidgetScrollArea",
            .role = .none,
            .expand = .both,
            .background = false,
        },
    );
}