mirror of
https://github.com/FranP-code/ChatGPT.git
synced 2025-10-13 00:13:25 +00:00
feat: hide dock icon (#35)
This commit is contained in:
@@ -3,15 +3,15 @@ use crate::{
|
||||
utils,
|
||||
};
|
||||
use tauri::{
|
||||
utils::assets::EmbeddedAssets, AboutMetadata, AppHandle, Context, CustomMenuItem, Manager,
|
||||
Menu, MenuItem, Submenu, SystemTray, SystemTrayEvent, SystemTrayMenu, WindowMenuEvent,
|
||||
AboutMetadata, AppHandle, CustomMenuItem, Manager, Menu, MenuItem, Submenu, SystemTray,
|
||||
SystemTrayEvent, SystemTrayMenu, WindowMenuEvent,
|
||||
};
|
||||
use tauri_plugin_positioner::{on_tray_event, Position, WindowExt};
|
||||
|
||||
// --- Menu
|
||||
pub fn init(context: &Context<EmbeddedAssets>) -> Menu {
|
||||
pub fn init() -> Menu {
|
||||
let chat_conf = ChatConfJson::get_chat_conf();
|
||||
let name = &context.package_info().name;
|
||||
let name = "ChatGPT";
|
||||
let app_menu = Submenu::new(
|
||||
name,
|
||||
Menu::new()
|
||||
@@ -25,18 +25,18 @@ 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 stay_on_top =
|
||||
CustomMenuItem::new("stay_on_top".to_string(), "Stay On Top").accelerator("CmdOrCtrl+T");
|
||||
let titlebar =
|
||||
CustomMenuItem::new("titlebar".to_string(), "Titlebar").accelerator("CmdOrCtrl+B");
|
||||
let theme_light = CustomMenuItem::new("theme_light".to_string(), "Light");
|
||||
let theme_dark = CustomMenuItem::new("theme_dark".to_string(), "Dark");
|
||||
let is_dark = chat_conf.theme == "Dark";
|
||||
|
||||
let always_on_top_menu = if chat_conf.always_on_top {
|
||||
always_on_top.selected()
|
||||
let stay_on_top_menu = if chat_conf.stay_on_top {
|
||||
stay_on_top.selected()
|
||||
} else {
|
||||
always_on_top
|
||||
stay_on_top
|
||||
};
|
||||
let titlebar_menu = if chat_conf.titlebar {
|
||||
titlebar.selected()
|
||||
@@ -62,9 +62,11 @@ pub fn init(context: &Context<EmbeddedAssets>) -> Menu {
|
||||
}),
|
||||
)
|
||||
.into(),
|
||||
always_on_top_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(),
|
||||
MenuItem::Separator.into(),
|
||||
CustomMenuItem::new("inject_script".to_string(), "Inject Script")
|
||||
.accelerator("CmdOrCtrl+J")
|
||||
@@ -119,7 +121,6 @@ pub fn init(context: &Context<EmbeddedAssets>) -> Menu {
|
||||
CustomMenuItem::new("scroll_bottom".to_string(), "Scroll to Bottom of Screen")
|
||||
.accelerator("CmdOrCtrl+Down"),
|
||||
)
|
||||
.add_native_item(MenuItem::Zoom)
|
||||
.add_native_item(MenuItem::Separator)
|
||||
.add_item(
|
||||
CustomMenuItem::new("reload".to_string(), "Refresh the Screen")
|
||||
@@ -127,6 +128,13 @@ pub fn init(context: &Context<EmbeddedAssets>) -> Menu {
|
||||
),
|
||||
);
|
||||
|
||||
let window_menu = Submenu::new(
|
||||
"Window",
|
||||
Menu::new()
|
||||
.add_native_item(MenuItem::Minimize)
|
||||
.add_native_item(MenuItem::Zoom),
|
||||
);
|
||||
|
||||
let help_menu = Submenu::new(
|
||||
"Help",
|
||||
Menu::new()
|
||||
@@ -143,6 +151,7 @@ pub fn init(context: &Context<EmbeddedAssets>) -> Menu {
|
||||
.add_submenu(preferences_menu)
|
||||
.add_submenu(edit_menu)
|
||||
.add_submenu(view_menu)
|
||||
.add_submenu(window_menu)
|
||||
.add_submenu(help_menu)
|
||||
}
|
||||
|
||||
@@ -165,6 +174,9 @@ pub fn menu_handler(event: WindowMenuEvent<tauri::Wry>) {
|
||||
"go_conf" => utils::open_file(utils::chat_root()),
|
||||
"clear_conf" => utils::clear_conf(&app),
|
||||
"awesome" => open(&app, conf::AWESOME_URL.to_string()),
|
||||
"hide_dock_icon" => {
|
||||
ChatConfJson::amend(&serde_json::json!({ "hide_dock_icon": true }), Some(app)).unwrap()
|
||||
}
|
||||
"titlebar" => {
|
||||
let chat_conf = conf::ChatConfJson::get_chat_conf();
|
||||
ChatConfJson::amend(
|
||||
@@ -182,19 +194,15 @@ pub fn menu_handler(event: WindowMenuEvent<tauri::Wry>) {
|
||||
};
|
||||
ChatConfJson::amend(&serde_json::json!({ "theme": theme }), Some(app)).unwrap();
|
||||
}
|
||||
"always_on_top" => {
|
||||
let mut always_on_top = state.always_on_top.lock().unwrap();
|
||||
*always_on_top = !*always_on_top;
|
||||
"stay_on_top" => {
|
||||
let mut stay_on_top = state.stay_on_top.lock().unwrap();
|
||||
*stay_on_top = !*stay_on_top;
|
||||
menu_handle
|
||||
.get_item(menu_id)
|
||||
.set_selected(*always_on_top)
|
||||
.set_selected(*stay_on_top)
|
||||
.unwrap();
|
||||
win.set_always_on_top(*always_on_top).unwrap();
|
||||
ChatConfJson::amend(
|
||||
&serde_json::json!({ "always_on_top": *always_on_top }),
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
win.set_always_on_top(*stay_on_top).unwrap();
|
||||
ChatConfJson::amend(&serde_json::json!({ "stay_on_top": *stay_on_top }), None).unwrap();
|
||||
}
|
||||
// View
|
||||
"reload" => win.eval("window.location.reload()").unwrap(),
|
||||
@@ -235,20 +243,70 @@ pub fn tray_menu() -> SystemTray {
|
||||
|
||||
// --- SystemTray Event
|
||||
pub fn tray_handler(handle: &AppHandle, event: SystemTrayEvent) {
|
||||
let core_win = handle.get_window("core").unwrap();
|
||||
on_tray_event(handle, &event);
|
||||
|
||||
if let SystemTrayEvent::LeftClick { .. } = event {
|
||||
core_win.minimize().unwrap();
|
||||
let mini_win = handle.get_window("mini").unwrap();
|
||||
mini_win.move_window(Position::TrayCenter).unwrap();
|
||||
match event {
|
||||
SystemTrayEvent::LeftClick { .. } => {
|
||||
let chat_conf = conf::ChatConfJson::get_chat_conf();
|
||||
|
||||
if mini_win.is_visible().unwrap() {
|
||||
mini_win.hide().unwrap();
|
||||
} else {
|
||||
mini_win.show().unwrap();
|
||||
if !chat_conf.hide_dock_icon {
|
||||
let core_win = handle.get_window("core").unwrap();
|
||||
core_win.minimize().unwrap();
|
||||
}
|
||||
|
||||
let tray_win = handle.get_window("tray").unwrap();
|
||||
tray_win.move_window(Position::TrayCenter).unwrap();
|
||||
|
||||
if tray_win.is_visible().unwrap() {
|
||||
tray_win.hide().unwrap();
|
||||
} else {
|
||||
tray_win.show().unwrap();
|
||||
}
|
||||
}
|
||||
SystemTrayEvent::RightClick { .. } => {
|
||||
let chat_conf = conf::ChatConfJson::get_chat_conf();
|
||||
let hide_dock = !chat_conf.hide_dock_icon;
|
||||
let title;
|
||||
let msg;
|
||||
|
||||
if !hide_dock {
|
||||
title = "Show Dock Icon";
|
||||
msg = "Are you sure you want to show the ChatGPT icon in the Dock?";
|
||||
} else {
|
||||
title = "Hide Dock Icon";
|
||||
msg = "Are you sure you want to hide the ChatGPT icon in the Dock?";
|
||||
}
|
||||
|
||||
let app = handle.clone();
|
||||
tauri::api::dialog::ask(
|
||||
handle.get_window("tray").as_ref(),
|
||||
title,
|
||||
msg,
|
||||
move |is_ok| {
|
||||
if is_ok {
|
||||
ChatConfJson::amend(
|
||||
&serde_json::json!({ "hide_dock_icon": hide_dock }),
|
||||
Some(app),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
|
||||
// if let SystemTrayEvent::LeftClick { .. } = event {
|
||||
// core_win.minimize().unwrap();
|
||||
// let tray_win = handle.get_window("tray").unwrap();
|
||||
// tray_win.move_window(Position::TrayCenter).unwrap();
|
||||
|
||||
// if tray_win.is_visible().unwrap() {
|
||||
// tray_win.hide().unwrap();
|
||||
// } else {
|
||||
// tray_win.show().unwrap();
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
pub fn open(app: &AppHandle, path: String) {
|
||||
|
||||
@@ -5,40 +5,52 @@ pub fn init(app: &mut App) -> std::result::Result<(), Box<dyn std::error::Error>
|
||||
let chat_conf = ChatConfJson::get_chat_conf();
|
||||
let url = chat_conf.origin.to_string();
|
||||
let theme = ChatConfJson::theme();
|
||||
window::mini_window(&app.app_handle());
|
||||
let handle = app.app_handle();
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
WindowBuilder::new(app, "core", WindowUrl::App(url.into()))
|
||||
.resizable(true)
|
||||
.fullscreen(false)
|
||||
.inner_size(800.0, 600.0)
|
||||
.hidden_title(true)
|
||||
.theme(theme)
|
||||
.always_on_top(chat_conf.always_on_top)
|
||||
.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(&chat_conf.ua_window)
|
||||
.build()?;
|
||||
std::thread::spawn(move || {
|
||||
window::tray_window(&handle);
|
||||
});
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
WindowBuilder::new(app, "core", WindowUrl::App(url.into()))
|
||||
.title("ChatGPT")
|
||||
.resizable(true)
|
||||
.fullscreen(false)
|
||||
.inner_size(800.0, 600.0)
|
||||
.theme(theme)
|
||||
.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/export.js"))
|
||||
.user_agent(&chat_conf.ua_window)
|
||||
.build()?;
|
||||
if chat_conf.hide_dock_icon {
|
||||
#[cfg(target_os = "macos")]
|
||||
app.set_activation_policy(tauri::ActivationPolicy::Accessory);
|
||||
} else {
|
||||
let app = app.handle();
|
||||
std::thread::spawn(move || {
|
||||
#[cfg(target_os = "macos")]
|
||||
WindowBuilder::new(&app, "core", WindowUrl::App(url.into()))
|
||||
.resizable(true)
|
||||
.fullscreen(false)
|
||||
.inner_size(800.0, 600.0)
|
||||
.hidden_title(true)
|
||||
.theme(theme)
|
||||
.always_on_top(chat_conf.stay_on_top)
|
||||
.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(&chat_conf.ua_window)
|
||||
.build().unwrap();
|
||||
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
WindowBuilder::new(&app, "core", WindowUrl::App(url.into()))
|
||||
.title("ChatGPT")
|
||||
.resizable(true)
|
||||
.fullscreen(false)
|
||||
.inner_size(800.0, 600.0)
|
||||
.theme(theme)
|
||||
.always_on_top(chat_conf.stay_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/export.js"))
|
||||
.user_agent(&chat_conf.ua_window)
|
||||
.build().unwrap();
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,25 +1,28 @@
|
||||
use crate::{conf, utils};
|
||||
use tauri::{utils::config::WindowUrl, window::WindowBuilder};
|
||||
|
||||
pub fn mini_window(handle: &tauri::AppHandle) {
|
||||
pub fn tray_window(handle: &tauri::AppHandle) {
|
||||
let chat_conf = conf::ChatConfJson::get_chat_conf();
|
||||
let theme = conf::ChatConfJson::theme();
|
||||
let app = handle.clone();
|
||||
|
||||
WindowBuilder::new(handle, "mini", WindowUrl::App(chat_conf.origin.into()))
|
||||
.resizable(false)
|
||||
.fullscreen(false)
|
||||
.inner_size(360.0, 540.0)
|
||||
.decorations(false)
|
||||
.always_on_top(true)
|
||||
.theme(theme)
|
||||
.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(&chat_conf.ua_tray)
|
||||
.build()
|
||||
.unwrap()
|
||||
.hide()
|
||||
.unwrap();
|
||||
std::thread::spawn(move || {
|
||||
WindowBuilder::new(&app, "tray", WindowUrl::App(chat_conf.origin.into()))
|
||||
.resizable(false)
|
||||
.fullscreen(false)
|
||||
.inner_size(360.0, 540.0)
|
||||
.decorations(false)
|
||||
.always_on_top(true)
|
||||
.theme(theme)
|
||||
.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(&chat_conf.ua_tray)
|
||||
.build()
|
||||
.unwrap()
|
||||
.hide()
|
||||
.unwrap();
|
||||
});
|
||||
}
|
||||
|
||||
2
src-tauri/src/assets/core.js
vendored
2
src-tauri/src/assets/core.js
vendored
@@ -41,7 +41,7 @@ window.invoke = invoke;
|
||||
window.transformCallback = transformCallback;
|
||||
|
||||
async function init() {
|
||||
if (__TAURI_METADATA__.__currentWindow.label === 'mini') {
|
||||
if (__TAURI_METADATA__.__currentWindow.label === 'tray') {
|
||||
document.getElementsByTagName('html')[0].style['font-size'] = '70%';
|
||||
}
|
||||
|
||||
|
||||
@@ -14,18 +14,20 @@ pub const ISSUES_URL: &str = "https://github.com/lencx/ChatGPT/issues";
|
||||
pub const UPDATE_LOG_URL: &str = "https://github.com/lencx/ChatGPT/blob/main/UPDATE_LOG.md";
|
||||
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,
|
||||
"stay_on_top": false,
|
||||
"theme": "Light",
|
||||
"titlebar": true,
|
||||
"hide_dock_icon": false,
|
||||
"default_origin": "https://chat.openai.com",
|
||||
"origin": "https://chat.openai.com",
|
||||
"ua_window": "",
|
||||
"ua_tray": ""
|
||||
}"#;
|
||||
pub const DEFAULT_CHAT_CONF_MAC: &str = r#"{
|
||||
"always_on_top": false,
|
||||
"stay_on_top": false,
|
||||
"theme": "Light",
|
||||
"titlebar": false,
|
||||
"hide_dock_icon": false,
|
||||
"default_origin": "https://chat.openai.com",
|
||||
"origin": "https://chat.openai.com",
|
||||
"ua_window": "",
|
||||
@@ -33,22 +35,27 @@ pub const DEFAULT_CHAT_CONF_MAC: &str = r#"{
|
||||
}"#;
|
||||
|
||||
pub struct ChatState {
|
||||
pub always_on_top: Mutex<bool>,
|
||||
pub stay_on_top: Mutex<bool>,
|
||||
}
|
||||
|
||||
impl ChatState {
|
||||
pub fn default(chat_conf: ChatConfJson) -> Self {
|
||||
ChatState {
|
||||
always_on_top: Mutex::new(chat_conf.always_on_top),
|
||||
stay_on_top: Mutex::new(chat_conf.stay_on_top),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
|
||||
pub struct ChatConfJson {
|
||||
// support macOS only
|
||||
pub titlebar: bool,
|
||||
pub always_on_top: bool,
|
||||
pub hide_dock_icon: bool,
|
||||
|
||||
// macOS and Windows
|
||||
pub theme: String,
|
||||
|
||||
pub stay_on_top: bool,
|
||||
pub default_origin: String,
|
||||
pub origin: String,
|
||||
pub ua_window: String,
|
||||
|
||||
@@ -29,7 +29,7 @@ fn main() {
|
||||
])
|
||||
.setup(setup::init)
|
||||
.plugin(tauri_plugin_positioner::init())
|
||||
.menu(menu::init(&context))
|
||||
.menu(menu::init())
|
||||
.system_tray(menu::tray_menu())
|
||||
.on_menu_event(menu::menu_handler)
|
||||
.on_system_tray_event(menu::tray_handler)
|
||||
|
||||
Reference in New Issue
Block a user