feat(updates): announce a new release where the user is looking
Reported: "当有新固件时,在主面板顶部要显示有升级信息。点击跳转到设置的软件更新." The banner is cheap; the reason it did not exist is that nothing on the main panel could know about a release. `useUpdateFlow` was mounted only inside Settings > 软件更新, so closing that panel dropped the only copy of the state. Mounting a second copy is what the hook already supports — it reads the status Rust remembers and subscribes to the broadcasts Rust already sends, so neither view issues an extra network check, and the banner survives closing Settings. The notice is persisted, which is the part without an obvious answer. The probe runs at most once a day, so a remembered status that lives only in memory makes "available" visible on the morning it is discovered and invisible every launch after — including the launch straight after an update installs, exactly when the user is most likely to look. Measured, not theorised: the first launch showed no banner, and ageing the probe stamp was the only way to make one appear. Remembering a claim means being wrong about it later, so a restored notice is re-judged rather than trusted. A version that no longer leads is dropped, since the user is already running it; `ready` keeps its promise only while the installer file is really on disk, and otherwise says `available`, which is still true and merely means downloading again. Restore runs before `setup_tray` so the tray row cannot contradict the banner, and a copy told to stop checking restores nothing, because that switch means "don't tell me". Two banners that used to be mutually exclusive now stack. An update notice and a broken screen-recording permission are different news, and chaining them meant a user who fixed the first never learned the second existed. Co-Authored-By: Codex <noreply@openai.com>main
parent
a95aebe1d0
commit
e1fb18bc6a
|
|
@ -511,6 +511,9 @@ pub fn run() {
|
|||
}
|
||||
}
|
||||
|
||||
// Before the tray is built, so a release noticed on an earlier run also
|
||||
// brings back its tray row and not just the banner.
|
||||
update::restore_notice(app.handle());
|
||||
setup_tray(app.handle())?;
|
||||
register_shortcuts(app.handle())?;
|
||||
drag_bridge::spawn(app.handle());
|
||||
|
|
|
|||
|
|
@ -84,7 +84,7 @@ struct ReleaseManifest {
|
|||
/// The newest manifest shape this build can read.
|
||||
const SUPPORTED_SCHEMA: u32 = 1;
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct UpdateStatus {
|
||||
/// "current" | "available" | "ready" | "unavailable" | "error"
|
||||
|
|
@ -530,6 +530,95 @@ fn download_directory() -> PathBuf {
|
|||
std::env::temp_dir().join("Shift Updates")
|
||||
}
|
||||
|
||||
/// Where the last notice is remembered, next to the history library and outside the
|
||||
/// app bundle, so an upgrade keeps it.
|
||||
fn notice_path(app: &AppHandle) -> Option<PathBuf> {
|
||||
Some(app.path().app_data_dir().ok()?.join("update-notice.json"))
|
||||
}
|
||||
|
||||
/// What a remembered notice is still worth after this build started, if anything.
|
||||
///
|
||||
/// Two things can have changed while Shift was closed. The release may have been
|
||||
/// installed, in which case the version being advertised is the one now running and
|
||||
/// the notice has to go — otherwise an update banner keeps offering the user the
|
||||
/// version they are already on, forever. And a `ready` notice promises an installer
|
||||
/// sitting in a temporary folder the system is free to empty, so that claim is only
|
||||
/// kept while the file is really there; without it the notice becomes `available`,
|
||||
/// which is true and simply means downloading again.
|
||||
fn revive_notice(status: UpdateStatus, running: &str) -> Option<UpdateStatus> {
|
||||
if !version_is_newer(&status.available_version, running) {
|
||||
return None;
|
||||
}
|
||||
let mut status = status;
|
||||
// The remembered field is the version that asked, not the one answering now.
|
||||
status.current_version = running.to_string();
|
||||
if status.state == "ready" {
|
||||
let installer_on_disk = status
|
||||
.path
|
||||
.as_deref()
|
||||
.is_some_and(|path| Path::new(path).is_file());
|
||||
if !installer_on_disk {
|
||||
status.state = "available".into();
|
||||
status.path = None;
|
||||
}
|
||||
}
|
||||
Some(status)
|
||||
}
|
||||
|
||||
/// Keep the remembered notice in step with the last check. Only a notice worth
|
||||
/// showing is written; any other outcome clears the file, so a stale answer can never
|
||||
/// outlive the check that produced it.
|
||||
fn remember_notice(app: &AppHandle, status: &UpdateStatus) {
|
||||
let Some(path) = notice_path(app) else {
|
||||
return;
|
||||
};
|
||||
if !matches!(status.state.as_str(), "available" | "ready") {
|
||||
let _ = fs::remove_file(path);
|
||||
return;
|
||||
}
|
||||
if let Ok(text) = serde_json::to_string(status) {
|
||||
if let Some(parent) = path.parent() {
|
||||
let _ = fs::create_dir_all(parent);
|
||||
}
|
||||
let _ = fs::write(path, text);
|
||||
}
|
||||
}
|
||||
|
||||
/// Restore the remembered notice at startup, so a release found yesterday is still
|
||||
/// announced today.
|
||||
///
|
||||
/// The probe is throttled to one a day, so without this the banner and its tray row
|
||||
/// appear on the morning a release is found and are gone for every launch after that
|
||||
/// — including the launch that follows an update, when the user is most likely to look.
|
||||
/// A copy that has been told to stop checking for updates is also a copy that does not
|
||||
/// want to be told, so nothing is restored while the switch is off.
|
||||
pub fn restore_notice(app: &AppHandle) {
|
||||
if !crate::settings::load(app).update_check_enabled {
|
||||
return;
|
||||
}
|
||||
let Some(path) = notice_path(app) else {
|
||||
return;
|
||||
};
|
||||
let remembered: Option<UpdateStatus> = fs::read_to_string(&path)
|
||||
.ok()
|
||||
.and_then(|text| serde_json::from_str(&text).ok());
|
||||
let Some(remembered) = remembered else {
|
||||
return;
|
||||
};
|
||||
match revive_notice(remembered, ¤t_version()) {
|
||||
Some(status) => {
|
||||
if let Ok(mut guard) = app.state::<UpdateState>().status.lock() {
|
||||
*guard = Some(status.clone());
|
||||
}
|
||||
let _ = app.emit("update://status", status);
|
||||
}
|
||||
// Read and rejected is still a reason to stop carrying it.
|
||||
None => {
|
||||
let _ = fs::remove_file(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sha256_of(path: &Path) -> Result<String, String> {
|
||||
let mut file = fs::File::open(path).map_err(|error| error.to_string())?;
|
||||
let mut digest = Sha256::new();
|
||||
|
|
@ -578,11 +667,13 @@ fn emit_progress(app: &AppHandle, downloaded: u64, total: u64, state: &str) {
|
|||
);
|
||||
}
|
||||
|
||||
/// Report the outcome to every open window and keep it for the next panel mount.
|
||||
/// Report the outcome to every open window, keep it for the next panel mount, and
|
||||
/// remember the ones worth carrying to the next launch.
|
||||
fn publish(app: &AppHandle, status: UpdateStatus) -> UpdateStatus {
|
||||
if let Ok(mut guard) = app.state::<UpdateState>().status.lock() {
|
||||
*guard = Some(status.clone());
|
||||
}
|
||||
remember_notice(app, &status);
|
||||
let _ = app.emit("update://status", status.clone());
|
||||
status
|
||||
}
|
||||
|
|
@ -1066,6 +1157,11 @@ pub fn update_clear(app: AppHandle) {
|
|||
let _ = fs::remove_file(artifact);
|
||||
}
|
||||
}
|
||||
// Discarding is the user saying no to this notice, so the remembered copy goes
|
||||
// with it; otherwise the banner would be back on the next launch.
|
||||
if let Some(path) = notice_path(&app) {
|
||||
let _ = fs::remove_file(path);
|
||||
}
|
||||
let _ = app.emit("update://status", serde_json::Value::Null);
|
||||
}
|
||||
|
||||
|
|
@ -1107,6 +1203,83 @@ pub fn spawn_startup_check(app: &AppHandle) {
|
|||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// A remembered notice, at the two states worth carrying across a launch.
|
||||
fn notice(state: &str, available: &str, path: Option<&str>) -> UpdateStatus {
|
||||
UpdateStatus {
|
||||
state: state.to_string(),
|
||||
current_version: "0.1.12".into(),
|
||||
available_version: available.into(),
|
||||
file_name: format!("Shift-{available}-macOS-universal.dmg"),
|
||||
size: 1,
|
||||
sha256: "0".repeat(64),
|
||||
notes: String::new(),
|
||||
date: "2026-11-19".into(),
|
||||
path: path.map(str::to_string),
|
||||
message: String::new(),
|
||||
feed_url: "https://forge/api/raw/mula/nex_shift/releases/shift-update.json".into(),
|
||||
signed: true,
|
||||
artifact_key: "macos".into(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The whole point of remembering a notice is that the next launch still shows
|
||||
/// it; everything else here is about what the next launch may have invalidated.
|
||||
#[test]
|
||||
fn a_notice_for_a_release_still_ahead_survives_the_launch() {
|
||||
let revived = revive_notice(notice("available", "0.2.0", None), "0.1.12").unwrap();
|
||||
assert_eq!(revived.state, "available");
|
||||
assert_eq!(revived.available_version, "0.2.0");
|
||||
// The remembered `current_version` is the version that asked, not the one
|
||||
// answering now, so an old file must not report the old build as running.
|
||||
assert_eq!(revived.current_version, "0.1.12");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_installed_release_stops_being_announced() {
|
||||
// The user updated, the app restarted, and the notice still says 0.2.0 is
|
||||
// available. Without this rule the banner offers the version already running.
|
||||
assert!(revive_notice(notice("ready", "0.2.0", None), "0.2.0").is_none());
|
||||
assert!(revive_notice(notice("ready", "0.2.0", None), "0.3.0").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_ready_notice_without_its_installer_downgrades_rather_than_lying() {
|
||||
// `ready` promises a verified file in a temporary folder the system may empty
|
||||
// while Shift is closed, so the claim only holds while the file is there.
|
||||
let missing = revive_notice(
|
||||
notice(
|
||||
"ready",
|
||||
"0.2.0",
|
||||
Some("/tmp/definitely-not-here-9f3c21.dmg"),
|
||||
),
|
||||
"0.1.12",
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
missing.state, "available",
|
||||
"cannot offer an install it cannot do"
|
||||
);
|
||||
assert_eq!(missing.path, None);
|
||||
assert_eq!(
|
||||
missing.available_version, "0.2.0",
|
||||
"the release is still news"
|
||||
);
|
||||
|
||||
let on_disk = std::env::temp_dir().join("shift-notice-test-9f3c21.dmg");
|
||||
fs::write(&on_disk, b"installer").unwrap();
|
||||
let kept = revive_notice(
|
||||
notice("ready", "0.2.0", Some(on_disk.to_str().unwrap())),
|
||||
"0.1.12",
|
||||
)
|
||||
.unwrap();
|
||||
let _ = fs::remove_file(&on_disk);
|
||||
assert_eq!(
|
||||
kept.state, "ready",
|
||||
"a real verified download stays installable"
|
||||
);
|
||||
assert_eq!(kept.path.as_deref(), on_disk.to_str());
|
||||
}
|
||||
|
||||
/// `set_var` is process-wide and tests run in parallel, so anything that reads
|
||||
/// or writes the feed override has to hold this.
|
||||
fn feed_guard() -> std::sync::MutexGuard<'static, ()> {
|
||||
|
|
|
|||
|
|
@ -60,6 +60,7 @@ import {
|
|||
import { SetupPanel } from "./SetupPanel";
|
||||
import { SharePanel } from "./SharePanel";
|
||||
import { UpdatePanel } from "./UpdatePanel";
|
||||
import { useUpdateFlow } from "../lib/useUpdate";
|
||||
import {
|
||||
basename,
|
||||
dirname,
|
||||
|
|
@ -297,6 +298,15 @@ export function MainWindow() {
|
|||
const [recent, setRecent] = useState<RecentItem[]>(getLocalRecentItems);
|
||||
/** Filter text for the history library, matched against the file name. */
|
||||
const [historyQuery, setHistoryQuery] = useState("");
|
||||
/**
|
||||
* The update channel, read here so a new release can be announced on the panel the
|
||||
* user actually looks at instead of only inside Settings. Mounting a second copy of
|
||||
* the flow next to Settings > 软件更新 is what the hook is built for: it reads the
|
||||
* status Rust already remembers and subscribes to the broadcasts it already sends,
|
||||
* so the two views converge and neither triggers an extra network check.
|
||||
*/
|
||||
const update = useUpdateFlow();
|
||||
const updatePending = update.status?.state === "available" || update.status?.state === "ready";
|
||||
const [sharePaths, setSharePaths] = useState<string[] | null>(null);
|
||||
const toastTimer = useRef<number | null>(null);
|
||||
const [, setNowTick] = useState(0);
|
||||
|
|
@ -357,6 +367,16 @@ export function MainWindow() {
|
|||
setSettingsOpen(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Settings → 软件更新, from either entry point: the tray's update row and the
|
||||
* banner at the top of this window both land here, so the two cannot drift apart
|
||||
* on which tab they open or on whether the panel is already showing another tab.
|
||||
*/
|
||||
function openUpdates() {
|
||||
setSettingsTab("updates");
|
||||
setSettingsOpen(true);
|
||||
}
|
||||
|
||||
async function flash(message: string, duration: number = FEEDBACK.notice) {
|
||||
if (toastTimer.current) window.clearTimeout(toastTimer.current);
|
||||
setToast(message);
|
||||
|
|
@ -830,8 +850,7 @@ export function MainWindow() {
|
|||
// The tray's update row lands here: open Settings on the Updates tab so the
|
||||
// download and its SHA256 are the next thing the user sees.
|
||||
void listen("app://open-updates", () => {
|
||||
setSettingsTab("updates");
|
||||
setSettingsOpen(true);
|
||||
openUpdates();
|
||||
}).then((unlisten) => unlisteners.push(unlisten));
|
||||
// A capture card's Send button: this window owns the peer list, so the card
|
||||
// asks here rather than duplicating the discovery UI.
|
||||
|
|
@ -1009,21 +1028,41 @@ export function MainWindow() {
|
|||
|
||||
<main className="home-main">
|
||||
<section className="home-drop">
|
||||
{screenAccess === false ? (
|
||||
<div className="permission-banner" role="alert">
|
||||
<ShieldAlert size={16} />
|
||||
<span>{t("screenPermissionPrompt")}</span>
|
||||
<button onClick={() => void api.openScreenRecordingSettings()}>
|
||||
{t("openScreenSettings")}
|
||||
</button>
|
||||
</div>
|
||||
) : hotkeys && !hotkeys.live ? (
|
||||
<div className="permission-banner" role="alert">
|
||||
<ShieldAlert size={16} />
|
||||
<span>{hotkeys.others.length > 0 ? t("hotkeysDead") : t("hotkeysNotLive")}</span>
|
||||
<button onClick={() => void reclaimHotkeys()}>{t("hotkeysFix")}</button>
|
||||
</div>
|
||||
) : null}
|
||||
{/* A stack rather than a chain of alternatives: an update and a broken
|
||||
permission are different news, and hiding one behind the other meant a
|
||||
user who fixed the first never learned about the second. A release is
|
||||
announced here, on the panel the user actually looks at, rather than
|
||||
only inside Settings. */}
|
||||
<div className="home-alerts">
|
||||
{updatePending ? (
|
||||
<div className="permission-banner is-update" role="status">
|
||||
<Sparkle size={16} />
|
||||
<span>
|
||||
{t(
|
||||
update.status?.state === "ready" ? "updateReadyOnPanel" : "updateAvailableOnPanel",
|
||||
{ version: update.status?.availableVersion ?? "" },
|
||||
)}
|
||||
</span>
|
||||
<button onClick={openUpdates}>{t("updateOpenPanel")}</button>
|
||||
</div>
|
||||
) : null}
|
||||
{screenAccess === false ? (
|
||||
<div className="permission-banner" role="alert">
|
||||
<ShieldAlert size={16} />
|
||||
<span>{t("screenPermissionPrompt")}</span>
|
||||
<button onClick={() => void api.openScreenRecordingSettings()}>
|
||||
{t("openScreenSettings")}
|
||||
</button>
|
||||
</div>
|
||||
) : null}
|
||||
{hotkeys && !hotkeys.live ? (
|
||||
<div className="permission-banner" role="alert">
|
||||
<ShieldAlert size={16} />
|
||||
<span>{hotkeys.others.length > 0 ? t("hotkeysDead") : t("hotkeysNotLive")}</span>
|
||||
<button onClick={() => void reclaimHotkeys()}>{t("hotkeysFix")}</button>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
<div className="home-drop-art" aria-hidden="true">
|
||||
{DROP_ICONS.map(({ icon: Icon, tone }, index) => {
|
||||
// Place every card on one circle: x follows sin(angle) and the outer
|
||||
|
|
|
|||
|
|
@ -232,6 +232,9 @@ const messages: Record<Lang, Record<string, string>> = {
|
|||
updateSystemPending: "Reading operating system…",
|
||||
updateIdle: "Not checked yet",
|
||||
updateAvailable: "Update available",
|
||||
updateAvailableOnPanel: "Shift {version} is available.",
|
||||
updateReadyOnPanel: "Shift {version} is downloaded and verified, ready to install.",
|
||||
updateOpenPanel: "Open software updates",
|
||||
updateCurrent: "Up to date",
|
||||
updateReady: "Downloaded and verified",
|
||||
updateUnavailable: "No artifact for this platform",
|
||||
|
|
@ -500,6 +503,9 @@ const messages: Record<Lang, Record<string, string>> = {
|
|||
updateSystemPending: "正在读取系统信息…",
|
||||
updateIdle: "尚未检查",
|
||||
updateAvailable: "发现新版本",
|
||||
updateAvailableOnPanel: "Shift {version} 可以升级了。",
|
||||
updateReadyOnPanel: "Shift {version} 已下载并校验完成,可以安装。",
|
||||
updateOpenPanel: "打开软件更新",
|
||||
updateCurrent: "已是最新版本",
|
||||
updateReady: "已下载并校验",
|
||||
updateUnavailable: "该平台没有对应安装包",
|
||||
|
|
|
|||
|
|
@ -1736,16 +1736,29 @@ kbd {
|
|||
backdrop-filter: blur(18px);
|
||||
}
|
||||
|
||||
.permission-banner {
|
||||
/* The one place that owns the top-centre geometry. Each banner used to carry it,
|
||||
which meant two banners were the same two rectangles and only an if/else chain
|
||||
kept them from printing over each other. Now they flow, so an update notice and a
|
||||
permission problem can both be read. */
|
||||
.home-alerts {
|
||||
position: absolute;
|
||||
top: 18px;
|
||||
left: 50%;
|
||||
z-index: 3;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
width: min(92%, 620px);
|
||||
transform: translateX(-50%);
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.permission-banner {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
max-width: min(92%, 620px);
|
||||
transform: translateX(-50%);
|
||||
max-width: 100%;
|
||||
padding: 10px 11px 10px 14px;
|
||||
border: 1px solid rgba(217, 79, 31, 0.2);
|
||||
border-radius: var(--radius-sm);
|
||||
|
|
@ -1756,6 +1769,30 @@ kbd {
|
|||
font-weight: 600;
|
||||
line-height: 1.4;
|
||||
backdrop-filter: blur(12px);
|
||||
/* The stack lets clicks through around a banner, but a banner is a control. */
|
||||
pointer-events: auto;
|
||||
}
|
||||
|
||||
/* An available release is good news, and the Settings panel already colours it with
|
||||
the same green as `.update-new`; a permission problem stays terracotta. */
|
||||
.permission-banner.is-update {
|
||||
border-color: rgba(20, 121, 74, 0.22);
|
||||
color: #14603f;
|
||||
background: rgba(238, 250, 244, 0.94);
|
||||
box-shadow: 0 8px 24px rgba(20, 121, 74, 0.1);
|
||||
}
|
||||
|
||||
.permission-banner.is-update svg {
|
||||
color: #14794a;
|
||||
}
|
||||
|
||||
.permission-banner.is-update button {
|
||||
color: #f4fffa;
|
||||
background: #14794a;
|
||||
}
|
||||
|
||||
.permission-banner.is-update button:hover {
|
||||
background: #0f6439;
|
||||
}
|
||||
|
||||
.permission-banner svg {
|
||||
|
|
|
|||
Loading…
Reference in New Issue