1
0
mirror of https://github.com/lencx/ChatGPT.git synced 2024-10-01 01:06:13 -04:00

chore: dalle2

This commit is contained in:
lencx 2023-01-06 14:39:25 +08:00
parent d0df8df108
commit 8c2303dec9
9 changed files with 86 additions and 41 deletions

View File

@ -29,9 +29,12 @@ reqwest = "0.11.13"
wry = "0.23.4"
dark-light = "1.0.0"
[dependencies.tauri-plugin-log]
git = "https://github.com/tauri-apps/tauri-plugin-log"
git = "https://github.com/lencx/tauri-plugin-log"
branch = "dev"
features = ["colored"]
[dependencies.tauri-plugin-autostart]
git = "https://github.com/lencx/tauri-plugin-autostart"
branch = "dev"
[features]
# by default Tauri runs in production mode

View File

@ -15,7 +15,11 @@ pub fn drag_window(app: AppHandle) {
#[command]
pub fn dalle2_window(app: AppHandle, query: String) {
window::dalle2_window(&app.app_handle(), query);
window::dalle2_window(
&app.app_handle(),
Some(query),
Some("ChatGPT & DALL·E 2".to_string()),
);
}
#[command]
@ -56,8 +60,8 @@ pub fn reset_chat_conf() -> ChatConfJson {
}
#[command]
pub fn run_check_update(app: AppHandle, silent: bool) {
utils::run_check_update(app, silent).unwrap();
pub fn run_check_update(app: AppHandle, silent: bool, has_msg: Option<bool>) {
utils::run_check_update(app, silent, has_msg).unwrap();
}
#[command]

View File

@ -1,4 +1,5 @@
use crate::{
app::window,
conf::{self, ChatConfJson},
utils,
};
@ -11,8 +12,6 @@ use tauri_plugin_positioner::{on_tray_event, Position, WindowExt};
#[cfg(target_os = "macos")]
use tauri::AboutMetadata;
use super::window;
// --- Menu
pub fn init() -> Menu {
let chat_conf = ChatConfJson::get_chat_conf();
@ -70,7 +69,15 @@ pub fn init() -> Menu {
CustomMenuItem::new("control_center".to_string(), "Control Center")
.accelerator("CmdOrCtrl+Shift+P")
.into(),
CustomMenuItem::new("dall_e2".to_string(), "Search DALLE-2").into(),
MenuItem::Separator.into(),
stay_on_top_menu.into(),
#[cfg(target_os = "macos")]
titlebar_menu.into(),
#[cfg(target_os = "macos")]
CustomMenuItem::new("hide_dock_icon".to_string(), "Hide Dock Icon").into(),
CustomMenuItem::new("inject_script".to_string(), "Inject Script")
.accelerator("CmdOrCtrl+J")
.into(),
MenuItem::Separator.into(),
Submenu::new(
"Theme",
@ -111,14 +118,6 @@ pub fn init() -> Menu {
// })
)
.into(),
stay_on_top_menu.into(),
#[cfg(target_os = "macos")]
titlebar_menu.into(),
#[cfg(target_os = "macos")]
CustomMenuItem::new("hide_dock_icon".to_string(), "Hide Dock Icon").into(),
CustomMenuItem::new("inject_script".to_string(), "Inject Script")
.accelerator("CmdOrCtrl+J")
.into(),
MenuItem::Separator.into(),
CustomMenuItem::new("sync_prompts".to_string(), "Sync Prompts").into(),
MenuItem::Separator.into(),
@ -178,6 +177,8 @@ pub fn init() -> Menu {
let window_menu = Submenu::new(
"Window",
Menu::new()
.add_item(CustomMenuItem::new("dalle2".to_string(), "DALL·E 2"))
.add_native_item(MenuItem::Separator)
.add_native_item(MenuItem::Minimize)
.add_native_item(MenuItem::Zoom),
);
@ -200,9 +201,9 @@ pub fn init() -> Menu {
Menu::new()
.add_submenu(app_menu)
.add_submenu(preferences_menu)
.add_submenu(window_menu)
.add_submenu(edit_menu)
.add_submenu(view_menu)
.add_submenu(window_menu)
.add_submenu(help_menu)
}
@ -216,7 +217,7 @@ pub fn menu_handler(event: WindowMenuEvent<tauri::Wry>) {
let core_window = app.get_window("core").unwrap();
let menu_handle = core_window.menu_handle();
let query = String::from("");
match menu_id {
// App
"about" => {
@ -228,11 +229,10 @@ pub fn menu_handler(event: WindowMenuEvent<tauri::Wry>) {
);
}
"check_update" => {
utils::run_check_update(app, false).unwrap();
utils::run_check_update(app, false, None).unwrap();
}
// Preferences
"control_center" => window::control_window(&app),
"dall_e2"=> window::dalle2_window(&app, query),
"restart" => tauri::api::process::restart(&app.env()),
"inject_script" => open(&app, script_path),
"go_conf" => utils::open_file(utils::chat_root()),
@ -274,8 +274,8 @@ pub fn menu_handler(event: WindowMenuEvent<tauri::Wry>) {
ChatConfJson::amend(&serde_json::json!({ "theme": theme }), Some(app)).unwrap();
}
"update_prompt" | "update_silent" | "update_disable" => {
dbg!(12);
for id in ["update_prompt", "update_silent", "update_disable"] {
// for id in ["update_prompt", "update_silent", "update_disable"] {
for id in ["update_prompt", "update_silent"] {
menu_handle.get_item(id).set_selected(false).unwrap();
}
let auto_update = match menu_id {
@ -313,6 +313,8 @@ pub fn menu_handler(event: WindowMenuEvent<tauri::Wry>) {
win.set_always_on_top(*stay_on_top).unwrap();
ChatConfJson::amend(&serde_json::json!({ "stay_on_top": *stay_on_top }), None).unwrap();
}
// Window
"dalle2" => window::dalle2_window(&app, None, None),
// View
"reload" => win.eval("window.location.reload()").unwrap(),
"go_back" => win.eval("window.history.go(-1)").unwrap(),

View File

@ -95,10 +95,12 @@ pub fn init(app: &mut App) -> std::result::Result<(), Box<dyn std::error::Error>
.unwrap();
});
}
// auto_update
if chat_conf.auto_update != "Disable" {
info!("stepup::run_check_update");
let app = app.handle();
utils::run_check_update(app, chat_conf.auto_update == "Silent").unwrap();
utils::run_check_update(app, chat_conf.auto_update == "Silent", None).unwrap();
}
Ok(())

View File

@ -1,4 +1,5 @@
use crate::{conf, utils};
use log::info;
use std::time::SystemTime;
use tauri::{utils::config::WindowUrl, window::WindowBuilder};
@ -27,7 +28,8 @@ pub fn tray_window(handle: &tauri::AppHandle) {
});
}
pub fn dalle2_window(handle: &tauri::AppHandle, query: String) {
pub fn dalle2_window(handle: &tauri::AppHandle, query: Option<String>, title: Option<String>) {
info!("dalle2_query: {:?}", query);
let timestamp = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
@ -35,22 +37,32 @@ pub fn dalle2_window(handle: &tauri::AppHandle, query: String) {
let theme = conf::ChatConfJson::theme();
let app = handle.clone();
let query = if query.is_some() {
format!(
"window.addEventListener('DOMContentLoaded', function() {{\nwindow.__CHATGPT_QUERY__='{}';\n}})",
query.unwrap()
)
} else {
"".to_string()
};
tauri::async_runtime::spawn(async move {
WindowBuilder::new(&app, format!("dalle2_{}", timestamp), WindowUrl::App("https://labs.openai.com".into()))
.title("ChatGPT & DALL·E 2")
.resizable(true)
.fullscreen(false)
.inner_size(800.0, 600.0)
.always_on_top(false)
.theme(theme)
.initialization_script(include_str!("../assets/core.js"))
.initialization_script(&format!(
"window.addEventListener('DOMContentLoaded', function() {{\nwindow.__CHATGPT_QUERY__='{}';\n}})",
query
))
.initialization_script(include_str!("../assets/dalle2.js"))
.build()
.unwrap();
WindowBuilder::new(
&app,
format!("dalle2_{}", timestamp),
WindowUrl::App("https://labs.openai.com/".into()),
)
.title(title.unwrap_or_else(|| "DALL·E 2".to_string()))
.resizable(true)
.fullscreen(false)
.inner_size(800.0, 600.0)
.always_on_top(false)
.theme(theme)
.initialization_script(include_str!("../assets/core.js"))
.initialization_script(&query)
.initialization_script(include_str!("../assets/dalle2.js"))
.build()
.unwrap();
});
}

View File

@ -10,6 +10,16 @@ async function init() {
// const searchBtn = document.querySelector('.image-prompt-form-wrapper form .image-prompt-btn');
const searchInput = document.querySelector('.image-prompt-form-wrapper form>.text-input');
if (searchInput) {
document.addEventListener("click", (e) => {
const origin = e.target.closest("a");
if (origin && origin.href && origin.target !== '_self') {
invoke('open_link', { url: origin.href });
}
});
clearInterval(window.searchInterval);
if (!window.__CHATGPT_QUERY__) return;
const query = decodeURIComponent(window.__CHATGPT_QUERY__);
searchInput.focus();
searchInput.value = query;
@ -19,7 +29,6 @@ async function init() {
// searchBtn.classList.add('active-style');
// searchBtn.removeAttribute('disabled');
// searchBtn.classList.remove('btn-disabled', 'btn-disabled-style');
clearInterval(window.searchInterval);
}
}, 200)
}

View File

@ -10,6 +10,7 @@ mod utils;
use app::{cmd, fs_extra, menu, setup};
use conf::{ChatConfJson, ChatState};
use tauri::api::path;
use tauri_plugin_autostart::MacosLauncher;
use tauri_plugin_log::{
fern::colors::{Color, ColoredLevelConfig},
LogTarget, LoggerBuilder,
@ -70,6 +71,10 @@ async fn main() {
])
.setup(setup::init)
.plugin(tauri_plugin_positioner::init())
.plugin(tauri_plugin_autostart::init(
MacosLauncher::LaunchAgent,
None,
))
.menu(menu::init())
.system_tray(menu::tray_menu())
.on_menu_event(menu::menu_handler)

View File

@ -130,7 +130,7 @@ pub async fn get_data(
}
}
pub fn run_check_update(app: AppHandle<Wry>, silent: bool) -> Result<()> {
pub fn run_check_update(app: AppHandle<Wry>, silent: bool, has_msg: Option<bool>) -> Result<()> {
tauri::async_runtime::spawn(async move {
let result = app.updater().check().await;
let update_resp = result.unwrap();
@ -144,6 +144,14 @@ pub fn run_check_update(app: AppHandle<Wry>, silent: bool) -> Result<()> {
prompt_for_install(app, update_resp).await.unwrap();
});
}
} else if let Some(v) = has_msg {
if v {
tauri::api::dialog::message(
app.app_handle().get_window("core").as_ref(),
"ChatGPT",
"Your ChatGPT is up to date",
);
}
}
});
Ok(())

View File

@ -26,7 +26,7 @@ export default function ChatLayout() {
})
const checkAppUpdate = async () => {
await invoke('run_check_update', { silent: false });
await invoke('run_check_update', { silent: false, hasMsg: true });
}
return (