Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

A long prompt shows more of itself in a tall window (#734)

BFlat59a030ac7e42
diff --git a/crates/ui-egui/src/prompt_box.rs b/crates/ui-egui/src/prompt_box.rs
index 03f29d77..603ce933 100644
--- a/crates/ui-egui/src/prompt_box.rs
+++ b/crates/ui-egui/src/prompt_box.rs
@@ -221,7 +221,8 @@ pub fn show(ui: &mut Ui, text: &mut String, attachments: &[PathBuf], b: PromptBo
// No "more below" fade: the frame after each keystroke the scroll area briefly
// measures a few points of overflow, and the fade blinked over the line being typed.
ui.spacing_mut().scroll.fade.strength = 0.0;
- egui::ScrollArea::vertical().id_salt(id.with("scroll")).max_height(line * b.rows.1 as f32 + 4.0).stick_to_bottom(true).show(ui, |ui| {
+ let tallest = tallest_text(line, b.rows.1, ui.ctx().content_rect().height());
+ egui::ScrollArea::vertical().id_salt(id.with("scroll")).max_height(tallest + 4.0).stick_to_bottom(true).show(ui, |ui| {
let r = ui.add(
egui::TextEdit::multiline(text)
.id(id)
@@ -305,6 +306,12 @@ fn fit(size: egui::Vec2, max: egui::Vec2) -> egui::Vec2 {
size * (max.x / size.x).min(max.y / size.y)
}
+/// How tall the text may grow before it scrolls: the box's own limit of `rows` lines, or
+/// two-fifths of a taller window, so a long prompt shows more of itself there (#473).
+fn tallest_text(line: f32, rows: usize, window: f32) -> f32 {
+ (line * rows as f32).max(window * 0.4)
+}
+
/// What Tab would complete the box to, when it starts with a partial command name. Only the
/// name is replaced, so a question after it is kept; `None` when there is nothing to complete.
fn completed_command(text: &str) -> Option<String> {
@@ -388,6 +395,14 @@ pub fn with_attachments(text: &str, files: &[PathBuf], root: &std::path::Path) -
mod tests {
use super::*;
+ /// #473: eight lines at most, whatever the window, left most of a detailed prompt out of view.
+ #[test]
+ fn a_long_prompt_shows_more_of_itself_in_a_tall_window() {
+ assert_eq!(tallest_text(20.0, 8, 300.0), 160.0, "a small window keeps the box's own limit");
+ assert_eq!(tallest_text(20.0, 8, 1400.0), 560.0, "a tall one shows up to two-fifths of it");
+ assert_eq!(tallest_text(20.0, 30, 1000.0), 600.0, "a box allowed more keeps it");
+ }
+
#[test]
fn up_goes_back_through_prompts_and_down_returns_to_what_was_being_typed() {
let list = [Prompt { text: "one".into(), ..Default::default() }, Prompt { text: "two".into(), ..Default::default() }];