Merge pull request #9438 from dotCooCoo/fix/script-console-footer-wrap

Keep Script Console actions inside narrow panels
This commit is contained in:
BFlat
2026-11-27 00:47:09 +07:00
committed by GitHub
2 changed files with 85 additions and 30 deletions
+44 -29
View File
@@ -17,14 +17,15 @@ use vectorcraft_engine::script::console::{Console, ConsoleLine, Ending, Entry};
use craft::behavior::{DisplayOrder, IconId, ItemSource};
use craft::keymap::CommandRef;
use craft::theme::variant::Tone;
use craft::ui::contract::{Bind, ScrollAnchor, Show, ShowCollection};
use craft::ui::buttons::Button;
use craft::ui::contract::{Bind, LeastWidth, Props as _, ScrollAnchor, Show, ShowCollection, ShowContainer, Sizing, clicked};
use craft::ui::layout::RowAlign;
use craft::ui::layout_components::{Row, Stack};
use craft::ui::list::{List, ListRow, RowInk};
use craft::ui::menu::PanelRow;
use craft::ui::notices::Notice;
use craft::ui::primitives::IconSpec;
use craft::ui::runtime::commands::{CommandState, CommandStates};
use craft::ui::sections::FooterBar;
use craft::ui::sections::{DialogRole, FooterButton};
use craft::ui::style::TypeRole;
use crate::VectorcraftApp;
@@ -165,11 +166,28 @@ pub(crate) fn menu_rows(_app: &VectorcraftApp) -> Vec<PanelRow> {
pub(crate) fn menu_states(app: &VectorcraftApp, states: &mut CommandStates) {
let any = !app.session.script_console().lines().is_empty();
let selected = selected_range(app).is_some();
for (id, title, on) in [(COMMANDS[0], tl!("Clear"), any), (COMMANDS[1], tl!("Copy Selected"), selected), (COMMANDS[2], tl!("Copy All"), any)] {
for (id, title, on) in buttons(any, selected) {
states.set(CommandRef::new(id), CommandState::new().enabled(on).title(title));
}
}
fn buttons(any: bool, selected: bool) -> [(&'static str, &'static str, bool); 3] {
[(COMMANDS[0], tl!("Clear"), any), (COMMANDS[1], tl!("Copy Selected"), selected), (COMMANDS[2], tl!("Copy All"), any)]
}
fn footer(ui: &mut Ui, buttons: [(&'static str, &'static str, bool); 3]) -> Option<&'static str> {
let mut picked = None;
Row::new().fill_width().align(RowAlign::End).wrap(true).show(ui, |ui, _| {
for (id, title, on) in buttons {
let button = Button::new(title).id_salt(id).least_width(LeastWidth::DialogButton).enabled(on);
if clicked(&button.show(ui)) {
picked = Some(id);
}
}
});
picked
}
/// What the panel's menu model depends on, for the menu cache's key.
pub(crate) fn key(app: &VectorcraftApp) -> u64 {
let c = app.session.script_console();
@@ -189,31 +207,28 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
selection.items.retain(held);
selection.lead = selection.lead.take().filter(held);
selection.anchor = selection.anchor.take().filter(held);
if let Some(n) = notice(c) {
Notice::new(Tone::Info, n).show(ui);
}
List::<u64>::new(tl!("Script Console")).scroll_anchor(ScrollAnchor::End).selection(Bind::bound(&mut app.ui.script_console_selection)).show_items(
ui,
&Lines { start, count },
&mut |key, icx| {
let line = key.checked_sub(start).and_then(|at| c.lines().get(at as usize));
let Some(line) = line else { return ListRow::new(String::new()) };
let row = ListRow::new(shown(line)).role(TypeRole::Mono).state(icx.row.clone());
match line.entry {
Entry::Error(_) => row.icon(IconSpec::svg(IconId::new("error"))).tone(Tone::Bad),
Entry::Start | Entry::End(_) | Entry::Report { .. } => row.ink(RowInk::Muted),
Entry::Output(_) | Entry::Log(_) => row,
}
},
);
let picked = FooterBar::buttons([
FooterButton::new(tl!("Clear"), DialogRole::Other).enabled(count > 0),
FooterButton::new(tl!("Copy Selected"), DialogRole::Other).enabled(selected_range(app).is_some()),
FooterButton::new(tl!("Copy All"), DialogRole::Other).enabled(count > 0),
])
.show_footer(ui)
.picked;
if let Some(id) = picked.and_then(|at| COMMANDS.get(at)) {
let buttons = buttons(count > 0, !selection.items.is_empty());
let mut picked = None;
Stack::new().fill().footer(|ui, _| picked = footer(ui, buttons).or(picked)).show(ui, |ui, _| {
if let Some(n) = notice(c) {
Notice::new(Tone::Info, n).show(ui);
}
List::<u64>::new(tl!("Script Console"))
.sizing(Sizing::Fill)
.scroll_anchor(ScrollAnchor::End)
.selection(Bind::bound(&mut *selection))
.show_items(ui, &Lines { start, count }, &mut |key, icx| {
let line = key.checked_sub(start).and_then(|at| c.lines().get(at as usize));
let Some(line) = line else { return ListRow::new(String::new()) };
let row = ListRow::new(shown(line)).role(TypeRole::Mono).state(icx.row.clone());
match line.entry {
Entry::Error(_) => row.icon(IconSpec::svg(IconId::new("error"))).tone(Tone::Bad),
Entry::Start | Entry::End(_) | Entry::Report { .. } => row.ink(RowInk::Muted),
Entry::Output(_) | Entry::Log(_) => row,
}
});
});
if let Some(id) = picked {
run_ui(app, id, json!({}));
}
}
@@ -8,7 +8,9 @@ use serde_json::json;
use vectorcraft_engine::Session;
use vectorcraft_engine::script::console::{Ending, Entry};
use super::script_console::{menu_states, shown};
use super::script_console::{menu_states, show, shown};
use crate::panel_docking::PANEL_MIN_WIDTH;
use crate::tests::panel_harness::{Panel, Place, ROOM, floated};
use crate::{Services, VectorcraftApp, script_runs};
fn app() -> VectorcraftApp {
@@ -37,6 +39,44 @@ fn enabled(app: &VectorcraftApp, id: &str) -> bool {
states.get(&CommandRef::new(id)).is_some_and(|s| s.is_enabled())
}
#[test]
fn the_console_footer_stays_inside_the_panel_at_minimum_width() {
let names = ["Clear", "Copy Selected", "Copy All"];
for lang in ["en", "fr", "de"] {
let mut app = app();
run(&mut app, "$.writeln('one')");
let panel = floated(app, "scriptConsole", lang, Place::Floating(PANEL_MIN_WIDTH), &names);
for name in names {
let bounds = panel.rects(name);
assert!(
bounds.iter().all(|rect| panel.body.expand(0.5).contains_rect(*rect)),
"{lang}: {name} at {bounds:?} is outside {:?}",
panel.body
);
}
}
let wide = floated(app(), "scriptConsole", "en", Place::Floating(380.0), &names);
assert!(wide.middles(&names).windows(2).all(|pair| (pair[0] - pair[1]).abs() < 1.0), "the wide footer uses one row");
}
#[test]
fn the_console_footer_buttons_run_their_commands_at_minimum_width() {
let mut app = app();
run(&mut app, "$.writeln('one'); $.writeln('two')");
let mut panel = Panel::new(app, show);
panel.room(egui::vec2(PANEL_MIN_WIDTH, ROOM.y)).frame();
assert!(!panel.enabled("Copy Selected"));
panel.click("#1 c.js: one");
panel.click("Copy Selected");
assert_eq!(panel.app().clipboard_out.take().as_deref(), Some("#1 c.js: one"));
panel.click("Copy All");
assert!(panel.app().clipboard_out.take().is_some_and(|text| text.contains("#1 c.js: two")));
panel.click("Clear");
assert!(panel.app().session.script_console().lines().is_empty());
assert!(!panel.enabled("Copy All"));
}
#[test]
fn clear_works_during_a_run_and_the_run_goes_on_writing() {
let mut app = app();