Compare commits
2 Commits
b3dc8096d7
...
4567963ff2
| Author | SHA1 | Date | |
|---|---|---|---|
|
4567963ff2
|
|||
|
d4cc520826
|
@@ -33,8 +33,6 @@ const zterm: *Dependency = b.dependency("zterm", .{
|
|||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
});
|
});
|
||||||
// ...
|
|
||||||
exe.root_module.addImport("zterm", zterm.module("zterm"));
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Documentation
|
### Documentation
|
||||||
|
|||||||
@@ -24,6 +24,86 @@ const QuitText = struct {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const TableText = struct {
|
||||||
|
pub fn element(this: *@This()) App.Element {
|
||||||
|
return .{
|
||||||
|
.ptr = this,
|
||||||
|
.vtable = &.{
|
||||||
|
.content = content,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn content(ctx: *anyopaque, _: *const App.Model, cells: []zterm.Cell, size: zterm.Point) !void {
|
||||||
|
_ = ctx;
|
||||||
|
_ = size;
|
||||||
|
var idx: usize = 0;
|
||||||
|
{
|
||||||
|
const text = "Normal ";
|
||||||
|
for (text) |cp| {
|
||||||
|
cells[idx].cp = cp;
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const text = "Bold ";
|
||||||
|
for (text) |cp| {
|
||||||
|
cells[idx].cp = cp;
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const text = "Dim ";
|
||||||
|
for (text) |cp| {
|
||||||
|
cells[idx].cp = cp;
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const text = "Italic ";
|
||||||
|
for (text) |cp| {
|
||||||
|
cells[idx].cp = cp;
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const text = "Underl ";
|
||||||
|
for (text) |cp| {
|
||||||
|
cells[idx].cp = cp;
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const text = "Blink ";
|
||||||
|
for (text) |cp| {
|
||||||
|
cells[idx].cp = cp;
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const text = "Invert ";
|
||||||
|
for (text) |cp| {
|
||||||
|
cells[idx].cp = cp;
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const text = "Hidden ";
|
||||||
|
for (text) |cp| {
|
||||||
|
cells[idx].cp = cp;
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const text = "Strikethrough";
|
||||||
|
for (text) |cp| {
|
||||||
|
cells[idx].cp = cp;
|
||||||
|
idx += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const TextStyles = struct {
|
const TextStyles = struct {
|
||||||
const text = "Example";
|
const text = "Example";
|
||||||
|
|
||||||
@@ -109,12 +189,21 @@ pub fn main() !void {
|
|||||||
|
|
||||||
var container = try App.Container.init(allocator, .{
|
var container = try App.Container.init(allocator, .{
|
||||||
.layout = .{
|
.layout = .{
|
||||||
.gap = 2,
|
// .gap = 2,
|
||||||
.padding = .{ .top = 5, .bottom = 3, .left = 3, .right = 3 },
|
.padding = .{ .top = 5, .bottom = 3, .left = 3, .right = 3 },
|
||||||
|
.direction = .vertical,
|
||||||
},
|
},
|
||||||
}, element);
|
}, element);
|
||||||
defer container.deinit();
|
defer container.deinit();
|
||||||
|
|
||||||
|
var table_head: TableText = .{};
|
||||||
|
try container.append(try .init(allocator, .{
|
||||||
|
.size = .{
|
||||||
|
.dim = .{ .y = 1 },
|
||||||
|
.grow = .horizontal,
|
||||||
|
},
|
||||||
|
}, table_head.element()));
|
||||||
|
|
||||||
var box = try App.Container.init(allocator, .{
|
var box = try App.Container.init(allocator, .{
|
||||||
.layout = .{ .direction = .vertical },
|
.layout = .{ .direction = .vertical },
|
||||||
}, text_styles.element());
|
}, text_styles.element());
|
||||||
@@ -126,10 +215,20 @@ pub fn main() !void {
|
|||||||
try app.start();
|
try app.start();
|
||||||
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
|
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
|
||||||
|
|
||||||
while (true) {
|
// event loop
|
||||||
const event = app.nextEvent();
|
loop: while (true) {
|
||||||
log.debug("received event: {s}", .{@tagName(event)});
|
// batch events since last iteration
|
||||||
|
const len = blk: {
|
||||||
|
app.queue.poll();
|
||||||
|
app.queue.lock();
|
||||||
|
defer app.queue.unlock();
|
||||||
|
break :blk app.queue.len();
|
||||||
|
};
|
||||||
|
|
||||||
|
// handle events
|
||||||
|
for (0..len) |_| {
|
||||||
|
const event = app.queue.pop();
|
||||||
|
log.debug("handling event: {s}", .{@tagName(event)});
|
||||||
// pre event handling
|
// pre event handling
|
||||||
switch (event) {
|
switch (event) {
|
||||||
.key => |key| if (key.eql(.{ .cp = 'c', .mod = .{ .ctrl = true } })) app.quit(),
|
.key => |key| if (key.eql(.{ .cp = 'c', .mod = .{ .ctrl = true } })) app.quit(),
|
||||||
@@ -146,9 +245,10 @@ pub fn main() !void {
|
|||||||
|
|
||||||
// post event handling
|
// post event handling
|
||||||
switch (event) {
|
switch (event) {
|
||||||
.quit => break,
|
.quit => break :loop,
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
container.resize(try renderer.resize());
|
container.resize(try renderer.resize());
|
||||||
container.reposition(.{});
|
container.reposition(.{});
|
||||||
|
|||||||
@@ -267,8 +267,8 @@ pub fn Scrollable(Model: type, Event: type) type {
|
|||||||
|
|
||||||
this.size = size;
|
this.size = size;
|
||||||
if (this.configuration.scrollbar) {
|
if (this.configuration.scrollbar) {
|
||||||
if (this.container.properties.size.dim.x > this.size.x or this.container.size.x > this.size.x) this.configuration.y_axis = true;
|
this.configuration.y_axis = this.container.properties.size.dim.x > this.size.x or this.container.size.x > this.size.x;
|
||||||
if (this.container.properties.size.dim.y > this.size.y or this.container.size.y > this.size.y) this.configuration.x_axis = true;
|
this.configuration.x_axis = this.container.properties.size.dim.y > this.size.y or this.container.size.y > this.size.y;
|
||||||
|
|
||||||
const min_size = this.container.element.minSize(size);
|
const min_size = this.container.element.minSize(size);
|
||||||
this.container.resize(.{
|
this.container.resize(.{
|
||||||
@@ -295,7 +295,43 @@ pub fn Scrollable(Model: type, Event: type) type {
|
|||||||
fn handle(ctx: *anyopaque, model: *Model, event: Event) !void {
|
fn handle(ctx: *anyopaque, model: *Model, event: Event) !void {
|
||||||
const this: *@This() = @ptrCast(@alignCast(ctx));
|
const this: *@This() = @ptrCast(@alignCast(ctx));
|
||||||
switch (event) {
|
switch (event) {
|
||||||
// TODO other means to scroll except with the mouse? (i.e. Ctrl-u/d, k/j, etc.?)
|
.init => this.container.resize(this.container.element.minSize(this.size)),
|
||||||
|
// TODO what about multiple scrollable `Element` usages? mouse
|
||||||
|
// can differ between them (due to the event being only send to
|
||||||
|
// the `Container` / `Element` one under the cursor!)
|
||||||
|
.key => |key| {
|
||||||
|
if (key.eql(.{ .cp = 'j' }) or key.eql(.{ .cp = input.Down })) {
|
||||||
|
const max_anchor_y = this.container_size.y -| this.size.y;
|
||||||
|
this.anchor.y = @min(this.anchor.y + 1, max_anchor_y);
|
||||||
|
}
|
||||||
|
if (key.eql(.{ .cp = 'k' }) or key.eql(.{ .cp = input.Up })) {
|
||||||
|
this.anchor.y -|= 1;
|
||||||
|
}
|
||||||
|
if (key.eql(.{ .cp = 'd', .mod = .{ .ctrl = true } })) {
|
||||||
|
// half page down
|
||||||
|
const half_page = this.size.y / 2;
|
||||||
|
const max_anchor_y = this.container_size.y -| this.size.y;
|
||||||
|
this.anchor.y = @min(this.anchor.y + half_page, max_anchor_y);
|
||||||
|
}
|
||||||
|
if (key.eql(.{ .cp = 'u', .mod = .{ .ctrl = true } })) {
|
||||||
|
// half page up
|
||||||
|
const half_page = this.size.y / 2;
|
||||||
|
this.anchor.y -|= half_page;
|
||||||
|
}
|
||||||
|
if (key.eql(.{ .cp = 'f', .mod = .{ .ctrl = true } }) or key.eql(.{ .cp = input.PageDown })) {
|
||||||
|
const max_anchor_y = this.container_size.y -| this.size.y;
|
||||||
|
this.anchor.y = @min(this.anchor.y + this.size.y, max_anchor_y);
|
||||||
|
}
|
||||||
|
if (key.eql(.{ .cp = 'b', .mod = .{ .ctrl = true } }) or key.eql(.{ .cp = input.PageDown })) {
|
||||||
|
this.anchor.y -|= this.size.y;
|
||||||
|
}
|
||||||
|
if (key.eql(.{ .cp = 'g' }) or key.eql(.{ .cp = input.Home })) {
|
||||||
|
this.anchor.y = 0;
|
||||||
|
}
|
||||||
|
if (key.eql(.{ .cp = 'G' }) or key.eql(.{ .cp = input.End })) {
|
||||||
|
this.anchor.y = this.container_size.y -| this.size.y;
|
||||||
|
}
|
||||||
|
},
|
||||||
.mouse => |mouse| switch (mouse.button) {
|
.mouse => |mouse| switch (mouse.button) {
|
||||||
Mouse.Button.wheel_up => if (this.container_size.y > this.size.y) {
|
Mouse.Button.wheel_up => if (this.container_size.y > this.size.y) {
|
||||||
this.anchor.y -|= 1;
|
this.anchor.y -|= 1;
|
||||||
@@ -1073,8 +1109,8 @@ test "scrollable vertical" {
|
|||||||
try renderer.render(@TypeOf(container), &container, Model, &.{});
|
try renderer.render(@TypeOf(container), &container, Model, &.{});
|
||||||
try testing.expectEqualCells(.{}, renderer.size, @import("test/element/scrollable.vertical.top.zon"), renderer.screen);
|
try testing.expectEqualCells(.{}, renderer.size, @import("test/element/scrollable.vertical.top.zon"), renderer.screen);
|
||||||
|
|
||||||
// scroll down 15 times (exactly to the end)
|
// scroll down 15 times (exactly to the end) (with both mouse and key inputs)
|
||||||
for (0..15) |_| try container.handle(&model, .{
|
for (0..7) |_| try container.handle(&model, .{
|
||||||
.mouse = .{
|
.mouse = .{
|
||||||
.button = .wheel_down,
|
.button = .wheel_down,
|
||||||
.kind = .press,
|
.kind = .press,
|
||||||
@@ -1082,6 +1118,9 @@ test "scrollable vertical" {
|
|||||||
.y = 5,
|
.y = 5,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
for (7..15) |_| try container.handle(&model, .{
|
||||||
|
.key = .{ .cp = 'j' },
|
||||||
|
});
|
||||||
try renderer.render(@TypeOf(container), &container, Model, &.{});
|
try renderer.render(@TypeOf(container), &container, Model, &.{});
|
||||||
try testing.expectEqualCells(.{}, renderer.size, @import("test/element/scrollable.vertical.bottom.zon"), renderer.screen);
|
try testing.expectEqualCells(.{}, renderer.size, @import("test/element/scrollable.vertical.bottom.zon"), renderer.screen);
|
||||||
|
|
||||||
@@ -1096,6 +1135,40 @@ test "scrollable vertical" {
|
|||||||
});
|
});
|
||||||
try renderer.render(@TypeOf(container), &container, Model, &.{});
|
try renderer.render(@TypeOf(container), &container, Model, &.{});
|
||||||
try testing.expectEqualCells(.{}, renderer.size, @import("test/element/scrollable.vertical.bottom.zon"), renderer.screen);
|
try testing.expectEqualCells(.{}, renderer.size, @import("test/element/scrollable.vertical.bottom.zon"), renderer.screen);
|
||||||
|
|
||||||
|
// scrolling up and down again
|
||||||
|
for (0..5) |_| try container.handle(&model, .{
|
||||||
|
.key = .{ .cp = 'k' },
|
||||||
|
});
|
||||||
|
for (0..5) |_| try container.handle(&model, .{
|
||||||
|
.key = .{ .cp = 'j' },
|
||||||
|
});
|
||||||
|
try renderer.render(@TypeOf(container), &container, Model, &.{});
|
||||||
|
try testing.expectEqualCells(.{}, renderer.size, @import("test/element/scrollable.vertical.bottom.zon"), renderer.screen);
|
||||||
|
|
||||||
|
// scrolling half page up and down again
|
||||||
|
try container.handle(&model, .{
|
||||||
|
.key = .{ .cp = 'u', .mod = .{ .ctrl = true } },
|
||||||
|
});
|
||||||
|
try container.handle(&model, .{
|
||||||
|
.key = .{ .cp = 'd', .mod = .{ .ctrl = true } },
|
||||||
|
});
|
||||||
|
try renderer.render(@TypeOf(container), &container, Model, &.{});
|
||||||
|
try testing.expectEqualCells(.{}, renderer.size, @import("test/element/scrollable.vertical.bottom.zon"), renderer.screen);
|
||||||
|
|
||||||
|
// scrolling page up
|
||||||
|
try container.handle(&model, .{
|
||||||
|
.key = .{ .cp = 'b', .mod = .{ .ctrl = true } },
|
||||||
|
});
|
||||||
|
try renderer.render(@TypeOf(container), &container, Model, &.{});
|
||||||
|
try testing.expectEqualCells(.{}, renderer.size, @import("test/element/scrollable.vertical.top.zon"), renderer.screen);
|
||||||
|
|
||||||
|
// and down again
|
||||||
|
try container.handle(&model, .{
|
||||||
|
.key = .{ .cp = 'f', .mod = .{ .ctrl = true } },
|
||||||
|
});
|
||||||
|
try renderer.render(@TypeOf(container), &container, Model, &.{});
|
||||||
|
try testing.expectEqualCells(.{}, renderer.size, @import("test/element/scrollable.vertical.bottom.zon"), renderer.screen);
|
||||||
}
|
}
|
||||||
|
|
||||||
test "scrollable vertical with scrollbar" {
|
test "scrollable vertical with scrollbar" {
|
||||||
|
|||||||
Reference in New Issue
Block a user