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" wry = "0.23.4"
dark-light = "1.0.0" dark-light = "1.0.0"
[dependencies.tauri-plugin-log] [dependencies.tauri-plugin-log]
git = "https://github.com/tauri-apps/tauri-plugin-log" git = "https://github.com/lencx/tauri-plugin-log"
branch = "dev" branch = "dev"
features = ["colored"] features = ["colored"]
[dependencies.tauri-plugin-autostart]
git = "https://github.com/lencx/tauri-plugin-autostart"
branch = "dev"
[features] [features]
# by default Tauri runs in production mode # by default Tauri runs in production mode

View File

@ -15,7 +15,11 @@ pub fn drag_window(app: AppHandle) {
#[command] #[command]
pub fn dalle2_window(app: AppHandle, query: String) { 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] #[command]
@ -56,8 +60,8 @@ pub fn reset_chat_conf() -> ChatConfJson {
} }
#[command] #[command]
pub fn run_check_update(app: AppHandle, silent: bool) { pub fn run_check_update(app: AppHandle, silent: bool, has_msg: Option<bool>) {
utils::run_check_update(app, silent).unwrap(); utils::run_check_update(app, silent, has_msg).unwrap();
} }
#[command] #[command]

View File

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

View File

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

View File

@ -10,6 +10,16 @@ async function init() {
// const searchBtn = document.querySelector('.image-prompt-form-wrapper form .image-prompt-btn'); // const searchBtn = document.querySelector('.image-prompt-form-wrapper form .image-prompt-btn');
const searchInput = document.querySelector('.image-prompt-form-wrapper form>.text-input'); const searchInput = document.querySelector('.image-prompt-form-wrapper form>.text-input');
if (searchInput) { 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__); const query = decodeURIComponent(window.__CHATGPT_QUERY__);
searchInput.focus(); searchInput.focus();
searchInput.value = query; searchInput.value = query;
@ -19,7 +29,6 @@ async function init() {
// searchBtn.classList.add('active-style'); // searchBtn.classList.add('active-style');
// searchBtn.removeAttribute('disabled'); // searchBtn.removeAttribute('disabled');
// searchBtn.classList.remove('btn-disabled', 'btn-disabled-style'); // searchBtn.classList.remove('btn-disabled', 'btn-disabled-style');
clearInterval(window.searchInterval);
} }
}, 200) }, 200)
} }

View File

@ -10,6 +10,7 @@ mod utils;
use app::{cmd, fs_extra, menu, setup}; use app::{cmd, fs_extra, menu, setup};
use conf::{ChatConfJson, ChatState}; use conf::{ChatConfJson, ChatState};
use tauri::api::path; use tauri::api::path;
use tauri_plugin_autostart::MacosLauncher;
use tauri_plugin_log::{ use tauri_plugin_log::{
fern::colors::{Color, ColoredLevelConfig}, fern::colors::{Color, ColoredLevelConfig},
LogTarget, LoggerBuilder, LogTarget, LoggerBuilder,
@ -70,6 +71,10 @@ async fn main() {
]) ])
.setup(setup::init) .setup(setup::init)
.plugin(tauri_plugin_positioner::init()) .plugin(tauri_plugin_positioner::init())
.plugin(tauri_plugin_autostart::init(
MacosLauncher::LaunchAgent,
None,
))
.menu(menu::init()) .menu(menu::init())
.system_tray(menu::tray_menu()) .system_tray(menu::tray_menu())
.on_menu_event(menu::menu_handler) .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 { tauri::async_runtime::spawn(async move {
let result = app.updater().check().await; let result = app.updater().check().await;
let update_resp = result.unwrap(); 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(); 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(()) Ok(())

View File

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