diff --git a/src/heavens-net/compo/root.zig b/src/heavens-net/compo/root.zig index 04e4791..e78aa5c 100644 --- a/src/heavens-net/compo/root.zig +++ b/src/heavens-net/compo/root.zig @@ -1,4 +1,5 @@ pub const menu = @import("./menu.zig"); pub const search = @import("./search.zig"); pub const stage = @import("./stage.zig"); +pub const taskcard = @import("./taskcard.zig"); pub const taskedit = @import("./taskedit.zig"); diff --git a/src/heavens-net/compo/search.zig b/src/heavens-net/compo/search.zig index 49ad3e7..5ba1204 100644 --- a/src/heavens-net/compo/search.zig +++ b/src/heavens-net/compo/search.zig @@ -1,19 +1,22 @@ const std = @import("std"); const dvui = @import("dvui"); -const ui = @import("../ui/root.zig"); +const ui = @import("../ui/root.zig"); +const compo = @import("./root.zig"); pub const Task = struct { id: usize, - name: [:0]const u8, + summary: [:0]const u8, + + done : bool = false, + staged : bool = false, + archived: bool = false, }; pub fn gui(ctx: anytype) !void { var win = try dvui.floatingWindow(@src(), .{}, .{}); defer win.deinit(); - const cw = dvui.currentWindow(); - try dvui.windowHeader("Search", "", null); // task adder @@ -45,36 +48,45 @@ pub fn gui(ctx: anytype) !void { for (0.., ctx.tasks()) |idx, task| { if (idx > 0) { - _ = try dvui.spacer(@src(), .{ .w = 0, .h = 1}, .{ + _ = try dvui.spacer(@src(), .{ .w = 0, .h = 4}, .{ .id_extra = idx, .expand = .horizontal, }); } - var hbox = try dvui.box(@src(), .horizontal, .{ - .id_extra = idx, - .expand = .horizontal, - .background = true, - .border = dvui.Rect.all(1), - }); - defer hbox.deinit(); - try dvui.label(@src(), "#{d} {s}", .{ task.id, task.name, }, .{}); - - if (hbox.data().borderRect().contains(cw.mouse_pt)) { - var icons = try dvui.box(@src(), .horizontal, .{ - .expand = .vertical, - .gravity_x = 1, - }); - defer icons.deinit(); - - dvui. - } + var subctx = TaskCardCtx(@TypeOf(ctx)) { .ctx = ctx, .task = task }; + try compo.taskcard.gui(@src(), &subctx); } try dvui.labelNoFmt(@src(), "no tasks anymore :)", .{ .gravity_x = 0.5 }); } } +fn TaskCardCtx(T: type) type { + return struct { + const Self = @This(); + + ctx : T, + task: Task, + + pub fn id(self: *const Self) usize { + return self.task.id; + } + pub fn summary(self: *const Self) []const u8 { + return self.task.summary; + } + pub fn done(self: *const Self) bool { + return self.task.done; + } + pub fn archived(self: *const Self) bool { + return self.task.archived; + } + pub fn staged(self: *const Self) bool { + return self.task.staged; + } + }; +} + pub const Mock = struct { const Self = @This(); const TaskList = std.ArrayList(Task); @@ -85,8 +97,8 @@ pub const Mock = struct { var ts = TaskList.init(alloc); errdefer ts.deinit(); - try ts.append(Task { .id = 0, .name = "helloworld", }); - try ts.append(Task { .id = 1, .name = "goodbye", }); + try ts.append(Task { .id = 0, .summary = "helloworld", }); + try ts.append(Task { .id = 1, .summary = "goodbye", }); return Mock { ._tasks = ts, diff --git a/src/heavens-net/compo/taskcard.zig b/src/heavens-net/compo/taskcard.zig new file mode 100644 index 0000000..eb08a2a --- /dev/null +++ b/src/heavens-net/compo/taskcard.zig @@ -0,0 +1,75 @@ +const std = @import("std"); +const dvui = @import("dvui"); + +const ui = @import("../ui/root.zig"); + +pub fn gui(src: std.builtin.SourceLocation, ctx: anytype) !void { + const id = ctx.id(); + const summary = ctx.summary(); + const done = ctx.done(); + const archived = ctx.archived(); + const staged = ctx.staged(); + + var card = try dvui.box(src, .horizontal, .{ + .id_extra = id, + .expand = .horizontal, + .background = true, + .border = dvui.Rect.all(1), + }); + defer card.deinit(); + + { + var check = done; + const changed = try dvui.checkbox(@src(), &check, null, .{ + .gravity_y = 0.5, + .gravity_x = 0, + }); + if (changed) { + std.debug.print("CHANGED\n", .{}); + } + } + + const hover = true; + if (hover) { + var icons = try dvui.box(@src(), .horizontal, .{ + .expand = .vertical, + .gravity_x = 1, + }); + defer icons.deinit(); + + if (staged) { + if (try buttonIcon(@src(), "unstage", dvui.entypo.light_down)) { + std.debug.print("UNSTAGE\n", .{}); + } + } else { + if (try buttonIcon(@src(), "stage", dvui.entypo.light_up)) { + std.debug.print("STAGE\n", .{}); + } + } + if (archived) { + if (try buttonIcon(@src(), "unarchive", dvui.entypo.back_in_time)) { + std.debug.print("UNARCHIVE\n", .{}); + } + } else { + if (try buttonIcon(@src(), "archive", dvui.entypo.archive)) { + std.debug.print("ARCHIVE\n", .{}); + } + } + } + + const open = try dvui.labelClick(@src(), "#{d} {s}", .{ ctx.id(), summary, }, .{ + .expand = .horizontal, + .gravity_x = 0, + }); + if (open) { + std.debug.print("OPEN\n", .{}); + } +} + +fn buttonIcon(src: std.builtin.SourceLocation, name: []const u8, icon: []const u8) !bool { + return try dvui.buttonIcon(src, name, icon, .{}, .{ + .margin = dvui.Rect.all(0), + .padding = dvui.Rect.all(4), + .gravity_y = 0.5, + }); +} diff --git a/src/heavens-net/ui/event.zig b/src/heavens-net/ui/event.zig index 072a38b..d461d3c 100644 --- a/src/heavens-net/ui/event.zig +++ b/src/heavens-net/ui/event.zig @@ -1,21 +1,30 @@ const dvui = @import("dvui"); -pub fn key(bind: []const u8) ?*const dvui.Event.Key { - for (dvui.events()) |e| { +pub fn key(bind: []const u8) ?*dvui.Event { + for (dvui.events()) |*e| { if (e.evt == .key) { if (e.evt.key.matchBind(bind)) { - return &e.evt.key; + return e; } } } return null; } pub fn keyDown(bind: []const u8) bool { - return if (key(bind)) |k| k.action == .down else false; + return if (key(bind)) |e| e.evt.key.action == .down else false; } pub fn keyPress(bind: []const u8) bool { - return if (key(bind)) |k| (k.action == .down or k.action == .repeat) else false; + return if (key(bind)) |e| (e.evt.key.action == .down or e.evt.key.action == .repeat) else false; } pub fn keyUp(bind: []const u8) bool { - return if (key(bind)) |k| k.action == .up else false; + return if (key(bind)) |e| e.evt.key.action == .up else false; +} + +pub fn mouse(action: dvui.Event.Mouse.Action, button: dvui.enums.Button) ?*dvui.Event { + for (dvui.events()) |*e| { + if (e.evt == .mouse and e.evt.mouse.action == action and e.evt.mouse.button == button) { + return e; + } + } + return null; }