側邊欄: 重新啟動以更新 stays inside the sidebar

- 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=<hours ago> shows the button; ANYBOT_SHOT_SIDEBAR=<points>
  sets the sidebar's width.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
王性驊 2026-11-11 17:25:59 +08:00
parent 215a5667cc
commit cb0e4f3465
5 changed files with 76 additions and 2 deletions

View File

@ -116,7 +116,7 @@ AnyBot 是一個桌面聊天 App,介面和運作方式照 Grok Bot 重做。
- **底部**
- 「⚙ 設定」按鈕,高 30 px。
- 最下面是「版本 YYYY-MM-DD HH:MM」,10.5 px 灰字。版本就是這份程式建好的時間;不知道時顯示「?」。
- 有新版本時多一顆強調色按鈕「⬆ 重新啟動以更新(新版本時間)」,按下立刻換成新版。
- 有新版本時多一顆強調色按鈕「⬆ 重新啟動以更新(新版本時間)」,按下立刻換成新版。按鈕佔滿側邊欄一整排、不會超出:時間今天的只寫「17:23」,別天寫「11/11 17:23」;放不下就不寫時間,再放不下就截成「…」。完整時間在滑過時看。
### 收合成直欄時

View File

@ -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=<hours ago>: 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::<u64>().unwrap_or(0) * 3600);
app.newer_build = std::time::SystemTime::now().checked_sub(ago);
}
// ANYBOT_SHOT_SIDEBAR=<points>: the sidebar that wide.
if let Some(w) = std::env::var("ANYBOT_SHOT_SIDEBAR").ok().and_then(|v| v.parse::<f32>().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));
}

View File

@ -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;
}
}

View File

@ -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<char> = texts.last().map(|t| t.chars().collect()).unwrap_or_default();
while !chars.is_empty() && measure(&format!("{}…", chars.iter().collect::<String>())) > room {
chars.pop();
}
format!("{}…", chars.into_iter().collect::<String>())
});
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);
}
}
}

View File

@ -16,6 +16,16 @@ pub fn time_label(t: Option<SystemTime>) -> String {
t.map(|t| chrono::DateTime::<chrono::Local>::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::<chrono::Local>::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<SystemTime>) -> Option<u64> {
@ -32,6 +42,15 @@ pub fn full(built: Option<SystemTime>) -> 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]