DVUI

addTab

Parameters

#
self:*TabsWidget
*TabsWidget
selected:bool
bool
at_options:AddTabOptions
AddTabOptions
opts:Options
Options

Source

Implementation

#
pub fn addTab(self: *TabsWidget, selected: bool, at_options: AddTabOptions, opts: Options) *ButtonWidget {
    // https://www.w3.org/TR/wai-aria/#tab
    var tab_defaults: Options = switch (self.init_options.dir) {
        .horizontal => .{ .id_extra = self.tab_index, .background = true, .corners = .{ .tl = .theme(5), .tr = .theme(5) }, .margin = .{ .x = 2, .w = 2 }, .role = .tab, .label = .{ .label_widget = .next } },
        .vertical => .{ .id_extra = self.tab_index, .background = true, .corners = .{ .tl = .theme(5), .bl = .theme(5) }, .margin = .{ .y = 2, .h = 2 }, .role = .tab, .label = .{ .label_widget = .next } },
    };

    self.tab_index += 1;

    if (selected) {
        tab_defaults.style = .window;
        tab_defaults.font = opts.fontGet().withWeight(.bold);
        tab_defaults.border = switch (self.init_options.dir) {
            .horizontal => .{ .x = 1, .y = 1, .w = 1 },
            .vertical => .{ .x = 1, .y = 1, .h = 1 },
        };
    } else {
        switch (self.init_options.dir) {
            .horizontal => tab_defaults.margin.?.h = 1,
            .vertical => tab_defaults.margin.?.w = 1,
        }
    }

    switch (self.init_options.dir) {
        .horizontal => tab_defaults.gravity_y = 1.0,
        .vertical => tab_defaults.gravity_x = 1.0,
    }

    const options = tab_defaults.override(opts);

    self.tab_button.init(@src(), .{}, options);
    if (at_options.process_events) {
        self.tab_button.processEvents();
    } else {
        self.tab_button.processHover();
    }
    self.tab_button.drawBackground();

    if (self.tab_button.focused() and self.tab_button.data().visible() and self.init_options.draw_focus) {
        const rs = self.tab_button.data().borderRectScale();
        const r = rs.r;
        const cr = self.tab_button.data().options.cornersGet().finalize(opts.theme).scale(dvui.currentWindow().natural_scale, CornerRect.Physical);

        switch (self.init_options.dir) {
            .horizontal => {
                var path: dvui.Path.Builder = .init(dvui.currentWindow().lifo());
                defer path.deinit();

                path.addPoint(r.bottomRight());

                const trc = cr.tr;
                const tr = Point.Physical{ .x = trc.rx, .y = trc.y };
                path.addCorner(trc, r, tr, tr, .tr);

                const tlc = cr.tr;
                const tl = Point.Physical{ .x = tlc.rx, .y = tlc.y };
                path.addCorner(tlc, r, tl, tl, .tl);

                path.addPoint(r.bottomLeft());

                path.build().stroke(.{ .thickness = 2 * rs.s, .color = dvui.themeGet().focus, .after = true });
            },
            .vertical => {
                var path: dvui.Path.Builder = .init(dvui.currentWindow().lifo());
                defer path.deinit();

                path.addPoint(r.topRight());

                const tlc = cr.tl;
                const tl = Point.Physical{ .x = tlc.rx, .y = tlc.y };
                path.addCorner(tlc, r, tl, tl, .tl);

                const blc = cr.bl;
                const bl = Point.Physical{ .x = blc.rx, .y = blc.y };
                path.addCorner(blc, r, bl, bl, .bl);

                path.addPoint(r.bottomRight());

                path.build().stroke(.{ .thickness = 2 * rs.s, .color = dvui.themeGet().focus, .after = true });
            },
        }
    }
    if (self.tab_button.data().accesskit_node()) |ak_node| {
        AccessKit.nodeSetSelected(ak_node, selected);
    }

    return &self.tab_button;
}