mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 05:37:04 +00:00
fw sync: CommentComposer::fixed_rows keeps the card one size
The composer's field grows with its text, but the card is measured before the text wraps, so a card near the bottom of the window was placed shorter than it laid out and clipped Post. fixed_rows (off by default) keeps the field at its three rows and scrolls the rest, so the card's buttons stay inside it. Trial: note_popup::a_composer_with_fixed_rows_keeps_post_inside_its_card_near_the_bottom.
This commit is contained in:
@@ -246,12 +246,13 @@ pub struct CommentComposer<'a> {
|
||||
hint: Option<Label>,
|
||||
kind: ComposerDraft,
|
||||
close: ComposerClose,
|
||||
fixed_rows: bool,
|
||||
}
|
||||
|
||||
impl<'a> CommentComposer<'a> {
|
||||
/// A composer at `anchor` (screen points; `None` hides it) that edits `draft`.
|
||||
pub fn new(anchor: Option<Pos2>, draft: &'a mut String) -> Self {
|
||||
CommentComposer { anchor, draft, header: None, hint: None, kind: ComposerDraft::New, close: ComposerClose::OnOutsidePress }
|
||||
CommentComposer { anchor, draft, header: None, hint: None, kind: ComposerDraft::New, close: ComposerClose::OnOutsidePress, fixed_rows: false }
|
||||
}
|
||||
|
||||
/// A header of `title` and the `author` above the field; the card is named `title`.
|
||||
@@ -260,6 +261,15 @@ impl<'a> CommentComposer<'a> {
|
||||
self
|
||||
}
|
||||
|
||||
/// The field keeps its three rows and scrolls longer text inside them (`TextField::max_lines`), so the card keeps
|
||||
/// one size. Off (the default), the field grows with its text, which the card's measure, taken before the text
|
||||
/// wraps, does not see: near the bottom of the window the card is placed shorter than it lays out and clips its
|
||||
/// buttons.
|
||||
pub fn fixed_rows(mut self) -> Self {
|
||||
self.fixed_rows = true;
|
||||
self
|
||||
}
|
||||
|
||||
/// What the field asks for, in place of "Add a comment": its name and its placeholder ("Text to insert" for an
|
||||
/// insertion).
|
||||
pub fn hint(mut self, hint: impl Into<Label>) -> Self {
|
||||
@@ -330,7 +340,7 @@ pub struct CommentComposerOutput {
|
||||
impl CommentComposer<'_> {
|
||||
/// Shows the composer and reports whether the user posted or closed it.
|
||||
pub fn show(self, ui: &mut Ui) -> CommentComposerOutput {
|
||||
let CommentComposer { anchor, draft, header, hint, kind, close } = self;
|
||||
let CommentComposer { anchor, draft, header, hint, kind, close, fixed_rows } = self;
|
||||
let mut output = CommentComposerOutput::default();
|
||||
let Some(anchor) = anchor else { return output };
|
||||
let words = Env::of(ui).words::<ThreadWords>();
|
||||
@@ -355,6 +365,7 @@ impl CommentComposer<'_> {
|
||||
}
|
||||
let hint = hint.clone().unwrap_or_else(|| words.add_comment.clone());
|
||||
let field = TextField::new(hint.clone(), Bind::bound(&mut *draft)).lines(COMPOSER_ROWS).placeholder(hint).submits().default_stop();
|
||||
let field = if fixed_rows { field.max_lines(COMPOSER_ROWS) } else { field };
|
||||
// An edit opens with the caret after the comment's text, so typing adds to it; a new comment's empty
|
||||
// field has nothing to replace either way.
|
||||
let field = if kind == ComposerDraft::New { field } else { field.keep_caret_on_open() }.show(ui);
|
||||
|
||||
@@ -339,3 +339,25 @@ fn an_edit_composer_opens_with_the_caret_after_the_text() {
|
||||
assert_eq!(draft.borrow().as_str(), want, "{kind:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// `CommentComposer::fixed_rows`: a card near the bottom of the window keeps its Post button inside it when its text
|
||||
/// runs past the field's three rows, and Post posts.
|
||||
#[trial]
|
||||
fn a_composer_with_fixed_rows_keeps_post_inside_its_card_near_the_bottom() {
|
||||
let posted = Rc::new(RefCell::new(None));
|
||||
let seen = Rc::clone(&posted);
|
||||
let mut draft = String::new();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let out = CommentComposer::new(Some(pos2(120.0, 470.0)), &mut draft).header("Text box", "Ada").fixed_rows().show(ui);
|
||||
if let Some(text) = out.posted {
|
||||
*seen.borrow_mut() = Some(text);
|
||||
}
|
||||
});
|
||||
fixture.room(vec2(800.0, 600.0)).settle();
|
||||
fixture.type_text(&"word ".repeat(20)).settle();
|
||||
let card = fixture.by_role(craft_ui::contract::Role::DIALOG).first().expect("the card").rect;
|
||||
let post = fixture.by_label("Post").expect("the Post button");
|
||||
assert!(card.contains_rect(post.rect), "Post {:?} lies inside the card {card:?}", post.rect);
|
||||
fixture.click(&post).settle();
|
||||
assert!(posted.borrow().as_deref().is_some_and(|text| text.starts_with("word word")), "Post posted: {:?}", posted.borrow());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user