mirror of
https://github.com/FranP-code/ChatGPT.git
synced 2025-10-13 00:13:25 +00:00
feat: export history
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
use tauri::Manager;
|
||||
use crate::utils;
|
||||
use std::fs;
|
||||
use tauri::{api, command, AppHandle, Manager};
|
||||
|
||||
#[tauri::command]
|
||||
pub fn drag_window(app: tauri::AppHandle) {
|
||||
#[command]
|
||||
pub fn drag_window(app: AppHandle) {
|
||||
app.get_window("core").unwrap().start_dragging().unwrap();
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn fullscreen(app: tauri::AppHandle) {
|
||||
#[command]
|
||||
pub fn fullscreen(app: AppHandle) {
|
||||
let win = app.get_window("core").unwrap();
|
||||
if win.is_fullscreen().unwrap() {
|
||||
win.set_fullscreen(false).unwrap();
|
||||
@@ -14,3 +16,15 @@ pub fn fullscreen(app: tauri::AppHandle) {
|
||||
win.set_fullscreen(true).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[command]
|
||||
pub fn download(_app: AppHandle, name: String, blob: Vec<u8>) {
|
||||
let path = api::path::download_dir().unwrap().join(name);
|
||||
fs::write(&path, blob).unwrap();
|
||||
utils::open_file(path);
|
||||
}
|
||||
|
||||
#[command]
|
||||
pub fn open_link(app: AppHandle, url: String) {
|
||||
api::shell::open(&app.shell_scope(), url, None).unwrap();
|
||||
}
|
||||
|
||||
@@ -1,22 +1,17 @@
|
||||
use crate::utils;
|
||||
use crate::{conf, utils};
|
||||
use tauri::{
|
||||
utils::assets::EmbeddedAssets, AboutMetadata, AppHandle, Context, CustomMenuItem, Manager,
|
||||
Menu, MenuItem, Submenu, SystemTray, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem,
|
||||
WindowMenuEvent,
|
||||
Menu, MenuItem, Submenu, SystemTray, SystemTrayEvent, SystemTrayMenu, WindowMenuEvent,
|
||||
};
|
||||
|
||||
// --- Menu
|
||||
pub fn init(context: &Context<EmbeddedAssets>) -> Menu {
|
||||
pub fn init(chat_conf: &conf::ChatConfJson, context: &Context<EmbeddedAssets>) -> Menu {
|
||||
let name = &context.package_info().name;
|
||||
let app_menu = Submenu::new(
|
||||
name,
|
||||
Menu::new()
|
||||
.add_native_item(MenuItem::About(name.into(), AboutMetadata::default()))
|
||||
.add_native_item(MenuItem::Separator)
|
||||
.add_item(
|
||||
CustomMenuItem::new("inject_script".to_string(), "Inject Script")
|
||||
.accelerator("CmdOrCtrl+J"),
|
||||
)
|
||||
.add_native_item(MenuItem::Separator)
|
||||
.add_native_item(MenuItem::Hide)
|
||||
.add_native_item(MenuItem::HideOthers)
|
||||
@@ -25,6 +20,28 @@ pub fn init(context: &Context<EmbeddedAssets>) -> Menu {
|
||||
.add_native_item(MenuItem::Quit),
|
||||
);
|
||||
|
||||
let always_on_top = CustomMenuItem::new("always_on_top".to_string(), "Always On Top")
|
||||
.accelerator("CmdOrCtrl+T");
|
||||
|
||||
let preferences_menu = Submenu::new(
|
||||
"Preferences",
|
||||
Menu::new()
|
||||
.add_item(
|
||||
CustomMenuItem::new("inject_script".to_string(), "Inject Script")
|
||||
.accelerator("CmdOrCtrl+J"),
|
||||
)
|
||||
.add_item(if chat_conf.always_on_top {
|
||||
always_on_top.selected()
|
||||
} else {
|
||||
always_on_top
|
||||
})
|
||||
.add_native_item(MenuItem::Separator)
|
||||
.add_item(
|
||||
CustomMenuItem::new("awesome".to_string(), "Awesome ChatGPT")
|
||||
.accelerator("CmdOrCtrl+Z"),
|
||||
),
|
||||
);
|
||||
|
||||
let edit_menu = Submenu::new(
|
||||
"Edit",
|
||||
Menu::new()
|
||||
@@ -74,6 +91,7 @@ pub fn init(context: &Context<EmbeddedAssets>) -> Menu {
|
||||
|
||||
Menu::new()
|
||||
.add_submenu(app_menu)
|
||||
.add_submenu(preferences_menu)
|
||||
.add_submenu(edit_menu)
|
||||
.add_submenu(view_menu)
|
||||
.add_submenu(help_menu)
|
||||
@@ -83,12 +101,27 @@ pub fn init(context: &Context<EmbeddedAssets>) -> Menu {
|
||||
pub fn menu_handler(event: WindowMenuEvent<tauri::Wry>) {
|
||||
let win = Some(event.window()).unwrap();
|
||||
let app = win.app_handle();
|
||||
let state: tauri::State<conf::ChatState> = app.state();
|
||||
let script_path = utils::script_path().to_string_lossy().to_string();
|
||||
let issues_url = "https://github.com/lencx/ChatGPT/issues".to_string();
|
||||
let menu_id = event.menu_item_id();
|
||||
|
||||
match event.menu_item_id() {
|
||||
// App
|
||||
"inject_script" => inject_script(&app, script_path),
|
||||
let core_window = app.get_window("core").unwrap();
|
||||
let menu_handle = core_window.menu_handle();
|
||||
|
||||
match menu_id {
|
||||
// Preferences
|
||||
"inject_script" => open(&app, script_path),
|
||||
"awesome" => open(&app, conf::AWESOME_URL.to_string()),
|
||||
"always_on_top" => {
|
||||
let mut always_on_top = state.always_on_top.lock().unwrap();
|
||||
*always_on_top = !*always_on_top;
|
||||
menu_handle
|
||||
.get_item(menu_id)
|
||||
.set_selected(*always_on_top)
|
||||
.unwrap();
|
||||
win.set_always_on_top(*always_on_top).unwrap();
|
||||
conf::ChatConfJson::update_chat_conf(*always_on_top);
|
||||
}
|
||||
// View
|
||||
"reload" => win.eval("window.location.reload()").unwrap(),
|
||||
"go_back" => win.eval("window.history.go(-1)").unwrap(),
|
||||
@@ -111,7 +144,7 @@ pub fn menu_handler(event: WindowMenuEvent<tauri::Wry>) {
|
||||
)
|
||||
.unwrap(),
|
||||
// Help
|
||||
"report_bug" => inject_script(&app, issues_url),
|
||||
"report_bug" => open(&app, conf::ISSUES_URL.to_string()),
|
||||
"dev_tools" => {
|
||||
win.open_devtools();
|
||||
win.close_devtools();
|
||||
@@ -122,35 +155,24 @@ pub fn menu_handler(event: WindowMenuEvent<tauri::Wry>) {
|
||||
|
||||
// --- SystemTray Menu
|
||||
pub fn tray_menu() -> SystemTray {
|
||||
SystemTray::new().with_menu(
|
||||
SystemTrayMenu::new()
|
||||
.add_item(CustomMenuItem::new("show".to_string(), "Show ChatGPT"))
|
||||
.add_item(CustomMenuItem::new("hide".to_string(), "Hide ChatGPT"))
|
||||
.add_item(CustomMenuItem::new(
|
||||
"inject_script".to_string(),
|
||||
"Inject Script",
|
||||
))
|
||||
.add_native_item(SystemTrayMenuItem::Separator)
|
||||
.add_item(CustomMenuItem::new("quit".to_string(), "Quit ChatGPT")),
|
||||
)
|
||||
SystemTray::new().with_menu(SystemTrayMenu::new())
|
||||
}
|
||||
|
||||
// --- SystemTray Event
|
||||
pub fn tray_handler(app: &AppHandle, event: SystemTrayEvent) {
|
||||
let script_path = utils::script_path().to_string_lossy().to_string();
|
||||
let win = app.get_window("core").unwrap();
|
||||
|
||||
if let SystemTrayEvent::MenuItemClick { id, .. } = event {
|
||||
match id.as_str() {
|
||||
"quit" => std::process::exit(0),
|
||||
"show" => win.show().unwrap(),
|
||||
"hide" => win.hide().unwrap(),
|
||||
"inject_script" => inject_script(app, script_path),
|
||||
_ => (),
|
||||
if let SystemTrayEvent::LeftClick { .. } = event {
|
||||
// TODO: tray window
|
||||
if win.is_visible().unwrap() {
|
||||
win.hide().unwrap();
|
||||
} else {
|
||||
win.show().unwrap();
|
||||
win.set_focus().unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn inject_script(app: &AppHandle, path: String) {
|
||||
pub fn open(app: &AppHandle, path: String) {
|
||||
tauri::api::shell::open(&app.shell_scope(), path, None).unwrap();
|
||||
}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
use crate::utils;
|
||||
use crate::{conf, utils};
|
||||
use tauri::{utils::config::WindowUrl, window::WindowBuilder, App};
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
use tauri::TitleBarStyle;
|
||||
|
||||
pub fn init(app: &mut App) -> std::result::Result<(), Box<dyn std::error::Error>> {
|
||||
pub fn init(
|
||||
app: &mut App,
|
||||
chat_conf: conf::ChatConfJson,
|
||||
) -> std::result::Result<(), Box<dyn std::error::Error>> {
|
||||
let conf = utils::get_tauri_conf().unwrap();
|
||||
let url = conf.build.dev_path.to_string();
|
||||
|
||||
@@ -15,12 +18,13 @@ pub fn init(app: &mut App) -> std::result::Result<(), Box<dyn std::error::Error>
|
||||
.inner_size(800.0, 600.0)
|
||||
.hidden_title(true)
|
||||
.title_bar_style(TitleBarStyle::Overlay)
|
||||
.always_on_top(chat_conf.always_on_top)
|
||||
.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/import.js"))
|
||||
.user_agent("5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36")
|
||||
.user_agent(conf::USER_AGENT)
|
||||
.build()?;
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
@@ -29,12 +33,13 @@ pub fn init(app: &mut App) -> std::result::Result<(), Box<dyn std::error::Error>
|
||||
.resizable(true)
|
||||
.fullscreen(false)
|
||||
.inner_size(800.0, 600.0)
|
||||
.always_on_top(chat_conf.always_on_top)
|
||||
.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/import.js"))
|
||||
.user_agent("5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36")
|
||||
.user_agent(conf::USER_AGENT)
|
||||
.build()?;
|
||||
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user