Skip to content

Commit

Permalink
Merge pull request #32 from RyKilleen/refactor-files
Browse files Browse the repository at this point in the history
refactor: reorg, replace extra clones with references, remove comments
  • Loading branch information
RyKilleen authored Mar 10, 2022
2 parents 0b4cb46 + 5a3c505 commit deacccd
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 81 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "brancato",
"version": "0.1.3",
"version": "0.2.0",
"private": true,
"dependencies": {
"@algolia/autocomplete-js": "^1.5.3",
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "brancato"
version = "0.1.3"
version = "0.2.0"
description = "A tool for stage-managing your life"
authors = ["Ryan Killeen"]
license = ""
Expand Down
3 changes: 0 additions & 3 deletions src-tauri/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub fn get_config() -> Config {
let read_path = config_dir.join("config.json");

let config_file = fs::read_to_string(&read_path);
println!("read path: {:#?}", read_path);
let config: Config = match config_file {
Ok(file) => serde_json::from_str(&file).unwrap(),
Err(_) => default,
Expand All @@ -37,8 +36,6 @@ pub fn set_config(config: &Config) {
let path = config_dir.join("config.json");
let prefix = path.parent().unwrap();
fs::create_dir_all(prefix).unwrap();
println!("save path: {:#?}", path);
println!("save data: {}", data);
fs::write(path, data).expect("Unable to write file");
}
}
124 changes: 51 additions & 73 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,45 @@
)]

mod config;
mod windows;
mod workflows;

use std::{env, path::Path, sync::Mutex};
use std::{env, sync::Mutex};
use tauri::{
AppHandle, CustomMenuItem, GlobalShortcutManager, Manager, RunEvent, State, SystemTray,
SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem,
};

use config::Config;
use windows::focus_window;
use workflows::run_step;

extern crate open;
#[derive(Default)]
struct AppState(Mutex<Config>);

