DVUI

install

Parameters

#
self:*Branch
*Branch

Source

Implementation

#
pub fn install(self: *Branch) void {
        self.installed = true;
        var check_button_hovered: bool = false;
        if (self.tree.drag_point) |dp| {
            const topleft = dp.plus(dvui.dragOffset().plus(.{ .x = 5, .y = 5 }));
            if (self.tree.id_branch.? == (self.init_options.branch_id orelse self.data().id.asUsize())) {
                // we are being dragged - put in floating widget
                self.data().register();
                dvui.parentSet(self.widget());

                self.floating_widget = @as(dvui.FloatingWidget, undefined);
                self.floating_widget.?.init(
                    @src(),
                    .{ .mouse_events = false },
                    .{ .rect = Rect.fromPoint(.cast(topleft.toNatural())), .min_size_content = self.tree.branch_size },
                );
            } else {
                self.wd = WidgetData.init(self.wd.src, .{}, wrapOuter(self.options));
                self.wd.register();
                dvui.parentSet(self.widget());

                var rs = self.wd.rectScale();

                var dragRect = Rect.Physical.fromPoint(topleft).toSize(self.tree.branch_size.scale(rs.s, Size.Physical));
                dragRect.h = 2.0;

                if (!rs.r.intersect(dragRect).empty()) {
                    // user is dragging a reorderable over this rect
                    if (!self.expanded) {
                        if (dvui.timerDone(self.data().id)) {
                            self.expanded = true;
                        } else {
                            _ = dvui.timer(self.data().id, 500_000);
                        }
                    }

                    if (!self.expanded) {
                        self.target_rs = rs;
                    } else {
                        check_button_hovered = true;
                    }

                    if (self.target_rs != null) {
                        rs.r.h = 2.0;
                        rs.r.fill(.{}, .{ .color = dvui.themeGet().focus, .fade = 1.0 });
                    }
                }
            }
        } else {
            self.wd = WidgetData.init(self.wd.src, .{}, wrapOuter(self.options));

            self.wd.register();
            dvui.parentSet(self.widget());
        }

        self.button.init(@src(), .{ .touch_drag = true }, wrapInner(self.options).override(.{ .expand = self.options.expand }));
        self.button.processEvents();
        self.button.drawBackground();
        self.button.drawFocus();

        // Check if the button is hovered if we are expanded, this allows us to set the target rs when
        // the entry is expanded
        if (self.button.hovered() and check_button_hovered) {
            var rs = self.data().rectScale();
            self.target_rs = rs;
            rs.r.h = 2.0;
            rs.r.fill(.{}, .{ .color = dvui.themeGet().focus, .fade = 1.0 });
        }

        self.tree.branch_size = self.button.data().rect.size();

        self.parent_focus_id = self.tree.current_branch_focus_id;
        self.tree.current_branch_focus_id = self.button.data().id;

        self.hbox.init(@src(), .{ .dir = .horizontal }, .{ .expand = .both });

        for (dvui.events()) |*e| {
            if (!self.button.matchEvent(e))
                continue;

            switch (e.evt) {
                .mouse => |me| {
                    if (!self.tree.init_options.enable_reordering) continue;
                    if (me.action == .motion) {
                        if (dvui.captured(self.button.data().id)) {
                            e.handle(@src(), self.button.data());
                            if (dvui.dragging(me.p, null)) |_| {
                                self.tree.dragStart(self.data().id.asUsize(), me.p);
                            }
                        }
                    }
                },
                .key => |ke| {
                    if (ke.action != .down and ke.action != .repeat)
                        continue;

                    switch (ke.code) {
                        .right => {
                            e.handle(@src(), self.button.data());
                            if (self.expanded) {
                                self.tree.group.focusNext(e.num);
                            } else {
                                self.expanded = true;
                            }
                        },
                        .left => {
                            e.handle(@src(), self.button.data());
                            if (self.expanded) {
                                self.expanded = false;
                            } else if (self.parent_focus_id) |pid| {
                                dvui.focusWidget(pid, null, e.num);
                            } else {
                                // no parent, so focus the first branch of the tree
                                while (dvui.focusedWidgetId() != null) {
                                    self.tree.group.focusPrev(e.num);
                                }
                                self.tree.group.focusNext(e.num);
                            }
                        },
                        else => {},
                    }
                },
                else => {},
            }
        }
    }