add new common component, taskcard
This commit is contained in:
parent
082a77a5eb
commit
0bff9feb35
@ -1,4 +1,5 @@
|
|||||||
pub const menu = @import("./menu.zig");
|
pub const menu = @import("./menu.zig");
|
||||||
pub const search = @import("./search.zig");
|
pub const search = @import("./search.zig");
|
||||||
pub const stage = @import("./stage.zig");
|
pub const stage = @import("./stage.zig");
|
||||||
|
pub const taskcard = @import("./taskcard.zig");
|
||||||
pub const taskedit = @import("./taskedit.zig");
|
pub const taskedit = @import("./taskedit.zig");
|
||||||
|
@ -2,18 +2,21 @@ const std = @import("std");
|
|||||||
const dvui = @import("dvui");
|
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 {
|
pub const Task = struct {
|
||||||
id: usize,
|
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 {
|
pub fn gui(ctx: anytype) !void {
|
||||||
var win = try dvui.floatingWindow(@src(), .{}, .{});
|
var win = try dvui.floatingWindow(@src(), .{}, .{});
|
||||||
defer win.deinit();
|
defer win.deinit();
|
||||||
|
|
||||||
const cw = dvui.currentWindow();
|
|
||||||
|
|
||||||
try dvui.windowHeader("Search", "", null);
|
try dvui.windowHeader("Search", "", null);
|
||||||
|
|
||||||
// task adder
|
// task adder
|
||||||
@ -45,36 +48,45 @@ pub fn gui(ctx: anytype) !void {
|
|||||||
|
|
||||||
for (0.., ctx.tasks()) |idx, task| {
|
for (0.., ctx.tasks()) |idx, task| {
|
||||||
if (idx > 0) {
|
if (idx > 0) {
|
||||||
_ = try dvui.spacer(@src(), .{ .w = 0, .h = 1}, .{
|
_ = try dvui.spacer(@src(), .{ .w = 0, .h = 4}, .{
|
||||||
.id_extra = idx,
|
.id_extra = idx,
|
||||||
.expand = .horizontal,
|
.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, }, .{});
|
var subctx = TaskCardCtx(@TypeOf(ctx)) { .ctx = ctx, .task = task };
|
||||||
|
try compo.taskcard.gui(@src(), &subctx);
|
||||||
if (hbox.data().borderRect().contains(cw.mouse_pt)) {
|
|
||||||
var icons = try dvui.box(@src(), .horizontal, .{
|
|
||||||
.expand = .vertical,
|
|
||||||
.gravity_x = 1,
|
|
||||||
});
|
|
||||||
defer icons.deinit();
|
|
||||||
|
|
||||||
dvui.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try dvui.labelNoFmt(@src(), "no tasks anymore :)", .{ .gravity_x = 0.5 });
|
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 {
|
pub const Mock = struct {
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
const TaskList = std.ArrayList(Task);
|
const TaskList = std.ArrayList(Task);
|
||||||
@ -85,8 +97,8 @@ pub const Mock = struct {
|
|||||||
var ts = TaskList.init(alloc);
|
var ts = TaskList.init(alloc);
|
||||||
errdefer ts.deinit();
|
errdefer ts.deinit();
|
||||||
|
|
||||||
try ts.append(Task { .id = 0, .name = "helloworld", });
|
try ts.append(Task { .id = 0, .summary = "helloworld", });
|
||||||
try ts.append(Task { .id = 1, .name = "goodbye", });
|
try ts.append(Task { .id = 1, .summary = "goodbye", });
|
||||||
|
|
||||||
return Mock {
|
return Mock {
|
||||||
._tasks = ts,
|
._tasks = ts,
|
||||||
|
75
src/heavens-net/compo/taskcard.zig
Normal file
75
src/heavens-net/compo/taskcard.zig
Normal file
@ -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,
|
||||||
|
});
|
||||||
|
}
|
@ -1,21 +1,30 @@
|
|||||||
const dvui = @import("dvui");
|
const dvui = @import("dvui");
|
||||||
|
|
||||||
pub fn key(bind: []const u8) ?*const dvui.Event.Key {
|
pub fn key(bind: []const u8) ?*dvui.Event {
|
||||||
for (dvui.events()) |e| {
|
for (dvui.events()) |*e| {
|
||||||
if (e.evt == .key) {
|
if (e.evt == .key) {
|
||||||
if (e.evt.key.matchBind(bind)) {
|
if (e.evt.key.matchBind(bind)) {
|
||||||
return &e.evt.key;
|
return e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
pub fn keyDown(bind: []const u8) bool {
|
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 {
|
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 {
|
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;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user