fw sync: F-P4: PrintPanel/PrintDialog/HostedPrint printer_search opt-in (Search printers field, No printers match., Save as PDF kept, 430 pt list); Select search_label, no_matches, search_kept, list_width; trials printer_picker_filters_a_large_fleet_without_changing_the_selection, printer_picker_keeps_save_as_pdf_available_when_the_filter_matches_nothing, printer_picker_scrolls_long_names_without_losing_the_search_or_pdf_choice

This commit is contained in:
Dylan M. Taylor
2026-11-23 08:27:17 -05:00
parent 10c2dc7ced
commit 874efb4576
5 changed files with 211 additions and 10 deletions
+14
View File
@@ -733,6 +733,9 @@ pub struct PrintDialog<'a> {
/// The pager under the sheet preview in the `Document` layout.
#[prop]
preview_pager: Option<PanePager>,
/// The printer list searches a long fleet ([`PrintPanel::printer_search`]).
#[prop]
printer_search: bool,
/// Makes Print keep the dialog open and report its press in `PrintDialogOutput::held`.
#[prop(vis = "pub(crate)")]
hold_ok: bool,
@@ -801,6 +804,7 @@ impl<'a> PrintDialog<'a> {
driver: DriverControls::None,
duplex_by_layout: false,
preview_pager: None,
printer_search: false,
hold_ok: false,
finish_held: false,
refusal: None,
@@ -826,6 +830,7 @@ impl<'a> PrintDialog<'a> {
driver,
duplex_by_layout,
preview_pager,
printer_search,
hold_ok,
finish_held,
refusal,
@@ -883,6 +888,7 @@ impl<'a> PrintDialog<'a> {
.destinations(destinations)
.duplex_by_layout(duplex_by_layout)
.inline(document)
.printer_search(printer_search)
.show(ui);
refresh |= panel.refresh;
settings_clicked |= panel.settings_clicked;
@@ -1005,6 +1011,7 @@ pub struct HostedPrint {
driver: DriverControls,
duplex_by_layout: bool,
preview_pager: Option<PanePager>,
printer_search: bool,
/// The destination the user picked, or automation set, not taken yet.
printer_chosen: Option<Destination>,
/// Whether Print files its answer for the app instead of closing.
@@ -1038,6 +1045,7 @@ impl HostedPrint {
driver: DriverControls::None,
duplex_by_layout: false,
preview_pager: None,
printer_search: false,
printer_chosen: None,
hold: false,
held: HeldAnswer::new(),
@@ -1146,6 +1154,11 @@ impl HostedPrint {
self.preview_pager = pager;
}
/// Whether the printer list searches a long fleet ([`PrintPanel::printer_search`]).
pub fn set_printer_search(&mut self, on: bool) {
self.printer_search = on;
}
/// The pager press since the last call.
pub fn take_preview_step(&mut self) -> Option<PagerStep> {
self.requests.take_preview_step()
@@ -1248,6 +1261,7 @@ impl StackDialog for HostedPrint {
.layout(self.layout)
.driver(self.driver)
.duplex_by_layout(self.duplex_by_layout)
.printer_search(self.printer_search)
.hold_ok(self.hold)
.finish_held(finish_held);
if let Some(pager) = self.preview_pager.clone() {
+26 -4
View File
@@ -42,6 +42,8 @@ craft_ui::words! {
save_as_pdf: "printPanel.saveAsPdf" = "Save as PDF";
default_mark: "printPanel.defaultMark" = "(default)";
finding: "printPanel.finding" = "Finding printers…";
search_printers: "printPanel.searchPrinters" = "Search printers";
no_printers_match: "printPanel.noPrintersMatch" = "No printers match.";
}
}
@@ -138,8 +140,16 @@ pub struct PrintPanel<'a> {
/// Why the driver gave no page.
#[prop]
page_error: Option<String>,
/// The printer list is for a long fleet: a "Search printers" field heads it, focused as it opens, and narrows the
/// printers by their names (case and accents ignored) while Save as PDF stays at the end; typing changes no choice,
/// only a pick does, and the open list is wide enough for long names (PRINTER_LIST_WIDTH, within the screen).
#[prop]
printer_search: bool,
}
/// How wide a searchable printer list opens, in points: room for a long network queue's name.
pub const PRINTER_LIST_WIDTH: f32 = 430.0;
impl<'a> PrintPanel<'a> {
/// The panel over `settings`, offering `printers`.
pub fn new(settings: Bind<'a, Settings>, printers: &'a [Printer]) -> Self {
@@ -154,12 +164,14 @@ impl<'a> PrintPanel<'a> {
inline: false,
page: None,
page_error: None,
printer_search: false,
}
}
/// Shows the panel.
pub fn show(self, ui: &mut Ui) -> PrintPanelOutput {
let PrintPanel { settings, printers, discovering, busy, driver, destinations, duplex_by_layout, inline, page, page_error } = self;
let PrintPanel { settings, printers, discovering, busy, driver, destinations, duplex_by_layout, inline, page, page_error, printer_search } =
self;
let env = Env::of(ui);
let words = env.words::<PrintWords>();
let mut queues: Vec<Entry<Destination>> = printers
@@ -193,10 +205,20 @@ impl<'a> PrintPanel<'a> {
let mut now = before.clone();
Field::new(words.printer.clone()).id_salt("printer").show(ui, |ui, _| {
Row::new().show(ui, |ui, _| {
let picked = Select::new(words.printer.clone(), Bind::given(now.destination.clone()), &queues)
let select = Select::new(words.printer.clone(), Bind::given(now.destination.clone()), &queues)
.placeholder(placeholder.clone())
.id_salt("queue")
.show(ui);
.id_salt("queue");
let select = if printer_search {
select
.searchable()
.search_label(words.search_printers.clone())
.no_matches(words.no_printers_match.clone())
.search_kept(|destination: &Destination| *destination == Destination::File)
.list_width(PRINTER_LIST_WIDTH)
} else {
select
};
let picked = select.show(ui);
if let Some(destination) = picked.value() {
if *destination != now.destination {
printer_chosen = Some(destination.clone());
+100
View File
@@ -53,3 +53,103 @@ fn a_printer_with_its_own_dialog_disables_the_job_controls() {
pick(&mut fixture, "Browser", "Office");
assert_eq!(disabled(&fixture), [false; 4], "a printer without its own dialog enables them again");
}
/// The long last queue of the trial fleet.
const LAST: &str = "München Satellite 119 - Engineering floor 4 (network queue) - colour duplex device, Building North";
/// A searchable panel over a fleet of 120 queues, Save as PDF after them, the first queue the default and chosen; the
/// settings it edits.
fn fleet(room: Vec2) -> (Fixture, Rc<RefCell<Settings>>) {
use craft_print::ui::Destinations;
let settings = Rc::new(RefCell::new(Settings::default()));
let first = "Office 000 - Engineering floor 4 (network queue)";
settings.borrow_mut().destination = Destination::Printer(first.into());
let shown = Rc::clone(&settings);
let mut printers: Vec<Printer> =
(0..119).map(|i| Printer::new(format!("Office {i:03} - Engineering floor 4 (network queue)")).is_default(i == 0)).collect();
printers.push(Printer::new(LAST));
let mut fixture = Fixture::new(move |ui| {
let _ = PrintPanel::new(Bind::bound(&mut *shown.borrow_mut()), &printers)
.destinations(Destinations::PrintersAndFile)
.printer_search(true)
.show(ui);
});
fixture.room(room);
fixture.settle();
(fixture, settings)
}
/// Opens the printer list, which shows `value`.
fn open_list(fixture: &mut Fixture, value: &str) {
let list =
fixture.by_role(Role::COMBO_BOX).into_iter().find(|list| fixture.tree().value_of(list.node).is_some_and(|shown| shown.starts_with(value)));
let list = list.unwrap_or_else(|| panic!("the printer list shows {value}"));
fixture.click(&list).idle(3);
}
/// Searching folds the names and changes no choice; a pick chooses and closes the list, and the list opens again on
/// the whole fleet (upstream PdfCraft printer_picker_filters_a_large_fleet_without_changing_the_selection).
#[trial]
fn printer_picker_filters_a_large_fleet_without_changing_the_selection() {
let (mut fixture, settings) = fleet(Vec2::new(1400.0, 900.0));
let initial = settings.borrow().destination.clone();
open_list(&mut fixture, "Office 000");
let search = fixture.by_label("Search printers").expect("the search field heads the list");
assert!(search.rect.width() > 350.0, "room for long device names: {:?}", search.rect);
fixture.type_text(" MÜNCHEN satellite ").idle(3);
assert_eq!(settings.borrow().destination, initial, "typing is not a printer choice");
assert!(fixture.by_label("Office 001 - Engineering floor 4 (network queue)").is_none(), "the search hides the other queues");
let last = fixture.by_label(LAST).expect("the search finds the queue");
fixture.click(&last).idle(3);
assert_eq!(settings.borrow().destination, Destination::Printer(LAST.into()), "the pick chooses the queue");
assert!(fixture.by_label("Search printers").is_none(), "choosing closes the list");
open_list(&mut fixture, "München");
let search = fixture.by_label("Search printers").expect("the list opens again");
assert!(fixture.tree().value_of(search.node).is_none_or(str::is_empty), "the search starts empty: the whole fleet shows again");
assert!(fixture.by_label("No printers match.").is_none());
}
/// Save as PDF stays a destination when no queue matches the search, and choosing it does not print
/// (upstream printer_picker_keeps_save_as_pdf_available_when_the_filter_matches_nothing).
#[trial]
fn printer_picker_keeps_save_as_pdf_available_when_the_filter_matches_nothing() {
let (mut fixture, settings) = fleet(Vec2::new(1400.0, 900.0));
let initial = settings.borrow().destination.clone();
open_list(&mut fixture, "Office 000");
let search = fixture.by_label("Search printers").expect("the search field heads the list");
fixture.click(&search).idle(2);
assert!(fixture.by_label("Search printers").is_some(), "a click in the search keeps the list open");
fixture.type_text("no-such-printer").idle(3);
assert!(fixture.by_label("No printers match.").is_some(), "the list says no queue matches");
assert_eq!(settings.borrow().destination, initial);
let pdf = fixture.by_label("Save as PDF").expect("Save as PDF stays in the list");
fixture.click(&pdf).idle(3);
assert_eq!(settings.borrow().destination, Destination::File, "the pick chooses Save as PDF");
assert!(fixture.by_label("Search printers").is_none(), "choosing closes the list");
}
/// In a smaller window the list keeps long names within the screen, the search and Save as PDF on it, and reopens
/// with the chosen queue in sight (upstream printer_picker_scrolls_long_names_without_losing_the_search_or_pdf_choice).
#[trial]
fn printer_picker_scrolls_long_names_without_losing_the_search_or_pdf_choice() {
let size = Vec2::new(1024.0, 768.0);
let (mut fixture, settings) = fleet(size);
open_list(&mut fixture, "Office 000");
fixture.type_text("münchen").idle(3);
let last = fixture.by_label(LAST).expect("the search finds the queue");
assert!(last.rect.left() >= 0.0 && last.rect.right() <= size.x, "the long name stays within the screen: {:?}", last.rect);
fixture.click(&last).idle(3);
assert_eq!(settings.borrow().destination, Destination::Printer(LAST.into()));
open_list(&mut fixture, "München");
let search = fixture.by_label("Search printers").expect("the search field heads the list").rect;
let row = fixture.by_label(LAST).expect("the chosen queue shows").rect;
assert!(search.top() >= 0.0 && row.top() >= search.bottom() && row.bottom() <= size.y, "the chosen queue {row:?} is in sight on reopening");
fixture.key(egui::Key::End, egui::Modifiers::NONE).idle(3);
let pdf = fixture.by_label("Save as PDF").expect("Save as PDF ends the list").rect;
assert!(pdf.bottom() <= size.y, "Save as PDF is on screen: {pdf:?}");
let pdf = fixture.by_label("Save as PDF").expect("Save as PDF ends the list");
fixture.click(&pdf).idle(3);
assert_eq!(settings.borrow().destination, Destination::File, "the PDF row is clickable with the whole fleet shown");
}
+17 -2
View File
@@ -3,7 +3,7 @@
use super::words::PickerWords;
use crate::contract::overlay::Popup;
use crate::contract::{Bind, BodyCx, Component, Cx, PopupHandle, Remember as _, Show as _, WordSetCx as _};
use crate::contract::{Bind, BodyCx, Component, Cx, PopupHandle, Props as _, Remember as _, Show as _, Sizing, WordSetCx as _};
use crate::runtime::env::Label;
use crate::runtime::focus::{self, FocusTarget};
use crate::text::TextField;
@@ -21,13 +21,28 @@ struct Query {
/// the query. The dropdown leaves the key focus on the picker; the field takes it once the layer has recorded the
/// picker as its opener, so typing reaches the field.
pub(super) fn show<C: Component>(ui: &mut Ui, body: &mut BodyCx<'_>, popup: PopupHandle, cx: &Cx<'_, C>, owner: &Label, list: Id) -> String {
show_named(ui, body, popup, cx, owner, (None, false), list)
}
/// [`show`] with the field named `named` when given, filling the popup's width with `fill`.
pub(super) fn show_named<C: Component>(
ui: &mut Ui,
body: &mut BodyCx<'_>,
popup: PopupHandle,
cx: &Cx<'_, C>,
owner: &Label,
(named, fill): (Option<&Label>, bool),
list: Id,
) -> String {
let words = cx.words::<PickerWords>();
let opening = Popup::opening(popup, cx);
let typed = body.memory::<Query>();
if opening {
*typed = Query::default();
}
let field = TextField::search(name(&words, owner), Bind::bound(&mut typed.text)).drives(list).show(ui);
let label = named.cloned().unwrap_or_else(|| name(&words, owner));
let field = TextField::search(label, Bind::bound(&mut typed.text)).drives(list);
let field = if fill { field.sizing(Sizing::FillWidth) } else { field }.show(ui);
let query = typed.text.clone();
let taken = &mut body.memory::<Query>().focused;
if field.response.has_focus() {
+54 -4
View File
@@ -16,6 +16,7 @@ use crate::contract::{
MeasureCx, Numeric, Overlay, OverlayUi, Overlays as _, PaintUi, PartsCx, Pointer as _, PopupHandle, PopupKind, Props as _, Role, Show as _,
ShowCollection as _, ShowContainer as _, Sizing, WordSetCx as _, step_stop, step_stops,
};
use crate::display::{Shade, Text};
use crate::layout_components::Row;
use crate::list::{List, ListRow, ThumbAspect};
use crate::menu::Menu;
@@ -25,6 +26,7 @@ use crate::runtime::focus::FocusMove;
use crate::runtime::overlay::CloseReason;
use crate::style::{ComponentStyle, Ink, StyleCx, SurfaceLook, TextLook, TypeRole};
use craft_behavior::nav::{StepResult, ValueAxes, ValueSteps};
use craft_behavior::overlay::MinWidth;
use craft_behavior::{
CancelReason, GesturePhase, HitLevel, HitPriority, IconId, Metrics, NavMove, PartKey, Selection, Sense, TypeAhead, Value, WheelPhase, wheel_step,
};
@@ -59,6 +61,17 @@ pub struct SelectStyle {
/// The icon of the button in the popup header that opens the header menu.
const HEADER_MENU_ICON: &str = "more";
/// The options a searchable select keeps whatever the query: a destination of its own beside the searched ones (Save
/// as PDF among the printers).
struct SearchKept<'a, T>(Box<dyn Fn(&T) -> bool + 'a>);
impl<'a, T> SearchKept<'a, T> {
/// Keeps the options `kept` answers true for.
fn new(kept: impl Fn(&T) -> bool + 'a) -> Self {
SearchKept(Box::new(kept))
}
}
/// The rows of the menu a select's popup header opens: the host's menu body.
struct HeaderMenu<'a>(Box<HeaderRows<'a>>);
@@ -94,6 +107,18 @@ pub struct Select<'a, T: Value + 'static> {
/// A filter field above the entries narrows them as the user types.
#[prop(flag)]
searchable: bool,
/// The search field's name; "{label} Search" without it.
#[prop(into)]
search_label: Option<Label>,
/// Shown above the list while the query matches none of the searched options; the list's "No matches" without it.
#[prop(into)]
no_matches: Option<Label>,
/// Options the search never hides, shown after the matches.
#[prop(func = "Fn(&T) -> bool")]
search_kept: Option<SearchKept<'a, T>>,
/// The open list is at least this wide, in points, within the screen; the field's width without it.
#[prop(quantity = "width of the open list, in points")]
list_width: Option<f32>,
/// Shown, muted, while the value is none of the entries.
#[prop(into)]
placeholder: Option<Label>,
@@ -123,6 +148,10 @@ impl<'a, T: Value + 'static> Select<'a, T> {
value,
entries,
searchable: false,
search_label: None,
no_matches: None,
search_kept: None,
list_width: None,
placeholder: None,
decorate: None,
picture: None,
@@ -314,14 +343,24 @@ impl<T: Value + 'static> Component for Select<'_, T> {
let words = cx.words::<PickerWords>();
let held = self.position(cx.value());
let (searchable, dress, picture) = (self.searchable, self.decorate.as_ref(), self.picture.as_ref());
let kept =
|at: usize| self.search_kept.as_ref().is_some_and(|kept| entries.get(at).and_then(Entry::value).is_some_and(|value| (kept.0)(value)));
let (search_label, no_matches, wide) = (self.search_label.clone(), self.no_matches.clone(), self.list_width.is_some());
let folder = cx.env.folder();
let mut header_menu = self.header_menu.as_mut();
let list = cx.id.with("options");
let events = ui.show(popup, Overlay::dropdown().list(list), |ui, body| {
let overlay = Overlay::dropdown().list(list);
let overlay = match self.list_width {
Some(width) if width.is_finite() && width > 0.0 => overlay.min_width(MinWidth::Points(width)),
_ => overlay,
};
let events = ui.show(popup, overlay, |ui, body| {
let mut query = String::new();
if searchable || header_menu.is_some() {
let _ = Row::new().show(ui, |ui, _| {
let row = if wide { Row::new().fill_width() } else { Row::new() };
let _ = row.show(ui, |ui, _| {
if searchable {
query = search::show(ui, body, popup, cx, &name, list);
query = search::show_named(ui, body, popup, cx, &name, (search_label.as_ref(), wide), list);
}
if let Some(rows) = header_menu.as_mut() {
let more = IconButton::new(IconId::new(HEADER_MENU_ICON)).popup(PopupKind::Menu).label(words.header_menu.clone()).show(ui);
@@ -334,6 +373,13 @@ impl<T: Value + 'static> Component for Select<'_, T> {
});
}
let rows = EntryRows::new(entries, &labels);
// A query that matches none of the searched options says so above the kept ones.
let matcher = craft_i18n::matcher::Matcher::new(&folder, query.trim());
let none_match = !matcher.is_empty()
&& !(0..entries.len()).filter(|&at| entries[at].is_item() && !kept(at)).any(|at| matcher.score(labels[at].as_str()).is_some());
if let (true, Some(text)) = (none_match, no_matches.as_ref()) {
Text::new(text.clone()).shade(Shade::Muted).show(ui);
}
let selection = held.filter(|_| query.trim().is_empty()).map_or_else(Selection::default, Selection::single);
// While a query narrows the list the lead starts nowhere, so Down lands on the first match and Enter picks it.
let lead = if query.trim().is_empty() { LeadOnOpen::First } else { LeadOnOpen::Selected };
@@ -342,7 +388,11 @@ impl<T: Value + 'static> Component for Select<'_, T> {
.empty(words.no_matches.clone())
.selection(Bind::given(selection))
.lead_on_open(lead)
.filter(&query, |at| vec![labels[*at].clone()])
// A kept option matches any query by carrying the query among its texts.
.filter(
&query,
|at| if kept(*at) { vec![labels[*at].clone(), Label::from(query.trim().to_owned())] } else { vec![labels[*at].clone()] },
)
.show_items(ui, &rows, &mut |at, _| rows.row(*at, held, dress, picture))
.events
});