lib/im: Allow action alignment in shortcut widget

This commit is contained in:
Erik Kundt
2025-12-28 00:19:37 +01:00
parent 16f7ced603
commit a0e9939ad1
6 changed files with 41 additions and 12 deletions
+3 -2
View File
@@ -12,7 +12,7 @@ use ratatui::text::{Span, Text};
use tokio::sync::broadcast;
use tokio::sync::mpsc::UnboundedReceiver;
use ratatui::layout::{Constraint, Flex, Position, Rect};
use ratatui::layout::{Alignment, Constraint, Flex, Position, Rect};
use ratatui::{Frame, Viewport};
use crate::event::{Event, Key};
@@ -571,8 +571,9 @@ where
frame: &mut Frame,
shortcuts: &[(&str, &str)],
divider: char,
alignment: Alignment,
) -> Response {
widget::Shortcuts::new(shortcuts, divider).ui(self, frame)
widget::Shortcuts::new(shortcuts, divider, alignment).ui(self, frame)
}
pub fn column_bar(
+22 -2
View File
@@ -2,7 +2,7 @@ use std::cmp;
use serde::{Deserialize, Serialize};
use ratatui::layout::{Direction, Layout, Position, Rect};
use ratatui::layout::{Alignment, Direction, Layout, Position, Rect};
use ratatui::style::{Style, Stylize};
use ratatui::text::{Line, Span, Text};
use ratatui::widgets::{Block, BorderType, Row, Scrollbar, ScrollbarState};
@@ -1043,16 +1043,18 @@ impl Widget for TextEdit<'_> {
pub struct Shortcuts {
pub shortcuts: Vec<(String, String)>,
pub divider: char,
pub alignment: Alignment,
}
impl Shortcuts {
pub fn new(shortcuts: &[(&str, &str)], divider: char) -> Self {
pub fn new(shortcuts: &[(&str, &str)], divider: char, alignment: Alignment) -> Self {
Self {
shortcuts: shortcuts
.iter()
.map(|(s, a)| (s.to_string(), a.to_string()))
.collect(),
divider,
alignment,
}
}
}
@@ -1095,6 +1097,24 @@ impl Widget for Shortcuts {
.iter()
.map(|(width, _)| Constraint::Length(*width as u16))
.collect();
let (row, widths) = match self.alignment {
Alignment::Left => ([row.as_slice(), &[Text::from("")]].concat(), widths),
Alignment::Center => (
[&[Text::from("")], row.as_slice(), &[Text::from("")]].concat(),
[
&[Constraint::Fill(1)],
widths.as_slice(),
&[Constraint::Fill(1)],
]
.concat(),
),
Alignment::Right => (
[&[Text::from("")], row.as_slice()].concat(),
[&[Constraint::Fill(1)], widths.as_slice()].concat(),
),
};
let table = Table::new([Row::new(row)], widths).column_spacing(0);
frame.render_widget(table, area);