feat: add palette_placement config option

This commit is contained in:
CJ van den Berg 2026-04-12 22:22:53 +02:00
parent d296d3cf63
commit 37bbb17da6
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 60 additions and 3 deletions

View file

@ -85,6 +85,8 @@ hint_window_style: WidgetStyle = .thick_boxed,
info_box_style: WidgetStyle = .bar_left_spacious, info_box_style: WidgetStyle = .bar_left_spacious,
info_box_width_limit: usize = 80, info_box_width_limit: usize = 80,
palette_placement: PalettePlacement = .top_center,
centered_view: bool = false, centered_view: bool = false,
centered_view_width: usize = 145, centered_view_width: usize = 145,
centered_view_min_screen_width: usize = 145, centered_view_min_screen_width: usize = 145,
@ -248,3 +250,10 @@ pub const AgeFormat = enum {
short, short,
long, long,
}; };
pub const PalettePlacement = enum {
top_center,
top_left,
top_right,
center,
};

View file

@ -187,10 +187,36 @@ fn prepare_resize_menu(self: *Self, _: *MenuType, _: Widget.Box) Widget.Box {
} }
fn prepare_resize(self: *Self) Widget.Box { fn prepare_resize(self: *Self) Widget.Box {
const screen = tui.screen();
const padding = tui.get_widget_style(widget_type).padding;
const w = self.menu_width(); const w = self.menu_width();
const x = self.menu_pos_x();
const h = self.menu.menu.widgets.items.len; const h = self.menu.menu.widgets.items.len;
return .{ .y = 0, .x = x, .w = w, .h = h }; return switch (tui.config().palette_placement) {
.top_center => .{
.y = 0,
.x = self.menu_pos_x(),
.w = w,
.h = h,
},
.top_left => .{
.y = 0,
.x = 0,
.w = w,
.h = h,
},
.top_right => .{
.y = 0,
.x = if (screen.w > (w - padding.right)) (screen.w - w - padding.right) else 0,
.w = w,
.h = h,
},
.center => .{
.y = if (screen.h > h) (screen.h - h) / 2 else 0,
.x = self.menu_pos_x(),
.w = w,
.h = h,
},
};
} }
fn do_resize(self: *Self) void { fn do_resize(self: *Self) void {

View file

@ -26,7 +26,17 @@ pub const Placement = enum {
top_center, top_center,
top_left, top_left,
top_right, top_right,
center,
primary_cursor, primary_cursor,
fn from_config(conf: @import("config").PalettePlacement) Placement {
return switch (conf) {
.top_center => .top_center,
.top_left => .top_left,
.top_right => .top_right,
.center => .center,
};
}
}; };
pub const ActivateMode = enum { pub const ActivateMode = enum {
@ -110,7 +120,10 @@ pub fn Create(options: type) type {
.mode = try keybind.mode("overlay/palette", allocator, .{ .mode = try keybind.mode("overlay/palette", allocator, .{
.insert_command = "overlay_insert_bytes", .insert_command = "overlay_insert_bytes",
}), }),
.placement = if (@hasDecl(options, "placement")) options.placement else .top_center, .placement = if (@hasDecl(options, "placement"))
options.placement
else
Placement.from_config(tui.config().palette_placement),
}; };
try self.commands.init(self); try self.commands.init(self);
self.mode.event_handler = EventHandler.to_owned(self); self.mode.event_handler = EventHandler.to_owned(self);
@ -204,6 +217,7 @@ pub fn Create(options: type) type {
.top_center => self.prepare_resize_top_center(screen, w), .top_center => self.prepare_resize_top_center(screen, w),
.top_left => self.prepare_resize_top_left(screen, w), .top_left => self.prepare_resize_top_left(screen, w),
.top_right => self.prepare_resize_top_right(screen, w, padding), .top_right => self.prepare_resize_top_right(screen, w, padding),
.center => self.prepare_resize_center(screen, w),
.primary_cursor => self.prepare_resize_primary_cursor(screen, w, padding), .primary_cursor => self.prepare_resize_primary_cursor(screen, w, padding),
}; };
} }
@ -248,6 +262,14 @@ pub fn Create(options: type) type {
return self.prepare_resize_at_y_x(screen, w, cursor.row + 1 + padding.top, cursor.col); return self.prepare_resize_at_y_x(screen, w, cursor.row + 1 + padding.top, cursor.col);
} }
fn prepare_resize_center(self: *Self, screen: Widget.Box, w: usize) Widget.Box {
const x = if (screen.w > w) (screen.w - w) / 2 else 0;
const h = @min(self.items + self.menu.header_count, self.view_rows + self.menu.header_count);
const y = if (screen.h > h) (screen.h - h) / 2 else 0;
self.view_rows = get_view_rows(screen) -| y;
return .{ .y = y, .x = x, .w = w, .h = h };
}
fn after_resize_menu(self: *Self, _: *Menu.State(*Self), _: Widget.Box) void { fn after_resize_menu(self: *Self, _: *Menu.State(*Self), _: Widget.Box) void {
return self.after_resize(); return self.after_resize();
} }