Something went wrong. Try again.
Fork of dvui, the immediate-mode Zig GUI, with a font face index for Pipit
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282const std = @import("std");const dvui = @import("../dvui.zig");
const Event = dvui.Event;const Options = dvui.Options;const Rect = dvui.Rect;const RectScale = dvui.RectScale;const Size = dvui.Size;const Widget = dvui.Widget;const WidgetData = dvui.WidgetData;const MenuWidget = dvui.MenuWidget;const ScrollAreaWidget = dvui.ScrollAreaWidget;
const FloatingMenuWidget = @This();
pub const FloatingMenuAvoid = enum { none, horizontal, vertical,
/// Pick horizontal or vertical based on the direction of the current /// parent menu (if any). auto,};
// this lets us maintain a chain of all the nested FloatingMenuWidgets without// forcing the user to manually do itvar current: ?*FloatingMenuWidget = null;
fn currentSet(p: ?*FloatingMenuWidget) ?*FloatingMenuWidget { const ret = current; current = p; return ret;}
pub fn currentGet() ?*FloatingMenuWidget { return current;}
pub var defaults: Options = .{ .name = "FloatingMenu", .corner_radius = Rect.all(5), .border = Rect.all(1), .padding = Rect.all(4), .background = true, .style = .window,};
pub const InitOptions = struct { from: Rect.Natural, avoid: FloatingMenuAvoid = .auto,};
render_ftb: dvui.RenderFrontToBack,wd: WidgetData,prev_windowId: dvui.Id = .zero,prev_last_focus: dvui.Id,parent_fmw: ?*FloatingMenuWidget = null,have_popup_child: bool = false,prevClip: Rect.Physical,scale_val: f32,menu: MenuWidget,scaler: dvui.ScaleWidget,scroll: ScrollAreaWidget,
/// It's expected to call this when `self` is `undefined`pub fn init(self: *FloatingMenuWidget, src: std.builtin.SourceLocation, init_opts: InitOptions, opts: Options) void { self.* = .{ // the widget itself doesn't have any styling, it comes from the // embedded MenuWidget // passing options.rect will stop WidgetData.init from calling // rectFor/minSizeForChild which is important because we are outside // normal layout .wd = .init(src, .{ .subwindow = true }, .{ .id_extra = opts.id_extra, .rect = .{} }), // get scale from parent .scale_val = dvui.parentGet().screenRectScale(Rect{}).s / dvui.windowNaturalScale(),
.render_ftb = undefined, .prev_last_focus = undefined, .prevClip = undefined, .menu = undefined, .scaler = undefined, .scroll = undefined, };
const options = defaults.themeOverride(opts.theme).override(opts); // NOTE: options is really for our embedded ScrollAreaWidget
self.render_ftb.initReset();
dvui.parentSet(self.widget());
self.parent_fmw = currentSet(self); // prevents parents from processing key events if focus is inside the floating window self.prev_last_focus = dvui.lastFocusedIdInFrame();
const avoid: dvui.PlaceOnScreenAvoid = switch (init_opts.avoid) { .none => .none, .horizontal => .horizontal, .vertical => .vertical, .auto => if (dvui.MenuWidget.current()) |pm| switch (pm.init_opts.dir) { .horizontal => .vertical, .vertical => .horizontal, } else .none, };
self.data().rect = Rect.fromPoint(.cast(init_opts.from.topLeft())); if (dvui.minSizeGet(self.data().id)) |_| { const ms = dvui.minSize(self.data().id, options.min_sizeGet()); self.data().rect = self.data().rect.toSize(ms); self.data().rect = .cast(dvui.placeOnScreen(dvui.windowRect(), init_opts.from, avoid, .cast(self.data().rect))); if (dvui.dataGet(null, self.data().id, "_check_focus", void) != null) { dvui.dataRemove(null, self.data().id, "_check_focus"); if (dvui.MenuWidget.current() == null or !dvui.MenuWidget.current().?.mouse_mode or self.data().rectScale().r.contains(dvui.currentWindow().mouse_pt)) { dvui.focusSubwindow(self.data().id, null); } } } else { self.data().rect = .cast(dvui.placeOnScreen(dvui.windowRect(), init_opts.from, avoid, .cast(self.data().rect))); dvui.dataSet(null, self.data().id, "_check_focus", {});
// need a second frame to fit contents (FocusWindow calls refresh but // here for clarity) dvui.refresh(null, @src(), self.data().id); }
self.data().register();
const rs = self.data().rectScale();
self.prev_windowId = dvui.subwindowCurrentSet(self.data().id, null).id; dvui.subwindowAdd(self.data().id, self.data().rect, rs.r, false, null, true); dvui.captureMouseMaintain(.{ .id = self.data().id, .rect = rs.r, .subwindow_id = self.data().id });
// first break out of whatever clip we were in (so box shadows work, since // they are outside our window) self.prevClip = dvui.clipGet(); dvui.clipSet(dvui.windowRectPixels());
// do this while the clip is the whole window const evts = dvui.events(); for (evts) |*e| { if (!dvui.eventMatch(e, .{ .id = self.data().id, .r = rs.r })) continue;
if (e.evt == .mouse and e.evt.mouse.action == .focus) { // focus but let the focus event propagate to widgets dvui.focusSubwindow(self.data().id, e.num); } }
self.scaler.init(@src(), .{ .scale = &self.scale_val }, .{ .expand = .both });
// we are using scroll to do border/background but floating windows // don't have margin, so turn that off self.scroll.init(@src(), .{ .horizontal = .none }, options.override(.{ .margin = .{}, .expand = .both }));
if (dvui.MenuWidget.current()) |pm| { pm.child_popup_rect = rs.r; }
self.menu.init(@src(), .{ .dir = .vertical, .parentSubwindowId = self.prev_windowId }, options.strip().override(.{ .role = .none, .expand = .horizontal }));}
pub fn close(self: *FloatingMenuWidget) void { self.menu.close();}
pub fn widget(self: *FloatingMenuWidget) Widget { return Widget.init(self, data, rectFor, screenRectScale, minSizeForChild);}
pub fn data(self: *FloatingMenuWidget) *WidgetData { return self.wd.validate();}
pub fn rectFor(self: *FloatingMenuWidget, id: dvui.Id, min_size: Size, e: Options.Expand, g: Options.Gravity) Rect { _ = id; return dvui.placeIn(self.data().contentRect().justSize(), min_size, e, g);}
pub fn screenRectScale(self: *FloatingMenuWidget, rect: Rect) RectScale { return self.data().contentRectScale().rectToRectScale(rect);}
pub fn minSizeForChild(self: *FloatingMenuWidget, s: Size) void { self.data().minSizeMax(self.data().options.padSize(s));}
pub fn chainFocused(self: *FloatingMenuWidget, self_call: bool) bool { if (!self_call) { // if we got called by someone else, then we have a popup child self.have_popup_child = true; }
var ret: bool = false;
// we have to call chainFocused on our parent if we have one so we // can't return early
if (self.data().id == dvui.focusedSubwindowId()) { // we are focused ret = true; }
if (self.parent_fmw) |pp| { // we had a parent popup, is that focused if (pp.chainFocused(false)) { ret = true; } } else if (self.prev_windowId == dvui.focusedSubwindowId()) { // no parent popup, is our parent window focused ret = true; }
return ret;}
pub fn deinit(self: *FloatingMenuWidget) void { defer if (dvui.widgetIsAllocated(self)) dvui.widgetFree(self); defer self.* = undefined;
const evts = dvui.events(); const rs = self.data().rectScale(); for (evts) |*e| { if (!dvui.eventMatch(e, .{ .id = self.data().id, .r = rs.r, .cleanup = true })) continue;
if (e.evt == .mouse) { if (e.evt.mouse.action == .focus) { // unhandled click, clear focus e.handle(@src(), self.data()); dvui.focusWidget(null, null, null); } } else if (e.evt == .key) { // catch any tabs that weren't handled by widgets if (e.evt.key.action == .down and e.evt.key.matchBind("next_widget")) { e.handle(@src(), self.data()); dvui.tabIndexNext(e.num); }
if (e.evt.key.action == .down and e.evt.key.matchBind("prev_widget")) { e.handle(@src(), self.data()); dvui.tabIndexPrev(e.num); } } }
if (!self.have_popup_child and !self.chainFocused(true)) { // if a popup chain is open and the user focuses a different window // (not the parent of the popups), then we want to close the popups
// only the last popup can do the check, you can't query the focus // status of children, only parents self.menu.close_chain(.unintentional); dvui.refresh(null, @src(), self.data().id); }
self.menu.deinit(); self.scroll.deinit(); self.scaler.deinit();
// in case no children ever show up, this will provide a visual indication // that there is an empty floating menu self.data().minSizeMax(self.data().options.padSize(.{ .w = 20, .h = 20 }));
self.data().minSizeSetAndRefresh();
// outside normal layout, don't call minSizeForChild or self.data().minSizeReportToParent();
_ = currentSet(self.parent_fmw); dvui.parentReset(self.data().id, self.data().parent); dvui.currentWindow().last_focused_id_this_frame = self.prev_last_focus; _ = dvui.subwindowCurrentSet(self.prev_windowId, null); dvui.clipSet(self.prevClip); self.render_ftb.deinit();}
test { @import("std").testing.refAllDecls(@This());}