Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

Text panel: tab widths follow the drawn label

Ikki Nishiharaa123da6d0da6
diff --git a/crates/ui-egui/src/panels/text.rs b/crates/ui-egui/src/panels/text.rs
index 4408779df..ba59f9a7e 100644
--- a/crates/ui-egui/src/panels/text.rs
+++ b/crates/ui-egui/src/panels/text.rs
@@ -16,6 +16,15 @@ use crate::theme::Tokens;
const TABS: [&str; 3] = ["Transcript", "Captions", "Graphics"];
+/// The width a tab label needs: 7 px per character, which the strip was laid out with for Latin
+/// text, or the label as drawn if that is wider (Japanese glyphs are about twice as wide, and a
+/// per-character estimate alone let a label run into the next tab). Measured in the semibold the
+/// active tab uses, so selecting a tab does not move its neighbours.
+fn tab_text_width(painter: &egui::Painter, label: &str) -> f32 {
+ let drawn = painter.layout_no_wrap(label.to_string(), Tokens::semibold(12.5), Color32::WHITE).size().x;
+ (label.chars().count() as f32 * 7.0).max(drawn)
+}
+
pub fn show(app: &mut FilmcraftApp, ui: &mut egui::Ui, rect: Rect) {
let t = app.tokens;
ui.painter().rect_filled(rect, 0.0, t.panel_bg);
@@ -23,7 +32,8 @@ pub fn show(app: &mut FilmcraftApp, ui: &mut egui::Ui, rect: Rect) {
let mut x = rect.min.x + 12.0;
for tab in TABS {
let shown = crate::i18n::t(tab);
- let w = shown.chars().count() as f32 * 7.0 + 16.0;
+ let text_w = tab_text_width(ui.painter(), shown);
+ let w = text_w + 16.0;
let r = Rect::from_min_size(pos2(x, rect.min.y + 4.0), vec2(w, 24.0));
let resp = ui.interact(r, egui::Id::new(("text-tab", tab)), Sense::click());
let active = app.ui.text_tab == tab;
@@ -35,7 +45,7 @@ pub fn show(app: &mut FilmcraftApp, ui: &mut egui::Ui, rect: Rect) {
if active { t.text } else { t.text_dim },
);
if active {
- ui.painter().line_segment([pos2(r.min.x, r.max.y), pos2(r.min.x + w - 16.0, r.max.y)], Stroke::new(2.0, t.text));
+ ui.painter().line_segment([pos2(r.min.x, r.max.y), pos2(r.min.x + text_w, r.max.y)], Stroke::new(2.0, t.text));
}
app.auto.add(&format!("text.tab.{tab}"), r, tab);
if resp.clicked() {
@@ -439,3 +449,37 @@ fn run(app: &mut FilmcraftApp, ui: &egui::Ui, actions: Vec<(String, Value)>) {
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ /// Latin labels keep the 7 px per character they were laid out with; Japanese labels are as
+ /// wide as they are drawn, so they no longer run into the next tab.
+ #[test]
+ fn tab_widths_follow_the_drawn_label() {
+ let ctx = egui::Context::default();
+ crate::theme::install(&ctx, &Tokens::for_kind(crate::theme::ThemeKind::default()));
+ // Japanese needs a craft-fonts build or an installed font; without one there is nothing wide to measure
+ if !crate::i18n::install_japanese_font(&ctx) {
+ return;
+ }
+ let labels = ["Transcript", "Captions", "Graphics", "文字起こし", "キャプション", "グラフィックス"];
+ let mut widths = Vec::new();
+ for _ in 0..2 {
+ // the added font is in place from the second pass
+ widths.clear();
+ let mut out = ctx.run_ui(egui::RawInput::default(), |ui| {
+ for label in labels {
+ widths.push(tab_text_width(ui.painter(), label));
+ }
+ });
+ out.textures_delta.clear();
+ }
+ assert_eq!(widths[..3], [70.0, 56.0, 56.0], "Latin tabs keep 7 px per character");
+ // 5, 6 and 7 Japanese characters of about 12.5 px each, far over the 7 px per character estimate
+ for ((width, chars), label) in widths[3..].iter().zip([5.0_f32, 6.0, 7.0]).zip(&labels[3..]) {
+ assert!(*width > chars * 7.0 * 1.5, "{label}: {width} px");
+ }
+ }
+}