chrome: size a stacked title in columns, with a margin; no ellipsis
Fable's P2.1 on estilo #34. TitleEdit.Size counted runes, so a CJK or wide-glyph title painted cut short until chrome.js fitted it, and stayed cut short with scripts off (11 kanji: 121px against 206px; 20 Ws: 193px against 326px). It now counts columns: East Asian wide and fullwidth runes two (golang.org/x/text/width, already in the module graph, now a direct requirement), capitals one and a half, the rest one, then a quarter more. Checked in Chromium and WebKit with scripts off: Latin, kanji, 20 Ws, 20 is, Arabic, mixed CJK and Latin, and emoji titles all render at least as wide as they need. The stacked title field drops text-overflow: ellipsis (clip), so a short guess can never show one. The one-row bar keeps it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+66
−17
chrome/chrome.css+4 −2chrome/chrome.go+25 −7chrome/stack_test.go+33 −6go.mod+4 −2
diff --git a/chrome/chrome.css b/chrome/chrome.css| index 4fb95fb..2278520 100644 |
| --- a/chrome/chrome.css |
| +++ b/chrome/chrome.css |
| @@ -335,9 +335,11 @@ |
| /* The stacked bar's title is as wide as its title, never the bar's |
| room: its size attribute is the first paint's guess, chrome.js |
| measures the field and corrects it, and it is capped at the space it |
| - has, where a longer title scrolls inside the field as it always has. */ |
| + has, where a longer title scrolls inside the field as it always has. |
| + No ellipsis: the field is as wide as its title, so one would only ever |
| + mark a guess that came out short, not a title cut off. */ |
| [estilo-bar~="stacked"] [estilo-bar-title] { flex: none; max-inline-size: 100%; } |
| -[estilo-bar~="stacked"] [estilo-bar-title-input] { flex: 0 1 auto; inline-size: auto; min-inline-size: 0; max-inline-size: 100%; } |
| +[estilo-bar~="stacked"] [estilo-bar-title-input] { flex: 0 1 auto; inline-size: auto; min-inline-size: 0; max-inline-size: 100%; text-overflow: clip; } |
| [estilo-bar~="stacked"] [estilo-bar-context] { max-inline-size: 100%; } |
| [estilo-bar~="stacked"] [estilo-menubar-title] { cursor: pointer; } |
diff --git a/chrome/chrome.go b/chrome/chrome.go| index 360b6ff..4b92009 100644 |
| --- a/chrome/chrome.go |
| +++ b/chrome/chrome.go |
| @@ -26,7 +26,8 @@ import ( |
| "strings" |
| "time" |
| "unicode" |
| - "unicode/utf8" |
| + |
| + "golang.org/x/text/width" |
| "amadan.net/oficina/estilo.git/menu" |
| "amadan.net/oficina/estilo.git/sky" |
| @@ -83,13 +84,30 @@ type TitleEdit struct { |
| // Size is the title field's size attribute in a stacked bar |
| // (Bar.Menubar set), where the field is as wide as its title rather than |
| -// the bar's room: the title's length in characters, and never under 4, |
| -// so an empty or one-letter title still leaves something to click. |
| -// It is a first approximation, painted before any script runs; |
| -// chrome.js then measures the field and corrects its width (a few pixels |
| -// either way). Nothing after the title on its row moves when it does. |
| +// the bar's room. It is the first paint's guess, before any script runs, |
| +// and with scripts off the only width the field gets, so it errs wide: a |
| +// field too wide only puts its (unpainted) edge further out, and one too |
| +// narrow cuts the title off. A size unit is about one average Latin |
| +// character, so the title is counted in columns: an East Asian wide or |
| +// fullwidth character (CJK, kana, Hangul, most emoji) is two, a capital |
| +// one and a half (20 Ws need 1.7 each), anything else one; then a |
| +// quarter more, rounded up, and never under 4, so an empty or one-letter |
| +// title still leaves something to click. chrome.js then measures the |
| +// field and corrects its width; nothing after the title on its row moves |
| +// when it does. |
| func (t TitleEdit) Size() int { |
| - return max(4, utf8.RuneCountInString(t.Value)) |
| + halves := 0 // half-columns, so a capital's one and a half stays whole |
| + for _, r := range t.Value { |
| + switch k := width.LookupRune(r).Kind(); { |
| + case k == width.EastAsianWide || k == width.EastAsianFullwidth: |
| + halves += 4 |
| + case unicode.IsUpper(r): |
| + halves += 3 |
| + default: |
| + halves += 2 |
| + } |
| + } |
| + return max(4, (halves*5+7)/8) // halves/2 columns, times 5/4, rounded up |
| } |
| // Action is one page action. Its Item says what activating it does (a |
diff --git a/chrome/stack_test.go b/chrome/stack_test.go| index 865eff6..136b678 100644 |
| --- a/chrome/stack_test.go |
| +++ b/chrome/stack_test.go |
| @@ -3,6 +3,7 @@ package chrome |
| import ( |
| "html/template" |
| "regexp" |
| + "strconv" |
| "strings" |
| "testing" |
| @@ -189,10 +190,26 @@ func TestSlottedBarsWrapEachActionAsAControl(t *testing.T) { |
| } |
| } |
| -// The first paint's width for the title field: its length in characters |
| -// (not bytes), and never under 4. |
| -func TestTheTitleSizeIsItsLengthInCharacters(t *testing.T) { |
| - for value, want := range map[string]int{"": 4, "Q3": 4, "Untitled document": 17, "Café plans": 10, "日本語のタイトル": 8, "مستند بلا عنوان": 15} { |
| +// The first paint's width for the title field: a guess in columns, with |
| +// room to spare, since a guess too wide only leaves the field's edge |
| +// further out and a guess too narrow cuts the title off with scripts off. |
| +// A wide or fullwidth character (CJK, kana, Hangul, most emoji) takes two |
| +// columns and a capital one and a half, and the whole is a quarter wider |
| +// again; never under 4. Measured at ecb81fc, where Size counted runes: |
| +// 11 kanji painted 121px against the 206px they need, 20 capital Ws 193px |
| +// against 326px. |
| +func TestTheTitleSizeIsItsWidthInColumnsWithAMargin(t *testing.T) { |
| + for value, want := range map[string]int{ |
| + "": 4, |
| + "Q3": 4, |
| + "Untitled document": 22, // 1 capital + 16: 17.5 columns |
| + "Café plans": 14, // 1 capital + 9: 10.5 |
| + "日本語のタイトル": 20, // 8 wide: 16 |
| + "Wide": 10, // 4 fullwidth: 8 |
| + "Plans 🎉🎉🎉": 16, // 1 capital + 5 + 3 wide emoji: 12.5 |
| + "WWWWWWWWWWWWWWWWWWWW": 38, // 20 capitals: 30 |
| + "مستند بلا عنوان": 19, // 15: Arabic has no capitals |
| + } { |
| if got := (TitleEdit{Value: value}).Size(); got != want { |
| t.Errorf("Size(%q) = %d, want %d", value, got, want) |
| } |
| @@ -200,7 +217,7 @@ func TestTheTitleSizeIsItsLengthInCharacters(t *testing.T) { |
| b := stacked() |
| b.ContextEdit.Value = "Café plans" |
| html := render(t, "estilo-bar", b) |
| - if !strings.Contains(html, `<input estilo-bar-title-input name="title" value="Café plans" size="10" aria-label=`) { |
| + if !strings.Contains(html, `<input estilo-bar-title-input name="title" value="Café plans" size="14" aria-label=`) { |
| t.Errorf("the stacked title has no size from its length:\n%s", html) |
| } |
| // Only a stacked bar sizes its title to the text. |
| @@ -215,7 +232,7 @@ func TestTheFittedTitleIsTheTitlePlusASize(t *testing.T) { |
| edit := signedIn("x").ContextEdit |
| plain := render(t, "estilo-bar-title", edit) |
| fit := render(t, "estilo-bar-title-fit", edit) |
| - if got := strings.Replace(fit, ` size="7"`, "", 1); got != plain { |
| + if got := strings.Replace(fit, ` size="`+strconv.Itoa(edit.Size())+`"`, "", 1); got != plain { |
| t.Errorf("estilo-bar-title-fit is not estilo-bar-title plus size:\n%s\n%s", fit, plain) |
| } |
| } |
| @@ -289,3 +306,13 @@ func TestTheStackedBarPublishesItsHeight(t *testing.T) { |
| t.Error("the controls do not rise to 32px on a coarse pointer") |
| } |
| } |
| + |
| +// A stacked title's field is sized to its title, from a guess before the |
| +// script runs: an ellipsis there would say the title was cut short when |
| +// only the guess was. The one-row bar keeps its ellipsis. |
| +func TestAStackedTitleNeverShowsAnEllipsis(t *testing.T) { |
| + src := string(CSS()) |
| + if !regexp.MustCompile(`\n\[estilo-bar~="stacked"\] \[estilo-bar-title-input\] \{[^}]*text-overflow: clip;`).MatchString(src[strings.Index(src, barStackMarker):]) { |
| + t.Error("the stacked title field still ends a short guess with an ellipsis") |
| + } |
| +} |
diff --git a/go.mod b/go.mod| index 3896316..9af7908 100644 |
| --- a/go.mod |
| +++ b/go.mod |
| @@ -2,7 +2,10 @@ module amadan.net/oficina/estilo.git |
| go 1.25.0 |
| -require github.com/carlosframework/rastrillo v0.23.0 |
| +require ( |
| + github.com/carlosframework/rastrillo v0.23.0 |
| + golang.org/x/text v0.20.0 |
| +) |
| require ( |
| github.com/dustin/go-humanize v1.0.1 // indirect |
| @@ -13,7 +16,6 @@ require ( |
| github.com/ncruces/go-strftime v1.0.0 // indirect |
| github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect |
| golang.org/x/sys v0.46.0 // indirect |
| - golang.org/x/text v0.20.0 // indirect |
| gorm.io/gorm v1.31.2 // indirect |
| gorm.io/plugin/dbresolver v1.6.2 // indirect |
| modernc.org/libc v1.74.1 // indirect |