Move back custom widgets into main project

Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
AnErrupTion
2026-03-17 21:59:24 +01:00
parent 64539f4342
commit a89c918c5d
12 changed files with 39 additions and 44 deletions

24
src/Environment.zig Normal file
View File

@@ -0,0 +1,24 @@
const ini = @import("ly-ui").ly_core.ini;
const Ini = ini.Ini;
const enums = @import("enums.zig");
const DisplayServer = enums.DisplayServer;
pub const DesktopEntry = struct {
Exec: []const u8 = "",
Name: []const u8 = "",
DesktopNames: ?[]u8 = null,
Terminal: ?bool = null,
};
pub const Entry = struct { @"Desktop Entry": DesktopEntry = .{} };
entry_ini: ?Ini(Entry) = null,
file_name: []const u8 = "",
name: []const u8 = "",
xdg_session_desktop: ?[]const u8 = null,
xdg_desktop_names: ?[]const u8 = null,
cmd: ?[]const u8 = null,
specifier: []const u8 = "",
display_server: DisplayServer = .wayland,
is_terminal: bool = false,

View File

@@ -7,10 +7,11 @@ const ly_core = @import("ly-ui").ly_core;
const interop = ly_core.interop;
const SharedError = ly_core.SharedError;
const LogFile = ly_core.LogFile;
const Environment = ly_core.Environment;
const utmp = interop.utmp;
const Utmp = utmp.utmpx;
const Environment = @import("Environment.zig");
pub const AuthOptions = struct {
tty: u8,
service_name: [:0]const u8,

111
src/components/InfoLine.zig Normal file
View File

@@ -0,0 +1,111 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const ly_ui = @import("ly-ui");
const keyboard = ly_ui.keyboard;
const TerminalBuffer = ly_ui.TerminalBuffer;
const Widget = ly_ui.Widget;
const CyclableLabel = ly_ui.CyclableLabel;
const MessageLabel = CyclableLabel(Message, Message);
const InfoLine = @This();
const Message = struct {
width: usize,
text: []const u8,
bg: u32,
fg: u32,
};
label: MessageLabel,
pub fn init(
allocator: Allocator,
buffer: *TerminalBuffer,
width: usize,
arrow_fg: u32,
arrow_bg: u32,
) InfoLine {
return .{
.label = MessageLabel.init(
allocator,
buffer,
drawItem,
null,
null,
width,
true,
arrow_fg,
arrow_bg,
),
};
}
pub fn deinit(self: *InfoLine) void {
self.label.deinit();
}
pub fn widget(self: *InfoLine) Widget {
return Widget.init(
"InfoLine",
self,
deinit,
null,
draw,
null,
handle,
null,
);
}
pub fn addMessage(self: *InfoLine, text: []const u8, bg: u32, fg: u32) !void {
if (text.len == 0) return;
try self.label.addItem(.{
.width = TerminalBuffer.strWidth(text),
.text = text,
.bg = bg,
.fg = fg,
});
}
pub fn clearRendered(self: InfoLine, allocator: Allocator) !void {
// Draw over the area
const spaces = try allocator.alloc(u8, self.label.width - 2);
defer allocator.free(spaces);
@memset(spaces, ' ');
TerminalBuffer.drawText(
spaces,
self.label.component_pos.x + 2,
self.label.component_pos.y,
TerminalBuffer.Color.DEFAULT,
TerminalBuffer.Color.DEFAULT,
);
}
fn draw(self: *InfoLine) void {
self.label.draw();
}
fn handle(self: *InfoLine, maybe_key: ?keyboard.Key, insert_mode: bool) !void {
self.label.handle(maybe_key, insert_mode);
}
fn drawItem(label: *MessageLabel, message: Message, x: usize, y: usize, width: usize) void {
if (message.width == 0) return;
const x_offset = if (label.text_in_center and width >= message.width) (width - message.width) / 2 else 0;
label.cursor = message.width + x_offset;
TerminalBuffer.drawConfinedText(
message.text,
x + x_offset,
y,
width,
message.fg,
message.bg,
);
}

116
src/components/Session.zig Normal file
View File

@@ -0,0 +1,116 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const ly_ui = @import("ly-ui");
const keyboard = ly_ui.keyboard;
const TerminalBuffer = ly_ui.TerminalBuffer;
const Widget = ly_ui.Widget;
const CyclableLabel = ly_ui.CyclableLabel;
const UserList = @import("UserList.zig");
const Environment = @import("../Environment.zig");
const Env = struct {
environment: Environment,
index: usize,
};
const EnvironmentLabel = CyclableLabel(Env, *UserList);
const Session = @This();
label: EnvironmentLabel,
user_list: *UserList,
pub fn init(
allocator: Allocator,
buffer: *TerminalBuffer,
user_list: *UserList,
width: usize,
text_in_center: bool,
fg: u32,
bg: u32,
) Session {
return .{
.label = EnvironmentLabel.init(
allocator,
buffer,
drawItem,
sessionChanged,
user_list,
width,
text_in_center,
fg,
bg,
),
.user_list = user_list,
};
}
pub fn deinit(self: *Session) void {
for (self.label.list.items) |*env| {
if (env.environment.entry_ini) |*entry_ini| entry_ini.deinit();
self.label.allocator.free(env.environment.file_name);
}
self.label.deinit();
}
pub fn widget(self: *Session) Widget {
return Widget.init(
"Session",
self,
deinit,
null,
draw,
null,
handle,
null,
);
}
pub fn addEnvironment(self: *Session, environment: Environment) !void {
const env = Env{ .environment = environment, .index = self.label.list.items.len };
try self.label.addItem(env);
addedSession(env, self.user_list);
}
fn draw(self: *Session) void {
self.label.draw();
}
fn handle(self: *Session, maybe_key: ?keyboard.Key, insert_mode: bool) !void {
self.label.handle(maybe_key, insert_mode);
}
fn addedSession(env: Env, user_list: *UserList) void {
const user = user_list.label.list.items[user_list.label.current];
if (!user.first_run) return;
user.session_index.* = env.index;
}
fn sessionChanged(env: Env, maybe_user_list: ?*UserList) void {
if (maybe_user_list) |user_list| {
user_list.label.list.items[user_list.label.current].session_index.* = env.index;
}
}
fn drawItem(label: *EnvironmentLabel, env: Env, x: usize, y: usize, width: usize) void {
if (width < 3) return;
const length = @min(TerminalBuffer.strWidth(env.environment.name), width - 3);
if (length == 0) return;
const x_offset = if (label.text_in_center and width >= length) (width - length) / 2 else 0;
label.cursor = length + x_offset;
TerminalBuffer.drawConfinedText(
env.environment.name,
x + x_offset,
y,
width,
label.fg,
label.bg,
);
}

140
src/components/UserList.zig Normal file
View File

@@ -0,0 +1,140 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const ly_ui = @import("ly-ui");
const keyboard = ly_ui.keyboard;
const TerminalBuffer = ly_ui.TerminalBuffer;
const Widget = ly_ui.Widget;
const CyclableLabel = ly_ui.CyclableLabel;
const Session = @import("Session.zig");
const SavedUsers = @import("../config/SavedUsers.zig");
const StringList = std.ArrayListUnmanaged([]const u8);
pub const User = struct {
name: []const u8,
session_index: *usize,
allocated_index: bool,
first_run: bool,
};
const UserLabel = CyclableLabel(User, *Session);
const UserList = @This();
label: UserLabel,
pub fn init(
allocator: Allocator,
buffer: *TerminalBuffer,
usernames: StringList,
saved_users: *SavedUsers,
session: *Session,
width: usize,
text_in_center: bool,
fg: u32,
bg: u32,
) !UserList {
var user_list = UserList{
.label = UserLabel.init(
allocator,
buffer,
drawItem,
usernameChanged,
session,
width,
text_in_center,
fg,
bg,
),
};
for (usernames.items) |username| {
if (username.len == 0) continue;
var maybe_session_index: ?*usize = null;
var first_run = true;
for (saved_users.user_list.items) |*saved_user| {
if (std.mem.eql(u8, username, saved_user.username)) {
maybe_session_index = &saved_user.session_index;
first_run = saved_user.first_run;
break;
}
}
var allocated_index = false;
if (maybe_session_index == null) {
maybe_session_index = try allocator.create(usize);
maybe_session_index.?.* = 0;
allocated_index = true;
}
try user_list.label.addItem(.{
.name = username,
.session_index = maybe_session_index.?,
.allocated_index = allocated_index,
.first_run = first_run,
});
}
return user_list;
}
pub fn deinit(self: *UserList) void {
for (self.label.list.items) |user| {
if (user.allocated_index) {
self.label.allocator.destroy(user.session_index);
}
}
self.label.deinit();
}
pub fn widget(self: *UserList) Widget {
return Widget.init(
"UserList",
self,
deinit,
null,
draw,
null,
handle,
null,
);
}
pub fn getCurrentUsername(self: UserList) []const u8 {
return self.label.list.items[self.label.current].name;
}
fn draw(self: *UserList) void {
self.label.draw();
}
fn handle(self: *UserList, maybe_key: ?keyboard.Key, insert_mode: bool) !void {
self.label.handle(maybe_key, insert_mode);
}
fn usernameChanged(user: User, maybe_session: ?*Session) void {
if (maybe_session) |session| {
session.label.current = @min(user.session_index.*, session.label.list.items.len - 1);
}
}
fn drawItem(label: *UserLabel, user: User, x: usize, y: usize, width: usize) void {
if (width < 3) return;
const length = @min(TerminalBuffer.strWidth(user.name), width - 3);
if (length == 0) return;
const x_offset = if (label.text_in_center and width >= length) (width - length) / 2 else 0;
label.cursor = length + x_offset;
TerminalBuffer.drawConfinedText(
user.name,
x + x_offset,
y,
width,
label.fg,
label.bg,
);
}

28
src/config/SavedUsers.zig Normal file
View File

@@ -0,0 +1,28 @@
const std = @import("std");
const SavedUsers = @This();
const User = struct {
username: []const u8,
session_index: usize,
first_run: bool,
allocated_username: bool,
};
user_list: std.ArrayList(User),
last_username_index: ?usize,
pub fn init() SavedUsers {
return .{
.user_list = .empty,
.last_username_index = null,
};
}
pub fn deinit(self: *SavedUsers, allocator: std.mem.Allocator) void {
for (self.user_list.items) |user| {
if (user.allocated_username) allocator.free(user.username);
}
self.user_list.deinit(allocator);
}

View File

@@ -11,11 +11,11 @@ const Color = TerminalBuffer.Color;
const Styling = TerminalBuffer.Styling;
const ly_core = ly_ui.ly_core;
const IniParser = ly_core.IniParser;
const SavedUsers = ly_core.SavedUsers;
const ini = ly_core.ini;
const Config = @import("Config.zig");
const OldSave = @import("OldSave.zig");
const SavedUsers = @import("SavedUsers.zig");
const color_properties = [_][]const u8{
"bg",

View File

@@ -9,6 +9,14 @@ pub const Animation = enum {
dur_file,
};
pub const DisplayServer = enum {
wayland,
shell,
xinitrc,
x11,
custom,
};
pub const Input = enum {
info_line,
session,

View File

@@ -10,11 +10,8 @@ const ly_ui = @import("ly-ui");
const Position = ly_ui.Position;
const BigLabel = ly_ui.BigLabel;
const CenteredBox = ly_ui.CenteredBox;
const InfoLine = ly_ui.InfoLine;
const Label = ly_ui.Label;
const Session = ly_ui.Session;
const Text = ly_ui.Text;
const UserList = ly_ui.UserList;
const TerminalBuffer = ly_ui.TerminalBuffer;
const Widget = ly_ui.Widget;
const ly_core = ly_ui.ly_core;
@@ -23,10 +20,6 @@ const UidRange = ly_core.UidRange;
const LogFile = ly_core.LogFile;
const SharedError = ly_core.SharedError;
const IniParser = ly_core.IniParser;
const SavedUsers = ly_core.SavedUsers;
const DisplayServer = ly_core.DisplayServer;
const Environment = ly_core.Environment;
const Entry = Environment.Entry;
const ini = ly_core.ini;
const Ini = ini.Ini;
@@ -37,10 +30,17 @@ const DurFile = @import("animations/DurFile.zig");
const GameOfLife = @import("animations/GameOfLife.zig");
const Matrix = @import("animations/Matrix.zig");
const auth = @import("auth.zig");
const InfoLine = @import("components/InfoLine.zig");
const Session = @import("components/Session.zig");
const UserList = @import("components/UserList.zig");
const Config = @import("config/Config.zig");
const Lang = @import("config/Lang.zig");
const migrator = @import("config/migrator.zig");
const OldSave = @import("config/OldSave.zig");
const SavedUsers = @import("config/SavedUsers.zig");
const DisplayServer = @import("enums.zig").DisplayServer;
const Environment = @import("Environment.zig");
const Entry = Environment.Entry;
const ly_version_str = "Ly version " ++ build_options.version;