From cb0e4f3465e01209c41acf6904e292be37864c72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=A7=E9=A9=8A?= Date: Wed, 11 Nov 2026 17:25:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=81=B4=E9=82=8A=E6=AC=84:=20=E9=87=8D?= =?UTF-8?q?=E6=96=B0=E5=95=9F=E5=8B=95=E4=BB=A5=E6=9B=B4=E6=96=B0=20stays?= =?UTF-8?q?=20inside=20the=20sidebar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - The button sized itself to its words, 「重新啟動以更新(2026-11-11 17:23)」, which ran past the sidebar's right edge (more so at its narrowest, 240 pt). It is now one row across the sidebar, never wider, words in the middle: the time short (「17:23」 today, 「11/11 17:23」 another day), dropped when it doesn't fit, and the words cut with … if even they don't. The whole time is in its tooltip. - Screenshot mode: ANYBOT_SHOT_NEWER= shows the button; ANYBOT_SHOT_SIDEBAR= sets the sidebar's width. Co-Authored-By: Claude Opus 5.5 --- FEATURES.md | 2 +- src/app.rs | 10 ++++++++++ src/app/sidebar_ui.rs | 7 ++++++- src/icons.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/version.rs | 19 +++++++++++++++++++ 5 files changed, 76 insertions(+), 2 deletions(-) diff --git a/FEATURES.md b/FEATURES.md index 668ee52..5bee714 100644 --- a/FEATURES.md +++ b/FEATURES.md @@ -116,7 +116,7 @@ AnyBot 是一個桌面聊天 App,介面和運作方式照 Grok Bot 重做。 - **底部** - 「⚙ 設定」按鈕,高 30 px。 - 最下面是「版本 YYYY-MM-DD HH:MM」,10.5 px 灰字。版本就是這份程式建好的時間;不知道時顯示「?」。 - - 有新版本時多一顆強調色按鈕「⬆ 重新啟動以更新(新版本時間)」,按下立刻換成新版。 + - 有新版本時多一顆強調色按鈕「⬆ 重新啟動以更新(新版本時間)」,按下立刻換成新版。按鈕佔滿側邊欄一整排、不會超出:時間今天的只寫「17:23」,別天寫「11/11 17:23」;放不下就不寫時間,再放不下就截成「…」。完整時間在滑過時看。 ### 收合成直欄時 diff --git a/src/app.rs b/src/app.rs index 61b2e21..483708f 100644 --- a/src/app.rs +++ b/src/app.rs @@ -731,6 +731,16 @@ impl App { app.state.theme = Theme::Dark; } // For checking each screen without the demo's waiting pop-up on top. + // ANYBOT_SHOT_NEWER=: a newer build is waiting (重新啟動以更新), built that + // long ago (0: just now). + if let Ok(h) = std::env::var("ANYBOT_SHOT_NEWER") { + let ago = std::time::Duration::from_secs(h.trim().parse::().unwrap_or(0) * 3600); + app.newer_build = std::time::SystemTime::now().checked_sub(ago); + } + // ANYBOT_SHOT_SIDEBAR=: the sidebar that wide. + if let Some(w) = std::env::var("ANYBOT_SHOT_SIDEBAR").ok().and_then(|v| v.parse::().ok()) { + app.state.sidebar_width = w.clamp(SIDEBAR_MIN_WIDTH, SIDEBAR_MAX_WIDTH); + } if std::env::var("ANYBOT_SHOT_NOPOPUP").is_ok() { app.popup_hidden.extend(app.state.bots.iter().map(|b| b.id)); } diff --git a/src/app/sidebar_ui.rs b/src/app/sidebar_ui.rs index b8c60c8..e641554 100644 --- a/src/app/sidebar_ui.rs +++ b/src/app/sidebar_ui.rs @@ -202,8 +202,13 @@ impl App { self.ch.focus_market = true; } }); + // A newer build is in place: one row across the sidebar, never wider — with the + // build's time when it fits, without it when not (the whole of it on hover). if let Some(tm) = self.newer_build { - if crate::icons::pill(ui, crate::icons::Icon::Update, &format!("{}({})", t("重新啟動以更新"), crate::version::time_label(Some(tm))), p.accent, Color32::WHITE, 30.0).clicked() { + let texts = [format!("{}({})", t("重新啟動以更新"), crate::version::short_time_label(tm)), t("重新啟動以更新")]; + let w = ui.available_width(); + let tip = format!("{}{}{}", t("新版本建置於 "), crate::version::time_label(Some(tm)), t("。重新啟動就換成新版,聊天記錄都會保留。")); + if crate::icons::wide_pill(ui, crate::icons::Icon::Update, &texts, p.accent, Color32::WHITE, 30.0, w).on_hover_text(tip).clicked() { self.relaunch_requested = true; } } diff --git a/src/icons.rs b/src/icons.rs index a416d09..4837f0e 100644 --- a/src/icons.rs +++ b/src/icons.rs @@ -283,6 +283,30 @@ pub fn pill(ui: &mut egui::Ui, icon: Icon, text: &str, fill: Color32, fg: Color3 r.on_hover_cursor(egui::CursorIcon::PointingHand) } +/// A pill exactly `width` wide (a whole row), icon and words in the middle. The words are the +/// first of `texts` that fits, or else the last cut short with "…" — never wider than the row. +pub fn wide_pill(ui: &mut egui::Ui, icon: Icon, texts: &[String], fill: Color32, fg: Color32, height: f32, width: f32) -> egui::Response { + let font = egui::FontId::proportional(13.0); + let icon_s = 14.0; + let room = (width - 12.0 * 2.0 - icon_s - 6.0).max(0.0); + let measure = |t: &str| ui.painter().layout_no_wrap(t.to_string(), font.clone(), fg).size().x; + let text = texts.iter().find(|t| measure(t) <= room).cloned().unwrap_or_else(|| { + let mut chars: Vec = texts.last().map(|t| t.chars().collect()).unwrap_or_default(); + while !chars.is_empty() && measure(&format!("{}…", chars.iter().collect::())) > room { + chars.pop(); + } + format!("{}…", chars.into_iter().collect::()) + }); + let galley = ui.painter().layout_no_wrap(text, font, fg); + let (rect, r) = ui.allocate_exact_size(vec2(width, height), egui::Sense::click()); + let f = if r.hovered() { fill.gamma_multiply(0.88) } else { fill }; + ui.painter().rect_filled(rect, height / 2.0, f); + let left = rect.center().x - (icon_s + 6.0 + galley.size().x) / 2.0; + paint(ui.painter(), Rect::from_center_size(egui::pos2(left + icon_s / 2.0, rect.center().y), vec2(icon_s, icon_s)), icon, fg); + ui.painter().galley(egui::pos2(left + icon_s + 6.0, rect.center().y - galley.size().y / 2.0), galley, fg); + r.on_hover_cursor(egui::CursorIcon::PointingHand) +} + /// A borderless icon button (hover shows a soft rounded square). pub fn button(ui: &mut egui::Ui, icon: Icon, size: f32, col: Color32, hover_fill: Color32) -> egui::Response { let (rect, r) = ui.allocate_exact_size(vec2(size, size), egui::Sense::click()); @@ -308,4 +332,20 @@ mod tests { assert_eq!(for_file("a.xlsx"), Icon::FileSheet); assert_eq!(for_file("a.png"), Icon::FileImage); } + + /// A wide pill is exactly as wide as asked, however long its words (the sidebar's + /// 重新啟動以更新 ran past the sidebar's edge). + #[test] + fn a_wide_pill_never_outgrows_its_row() { + let ctx = egui::Context::default(); + for width in [60.0, 160.0, 400.0] { + let mut got = 0.0; + let mut out = ctx.run_ui(egui::RawInput::default(), |root| { + let texts = ["重新啟動以更新(2026-11-11 17:23)".to_string(), "重新啟動以更新".to_string()]; + got = wide_pill(root, Icon::Update, &texts, Color32::BLUE, Color32::WHITE, 30.0, width).rect.width(); + }); + out.textures_delta.clear(); // (no renderer here to hand the font atlas to) + assert_eq!(got, width); + } + } } diff --git a/src/version.rs b/src/version.rs index 765ed68..fa9306d 100644 --- a/src/version.rs +++ b/src/version.rs @@ -16,6 +16,16 @@ pub fn time_label(t: Option) -> String { t.map(|t| chrono::DateTime::::from(t).format("%Y-%m-%d %H:%M").to_string()).unwrap_or_else(|| "?".into()) } +/// The same, short, for a narrow place: "17:23" today, "11/11 17:23" another day. +pub fn short_time_label(t: SystemTime) -> String { + let t = chrono::DateTime::::from(t); + if t.date_naive() == chrono::Local::now().date_naive() { + t.format("%H:%M").to_string() + } else { + t.format("%m/%d %H:%M").to_string() + } +} + /// Milliseconds since 1970, for the phone: it shows the time in its own zone (a server's container /// runs in UTC, so its own "local time" would be hours off for the person reading it). pub fn epoch_ms(t: Option) -> Option { @@ -32,6 +42,15 @@ pub fn full(built: Option) -> String { #[cfg(test)] mod tests { + #[test] + fn a_short_time_says_the_day_only_when_it_isnt_today() { + let now = SystemTime::now(); + assert_eq!(short_time_label(now).len(), 5, "{}", short_time_label(now)); + let before = short_time_label(now - std::time::Duration::from_secs(3 * 86400)); + assert_eq!(before.len(), 11, "{before}"); + assert!(before.contains('/')); + } + use super::*; #[test]