Skip to content

Commit

Permalink
feat: use embedded stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
SpikeHD committed Jul 15, 2023
1 parent ff38a23 commit 009976a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

.yarn/*

node_modules
dist
dist-ssr
Expand Down
23 changes: 1 addition & 22 deletions src-tauri/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,4 @@ fn open_folder(path: PathBuf) {
#[cfg(target_os = "linux")]
fn open_folder(path: PathBuf) {
Command::new("xdg-open").arg(path).spawn().unwrap();
}

#[cfg(target_os = "windows")]
pub fn resource_folder() -> PathBuf {
let mut path = std::env::current_exe().unwrap();
path.pop();

path
}

#[cfg(target_os = "linux")]
pub fn resource_folder() -> PathBuf {
PathBuf::from("/usr/lib/dorion/")
}

#[cfg(target_os = "macos")]
pub fn resource_folder() -> PathBuf {
let mut path = std::env::current_exe().unwrap();
path.pop();

path
}
}
20 changes: 15 additions & 5 deletions src-tauri/src/injection.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use std::{collections::HashMap, env, fs, path::PathBuf, thread, time::Duration};
use tauri::{regex::Regex, Manager};

use crate::{helpers::resource_folder, js_preprocess::eval_js_imports, plugin};
use crate::{js_preprocess::eval_js_imports, plugin};

#[tauri::command]
pub async fn get_injection_js(theme_js: &str) -> Result<String, ()> {
pub async fn get_injection_js(win: tauri::Window, theme_js: &str) -> Result<String, ()> {
let theme_rxg = Regex::new(r"/\* __THEMES__ \*/").unwrap();
let injection_js = match fs::read_to_string(resource_folder().join("injection/injection_min.js"))
let js_path = win
.app_handle()
.path_resolver()
.resolve_resource(PathBuf::from("injection/injection_min.js"))
.unwrap();
let injection_js = match fs::read_to_string(js_path)
{
Ok(f) => f,
Err(e) => {
Expand All @@ -33,14 +38,19 @@ pub fn do_injection(win: tauri::Window) {

// Gotta make sure the window location is where it needs to be
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_secs(2));
std::thread::sleep(std::time::Duration::from_secs(1));

preinject(&win);
});
}

pub fn preinject(window: &tauri::Window) {
let injection_js = match fs::read_to_string(resource_folder().join("injection/preinject_min.js"))
let js_path = window
.app_handle()
.path_resolver()
.resolve_resource(PathBuf::from("injection/preinject_min.js"))
.unwrap();
let injection_js = match fs::read_to_string(js_path)
{
Ok(f) => f,
Err(e) => {
Expand Down
26 changes: 15 additions & 11 deletions src-tauri/src/local_html.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
use std::fs;
use std::{fs, path::PathBuf};

use crate::helpers::resource_folder;
use tauri::Manager;

pub fn get_html(dir: &str) -> String {
match fs::read_to_string(resource_folder().join(dir)) {
pub fn get_html(app: tauri::AppHandle, dir: &str) -> String {
let dir = app
.path_resolver()
.resolve_resource(PathBuf::from(dir))
.unwrap();
match fs::read_to_string(&dir) {
Ok(f) => f,
Err(e) => {
println!("Failed to read {} in local dir: {}", dir, e);
println!("Failed to read {:?} in local dir: {}", dir, e);

String::new()
}
}
}

#[tauri::command]
pub fn get_index() -> String {
get_html("html/index.html")
pub fn get_index(win: tauri::Window) -> String {
get_html(win.app_handle(), "html/index.html")
}

#[tauri::command]
pub fn get_top_bar() -> String {
get_html("html/top.html")
pub fn get_top_bar(win: tauri::Window) -> String {
get_html(win.app_handle(), "html/top.html")
}

#[tauri::command]
pub fn get_notif() -> String {
get_html("html/notification.html")
pub fn get_notif(win: tauri::Window) -> String {
get_html(win.app_handle(), "html/notification.html")
}
10 changes: 7 additions & 3 deletions src-tauri/src/notifications.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::PathBuf;

use tauri::Icon;
use tauri::{Icon, Manager};

pub fn set_notif_icon(window: tauri::Window, amount: u16) {
let icon_num = if amount > 9 { 9 } else { amount };
Expand All @@ -10,15 +10,19 @@ pub fn set_notif_icon(window: tauri::Window, amount: u16) {
let mut icon_path = PathBuf::from("icons/icon");
icon_path.set_extension("ico");

window.set_icon(Icon::File(icon_path)).unwrap_or(());
window.set_icon(Icon::File(
window.app_handle().path_resolver().resolve_resource(icon_path).unwrap(),
)).unwrap_or(());
return;
}

let icon_name = format!("icon_{}", icon_num);
let mut icon_path = PathBuf::from("icons/").join(icon_name);
icon_path.set_extension("ico");

window.set_icon(Icon::File(icon_path)).unwrap_or(());
window.set_icon(Icon::File(
window.app_handle().path_resolver().resolve_resource(icon_path).unwrap(),
)).unwrap_or(());
}

#[tauri::command]
Expand Down

0 comments on commit 009976a

Please sign in to comment.