Skip to content

Commit

Permalink
small fixes but still broken
Browse files Browse the repository at this point in the history
  • Loading branch information
jhideki committed Apr 9, 2024
1 parent adaf7de commit f196915
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 7 deletions.
38 changes: 34 additions & 4 deletions src-tauri/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
keybinds: HashMap<String, u32>,
}
Expand Down Expand Up @@ -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");
Expand All @@ -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;
}
Expand All @@ -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()
}
Expand All @@ -76,6 +79,33 @@ pub fn set_data(shortcut: PathBuf, shortcut_num: usize) {
}
}

pub fn read_config() -> Result<Config, Box<dyn Error>> {
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<PathBuf, Box<dyn Error>> {
let mut appdata_path = match env::var_os("LOCALAPPDATA") {
Some(path) => PathBuf::from(path),
Expand Down
19 changes: 17 additions & 2 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,6 +55,15 @@ fn manage_core(core: State<'_, Arc<Mutex<CoreProcess>>>) -> 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::<usize>() {
Expand All @@ -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");
}
1 change: 1 addition & 0 deletions ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<button id="shortcutButton10">Set Shortcut 10</button>
</div>
</div>
<div class"bottom-section" id="shortcutContainer"></div>
<button id="startButton" class="start-button">Start Program</button>
</div>
</body>
Expand Down
25 changes: 24 additions & 1 deletion ui/main.js
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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();
});
});
});
7 changes: 7 additions & 0 deletions ui/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f196915

Please sign in to comment.