update banner via gitea releases
This commit is contained in:
@@ -2,6 +2,7 @@ mod bootstrap;
|
||||
mod capture;
|
||||
mod config;
|
||||
mod encode;
|
||||
mod updates;
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Mutex;
|
||||
@@ -459,6 +460,16 @@ fn reveal_in_explorer(path: String) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn open_url(url: String) -> Result<(), String> {
|
||||
use std::process::Command;
|
||||
Command::new("cmd")
|
||||
.args(["/C", "start", "", &url])
|
||||
.spawn()
|
||||
.map_err(|e| format!("opening url: {e}"))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn export(state: State<'_, AppState>, request: ExportRequest) -> Result<ExportResult, String> {
|
||||
let cfg = state.config.lock().expect("config mutex").clone();
|
||||
@@ -548,6 +559,27 @@ pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
.manage(state)
|
||||
.setup(|app| {
|
||||
let check_on_launch = {
|
||||
let st = app.state::<AppState>();
|
||||
let cfg = st.config.lock().expect("config mutex");
|
||||
cfg.updates.check_on_launch
|
||||
};
|
||||
if check_on_launch {
|
||||
let handle = app.handle().clone();
|
||||
tauri::async_runtime::spawn(async move {
|
||||
match updates::check_for_update().await {
|
||||
Ok(Some(info)) => {
|
||||
tracing::info!(version = info.version, "update available");
|
||||
let _ = handle.emit("update-available", &info);
|
||||
}
|
||||
Ok(None) => tracing::debug!("no update available"),
|
||||
Err(e) => tracing::warn!("update check failed: {e:#}"),
|
||||
}
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
get_status,
|
||||
get_config,
|
||||
@@ -560,6 +592,7 @@ pub fn run() {
|
||||
export,
|
||||
extract_thumbnails,
|
||||
reveal_in_explorer,
|
||||
open_url,
|
||||
show_overlay,
|
||||
hide_overlay,
|
||||
get_overlay_rect,
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
use anyhow::Result;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
const GITEA_LATEST: &str =
|
||||
"https://git.lashman.live/api/v1/repos/lashman/bytesize/releases/latest";
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct UpdateInfo {
|
||||
pub version: String,
|
||||
pub current_version: String,
|
||||
pub release_url: String,
|
||||
pub body: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct GiteaRelease {
|
||||
tag_name: String,
|
||||
body: Option<String>,
|
||||
html_url: String,
|
||||
}
|
||||
|
||||
pub async fn check_for_update() -> Result<Option<UpdateInfo>> {
|
||||
let client = reqwest::Client::builder()
|
||||
.user_agent(concat!("ByteSize/", env!("CARGO_PKG_VERSION")))
|
||||
.build()?;
|
||||
let resp = client.get(GITEA_LATEST).send().await?;
|
||||
if !resp.status().is_success() {
|
||||
tracing::debug!("update check returned status {}", resp.status());
|
||||
return Ok(None);
|
||||
}
|
||||
let body = resp.text().await?;
|
||||
let release: GiteaRelease = serde_json::from_str(&body)?;
|
||||
let latest = release.tag_name.trim_start_matches('v').to_string();
|
||||
let current = env!("CARGO_PKG_VERSION").to_string();
|
||||
if is_newer(&latest, ¤t) {
|
||||
Ok(Some(UpdateInfo {
|
||||
version: latest,
|
||||
current_version: current,
|
||||
release_url: release.html_url,
|
||||
body: release.body.unwrap_or_default(),
|
||||
}))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
fn is_newer(latest: &str, current: &str) -> bool {
|
||||
let mut l: Vec<u32> = latest.split('.').filter_map(|s| s.parse().ok()).collect();
|
||||
let mut c: Vec<u32> = current.split('.').filter_map(|s| s.parse().ok()).collect();
|
||||
let max = l.len().max(c.len());
|
||||
l.resize(max, 0);
|
||||
c.resize(max, 0);
|
||||
l > c
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
import FirstRun from "./lib/FirstRun.svelte"
|
||||
import Main from "./lib/Main.svelte"
|
||||
import Settings from "./lib/Settings.svelte"
|
||||
import UpdateBanner from "./lib/UpdateBanner.svelte"
|
||||
|
||||
type View = "loading" | "bootstrap" | "first-run" | "main" | "settings"
|
||||
|
||||
@@ -29,6 +30,8 @@
|
||||
onMount(refresh)
|
||||
</script>
|
||||
|
||||
<UpdateBanner />
|
||||
|
||||
{#if view === "loading"}
|
||||
<Loading />
|
||||
{:else if view === "bootstrap"}
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from "svelte"
|
||||
import { api, type UpdateInfo } from "./api"
|
||||
|
||||
let info: UpdateInfo | null = $state(null)
|
||||
|
||||
onMount(() => {
|
||||
const unlistenPromise = api.onUpdateAvailable((i) => {
|
||||
info = i
|
||||
})
|
||||
return () => {
|
||||
unlistenPromise.then((fn) => fn())
|
||||
}
|
||||
})
|
||||
|
||||
function dismiss() {
|
||||
info = null
|
||||
}
|
||||
|
||||
function viewRelease() {
|
||||
if (info) {
|
||||
void api.openUrl(info.release_url)
|
||||
}
|
||||
}
|
||||
|
||||
let bodyPreview = $derived.by(() => {
|
||||
const i = info
|
||||
if (!i) return ""
|
||||
return i.body.split("\n").slice(0, 3).join(" ").slice(0, 140)
|
||||
})
|
||||
</script>
|
||||
|
||||
{#if info}
|
||||
<div class="banner">
|
||||
<div class="text">
|
||||
<strong>update available</strong>
|
||||
<span class="ver">v{info.version}</span>
|
||||
<span class="from">(you have v{info.current_version})</span>
|
||||
{#if bodyPreview}
|
||||
<p class="body">{bodyPreview}{info.body.length > 140 ? "..." : ""}</p>
|
||||
{/if}
|
||||
</div>
|
||||
<div class="actions">
|
||||
<button class="primary" onclick={viewRelease}>view release</button>
|
||||
<button onclick={dismiss}>dismiss</button>
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<style>
|
||||
.banner {
|
||||
background: linear-gradient(180deg, #1f2e2a, #1a2522);
|
||||
border-bottom: 1px solid #4a8;
|
||||
padding: 0.75rem 1.25rem;
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
.text {
|
||||
flex: 1;
|
||||
min-width: 240px;
|
||||
color: #d8eee5;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
.ver {
|
||||
color: #4a8;
|
||||
font-weight: 600;
|
||||
margin: 0 0.4rem;
|
||||
}
|
||||
.from {
|
||||
color: #7a8e88;
|
||||
font-size: 0.8rem;
|
||||
}
|
||||
.body {
|
||||
margin: 0.3rem 0 0;
|
||||
color: #9eb3ac;
|
||||
font-size: 0.8rem;
|
||||
line-height: 1.4;
|
||||
}
|
||||
.actions {
|
||||
display: flex;
|
||||
gap: 0.4rem;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
button {
|
||||
font: inherit;
|
||||
padding: 0.4rem 0.8rem;
|
||||
border: 1px solid #2a3a35;
|
||||
background: transparent;
|
||||
color: #d8eee5;
|
||||
cursor: pointer;
|
||||
border-radius: 4px;
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
button:hover {
|
||||
border-color: #4a8;
|
||||
}
|
||||
button.primary {
|
||||
background: #4a8;
|
||||
border-color: #4a8;
|
||||
color: #0e1a16;
|
||||
}
|
||||
button.primary:hover {
|
||||
background: #5b9;
|
||||
border-color: #5b9;
|
||||
}
|
||||
</style>
|
||||
@@ -51,6 +51,13 @@ export type DownloadProgress = {
|
||||
total: number | null
|
||||
}
|
||||
|
||||
export type UpdateInfo = {
|
||||
version: string
|
||||
current_version: string
|
||||
release_url: string
|
||||
body: string
|
||||
}
|
||||
|
||||
export type CaptureRect = {
|
||||
x: number
|
||||
y: number
|
||||
@@ -104,6 +111,9 @@ export const api = {
|
||||
getOverlayRect: () => invoke<CaptureRect>("get_overlay_rect"),
|
||||
extractThumbnails: (fps: number) =>
|
||||
invoke<string[]>("extract_thumbnails", { fps }),
|
||||
openUrl: (url: string) => invoke<void>("open_url", { url }),
|
||||
onUpdateAvailable: (handler: (info: UpdateInfo) => void): Promise<UnlistenFn> =>
|
||||
listen<UpdateInfo>("update-available", (e) => handler(e.payload)),
|
||||
}
|
||||
|
||||
export function formatBytes(n: number): string {
|
||||
|
||||
Reference in New Issue
Block a user