Keep the in-cell editor the cell's width while the text fits (#2336)

This commit is contained in:
MarekSpodBiedry
2026-10-23 17:56:16 +02:00
parent ba98039988
commit 8f2a60d176
+15 -1
View File
@@ -3094,6 +3094,12 @@ fn editor_slack(size: f32) -> f32 {
14.0 + size
}
/// Width of the one-line in-cell editor. While the text fits the cell (with the usual 14 px margin) the
/// editor is exactly the cell wide, as in Excel; once it overflows, the extra `editor_slack` applies.
fn editor_width(text_w: f32, cell_w: f32, size: f32) -> f32 {
if text_w + 14.0 <= cell_w { cell_w } else { text_w + editor_slack(size) }
}
fn in_cell_editor(app: &mut SheetApp, ui: &mut egui::Ui, geo: &Geo, sh: &Sheet, wb: &Workbook) {
let Some(ed) = app.editor.as_ref() else { return };
if ed.sheet != wb.active_sheet {
@@ -3120,7 +3126,7 @@ fn in_cell_editor(app: &mut SheetApp, ui: &mut egui::Ui, geo: &Geo, sh: &Sheet,
let text_w = ui.painter().layout_no_wrap(ed.text.clone(), font.clone(), Color32::BLACK).size().x;
// Excel keeps the cell's height for one line and only grows for extra lines.
let h = if lines > 1.0 { (size * 1.3 * lines + 4.0).max(rect.height()) } else { rect.height() };
((text_w + editor_slack(size)).max(rect.width()).min(geo.cells.right() - rect.left()), h)
(editor_width(text_w, rect.width(), size).min(geo.cells.right() - rect.left()), h)
};
// Grows to the left on a right-to-left sheet.
let erect = geo.mirror_rect(Rect::from_min_size(rect.min, vec2(w, h)));
@@ -4009,6 +4015,14 @@ pub(crate) fn heading_font_size(zoom: f32) -> f32 {
#[cfg(test)]
mod tests {
#[test]
fn editor_matches_the_cell_while_the_text_fits() {
use super::editor_width;
assert_eq!(editor_width(10.0, 80.0, 7.0), 80.0);
assert_eq!(editor_width(66.0, 80.0, 7.0), 80.0);
assert_eq!(editor_width(67.0, 80.0, 7.0), 88.0);
}
#[test]
fn editor_slack_fits_one_more_glyph_at_high_zoom() {
use super::editor_slack;