mirror of
				https://github.com/MindWorkAI/AI-Studio.git
				synced 2025-11-04 06:00:21 +00:00 
			
		
		
		
	Added the possibility that a user selects a directory
This commit is contained in:
		
							parent
							
								
									07832061ef
								
							
						
					
					
						commit
						e9f4d5fecd
					
				@ -0,0 +1,8 @@
 | 
			
		||||
namespace AIStudio.Tools.Rust;
 | 
			
		||||
 | 
			
		||||
/// <summary>
 | 
			
		||||
/// Data structure for selecting a directory.
 | 
			
		||||
/// </summary>
 | 
			
		||||
/// <param name="UserCancelled">Was the directory selection canceled?</param>
 | 
			
		||||
/// <param name="SelectedDirectory">The selected directory, if any.</param>
 | 
			
		||||
public readonly record struct DirectorySelectionResponse(bool UserCancelled, string SelectedDirectory);
 | 
			
		||||
							
								
								
									
										7
									
								
								app/MindWork AI Studio/Tools/Rust/PreviousDirectory.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								app/MindWork AI Studio/Tools/Rust/PreviousDirectory.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,7 @@
 | 
			
		||||
namespace AIStudio.Tools.Rust;
 | 
			
		||||
 | 
			
		||||
/// <summary>
 | 
			
		||||
/// Data structure for selecting a directory when a previous directory was selected.
 | 
			
		||||
/// </summary>
 | 
			
		||||
/// <param name="Path">The path of the previous directory.</param>
 | 
			
		||||
public readonly record struct PreviousDirectory(string Path);
 | 
			
		||||
@ -322,6 +322,19 @@ public sealed class RustService : IDisposable
 | 
			
		||||
        
 | 
			
		||||
        return state;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public async Task<DirectorySelectionResponse> SelectDirectory(string title, string? initialDirectory = null)
 | 
			
		||||
    {
 | 
			
		||||
        PreviousDirectory? previousDirectory = initialDirectory is null ? null : new (initialDirectory);
 | 
			
		||||
        var result = await this.http.PostAsJsonAsync($"/select/directory?title={title}", previousDirectory, this.jsonRustSerializerOptions);
 | 
			
		||||
        if (!result.IsSuccessStatusCode)
 | 
			
		||||
        {
 | 
			
		||||
            this.logger!.LogError($"Failed to select a directory: '{result.StatusCode}'");
 | 
			
		||||
            return new DirectorySelectionResponse(true, string.Empty);
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        return await result.Content.ReadFromJsonAsync<DirectorySelectionResponse>(this.jsonRustSerializerOptions);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #region IDisposable
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -9,7 +9,7 @@ authors = ["Thorsten Sommer"]
 | 
			
		||||
tauri-build = { version = "1.5", features = [] }
 | 
			
		||||
 | 
			
		||||
[dependencies]
 | 
			
		||||
tauri = { version = "1.8", features = [ "http-all", "updater", "shell-sidecar", "shell-open"] }
 | 
			
		||||
tauri = { version = "1.8", features = [ "http-all", "updater", "shell-sidecar", "shell-open", "dialog"] }
 | 
			
		||||
tauri-plugin-window-state = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
 | 
			
		||||
serde = { version = "1.0", features = ["derive"] }
 | 
			
		||||
serde_json = "1.0"
 | 
			
		||||
 | 
			
		||||
@ -2,11 +2,13 @@ use std::sync::Mutex;
 | 
			
		||||
use std::time::Duration;
 | 
			
		||||
use log::{error, info, warn};
 | 
			
		||||
use once_cell::sync::Lazy;
 | 
			
		||||
use rocket::get;
 | 
			
		||||
use rocket::{get, post};
 | 
			
		||||
use rocket::serde::json::Json;
 | 
			
		||||
use rocket::serde::Serialize;
 | 
			
		||||
use serde::Deserialize;
 | 
			
		||||
use tauri::updater::UpdateResponse;
 | 
			
		||||
use tauri::{Manager, Window};
 | 
			
		||||
use tauri::api::dialog::blocking::FileDialogBuilder;
 | 
			
		||||
use tokio::time;
 | 
			
		||||
use crate::api_token::APIToken;
 | 
			
		||||
use crate::dotnet::stop_dotnet_server;
 | 
			
		||||
@ -219,4 +221,53 @@ pub async fn install_update(_token: APIToken) {
 | 
			
		||||
            error!(Source = "Updater"; "No update available to install. Did you check for updates first?");
 | 
			
		||||
        },
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Let the user select a directory.
 | 
			
		||||
#[post("/select/directory?<title>", data = "<previous_directory>")]
 | 
			
		||||
pub fn select_directory(_token: APIToken, title: &str, previous_directory: Option<Json<PreviousDirectory>>) -> Json<DirectorySelectionResponse> {
 | 
			
		||||
    let folder_path = match previous_directory {
 | 
			
		||||
        Some(previous) => {
 | 
			
		||||
            let previous_path = previous.path.as_str();
 | 
			
		||||
            FileDialogBuilder::new()
 | 
			
		||||
                .set_title(title)
 | 
			
		||||
                .set_directory(previous_path)
 | 
			
		||||
                .pick_folder()
 | 
			
		||||
        },
 | 
			
		||||
 | 
			
		||||
        None => {
 | 
			
		||||
            FileDialogBuilder::new()
 | 
			
		||||
                .set_title(title)
 | 
			
		||||
                .pick_folder()
 | 
			
		||||
        },
 | 
			
		||||
    };
 | 
			
		||||
    
 | 
			
		||||
    match folder_path {
 | 
			
		||||
        Some(path) => {
 | 
			
		||||
            info!("User selected directory: {path:?}");
 | 
			
		||||
            Json(DirectorySelectionResponse {
 | 
			
		||||
                user_cancelled: false,
 | 
			
		||||
                selected_directory: path.to_str().unwrap().to_string(),
 | 
			
		||||
            })
 | 
			
		||||
        },
 | 
			
		||||
 | 
			
		||||
        None => {
 | 
			
		||||
            info!("User cancelled directory selection.");
 | 
			
		||||
            Json(DirectorySelectionResponse {
 | 
			
		||||
                user_cancelled: true,
 | 
			
		||||
                selected_directory: String::from(""),
 | 
			
		||||
            })
 | 
			
		||||
        },
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Clone, Deserialize)]
 | 
			
		||||
pub struct PreviousDirectory {
 | 
			
		||||
    path: String,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Serialize)]
 | 
			
		||||
pub struct DirectorySelectionResponse {
 | 
			
		||||
    user_cancelled: bool,
 | 
			
		||||
    selected_directory: String,
 | 
			
		||||
}
 | 
			
		||||
@ -84,6 +84,7 @@ pub fn start_runtime_api() {
 | 
			
		||||
                crate::clipboard::set_clipboard,
 | 
			
		||||
                crate::app_window::check_for_update,
 | 
			
		||||
                crate::app_window::install_update,
 | 
			
		||||
                crate::app_window::select_directory,
 | 
			
		||||
                crate::secret::get_secret,
 | 
			
		||||
                crate::secret::store_secret,
 | 
			
		||||
                crate::secret::delete_secret,
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user