Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List widget #17

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub fn build(b: *std.Build) void {
"event_handler",
"palette",
"unicode",
"list",
};
inline for (executables) |name| {
const exe = b.addExecutable(.{
Expand Down
6 changes: 3 additions & 3 deletions examples/src/fps_counter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const FPSCounter = struct {
return tuile.Widget.init(self);
}

pub fn render(self: *FPSCounter, area: tuile.Rect, frame: tuile.render.Frame, _: tuile.Theme) !void {
pub fn render(self: *FPSCounter, area: tuile.Rect(i32), frame: tuile.render.Frame, _: tuile.Theme) !void {
self.frames += 1;
if (self.frames >= window_size) {
const now = try std.time.Instant.now();
Expand All @@ -63,10 +63,10 @@ const FPSCounter = struct {
self.frames = 0;
}

_ = try frame.writeSymbols(area.min, &self.buffer, area.width());
_ = try frame.writeSymbols(area.min, &self.buffer, @intCast(area.width()));
}

pub fn layout(self: *FPSCounter, _: tuile.Constraints) !tuile.Vec2 {
pub fn layout(self: *FPSCounter, _: tuile.Constraints) !tuile.Vec2u {
return .{ .x = @intCast(self.buffer.len), .y = 1 };
}

Expand Down
62 changes: 62 additions & 0 deletions examples/src/list.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const std = @import("std");
const tuile = @import("tuile");

pub fn main() !void {
var tui = try tuile.Tuile.init(.{});
defer tui.deinit();

const layout = tuile.block(
.{ .layout = .{ .max_height = 12 }, .border_type = .solid, .border = tuile.Border.all() },
tuile.list(
.{ .layout = .{ .alignment = tuile.Align.topCenter(), .min_height = 10 } },
&.{
.{
.label = try tuile.label(.{
// .text = "Item 1\nNew line 1\nNew line 2",
.text = "Item 1",
.layout = .{ .alignment = tuile.Align.topLeft() },
}),
.value = null,
},
.{
.label = try tuile.label(.{
.text = "Item 2 - long line long line long line",
.layout = .{ .alignment = tuile.Align.topLeft() },
}),
.value = null,
},
.{
.label = try tuile.label(.{
.text = "Item 3",
.layout = .{ .alignment = tuile.Align.topLeft() },
}),
.value = null,
},
.{
.label = try tuile.label(.{
.text = "Item 4",
.layout = .{ .alignment = tuile.Align.topLeft() },
}),
.value = null,
},
.{
.label = try tuile.label(.{
.text = "Item 5",
.layout = .{ .alignment = tuile.Align.topLeft() },
}),
.value = null,
},
.{
.label = try tuile.label(.{
.text = "Item 6",
.layout = .{ .alignment = tuile.Align.topLeft() },
}),
.value = null,
},
},
),
);

try tui.add(layout);
try tui.run();
}
71 changes: 0 additions & 71 deletions src/Rect.zig

This file was deleted.

57 changes: 0 additions & 57 deletions src/Vec2.zig

This file was deleted.

14 changes: 7 additions & 7 deletions src/backends/Backend.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const std = @import("std");
const Vec2 = @import("../Vec2.zig");
const Vec2u = @import("../vec2.zig").Vec2u;
const events = @import("../events.zig");
const display = @import("../display.zig");
const internal = @import("../internal.zig");
Expand All @@ -14,8 +14,8 @@ pub const VTable = struct {
destroy: *const fn (context: *anyopaque) void,
poll_event: *const fn (context: *anyopaque) anyerror!?events.Event,
refresh: *const fn (context: *anyopaque) anyerror!void,
print_at: *const fn (context: *anyopaque, pos: Vec2, text: []const u8) anyerror!void,
window_size: *const fn (context: *anyopaque) anyerror!Vec2,
print_at: *const fn (context: *anyopaque, pos: Vec2u, text: []const u8) anyerror!void,
window_size: *const fn (context: *anyopaque) anyerror!Vec2u,
enable_effect: *const fn (context: *anyopaque, effect: display.Style.Effect) anyerror!void,
disable_effect: *const fn (context: *anyopaque, effect: display.Style.Effect) anyerror!void,
use_color: *const fn (context: *anyopaque, color: display.ColorPair) anyerror!void,
Expand All @@ -42,12 +42,12 @@ pub fn init(context: anytype) Backend {
return ptr_info.Pointer.child.refresh(self);
}

pub fn printAt(pointer: *anyopaque, pos: Vec2, text: []const u8) anyerror!void {
pub fn printAt(pointer: *anyopaque, pos: Vec2u, text: []const u8) anyerror!void {
const self: T = @ptrCast(@alignCast(pointer));
return ptr_info.Pointer.child.printAt(self, pos, text);
}

pub fn windowSize(pointer: *anyopaque) anyerror!Vec2 {
pub fn windowSize(pointer: *anyopaque) anyerror!Vec2u {
const self: T = @ptrCast(@alignCast(pointer));
return ptr_info.Pointer.child.windowSize(self);
}
Expand Down Expand Up @@ -101,11 +101,11 @@ pub fn refresh(self: Backend) anyerror!void {
return self.vtable.refresh(self.context);
}

pub fn printAt(self: Backend, pos: Vec2, text: []const u8) anyerror!void {
pub fn printAt(self: Backend, pos: Vec2u, text: []const u8) anyerror!void {
return self.vtable.print_at(self.context, pos, text);
}

pub fn windowSize(self: Backend) anyerror!Vec2 {
pub fn windowSize(self: Backend) anyerror!Vec2u {
return self.vtable.window_size(self.context);
}

Expand Down
6 changes: 3 additions & 3 deletions src/backends/Crossterm.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const std = @import("std");
const internal = @import("../internal.zig");
const Backend = @import("Backend.zig");
const Vec2 = @import("../Vec2.zig");
const Vec2u = @import("../vec2.zig").Vec2u;
const events = @import("../events.zig");
const display = @import("../display.zig");
const render = @import("../render.zig");
Expand Down Expand Up @@ -126,11 +126,11 @@ pub fn refresh(_: *Crossterm) !void {
crossterm_refresh();
}

pub fn printAt(_: *Crossterm, pos: Vec2, text: []const u8) !void {
pub fn printAt(_: *Crossterm, pos: Vec2u, text: []const u8) !void {
crossterm_print_at(.{ .x = @intCast(pos.x), .y = @intCast(pos.y) }, text.ptr, @intCast(text.len));
}

pub fn windowSize(_: *Crossterm) !Vec2 {
pub fn windowSize(_: *Crossterm) !Vec2u {
const size = crossterm_window_size();
return .{ .x = @intCast(size.x), .y = @intCast(size.y) };
}
Expand Down
6 changes: 3 additions & 3 deletions src/backends/Ncurses.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const std = @import("std");
const internal = @import("../internal.zig");
const Backend = @import("Backend.zig");
const Vec2 = @import("../Vec2.zig");
const Vec2u = @import("../vec2.zig").Vec2u;
const events = @import("../events.zig");
const display = @import("../display.zig");

Expand Down Expand Up @@ -136,11 +136,11 @@ pub fn refresh(_: *Ncurses) !void {
if (c.refresh() == c.ERR) return error.NcursesError;
}

pub fn printAt(_: *Ncurses, pos: Vec2, text: []const u8) !void {
pub fn printAt(_: *Ncurses, pos: Vec2u, text: []const u8) !void {
_ = c.mvaddnstr(@intCast(pos.y), @intCast(pos.x), text.ptr, @intCast(text.len));
}

pub fn windowSize(self: *Ncurses) !Vec2 {
pub fn windowSize(self: *Ncurses) !Vec2u {
_ = self;
const x = c.getmaxx(c.stdscr);
const y = c.getmaxy(c.stdscr);
Expand Down
25 changes: 14 additions & 11 deletions src/backends/Testing.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const std = @import("std");
const internal = @import("../internal.zig");
const Backend = @import("Backend.zig");
const Vec2 = @import("../Vec2.zig");
const vec2 = @import("../vec2.zig");
const Vec2u = vec2.Vec2u;
const Vec2i = vec2.Vec2i;
const events = @import("../events.zig");
const display = @import("../display.zig");
const render = @import("../render.zig");
Expand All @@ -10,21 +12,22 @@ const Testing = @This();

frame_buffer: std.ArrayListUnmanaged(render.Cell),

window_size: Vec2,
window_size: Vec2u,

frame: render.Frame,

pub fn create(window_size: Vec2) !*Testing {
pub fn create(window_size: Vec2u) !*Testing {
const self = try internal.allocator.create(Testing);
var buffer = std.ArrayListUnmanaged(render.Cell){};
try buffer.resize(internal.allocator, window_size.x * window_size.y);
const window_area = .{
.min = Vec2i.zero(),
.max = window_size.as(i32),
};
const frame = render.Frame{
.size = window_size,
.buffer = buffer.items,
.area = .{
.min = Vec2.zero(),
.max = window_size,
},
.total_area = window_area,
.area = window_area,
};
frame.clear(display.color("black"), display.color("white"));

Expand All @@ -51,11 +54,11 @@ pub fn pollEvent(_: *Testing) !?events.Event {

pub fn refresh(_: *Testing) !void {}

pub fn printAt(self: *Testing, pos: Vec2, text: []const u8) !void {
self.frame.setSymbol(pos, text);
pub fn printAt(self: *Testing, pos: Vec2u, text: []const u8) !void {
self.frame.setSymbol(pos.as(i32), text);
}

pub fn windowSize(self: *Testing) !Vec2 {
pub fn windowSize(self: *Testing) !Vec2u {
return self.window_size;
}

Expand Down
Loading