mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 00:34:25 +00:00
Text editor runs in a child that takes no place in the flow
The editor of a text field ran in a scope of the parent Ui, which moved the parent's cursor back to the end of the text part after the root had taken its place: a member after a search field started inside the field's clear button.
This commit is contained in:
@@ -124,7 +124,10 @@ pub(crate) fn show(ui: &mut Ui, edit: Edit<'_>) -> Shown {
|
||||
ui.fonts_mut(|fonts| crate::runtime::egui::text_layout::layout_with(fonts, job))
|
||||
};
|
||||
let hint_text = hint.map(|hint| egui::RichText::new(hint).font(FontId::proportional(font_id.size)));
|
||||
let output = ui.scope_builder(egui::UiBuilder::new().max_rect(rect), |ui| {
|
||||
// The component's root took its place in the flow already: the editor runs in a child that takes none, so the
|
||||
// parent's cursor stays past the root, not past the text part inside it.
|
||||
let output = {
|
||||
let ui = &mut super::alloc::child(ui, egui::UiBuilder::new().max_rect(rect));
|
||||
// The text shows only inside the text part, however wide the line is.
|
||||
ui.set_clip_rect(rect.intersect(ui.clip_rect()));
|
||||
if !enabled && !focusable {
|
||||
@@ -179,8 +182,7 @@ pub(crate) fn show(ui: &mut Ui, edit: Edit<'_>) -> Shown {
|
||||
});
|
||||
}
|
||||
shown
|
||||
});
|
||||
let output = output.inner;
|
||||
};
|
||||
let focused = output.response.has_focus();
|
||||
let caret = output.cursor_range.map(|range| {
|
||||
let at = output.galley.pos_from_cursor(range.primary);
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
//! `Bar`: a body too wide for the bar shrinks its members before it scrolls, a captioned field among them too.
|
||||
|
||||
use craft_ui::bars::{Bar, BarKind};
|
||||
use craft_ui::buttons::Button;
|
||||
use craft_ui::contract::{Bind, Entry, Show as _, ShowContainer as _};
|
||||
use craft_ui::bars::{Bar, BarKind, BreadcrumbBar, path_crumbs};
|
||||
use craft_ui::buttons::{Button, IconButton};
|
||||
use craft_ui::contract::{Bind, Entry, Props as _, Show as _, ShowContainer as _};
|
||||
use craft_ui::display::Text;
|
||||
use craft_ui::form::Field;
|
||||
use craft_ui::pickers::Select;
|
||||
use craft_ui::testing::{Fixture, shrink_least_runs, trial};
|
||||
use craft_ui::text::TextField;
|
||||
use craft_behavior::IconId;
|
||||
use egui::{Rect, Vec2};
|
||||
use std::cell::Cell;
|
||||
use std::rc::Rc;
|
||||
@@ -88,3 +91,32 @@ fn a_captioned_select_in_a_narrow_bar_narrows_with_its_caption_beside_it_before_
|
||||
let crowded = captioned(300.0, 6);
|
||||
assert!(crowded.last.max.x > 300.0, "six buttons fit a 300-point bar: {:?}", crowded.last);
|
||||
}
|
||||
|
||||
/// The rects of an up button, a path, a search field and a count, in that order, in a tool bar `width` wide.
|
||||
fn header(width: f32) -> Vec<Rect> {
|
||||
let seen = Rc::new(std::cell::RefCell::new(Vec::new()));
|
||||
let out = Rc::clone(&seen);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let crumbs = path_crumbs("Project");
|
||||
let mut search = String::new();
|
||||
Bar::new("Header", BarKind::Tool).show(ui, |ui, _| {
|
||||
let up = IconButton::new(IconId::new("chevron_up")).label("Up").show(ui).rect;
|
||||
let path = BreadcrumbBar::new("Path", &crumbs).show(ui).response.rect;
|
||||
let field = TextField::search("Search", Bind::bound(&mut search)).show(ui).response.rect;
|
||||
let count = Text::new("1 of 9 items selected").show(ui).rect;
|
||||
*out.borrow_mut() = vec![up, path, field, count];
|
||||
});
|
||||
});
|
||||
fixture.room(Vec2::new(width, 80.0)).settle();
|
||||
seen.borrow().clone()
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn the_members_of_a_tool_bar_never_overlap_as_it_narrows() {
|
||||
for width in [900.0, 342.0, 260.0] {
|
||||
let rects = header(width);
|
||||
for pair in rects.windows(2) {
|
||||
assert!(pair[0].max.x <= pair[1].min.x + SLACK, "at {width} points {:?} runs into {:?}", pair[0], pair[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user