refactor: merge expander and spacer status bar widgets
This commit is contained in:
parent
0b42308321
commit
fa5da6e6d9
3 changed files with 20 additions and 56 deletions
|
@ -5,26 +5,32 @@ const Plane = @import("renderer").Plane;
|
|||
const Widget = @import("../Widget.zig");
|
||||
|
||||
plane: Plane,
|
||||
layout: Widget.Layout,
|
||||
on_event: ?Widget.EventHandler,
|
||||
|
||||
const Self = @This();
|
||||
|
||||
pub fn create(a: std.mem.Allocator, parent: Plane, event_handler: ?Widget.EventHandler) @import("widget.zig").CreateError!Widget {
|
||||
pub fn Create(comptime layout_: Widget.Layout) @import("widget.zig").CreateFunction {
|
||||
return struct {
|
||||
fn create(a: std.mem.Allocator, parent: Plane, event_handler: ?Widget.EventHandler) @import("widget.zig").CreateError!Widget {
|
||||
const self: *Self = try a.create(Self);
|
||||
self.* = .{
|
||||
.plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent),
|
||||
.layout = layout_,
|
||||
.on_event = event_handler,
|
||||
};
|
||||
return Widget.to(self);
|
||||
}
|
||||
}.create;
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Self, a: std.mem.Allocator) void {
|
||||
self.plane.deinit();
|
||||
a.destroy(self);
|
||||
}
|
||||
|
||||
pub fn layout(_: *Self) Widget.Layout {
|
||||
return .dynamic;
|
||||
pub fn layout(self: *Self) Widget.Layout {
|
||||
return self.layout;
|
||||
}
|
||||
|
||||
pub fn render(self: *Self, theme: *const Widget.Theme) bool {
|
|
@ -1,43 +0,0 @@
|
|||
const std = @import("std");
|
||||
const tp = @import("thespian");
|
||||
const Plane = @import("renderer").Plane;
|
||||
|
||||
const Widget = @import("../Widget.zig");
|
||||
|
||||
plane: Plane,
|
||||
on_event: ?Widget.EventHandler,
|
||||
|
||||
const Self = @This();
|
||||
|
||||
pub fn create(a: std.mem.Allocator, parent: Plane, event_handler: ?Widget.EventHandler) @import("widget.zig").CreateError!Widget {
|
||||
const self: *Self = try a.create(Self);
|
||||
self.* = .{
|
||||
.plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent),
|
||||
.on_event = event_handler,
|
||||
};
|
||||
return Widget.to(self);
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Self, a: std.mem.Allocator) void {
|
||||
self.plane.deinit();
|
||||
a.destroy(self);
|
||||
}
|
||||
|
||||
pub fn layout(_: *Self) Widget.Layout {
|
||||
return .{ .static = 1 };
|
||||
}
|
||||
|
||||
pub fn render(self: *Self, theme: *const Widget.Theme) bool {
|
||||
self.plane.set_base_style(" ", theme.statusbar);
|
||||
self.plane.erase();
|
||||
return false;
|
||||
}
|
||||
|
||||
pub fn receive(self: *Self, from: tp.pid_ref, m: tp.message) error{Exit}!bool {
|
||||
var btn: u32 = 0;
|
||||
if (try m.match(.{ "D", tp.any, tp.extract(&btn), tp.more })) {
|
||||
if (self.on_event) |h| h.send(from, m) catch {};
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
|
@ -2,7 +2,7 @@ const std = @import("std");
|
|||
const Widget = @import("../Widget.zig");
|
||||
const Plane = @import("renderer").Plane;
|
||||
|
||||
const widgets = std.static_string_map.StaticStringMap(create_fn).initComptime(.{
|
||||
const widgets = std.static_string_map.StaticStringMap(CreateFunction).initComptime(.{
|
||||
.{ "mode", @import("modestate.zig").create },
|
||||
.{ "file", @import("filestate.zig").create },
|
||||
.{ "log", @import("minilog.zig").create },
|
||||
|
@ -11,11 +11,12 @@ const widgets = std.static_string_map.StaticStringMap(create_fn).initComptime(.{
|
|||
.{ "linenumber", @import("linenumstate.zig").create },
|
||||
.{ "modifiers", @import("modstate.zig").create },
|
||||
.{ "keystate", @import("keystate.zig").create },
|
||||
.{ "expander", @import("expander.zig").create },
|
||||
.{ "spacer", @import("spacer.zig").create },
|
||||
.{ "expander", @import("blank.zig").Create(.dynamic) },
|
||||
.{ "spacer", @import("blank.zig").Create(.{ .static = 1 }) },
|
||||
.{ "clock", @import("clock.zig").create },
|
||||
});
|
||||
pub const CreateError = error{ OutOfMemory, Exit };
|
||||
const create_fn = *const fn (a: std.mem.Allocator, parent: Plane, event_handler: ?Widget.EventHandler) CreateError!Widget;
|
||||
pub const CreateFunction = *const fn (a: std.mem.Allocator, parent: Plane, event_handler: ?Widget.EventHandler) CreateError!Widget;
|
||||
|
||||
pub fn create(name: []const u8, a: std.mem.Allocator, parent: Plane, event_handler: ?Widget.EventHandler) CreateError!?Widget {
|
||||
const create_ = widgets.get(name) orelse return null;
|
||||
|
|
Loading…
Add table
Reference in a new issue