feat: add menu item

This commit is contained in:
lencx 2022-12-11 21:17:44 +08:00
parent 050b7010fc
commit 3eac43541e
9 changed files with 66 additions and 43 deletions

View File

@ -12,7 +12,15 @@
},
"license": "MIT",
"author": "lencx <cxin1314@gmail.com>",
"keywords": ["chatgpt", "app", "desktop", "tauri", "macos", "linux", "windows"],
"keywords": [
"chatgpt",
"app",
"desktop",
"tauri",
"macos",
"linux",
"windows"
],
"homepage": "https://github.com/lencx/ChatGPT",
"bugs": "https://github.com/lencx/ChatGPT/issues",
"repository": {

View File

@ -1,4 +1,5 @@
use crate::{
app::window,
conf::{self, ChatConfJson},
utils,
};
@ -69,6 +70,9 @@ pub fn init(chat_conf: &conf::ChatConfJson, context: &Context<EmbeddedAssets>) -
always_on_top_menu.into(),
#[cfg(target_os = "macos")]
titlebar_menu.into(),
CustomMenuItem::new("switch_origin".to_string(), "Switch Origin")
.accelerator("CmdOrCtrl+O")
.into(),
CustomMenuItem::new("inject_script".to_string(), "Inject Script")
.accelerator("CmdOrCtrl+J")
.into(),
@ -152,6 +156,10 @@ pub fn menu_handler(event: WindowMenuEvent<tauri::Wry>) {
// Preferences
"inject_script" => open(&app, script_path),
"awesome" => open(&app, conf::AWESOME_URL.to_string()),
"switch_origin" => {
window::origin_window(&app);
// app.get_window("origin").unwrap().show();
}
"titlebar" => {
let chat_conf = conf::ChatConfJson::get_chat_conf();
ChatConfJson::amend(&serde_json::json!({ "titlebar": !chat_conf.titlebar })).unwrap();

View File

@ -1,13 +1,16 @@
use crate::{app::window, conf, utils};
use crate::{
app::window,
conf::{ChatConfJson, USER_AGENT},
utils,
};
use tauri::{utils::config::WindowUrl, window::WindowBuilder, App, Manager};
pub fn init(
app: &mut App,
chat_conf: conf::ChatConfJson,
chat_conf: ChatConfJson,
) -> std::result::Result<(), Box<dyn std::error::Error>> {
let tauri_conf = utils::get_tauri_conf().unwrap();
let url = tauri_conf.build.dev_path.to_string();
let theme = conf::ChatConfJson::theme();
let url = chat_conf.origin.to_string();
let theme = ChatConfJson::theme();
window::mini_window(&app.app_handle());
#[cfg(target_os = "macos")]
@ -18,13 +21,13 @@ pub fn init(
.hidden_title(true)
.theme(theme)
.always_on_top(chat_conf.always_on_top)
.title_bar_style(conf::ChatConfJson::titlebar())
.title_bar_style(ChatConfJson::titlebar())
.initialization_script(&utils::user_script())
.initialization_script(include_str!("../assets/html2canvas.js"))
.initialization_script(include_str!("../assets/jspdf.js"))
.initialization_script(include_str!("../assets/core.js"))
.initialization_script(include_str!("../assets/export.js"))
.user_agent(conf::USER_AGENT)
.user_agent(USER_AGENT)
.build()?;
#[cfg(not(target_os = "macos"))]
@ -40,7 +43,7 @@ pub fn init(
.initialization_script(include_str!("../assets/jspdf.js"))
.initialization_script(include_str!("../assets/core.js"))
.initialization_script(include_str!("../assets/export.js"))
.user_agent(conf::USER_AGENT)
.user_agent(USER_AGENT)
.build()?;
Ok(())

View File

@ -2,12 +2,10 @@ use crate::{conf, utils};
use tauri::{utils::config::WindowUrl, window::WindowBuilder};
pub fn mini_window(handle: &tauri::AppHandle) {
let tauri_conf = utils::get_tauri_conf().unwrap();
let url = tauri_conf.build.dev_path.to_string();
// let chat_conf = conf::ChatConfJson::get_chat_conf();
let chat_conf = conf::ChatConfJson::get_chat_conf();
let theme = conf::ChatConfJson::theme();
WindowBuilder::new(handle, "mini", WindowUrl::App(url.into()))
WindowBuilder::new(handle, "mini", WindowUrl::App(chat_conf.origin.into()))
.resizable(false)
.fullscreen(false)
.inner_size(360.0, 540.0)
@ -25,3 +23,16 @@ pub fn mini_window(handle: &tauri::AppHandle) {
.hide()
.unwrap();
}
pub fn origin_window(handle: &tauri::AppHandle) {
let theme = conf::ChatConfJson::theme();
WindowBuilder::new(handle, "main", WindowUrl::External("/".parse().unwrap()))
.resizable(false)
.fullscreen(false)
.inner_size(400.0, 300.0)
.always_on_top(true)
.theme(theme)
.build()
.unwrap();
}

View File

@ -71,7 +71,7 @@ async function init() {
document.addEventListener("click", (e) => {
const origin = e.target.closest("a");
if (origin && origin.href && origin.target !== '_self') {
origin.target = "_self";
invoke('open_link', { url: origin.href });
}
});

0
src-tauri/src/assets/origin.js vendored Normal file
View File

View File

@ -8,6 +8,12 @@ pub const USER_AGENT: &str = "5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKi
pub const PHONE_USER_AGENT: &str = "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1";
pub const ISSUES_URL: &str = "https://github.com/lencx/ChatGPT/issues";
pub const AWESOME_URL: &str = "https://github.com/lencx/ChatGPT/blob/main/AWESOME.md";
pub const DEFAULT_CHAT_CONF: &str = r#"{
"always_on_top": false,
"theme": "Light",
"titlebar": true,
"origin": "https://chat.openai.com/"
}"#;
pub struct ChatState {
pub always_on_top: Mutex<bool>,
@ -26,6 +32,7 @@ pub struct ChatConfJson {
pub always_on_top: bool,
pub theme: String,
pub titlebar: bool,
pub origin: String,
}
impl ChatConfJson {
@ -35,22 +42,10 @@ impl ChatConfJson {
let conf_file = ChatConfJson::conf_path();
if !exists(&conf_file) {
create_file(&conf_file).unwrap();
fs::write(&conf_file, DEFAULT_CHAT_CONF).unwrap();
#[cfg(target_os = "macos")]
let content = r#"{
"always_on_top": false,
"theme": "Light",
"titlebar": false
}"#;
#[cfg(not(target_os = "macos"))]
let content = r#"{
"always_on_top": false,
"theme": "Light",
"titlebar": true
}"#;
fs::write(&conf_file, content).unwrap();
ChatConfJson::amend(&serde_json::json!({ "titlebar": false })).unwrap();
}
conf_file
}
@ -77,7 +72,10 @@ impl ChatConfJson {
config.insert(k, v);
}
fs::write(ChatConfJson::conf_path(), serde_json::to_string(&config)?)?;
fs::write(
ChatConfJson::conf_path(),
serde_json::to_string_pretty(&config)?,
)?;
Ok(())
}
@ -100,11 +98,6 @@ impl ChatConfJson {
}
pub fn chat_conf_default() -> Self {
serde_json::from_value(serde_json::json!({
"always_on_top": false,
"theme": "Light",
"titlebar": true
}))
.unwrap()
serde_json::from_value(serde_json::json!(DEFAULT_CHAT_CONF)).unwrap()
}
}

View File

@ -4,18 +4,18 @@ use std::{
path::{Path, PathBuf},
process::Command,
};
use tauri::utils::config::Config;
// use tauri::utils::config::Config;
pub fn chat_root() -> PathBuf {
tauri::api::path::home_dir().unwrap().join(".chatgpt")
}
pub fn get_tauri_conf() -> Option<Config> {
let config_file = include_str!("../tauri.conf.json");
let config: Config =
serde_json::from_str(config_file).expect("failed to parse tauri.conf.json");
Some(config)
}
// pub fn get_tauri_conf() -> Option<Config> {
// let config_file = include_str!("../tauri.conf.json");
// let config: Config =
// serde_json::from_str(config_file).expect("failed to parse tauri.conf.json");
// Some(config)
// }
pub fn exists(path: &Path) -> bool {
Path::new(path).exists()

View File

@ -2,7 +2,7 @@
"build": {
"beforeDevCommand": "",
"beforeBuildCommand": "",
"devPath": "https://chat.openai.com/",
"devPath": "../dist",
"distDir": "../dist"
},
"package": {