feat(tabs): don't write tab styles config if it doesn't already exist

This commit is contained in:
CJ van den Berg 2025-01-30 13:14:00 +01:00
parent 8eb536b5b9
commit 2371140b3f
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 15 additions and 2 deletions

View file

@ -406,6 +406,16 @@ pub fn free_config(allocator: std.mem.Allocator, bufs: [][]const u8) void {
var config_mutex: std.Thread.Mutex = .{};
pub fn exists_config(T: type) bool {
config_mutex.lock();
defer config_mutex.unlock();
const json_file_name = get_app_config_file_name(application_name, @typeName(T)) catch return false;
const text_file_name = json_file_name[0 .. json_file_name.len - ".json".len];
var file = std.fs.openFileAbsolute(text_file_name, .{ .mode = .read_only }) catch return false;
defer file.close();
return true;
}
pub fn read_config(T: type, allocator: std.mem.Allocator) struct { T, [][]const u8 } {
config_mutex.lock();
defer config_mutex.unlock();

View file

@ -80,8 +80,11 @@ const TabBar = struct {
fn init(allocator: std.mem.Allocator, parent: Plane, event_handler: ?EventHandler) !Self {
var w = try WidgetList.createH(allocator, parent, "tabs", .dynamic);
w.ctx = w;
const tab_style, const tab_style_bufs = root.read_config(Style, allocator);
root.write_config(tab_style, allocator) catch {};
const tab_style, const tab_style_bufs: [][]const u8 = if (root.exists_config(Style)) blk: {
const tab_style, const tab_style_bufs = root.read_config(Style, allocator);
root.write_config(tab_style, allocator) catch {};
break :blk .{ tab_style, tab_style_bufs };
} else .{ Style{}, &.{} };
return .{
.allocator = allocator,
.plane = w.plane,