From f19691557b294d5bbcfa3e15f06abf723afb1fdc Mon Sep 17 00:00:00 2001 From: Jonathan Ami <126116089+jonathan-ami@users.noreply.github.com> Date: Tue, 9 Apr 2024 15:51:09 -0700 Subject: [PATCH] small fixes but still broken --- src-tauri/src/config.rs | 38 ++++++++++++++++++++++++++++++++++---- src-tauri/src/main.rs | 19 +++++++++++++++++-- ui/index.html | 1 + ui/main.js | 25 ++++++++++++++++++++++++- ui/styles.css | 7 +++++++ 5 files changed, 83 insertions(+), 7 deletions(-) diff --git a/src-tauri/src/config.rs b/src-tauri/src/config.rs index a1f8326..66fba1b 100644 --- a/src-tauri/src/config.rs +++ b/src-tauri/src/config.rs @@ -7,7 +7,7 @@ use std::io::{Read, Seek, SeekFrom, Write}; use std::path::PathBuf; #[derive(Serialize, Deserialize)] -struct Config { +pub struct Config { programs: Vec, keybinds: HashMap, } @@ -37,7 +37,7 @@ pub fn set_data(shortcut: PathBuf, shortcut_num: usize) { println!("{:?}", shortcut); let mut path = match get_path() { Ok(path) => path, - Err(e) => return, + Err(_) => return, }; path.push("config.json"); @@ -46,10 +46,11 @@ pub fn set_data(shortcut: PathBuf, shortcut_num: usize) { .write(true) .read(true) .create(true) + //.truncate(true) .open(&path) { Ok(file) => file, - Err(e) => { + Err(_) => { eprintln!("Error opening file"); return; } @@ -60,10 +61,12 @@ pub fn set_data(shortcut: PathBuf, shortcut_num: usize) { eprintln!("Failed to read user data from file: {}", e); return; } + println!("json data: {}", json_data); let mut config = match serde_json::from_str(&json_data) { Ok(data) => data, - Err(_) => { + Err(e) => { + println!("error: {}", e); println!("creating a new config!"); Config::new() } @@ -76,6 +79,33 @@ pub fn set_data(shortcut: PathBuf, shortcut_num: usize) { } } +pub fn read_config() -> Result> { + let mut path = match get_path() { + Ok(path) => path, + Err(e) => return Err(e), + }; + path.push("config.json"); + println!("{:?}", path); + let mut file = match OpenOptions::new().read(true).open(&path) { + Ok(file) => file, + Err(e) => { + return Err(format!("Error opening file {}", e).into()); + } + }; + + let mut json_data = String::new(); + if let Err(e) = file.read_to_string(&mut json_data) { + return Err(format!("Failed to read user data from file {}", e).into()); + } + println!("config in read_config() {}:", json_data); + + let config = match serde_json::from_str(&json_data) { + Ok(data) => data, + Err(_) => Config::new(), + }; + Ok(config) +} + fn get_path() -> Result> { let mut appdata_path = match env::var_os("LOCALAPPDATA") { Some(path) => PathBuf::from(path), diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 2bd75f0..7b290e6 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -2,7 +2,9 @@ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] mod config; -use config::set_data; +use config::Config; +use config::{read_config, set_data}; +use serde::{Deserialize, Serialize}; use std::process::{Child, Command}; use std::sync::{Arc, Mutex}; use tauri::api::dialog::FileDialogBuilder; @@ -53,6 +55,15 @@ fn manage_core(core: State<'_, Arc>>) -> String { } } +#[tauri::command] +fn load_shortcut_data() -> String { + match read_config() { + Ok(config) => return serde_json::to_string(&config).expect("failed bud"), + Err(e) => println!("error reading config {}", e), + } + "failed to read config".to_string() +} + #[tauri::command] fn set_shortcut(shortcut_id: String) { let shortcut_id = match shortcut_id.parse::() { @@ -73,7 +84,11 @@ fn main() { let core = Arc::new(Mutex::new(CoreProcess::new())); tauri::Builder::default() .manage(core) - .invoke_handler(tauri::generate_handler![manage_core, set_shortcut]) + .invoke_handler(tauri::generate_handler![ + manage_core, + set_shortcut, + load_shortcut_data + ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); } diff --git a/ui/index.html b/ui/index.html index 05cfbca..2207149 100644 --- a/ui/index.html +++ b/ui/index.html @@ -41,6 +41,7 @@ +
diff --git a/ui/main.js b/ui/main.js index a31eeba..5c29baa 100644 --- a/ui/main.js +++ b/ui/main.js @@ -1,10 +1,32 @@ const { invoke } = window.__TAURI__.tauri; let startButton; +let configData; +let programShortcuts; + async function start() { startButton.textContent = await invoke("manage_core"); } +async function loadData(){ + configData = await invoke("load_shortcut_data"); + console.log(configData); + programShortcuts = JSON.parse(configData)["programs"]; + const container = document.getElementById("shortcutContainer"); + container.innerHTML = ""; + for (let i = 0; i < programShortcuts.length; i++){ + if(programShortcuts[i] !== ""){ + const textNode = document.createTextNode("Shorcut " + (i+1) + ": " + programShortcuts[i]); + const div = document.createElement("div"); + div.appendChild(textNode); + div.classList.add("shortcut-text"); + container.appendChild(div); + } + } +} + +loadData(); + window.addEventListener("DOMContentLoaded", () => { startButton = document.getElementById("startButton"); const shortcutButton = document.querySelectorAll(".shortcut-button"); @@ -14,9 +36,10 @@ window.addEventListener("DOMContentLoaded", () => { }); shortcutButton.forEach(function (element) { const button = element.querySelector("button"); + const id = button.id.substring("shortcutButton".length); button.addEventListener("click", function () { - const id = button.id.substring("shortcutButton".length); invoke("set_shortcut", { shortcutId: id }); + loadData(); }); }); }); diff --git a/ui/styles.css b/ui/styles.css index ad86631..08670b0 100644 --- a/ui/styles.css +++ b/ui/styles.css @@ -47,6 +47,13 @@ body { cursor: pointer; transition: background-color 0.3s ease; } +.shortcut-text{ + background-color: #007bff; /* Blue background color */ + color: white; /* Text color */ + padding: 5px 10px; /* Padding */ + border-radius: 5px; /* Rounded corners */ + margin-bottom: 5px; /* Margin between text nodes */ +} .shortcut-button button:hover { background-color: rgba(255, 255, 255, 0.2);