diff options
-rw-r--r-- | pick.zig | 215 |
1 files changed, 114 insertions, 101 deletions
@@ -67,111 +67,24 @@ pub const Ctx = struct { gpa.free(ctx.path[0..std.fs.MAX_PATH_BYTES]); } - pub const ExitStatus = enum { ok, exit }; - pub fn frame(ctx: *Ctx) !ExitStatus { - const bg = dvui.Options{ - .background = true, - .expand = .horizontal, - .corner_radius = dvui.Rect.all(0), - }; + const bg = dvui.Options{ + .background = true, + .expand = .horizontal, + .corner_radius = dvui.Rect.all(0), + }; - const opts = dvui.Options{ - .corner_radius = dvui.Rect.all(0), - }; + const opts = dvui.Options{ + .corner_radius = dvui.Rect.all(0), + }; - var box = try dvui.box(@src(), .vertical, bg.override(.{ - .expand = .both, - })); + pub const ExitStatus = enum { ok, exit }; + pub fn frame(ctx: *Ctx) !ExitStatus { + var box = try dvui.box(@src(), .vertical, bg.override(.{ .expand = .both })); defer box.deinit(); - { - var top = try dvui.box(@src(), .horizontal, bg); - defer top.deinit(); - - if (try dvui.button(@src(), "..", opts)) { - try std.os.chdir(".."); - try ctx.list_files(); - } - - const te = try dvui.textEntry(@src(), .{ .text = ctx.path }, bg); - defer te.deinit(); - } - { - var bottom = try dvui.box(@src(), .horizontal, opts.override(.{ - .expand = .horizontal, - .gravity_y = 1, - })); - defer bottom.deinit(); - - const button_enabled = ctx.filename.len != 0; - var button_opts = opts.override(.{ .gravity_x = 1 }); - if (button_enabled) { - button_opts.color_style = .accent; - } else { - button_opts.color_style = .control; - button_opts.color_hover = button_opts.color(.fill); - } - if (try dvui.button(@src(), "ok", button_opts) and button_enabled) { - std.debug.print("{s}/{s}\n", .{ ctx.path, ctx.filename }); - return .exit; - } - - const tl = try dvui.textEntry(@src(), .{ .text = ctx.filename }, opts.override(.{ - .expand = .horizontal, - })); - defer tl.deinit(); - } - { - var inner = try dvui.box(@src(), .vertical, .{ - .expand = .both, - .margin = dvui.Rect.all(12), - }); - defer inner.deinit(); - - var scroll = try dvui.scrollArea(@src(), .{}, .{ .expand = .both }); - defer scroll.deinit(); - - entries: for (ctx.entries.items, 0..) |entry, i| { - const name, const icon, const color_style: dvui.Theme.ColorStyle = switch (entry.kind) { - .directory => .{ "folder", entypo.folder, .control }, - .file => .{ "file", entypo.text_document, .window }, - else => .{ "other", entypo.help, .content }, - }; - - const id = opts.override(.{ .id_extra = i }); - const wide = id.override(.{ - .color_style = color_style, - .expand = .horizontal, - .margin = dvui.Rect.all(0), - }); - - var bw = dvui.ButtonWidget.init(@src(), .{}, wide); - try bw.install(); - bw.processEvents(); - try bw.drawBackground(); - - const m = try dvui.menu(@src(), .horizontal, id); - try dvui.icon(@src(), name, icon, id.override(.{ .gravity_y = 0.4 })); - try dvui.labelNoFmt(@src(), entry.name, id); - m.deinit(); - - var clicked = bw.clicked(); - try bw.drawFocus(); - bw.deinit(); - - if (clicked) switch (entry.kind) { - .directory => { - try std.os.chdir(entry.name); - try ctx.list_files(); - break :entries; - }, - else => { - ctx.filename.len = entry.name.len; - std.mem.copy(u8, ctx.filename, entry.name); - }, - }; - } - } + try ctx.draw_top_path(); + if (try ctx.draw_bottom_filename()) |status| return status; + try ctx.draw_file_list(); return .ok; } @@ -209,4 +122,104 @@ pub const Ctx = struct { std.mem.sort(Entry, items, {}, sort.less_than_name); std.mem.sort(Entry, items, {}, sort.less_than_kind); } + + fn draw_top_path(ctx: *Ctx) !void { + var box = try dvui.box(@src(), .horizontal, bg); + defer box.deinit(); + + if (try dvui.button(@src(), "..", opts)) { + try std.os.chdir(".."); + try ctx.list_files(); + } + + const te = try dvui.textEntry(@src(), .{ .text = ctx.path }, bg); + defer te.deinit(); + } + + fn draw_bottom_filename(ctx: *Ctx) !?ExitStatus { + var box = try dvui.box(@src(), .horizontal, opts.override(.{ + .expand = .horizontal, + .gravity_y = 1, + })); + defer box.deinit(); + + const button_enabled = ctx.filename.len != 0; + var button_opts = opts.override(.{ .gravity_x = 1 }); + if (button_enabled) { + button_opts.color_style = .accent; + } else { + button_opts.color_style = .control; + button_opts.color_hover = button_opts.color(.fill); + } + if (try dvui.button(@src(), "ok", button_opts) and button_enabled) { + std.debug.print("{s}/{s}\n", .{ ctx.path, ctx.filename }); + return .exit; + } + + const tl = try dvui.textEntry(@src(), .{ .text = ctx.filename }, bg.override(.{ + .expand = .horizontal, + })); + defer tl.deinit(); + + return null; + } + + fn draw_file_list(ctx: *Ctx) !void { + var box = try dvui.box(@src(), .vertical, .{ + .expand = .both, + .margin = dvui.Rect.all(12), + }); + defer box.deinit(); + + var scroll = try dvui.scrollArea(@src(), .{}, .{ .expand = .both }); + defer scroll.deinit(); + + files: for (ctx.entries.items, 0..) |entry, i| { + const name, const icon, const color_style = @as( + struct { []const u8, []const u8, dvui.Theme.ColorStyle }, + switch (entry.kind) { + .directory => .{ "folder", entypo.folder, .control }, + .file => .{ "file", entypo.text_document, .window }, + else => .{ "other", entypo.help, .content }, + }, + ); + + const id = opts.override(.{ .id_extra = i }); + const wide = id.override(.{ + .color_style = color_style, + .expand = .horizontal, + .margin = dvui.Rect.all(0), + }); + + const clicked = clicked: { + var button = dvui.ButtonWidget.init(@src(), .{}, wide); + defer button.deinit(); + + try button.install(); + button.processEvents(); + try button.drawBackground(); + + const inner = try dvui.box(@src(), .horizontal, id); + defer inner.deinit(); + + try dvui.icon(@src(), name, icon, id.override(.{ .gravity_y = 0.4 })); + try dvui.labelNoFmt(@src(), entry.name, id); + + try button.drawFocus(); + break :clicked button.clicked(); + }; + + if (clicked) switch (entry.kind) { + .directory => { + try std.os.chdir(entry.name); + try ctx.list_files(); + break :files; + }, + else => { + ctx.filename.len = entry.name.len; + std.mem.copy(u8, ctx.filename, entry.name); + }, + }; + } + } }; |