Skip to content

Commit

Permalink
fix progress indicators
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoBSalgueiro committed Feb 8, 2024
1 parent 8d02621 commit d09de3c
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 68 deletions.
35 changes: 19 additions & 16 deletions src-tauri/src/chess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,13 @@ pub struct AnalysisOptions {
pub reversed: bool,
}

#[derive(Clone, Type, serde::Serialize, Event)]
pub struct ReportProgress {
pub progress: f64,
pub id: u64,
pub finished: bool,
}

#[tauri::command]
#[specta::specta]
pub async fn analyze_game(
Expand Down Expand Up @@ -589,14 +596,12 @@ pub async fn analyze_game(
let mut novelty_found = false;

for (i, (_, moves, _)) in fens.iter().enumerate() {
app.emit_all(
"report_progress",
DownloadProgress {
progress: (i as f64 / fens.len() as f64) * 100.0,
id: 0,
finished: false,
},
)?;
ReportProgress {
progress: (i as f64 / fens.len() as f64) * 100.0,
id: 0,
finished: false,
}
.emit_all(&app)?;

proc.set_options(EngineOptions {
// threads: 4,
Expand Down Expand Up @@ -667,14 +672,12 @@ pub async fn analyze_game(
}
}
}
app.emit_all(
"report_progress",
DownloadProgress {
progress: 1.0,
id: 0,
finished: true,
},
)?;
ReportProgress {
progress: 100.0,
id: 0,
finished: true,
}
.emit_all(&app)?;
Ok(analysis)
}

Expand Down
17 changes: 8 additions & 9 deletions src-tauri/src/fide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use tauri::{
api::path::{app_config_dir, resolve_path, BaseDirectory},
Manager,
};
use tauri_specta::Event;

use crate::error::Error;
use crate::{error::Error, fs::DownloadProgress};
use crate::{fs::download_file, AppState};

#[derive(Debug, Deserialize, Serialize, Type, Clone, Decode, Encode)]
Expand Down Expand Up @@ -120,14 +121,12 @@ pub async fn download_fide_db(
let mut fide_players = state.fide_players.write().await;
*fide_players = players_list.players;

app.emit_all(
"download_progress",
crate::fs::DownloadProgress {
progress: 100.0,
id: 0,
finished: true,
},
)?;
DownloadProgress {
progress: 100.0,
id: 0,
finished: true,
}
.emit_all(&app)?;

remove_file(&xml_path)?;

Expand Down
14 changes: 6 additions & 8 deletions src-tauri/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,12 @@ pub async fn download_file(
}

if finalize {
app.emit_all(
"download_progress",
DownloadProgress {
progress: 100.0,
id,
finished: true,
},
)?;
DownloadProgress {
progress: 100.0,
id,
finished: true,
}
.emit_all(&app)?;
}
// remove_file(&path).await;
Ok(())
Expand Down
5 changes: 3 additions & 2 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::{fs::create_dir_all, path::Path};

use chess::{BestMovesPayload, EngineProcess};
use chess::{BestMovesPayload, EngineProcess, ReportProgress};
use dashmap::DashMap;
use db::{DatabaseProgress, NormalizedGame, PositionQuery, PositionStats};
use derivative::Derivative;
Expand Down Expand Up @@ -129,7 +129,8 @@ fn main() {
.events(tauri_specta::collect_events!(
BestMovesPayload,
DatabaseProgress,
DownloadProgress
DownloadProgress,
ReportProgress
));

#[cfg(debug_assertions)]
Expand Down
7 changes: 5 additions & 2 deletions src/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,13 @@ try {
export const events = __makeEvents__<{
bestMovesPayload: BestMovesPayload,
databaseProgress: DatabaseProgress,
downloadProgress: DownloadProgress
downloadProgress: DownloadProgress,
reportProgress: ReportProgress
}>({
bestMovesPayload: "plugin:tauri-specta:best-moves-payload",
databaseProgress: "plugin:tauri-specta:database-progress",
downloadProgress: "plugin:tauri-specta:download-progress"
downloadProgress: "plugin:tauri-specta:download-progress",
reportProgress: "plugin:tauri-specta:report-progress"
})

/** user-defined types **/
Expand All @@ -124,6 +126,7 @@ export type EngineOption = { name: string; value: string }
export type EngineOptions = { fen: string; moves: string[]; extraOptions: EngineOption[] }
export type GoMode = { t: "Depth"; c: number } | { t: "Time"; c: number } | { t: "Nodes"; c: number } | { t: "Infinite" }
export type MonthData = { count: number; avg_elo: number }
export type ReportProgress = { progress: number; id: bigint; finished: boolean }
export type Results = { won: number; lost: number; draw: number }
export type Score = { type: "cp"; value: number } | { type: "mate"; value: number }
/**
Expand Down
44 changes: 20 additions & 24 deletions src/components/common/ProgressButton.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { Button, Progress, useMantineTheme } from "@mantine/core";
import { listen } from "@tauri-apps/api/event";
import { EventCallback, UnlistenFn, listen } from "@tauri-apps/api/event";
import { memo, useEffect, useState } from "react";
import * as classes from "./ProgressButton.css";

type ProgressPayload = {
id: number;
type Payload = {
id: bigint;
progress: number;
finished: boolean;
};

type Props = {
type Props<T> = {
id: number;
initInstalled: boolean;
progressEvent: string;
progressEvent: { listen: (handler: EventCallback<T>) => Promise<UnlistenFn> };
onClick: (id: number) => void;
leftIcon?: React.ReactNode;
labels: {
Expand All @@ -27,7 +27,7 @@ type Props = {
setInProgress: (inProgress: boolean) => void;
};

function ProgressButton({
function ProgressButton<T extends Payload>({
id,
initInstalled,
progressEvent,
Expand All @@ -38,28 +38,24 @@ function ProgressButton({
redoable,
inProgress,
setInProgress,
}: Props) {
}: Props<T>) {
const [progress, setProgress] = useState(0);
const [completed, setCompleted] = useState(initInstalled);

useEffect(() => {
async function getProgress() {
const unlisten = await listen<ProgressPayload>(
progressEvent,
async ({ payload }) => {
if (payload.id !== id) return;
if (payload.finished) {
setInProgress(false);
setCompleted(true);
setProgress(0);
unlisten();
} else {
setProgress(payload.progress);
}
},
);
}
getProgress();
const unlisten = progressEvent.listen(async ({ payload }) => {
if (Number(payload.id) !== id) return;
if (payload.finished) {
setInProgress(false);
setCompleted(true);
setProgress(0);
} else {
setProgress(payload.progress);
}
});
return () => {
unlisten.then((f) => f());
};
}, []);

let label: string;
Expand Down
3 changes: 2 additions & 1 deletion src/components/databases/AddDatabase.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { events } from "@/bindings";
import { DatabaseInfo, getDatabases, useDefaultDatabases } from "@/utils/db";
import { formatBytes, formatNumber } from "@/utils/format";
import { invoke } from "@/utils/invoke";
Expand Down Expand Up @@ -225,7 +226,7 @@ function DatabaseCard({
</Group>
<ProgressButton
id={databaseId}
progressEvent="download_progress"
progressEvent={events.downloadProgress}
initInstalled={initInstalled}
labels={{
completed: "Installed",
Expand Down
4 changes: 2 additions & 2 deletions src/components/databases/FideInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { IconCloud } from "@tabler/icons-react";
import * as Flags from "mantine-flagpack";

import { commands } from "@/bindings";
import { events, commands } from "@/bindings";
import { invoke } from "@tauri-apps/api";
import { BaseDirectory, exists } from "@tauri-apps/api/fs";
import { useEffect, useState } from "react";
Expand Down Expand Up @@ -67,7 +67,7 @@ function FideInfo({
<ProgressButton
id={0}
initInstalled={false}
progressEvent={"download_progress"}
progressEvent={events.downloadProgress}
onClick={() => invoke("download_fide_db")}
labels={{
completed: "Downloaded",
Expand Down
4 changes: 2 additions & 2 deletions src/components/engines/AddEngine.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { enginesAtom } from "@/atoms/atoms";
import { commands } from "@/bindings";
import { events, commands } from "@/bindings";
import {
LocalEngine,
RemoteEngine,
Expand Down Expand Up @@ -255,7 +255,7 @@ function EngineCard({
</Group>
<ProgressButton
id={engineId}
progressEvent="download_progress"
progressEvent={events.downloadProgress}
initInstalled={initInstalled}
labels={{
completed: "Installed",
Expand Down
3 changes: 2 additions & 1 deletion src/components/panels/analysis/AnalysisPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
engineMovesFamily,
enginesAtom,
} from "@/atoms/atoms";
import { events } from "@/bindings";
import EvalChart from "@/components/common/EvalChart";
import ProgressButton from "@/components/common/ProgressButton";
import { TreeStateContext } from "@/components/common/TreeStateContext";
Expand Down Expand Up @@ -289,7 +290,7 @@ function AnalysisPanel({
leftIcon={<IconZoomCheck size="0.875rem" />}
onClick={() => toggleReportingMode()}
initInstalled={false}
progressEvent="report_progress"
progressEvent={events.reportProgress}
labels={{
action: "Generate report",
completed: "Report generated",
Expand Down
3 changes: 2 additions & 1 deletion src/components/puzzles/AddPuzzle.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { events } from "@/bindings";
import { getDefaultPuzzleDatabases } from "@/utils/db";
import { formatBytes, formatNumber } from "@/utils/format";
import { invoke } from "@/utils/invoke";
Expand Down Expand Up @@ -122,7 +123,7 @@ function PuzzleDbCard({
</Group>
<ProgressButton
id={databaseId}
progressEvent="download_progress"
progressEvent={events.downloadProgress}
initInstalled={initInstalled}
labels={{
completed: "Installed",
Expand Down

0 comments on commit d09de3c

Please sign in to comment.