From 6697c38af03a4d9ca3cedb407ca610fc4bf7bf72 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Tue, 25 Apr 2023 20:08:07 +0200 Subject: [PATCH] Frontend now listen to event sent from backend --- src-tauri/src/launcher/mod.rs | 14 ++++++++++++-- src-tauri/src/main.rs | 4 ++-- src/components/login.js | 18 +++++++++++++++--- src/index.html | 2 +- src/main.js | 10 +++++++--- 5 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src-tauri/src/launcher/mod.rs b/src-tauri/src/launcher/mod.rs index fda6cfe..bb49564 100644 --- a/src-tauri/src/launcher/mod.rs +++ b/src-tauri/src/launcher/mod.rs @@ -5,6 +5,7 @@ use std::path::{Path, self}; use anyhow::{Result, bail}; use reqwest::{Client, StatusCode}; use serde::{Serialize, Deserialize}; +use tauri::Manager; use tokio::{fs, io::{AsyncWriteExt, AsyncSeekExt}}; use crate::authentification::GameProfile; @@ -19,6 +20,13 @@ const ACTUAL_OS: OSName = OSName::Linux; #[cfg(target_os="macos")] const ACTUAL_OS: OSName = OSName::MacOsX; +#[derive(Clone, serde::Serialize)] +struct ProgressMessage { + p_type: String, + current: usize, + total: usize, +} + pub struct ClientOptions<'a> { pub authorization: GameProfile, pub root_path: &'a Path, @@ -55,7 +63,7 @@ impl<'a> MinecraftClient<'_> { Ok(details) } - pub async fn download_assets(&mut self) -> Result<()> { + pub async fn download_assets(&mut self, app: tauri::AppHandle) -> Result<()> { // create root folder if it doesn't exist if !self.opts.root_path.exists() { fs::create_dir_all(self.opts.root_path).await?; @@ -65,7 +73,8 @@ impl<'a> MinecraftClient<'_> { fs::create_dir(lib).await?; } self.filter_non_necessary_librairies(); - for (_, i) in self.details.libraries.iter().enumerate() { + let total = self.details.libraries.len(); + for (progress, i) in self.details.libraries.iter().enumerate() { let p = i.downloads.artifact.path.clone(); let mut splited = p.split("/").collect::>(); let filename = splited.pop().ok_or(anyhow::anyhow!("Invalid filename"))?; // remove last element @@ -112,6 +121,7 @@ impl<'a> MinecraftClient<'_> { } else { println!("{} already downloaded", i.name); } + app.emit_all("progress", ProgressMessage { p_type: "libraries".to_string(), current: progress + 1, total })?; } Ok(()) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 086fac4..d5d7c73 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -42,7 +42,7 @@ async fn login(app: tauri::AppHandle, _window: tauri::Window, state: tauri::Stat } #[tauri::command] -async fn download(state: tauri::State<'_, Mutex>) -> Result { +async fn download(app: tauri::AppHandle, state: tauri::State<'_, Mutex>) -> Result { if let Some(base_dir) = BaseDirs::new() { let data_folder = base_dir.data_dir().join(".altarik_test"); let root_path = data_folder.as_path(); @@ -66,7 +66,7 @@ async fn download(state: tauri::State<'_, Mutex>) -> Result { - match client.download_assets().await { + match client.download_assets(app).await { Ok(_) => { Ok("Content downloaded".to_string()) }, diff --git a/src/components/login.js b/src/components/login.js index e2294d2..33e36eb 100644 --- a/src/components/login.js +++ b/src/components/login.js @@ -1,4 +1,4 @@ -export default { +const vue = { data() { return { button_message: "Login to minecraft", @@ -7,6 +7,15 @@ export default { hideDownloadButton: true, } }, + created() { + console.log(this) + this.listen('progress', (event) => { + // event.event is the event name (useful if you want to use a single callback fn for multiple event types) + // event.payload is the payload object + console.log(event.payload) + this.greet_message = event.payload + }) + }, methods: { login (e) { e.preventDefault() @@ -25,7 +34,7 @@ export default { e.preventDefault() if(!this.hideDownloadButton) { this.invoke("download", {}).then(value => { - this.greet_message = value + // this.greet_message = value }).catch(err => { this.greet_message = "Error: " + err }) @@ -33,7 +42,8 @@ export default { }, }, props: { - invoke: Object + invoke: Object, + listen: Object, }, template: `

Welcome to Tauri!

@@ -48,3 +58,5 @@ export default {

{{ greet_message }}

` } + +export default vue; \ No newline at end of file diff --git a/src/index.html b/src/index.html index 2e69577..b0b2d48 100644 --- a/src/index.html +++ b/src/index.html @@ -16,7 +16,7 @@
- +
diff --git a/src/main.js b/src/main.js index a1afad2..7fc3366 100644 --- a/src/main.js +++ b/src/main.js @@ -1,13 +1,15 @@ const { invoke } = window.__TAURI__.tauri; +const { listen } = window.__TAURI__.event; const { createApp } = Vue import loginpage from './components/login.js' -createApp({ +let app = createApp({ data() { return { - invoke: invoke + invoke: invoke, + listen: listen, } }, mounted() { @@ -16,4 +18,6 @@ createApp({ components: { loginpage } -}).mount('#container') +}); + +app.mount('#container')