DVUI

add

Parameters

#
self:*Subwindows
*Subwindows
gpa:std.mem.Allocator
std.mem.Allocator
id:Id
Id
rect:Rect
Rect
rect_pixels:Rect.Physical
Rect.Physical
modal:bool
bool
stay_above_parent_window:?Id
?Id
mouse_events:bool
bool

Source

Implementation

#
pub fn add(self: *Subwindows, gpa: std.mem.Allocator, id: Id, rect: Rect, rect_pixels: Rect.Physical, modal: bool, stay_above_parent_window: ?Id, mouse_events: bool) !void {
    if (self.get(id)) |sw| {
        if (sw.render_cmds.items.len > 0 or sw.render_cmds_after.items.len > 0) {
            dvui.log.warn("subwindowAdd {x} is clearing some drawing commands (did you try to draw between subwindowCurrentSet and subwindowAdd?)\n", .{id});
        }
        // this window was here previously, just update data, so it stays in the same place in the stack
        sw.used = true;
        sw.rect = rect;
        sw.rect_pixels = rect_pixels;
        sw.modal = modal;
        sw.stay_above_parent_window = stay_above_parent_window;
        sw.mouse_events = mouse_events;

        sw.render_cmds.clearRetainingCapacity();
        sw.render_cmds_after.clearRetainingCapacity();
        return;
    }
    // haven't seen this window before
    const sw = Subwindow{
        .id = id,
        .rect = rect,
        .rect_pixels = rect_pixels,
        .modal = modal,
        .stay_above_parent_window = stay_above_parent_window,
        .mouse_events = mouse_events,
    };
    if (stay_above_parent_window) |subwin_id| {
        // it wants to be above subwin_id
        var i: usize = 0;
        while (i < self.stack.items.len and self.stack.items[i].id != subwin_id) {
            i += 1;
        }
        if (i < self.stack.items.len) {
            i += 1;
        }
        // i points just past subwin_id, go until we run out of subwindows that want to be on top of this subwin_id
        while (i < self.stack.items.len and self.stack.items[i].stay_above_parent_window == subwin_id) {
            i += 1;
        }
        // i points just past all subwindows that want to be on top of this subwin_id
        try self.stack.insert(gpa, i, sw);
    } else {
        // just put it on the top
        try self.stack.append(gpa, sw);
    }
}