Skip to content

Commit

Permalink
chore: dalle2
Browse files Browse the repository at this point in the history
  • Loading branch information
lencx committed Jan 6, 2023
1 parent d0df8df commit 8c2303d
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 41 deletions.
5 changes: 4 additions & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions src-tauri/src/app/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
36 changes: 19 additions & 17 deletions src-tauri/src/app/menu.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
app::window,
conf::{self, ChatConfJson},
utils,
};
Expand All @@ -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();
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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),
);
Expand All @@ -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)
}

Expand All @@ -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" => {
Expand All @@ -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()),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
Expand Down
4 changes: 3 additions & 1 deletion src-tauri/src/app/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
44 changes: 28 additions & 16 deletions src-tauri/src/app/window.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{conf, utils};
use log::info;
use std::time::SystemTime;
use tauri::{utils::config::WindowUrl, window::WindowBuilder};

Expand Down Expand Up @@ -27,30 +28,41 @@ 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()
.as_secs();
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();
});
}

Expand Down
11 changes: 10 additions & 1 deletion src-tauri/src/assets/dalle2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
}
Expand Down
5 changes: 5 additions & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion src-tauri/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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(())
Expand Down
2 changes: 1 addition & 1 deletion src/layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit 8c2303d

Please sign in to comment.