minor changes in scrolling

This commit is contained in:
Uber Veng
2026-05-25 20:17:31 +07:00
parent 25ccff3a37
commit b438cbd6b4
3 changed files with 239 additions and 181 deletions

226
src/ui.rs
View File

@@ -28,40 +28,115 @@ pub fn render(f: &mut Frame, app: &mut App) {
}
}
fn render_menu(f: &mut Frame, app: &App, area: Rect) {
fn render_menu(f: &mut Frame, app: &mut App, area: Rect) {
const SCROLLOFF: usize = 1;
let items = app.current_items();
let list_items: Vec<ListItem> = items
let total = items.len();
let inner_w = area.width.saturating_sub(2) as usize;
let inner_h = area.height.saturating_sub(2) as usize;
let sel = if total > 0 { app.selected_index.min(total - 1) } else { 0 };
// Wrap each item title into visual lines
let item_lines: Vec<Vec<String>> = items.iter().map(|item| {
let prefix = match &item.kind {
crate::menu::MenuItemKind::Category { .. } => "📁 ",
crate::menu::MenuItemKind::Action { .. } => "",
};
wrap_to_lines(&format!("{}{}", prefix, item.title), inner_w)
}).collect();
// How many items fit starting from offset
let vis_from = |off: usize| -> usize {
let mut rows = 0usize;
let mut n = 0usize;
for lines in item_lines.get(off..).unwrap_or(&[]) {
let h = lines.len().max(1);
if rows + h > inner_h { break; }
rows += h;
n += 1;
}
n
};
// Clamp scroll offset for scrolloff margin
let offset = &mut app.menu_scroll_offset;
if total == 0 {
*offset = 0;
} else {
// Ensure sel is not before offset
if *offset > sel {
*offset = sel;
}
// Scroll up: sel must not be within top SCROLLOFF items
if sel < offset.saturating_add(SCROLLOFF) && *offset > 0 {
*offset = sel.saturating_sub(SCROLLOFF);
}
// Scroll down: sel must not be within bottom SCROLLOFF items
loop {
let vis = vis_from(*offset);
if vis == 0 { break; }
let trigger = offset.saturating_add(vis).saturating_sub(SCROLLOFF);
if sel >= trigger && *offset + vis < total {
*offset += 1;
} else {
break;
}
}
// Clamp to valid range
let max_off = total.saturating_sub(1);
*offset = (*offset).min(max_off);
}
let start = *offset;
let vis = vis_from(start);
let end = (start + vis).min(total);
let list_items: Vec<ListItem> = item_lines[start..end]
.iter()
.enumerate()
.map(|(i, item)| {
let prefix = match &item.kind {
crate::menu::MenuItemKind::Category { .. } => "📁 ",
crate::menu::MenuItemKind::Action { .. } => "",
};
let content = Line::from(Span::raw(format!("{}{}", prefix, item.title)));
if i == app.selected_index {
ListItem::new(content).style(Style::default().bg(Color::Blue))
.map(|(i, lines)| {
let abs = start + i;
let text: Vec<Line> = lines.iter().map(|l| Line::from(l.clone())).collect();
let li = ListItem::new(ratatui::text::Text::from(text));
if abs == sel {
li.style(Style::default().bg(Color::Blue).add_modifier(Modifier::BOLD))
} else {
ListItem::new(content)
li
}
})
.collect();
let list = List::new(list_items)
.block(
Block::default()
.borders(Borders::ALL)
.title(" Меню ")
.title(
Line::from(Span::styled(
format!(" v{} ", env!("CARGO_PKG_VERSION")),
Style::default().fg(Color::DarkGray),
))
.alignment(Alignment::Right),
),
)
.highlight_style(Style::default().add_modifier(Modifier::BOLD));
let list = List::new(list_items).block(
Block::default()
.borders(Borders::ALL)
.title(" Меню ")
.title(
Line::from(Span::styled(
format!(" v{} ", env!("CARGO_PKG_VERSION")),
Style::default().fg(Color::DarkGray),
))
.alignment(Alignment::Right),
),
);
f.render_widget(list, area);
if total > vis {
let max_off = total.saturating_sub(vis);
let scrollbar = Scrollbar::default()
.orientation(ScrollbarOrientation::VerticalRight)
.begin_symbol(Some(""))
.end_symbol(Some(""))
.thumb_symbol("");
let mut sb_state = ScrollbarState::new(max_off).position(start);
let sb_area = Rect {
x: area.x + area.width.saturating_sub(1),
y: area.y + 1,
width: 1,
height: area.height.saturating_sub(2),
};
f.render_stateful_widget(scrollbar, sb_area, &mut sb_state);
}
}
fn render_description(f: &mut Frame, app: &App, area: Rect) {
@@ -127,9 +202,7 @@ fn render_popup(f: &mut Frame, popup: &mut Popup) {
Popup::ExecutingStructured {
reply_tx: _,
script_pid: _,
kill_tx: _,
task_handles: _,
log_buffer,
current_command,
input_buffer,
@@ -137,6 +210,7 @@ fn render_popup(f: &mut Frame, popup: &mut Popup) {
form_cursor,
form_values,
log_scroll_pos,
log_scroll_x,
log_follow_bottom,
finished,
} => {
@@ -172,13 +246,20 @@ fn render_popup(f: &mut Frame, popup: &mut Popup) {
let start = pos;
let end = (start + visible).min(total);
let viewport_w = chunks[0].width.saturating_sub(2) as usize;
let max_content_w: usize = log_buffer
.iter()
.map(|l| ansi::parse_line(l).iter().map(|(_, s)| s.chars().count()).sum::<usize>())
.max()
.unwrap_or(0);
let max_scroll_x = max_content_w.saturating_sub(viewport_w);
*log_scroll_x = (*log_scroll_x).min(max_scroll_x);
let scroll_x = *log_scroll_x;
let log_items: Vec<ListItem> = log_buffer[start..end]
.iter()
.map(|line| {
let spans: Vec<Span> = ansi::parse_line(line)
.into_iter()
.map(|(style, text)| Span::styled(text, style))
.collect();
let spans: Vec<Span> = h_scroll(ansi::parse_line(line), scroll_x);
ListItem::new(Line::from(spans))
})
.collect();
@@ -247,6 +328,26 @@ fn render_popup(f: &mut Frame, popup: &mut Popup) {
f.render_stateful_widget(scrollbar, sb_area, &mut sb_state);
}
// ── Horizontal scrollbar ─────────────────────────────────────────
if max_content_w > viewport_w {
let mut h_state = ScrollbarState::new(max_scroll_x).position(scroll_x);
let hscroll_area = Rect {
x: chunks[0].x + 1,
y: chunks[0].y + chunks[0].height.saturating_sub(1),
width: chunks[0].width.saturating_sub(2),
height: 1,
};
f.render_stateful_widget(
Scrollbar::new(ScrollbarOrientation::HorizontalBottom)
.begin_symbol(Some(""))
.end_symbol(Some(""))
.thumb_symbol("")
.track_symbol(Some("")),
hscroll_area,
&mut h_state,
);
}
// ── Interactive command area ─────────────────────────────────────
if cmd_height > 0 {
if let Some(cmd) = current_command {
@@ -628,3 +729,66 @@ fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
)
.split(popup_layout[1])[1]
}
/// Wrap `text` into lines of at most `max_width` chars (word-wrap, hard-break on long words).
fn wrap_to_lines(text: &str, max_width: usize) -> Vec<String> {
if max_width == 0 || text.is_empty() {
return vec![text.to_string()];
}
let mut lines: Vec<String> = Vec::new();
let mut current = String::new();
for word in text.split_whitespace() {
let wlen = word.chars().count();
if current.is_empty() {
if wlen > max_width {
let chars: Vec<char> = word.chars().collect();
for chunk in chars.chunks(max_width) {
let s: String = chunk.iter().collect();
if s.chars().count() == max_width {
lines.push(s);
} else {
current = s;
}
}
} else {
current = word.to_string();
}
} else if current.chars().count() + 1 + wlen <= max_width {
current.push(' ');
current.push_str(word);
} else {
lines.push(std::mem::take(&mut current));
current = word.to_string();
}
}
if !current.is_empty() {
lines.push(current);
}
if lines.is_empty() {
lines.push(String::new());
}
lines
}
/// Skip `offset` display chars from the start of a parsed ANSI span list.
fn h_scroll(parsed: Vec<(ratatui::style::Style, String)>, offset: usize) -> Vec<Span<'static>> {
let mut skip = offset;
let mut result = Vec::new();
for (style, text) in parsed {
if skip == 0 {
result.push(Span::styled(text, style));
} else {
let len = text.chars().count();
if skip >= len {
skip -= len;
} else {
let trimmed: String = text.chars().skip(skip).collect();
skip = 0;
if !trimmed.is_empty() {
result.push(Span::styled(trimmed, style));
}
}
}
}
result
}