widgets: add Scrollbar widget

This commit is contained in:
Tim Culverhouse 2024-05-06 18:50:56 -05:00
parent fa7d94f3df
commit 920e490e4c
2 changed files with 36 additions and 1 deletions

View file

@ -1,5 +1,6 @@
pub const border = @import("widgets/border.zig");
pub const alignment = @import("widgets/alignment.zig");
pub const TextInput = @import("widgets/TextInput.zig");
pub const Scrollbar = @import("widgets/Scrollbar.zig");
pub const Table = @import("widgets/Table.zig");
pub const TextInput = @import("widgets/TextInput.zig");
pub const nvim = @import("widgets/nvim.zig");

34
src/widgets/Scrollbar.zig Normal file
View file

@ -0,0 +1,34 @@
const std = @import("std");
const vaxis = @import("../main.zig");
const Scrollbar = @This();
/// character to use for the scrollbar
character: vaxis.Cell.Character = .{ .grapheme = "", .width = 1 },
/// style to draw the bar character with
style: vaxis.Style = .{},
/// index of the top of the visible area
top: usize = 0,
/// total items in the list
total: usize,
/// total items that fit within the view area
view_size: usize,
pub fn draw(self: Scrollbar, win: vaxis.Window) void {
// don't draw when 0 items
if (self.total < 1) return;
// don't draw when all items can be shown
if (self.view_size >= self.total) return;
var bar_height = self.view_size * win.height / self.total;
if (bar_height < 0) bar_height = 1;
const bar_top = self.top * win.height / self.total;
var i: usize = 0;
while (i < bar_height) : (i += 1)
win.writeCell(0, i + bar_top, .{ .char = self.character, .style = self.style });
}