// On Windows, some apps expect a relative working directory (Looking at you, OBS....)
fn open_app(path: &str) {
let path = Path::new(&path);
println!("{}", path.display());
let dir = path.parent().expect("Path doesn't exist");
env::set_current_dir(dir).expect("Couldn't set current dir");
open::that(path).expect("Dang")
fn _get_state(state: State<AppState>) -> Config {
let config_mutex = state.0.lock().expect("Could not lock mutex");
let config = config_mutex.clone();
config
}

#[tauri::command]
fn save_workflows(state: State<AppState>, app: AppHandle, config: Config) {
fn save_workflows(
state: State<AppState>,
app: AppHandle,
config: Config,
) -> Result<(), tauri::Error> {
let mut app_state = state.0.lock().expect("Could not lock mutex");
// Save to file
config::set_config(&config);
// Update state
*app_state = config;
// Instruct client
app.get_window("omnibar").unwrap().emit("state-updated", "");
}

#[derive(Default)]
struct AppState(Mutex<Config>);

fn _get_state(state: State<AppState>) -> Config {
let config_mutex = state.0.lock().expect("Could not lock mutex");
let config = config_mutex.clone();
config
app
.get_window("omnibar")
.unwrap()
.emit("state-updated", "")?;
Ok(())
}

#[tauri::command]
Expand All @@ -54,34 +54,33 @@ fn get_state(state: State<AppState>) -> Config {
async fn run_workflow(state: State<'_, AppState>, label: String) -> Result<(), ()> {
let current_state = _get_state(state);

let workflow = current_state
let mut workflow = current_state
.workflows
.iter()
.into_iter()
.find(|x| x.name == label)
.unwrap();
.expect("Couldn't find workflow");

let _ = &workflow
.steps
.iter_mut()
.for_each(|step| run_step(&step.value));

for p in &workflow.steps {
if p.value.contains("https") {
open::that_in_background(&p.value);
} else {
open_app(&p.value);
}
}
Ok(())
}

#[tauri::command]
async fn open_settings(app: AppHandle) {
focus_window(&app, "settings".to_owned());
async fn open_settings(app: AppHandle) -> Result<(), tauri::Error> {
focus_window(&app, "settings".to_owned())?;
Ok(())
}

fn focus_window(app: &AppHandle, label: String) {
let window = app.get_window(&label).unwrap();
window.show();
window.unminimize();
window.set_focus();
}
fn open_omnibar(app: &AppHandle) -> Result<(), tauri::Error> {
let label = "omnibar".to_owned();
focus_window(app, String::from(&label))?;
app.get_window(&label).unwrap().emit("omnibar-focus", "")?;

Ok(())
}
fn main() {
let user_config = config::get_config();

Expand All @@ -103,27 +102,16 @@ fn main() {
size: _,
..
} => {
focus_window(app, "settings".to_owned());
}
SystemTrayEvent::RightClick {
position: _,
size: _,
..
} => {
println!("system tray received a right click");
}
SystemTrayEvent::DoubleClick {
position: _,
size: _,
..
} => {
println!("system tray received a double click");
focus_window(app, "settings".to_owned()).ok();
}

SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
"quit" => {
std::process::exit(0);
}
"settings" => focus_window(app, "settings".to_owned()),
"settings" => {
focus_window(app, "settings".to_owned()).ok();
}
"hide" => {
app.get_window("settings").unwrap().hide().unwrap();
}
Expand All @@ -132,18 +120,15 @@ fn main() {
_ => {}
})
.on_window_event(|event| match event.event() {
tauri::WindowEvent::Focused(focused) => {
let label = event.window().label();
match label {
"settings" => {}
"omnibar" => {
if !focused {
event.window().hide().expect("Failed to hide window");
}
tauri::WindowEvent::Focused(focused) => match event.window().label() {
"settings" => {}
"omnibar" => {
if !focused {
event.window().hide().expect("Failed to hide window");
}
_ => {}
}
}
_ => {}
},
_ => {}
})
.manage(AppState(Mutex::new(user_config)))
Expand All @@ -163,22 +148,15 @@ fn main() {
app_handle
.global_shortcut_manager()
.register("Alt+m", move || {
let app_handle = app_handle.clone();
let label = "omnibar".to_owned();
focus_window(&app_handle, "omnibar".to_owned());
app_handle
.get_window(&label)
.unwrap()
.emit("omnibar-focus", "");
open_omnibar(&app_handle).ok();
})
.expect("Couldn't create shortcut");
}

// // Triggered when a window is trying to close
RunEvent::CloseRequested { label, api, .. } => {
api.prevent_close();
let app_handle = app_handle.clone();
app_handle.get_window(&label).unwrap().hide().unwrap();
let _ = &app_handle.get_window(&label).unwrap().hide().unwrap();
}

// Keep the event loop running even if all windows are closed
Expand Down
9 changes: 9 additions & 0 deletions src-tauri/src/windows.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use tauri::{AppHandle, Manager};

pub fn focus_window(app: &AppHandle, label: String) -> Result<(), tauri::Error> {
let window = app.get_window(&label).unwrap();
window.show()?;
window.unminimize()?;
window.set_focus()?;
Ok(())
}
19 changes: 19 additions & 0 deletions src-tauri/src/workflows.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
extern crate open;
use serde::{Deserialize, Serialize};
use std::{env, path::Path};

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Step {
pub value: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Workflow {
pub name: String,
pub steps: Vec<Step>,
}

// On Windows, some apps expect a relative working directory (Looking at you, OBS....)
pub fn open_app(path: &str) {
let path = Path::new(&path);
let dir = path.parent().expect("Path doesn't exist");
env::set_current_dir(dir).expect("Couldn't set current dir");
open::that(path).expect("Dang")
}

pub fn run_step(path: &str) {
if path.contains("https") {
open::that_in_background(&path);
} else {
open_app(&path);
}
}

0 comments on commit deacccd

Please sign in to comment.