Fix menu overflow in open dialog, menu scrolling

This commit is contained in:
Chris 2024-10-05 17:08:43 +02:00
parent eb09b4698f
commit 923aed5cbe
2 changed files with 15 additions and 4 deletions

View File

@ -503,12 +503,13 @@ class Editor
foreach ($files as $file) { foreach ($files as $file) {
$items[$file] = sprintf( $items[$file] = sprintf(
"%-30s %10s %20s", "%-30s %10s %20s",
is_dir($file) ? ("<".basename($file).">") : (basename($file)), mb_substr(is_dir($file) ? ("<".basename($file).">") : (basename($file)), 0, 30),
is_dir($file) ? "<DIR>" : filesize($file), is_dir($file) ? "<DIR>" : filesize($file),
date("Y-m-d H:i:s", filemtime($file)) date("Y-m-d H:i:s", filemtime($file))
); );
} }
$menu = new Menu($this->term, $items, $wd); $menu = new Menu($this->term, $items, $wd);
$this->redrawInfoBar([ '^C' => "Cancel", '↑↓' => "Navigate", 'Enter' => "Select/Open" ]);
$sel = $menu->display(0, 0, 70, 20, null); $sel = $menu->display(0, 0, 70, 20, null);
if ($sel === null) { if ($sel === null) {
$this->redrawEditor(); $this->redrawEditor();

View File

@ -70,6 +70,7 @@ class Menu
} elseif ($ch == "k{DOWN}") { } elseif ($ch == "k{DOWN}") {
$this->index++; $this->index++;
if ($this->index >= count($this->items)) $this->index = 0; if ($this->index >= count($this->items)) $this->index = 0;
$this->redraw($left, $top, $width, $height); $this->redraw($left, $top, $width, $height);
} }
} }
@ -83,6 +84,14 @@ class Menu
$visibleItems = $height - 2; $visibleItems = $height - 2;
// scroll if index is out of view
if ($this->index < $this->scroll) {
$this->scroll = $this->index;
}
if ($this->index - $visibleItems >= $this->scroll) {
$this->scroll = $this->index - $visibleItems + 1;
}
$scrollTop = $this->scroll; $scrollTop = $this->scroll;
$scrollVisible = $visibleItems / count($this->items); // / $visibleItems; $scrollVisible = $visibleItems / count($this->items); // / $visibleItems;
$scrollBottom = $scrollTop + $scrollVisible; $scrollBottom = $scrollTop + $scrollVisible;
@ -103,7 +112,7 @@ class Menu
; ;
$keys = array_keys($this->items); $keys = array_keys($this->items);
for ($n = 0; $n < $visibleItems; $n++) { for ($n = 0; $n < $visibleItems; $n++) {
$key = $keys[$n]??null; $key = $keys[$n + $this->scroll]??null;
$item = ($key ? ($this->items[$key]) : null); $item = ($key ? ($this->items[$key]) : null);
if ($item === "---") { if ($item === "---") {
$item = "\e[2m".str_repeat("\u{2500}", $width - 2)."\e[22m"; $item = "\e[2m".str_repeat("\u{2500}", $width - 2)."\e[22m";
@ -111,8 +120,9 @@ class Menu
$item = str_repeat(" ", $width - 2); $item = str_repeat(" ", $width - 2);
} else { } else {
$item = mb_substr($item, 0, $width); $item = mb_substr($item, 0, $width);
$item = " " . $item . str_repeat(" ", $width - 3 - $this->itemlen($item)) . "\e[40;37m"; $padlen = $width - 3 - $this->itemlen($item);
$item = (($n == $this->index)?"\e[37;44m":"\e[40;37m") . $item; $item = " " . $item . ($padlen>0?str_repeat(" ", $padlen):"") . "\e[40;37m";
$item = (($n + $this->scroll == $this->index)?"\e[37;44m":"\e[40;37m") . $item;
} }
if ($n >= $thumbTop && $n <= $thumbBottom) { if ($n >= $thumbTop && $n <= $thumbBottom) {
$scrollbar = "\e[97;1m".self::U_EDGE_VERTICAL_THUMB."\e[40;37;22m"; $scrollbar = "\e[97;1m".self::U_EDGE_VERTICAL_THUMB."\e[40;37;22m";