Skip to content

Commit

Permalink
feat: add quick access in tray menus
Browse files Browse the repository at this point in the history
  • Loading branch information
lisiur committed Apr 11, 2023
1 parent 4186228 commit 8af4c2b
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 21 deletions.
2 changes: 1 addition & 1 deletion gui/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub async fn exec_command(
#[tauri::command]
pub async fn show_window(label: &str, window: Window) -> Result<()> {
log::debug!("show_window: {}", label);
window::show_window(label, window)?;
window::focus_window(&window);

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions gui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async fn main() {
.plugin(tauri_plugin_single_instance::init(|handle, _argv, _cwd| {
let handle = handle.clone();
tokio::spawn(async move {
show_or_create_main_window(&handle).await.unwrap();
show_or_create_main_window(&handle, "index.html").await.unwrap();
});
}))
.system_tray(tray::system_tray())
Expand All @@ -89,7 +89,7 @@ async fn main() {
if !hide_main_window {
let handle = app_handle.clone();
tokio::spawn(async move {
show_or_create_main_window(&handle).await.unwrap();
show_or_create_main_window(&handle, "index.html").await.unwrap();
});
}

Expand Down
35 changes: 32 additions & 3 deletions gui/src/tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ pub fn system_tray() -> SystemTray {
let tray_menu = SystemTrayMenu::new()
.add_item(CustomMenuItem::new("about", "About"))
.add_native_item(SystemTrayMenuItem::Separator)
.add_item(CustomMenuItem::new("show", "Main Window"))
.add_item(CustomMenuItem::new("main_window", "Casual Chat"))
.add_item(CustomMenuItem::new("chats_window", "Chats"))
.add_item(CustomMenuItem::new("prompts_window", "Prompts"))
.add_item(CustomMenuItem::new("setting", "Setting"))
.add_native_item(SystemTrayMenuItem::Separator)
.add_item(CustomMenuItem::new("quit", "Quit"));

Expand All @@ -22,15 +25,41 @@ pub fn on_system_tray_event(handle: &AppHandle, event: SystemTrayEvent) {
toggle_tray_window(handle, position, size).unwrap();
}
SystemTrayEvent::MenuItemClick { id, .. } => match id.as_ref() {
"show" => {
"main_window" => {
let handle = handle.clone();
tokio::spawn(async move {
show_or_create_main_window(&handle).await.unwrap();
show_or_create_main_window(&handle, "index.html")
.await
.unwrap();
});
}
"chats_window" => {
let handle = handle.clone();
tokio::spawn(async move {
show_or_create_main_window(&handle, "index.html#/main/chat")
.await
.unwrap();
});
}
"prompts_window" => {
let handle = handle.clone();
tokio::spawn(async move {
show_or_create_main_window(&handle, "index.html#/main/prompt")
.await
.unwrap();
});
}
"about" => {
show_or_create_about_window(handle).unwrap();
}
"setting" => {
let handle = handle.clone();
tokio::spawn(async move {
show_or_create_main_window(&handle, "index.html#/main/setting")
.await
.unwrap();
});
}
"quit" => {
handle.exit(0);
}
Expand Down
34 changes: 24 additions & 10 deletions gui/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,6 @@ impl Default for WindowOptions {
}
}

pub fn show_window(label: &str, window: Window) -> Result<()> {
if let Some(win) = window.get_window(label) {
win.show()?;
log::debug!("show window {}", label);
}
Ok(())
}

pub fn show_or_create_window_in_background(
handle: &AppHandle,
label: &str,
Expand All @@ -68,6 +60,8 @@ pub fn show_or_create_window_in_background(
let window = match handle.get_window(label) {
Some(win) => {
focus_window(&win);
win.eval(&format!("window.location.href = '{}'", options.url))
.unwrap();
log::debug!("show window {}", label);
win
}
Expand Down Expand Up @@ -116,7 +110,7 @@ pub fn create_window_in_background(
Ok(window)
}

pub async fn show_or_create_main_window(handle: &AppHandle) -> Result<Window> {
pub async fn show_or_create_main_window(handle: &AppHandle, url: &str) -> Result<Window> {
log::debug!("show_or_create_main_window");
let setting = handle.state::<AppSetting>();
let hide_taskbar = setting.0.lock().await.hide_taskbar;
Expand All @@ -128,7 +122,7 @@ pub async fn show_or_create_main_window(handle: &AppHandle) -> Result<Window> {
"main",
WindowOptions {
title: "".to_string(),
url: "index.html".to_string(),
url: url.to_string(),
width: 860.0,
height: 720.0,
title_bar_style: Some(TitleBarStyle::Overlay),
Expand Down Expand Up @@ -299,3 +293,23 @@ pub fn show_or_create_about_window(handle: &AppHandle) -> Result<Window> {

Ok(window)
}

#[allow(unused)]
pub fn show_or_create_setting_window(handle: &AppHandle) -> Result<Window> {
let window = show_or_create_window_in_background(
handle,
"setting",
WindowOptions {
title: "Setting".to_string(),
url: "index.html/#/setting".to_string(),
width: 640.0,
height: 720.0,
skip_taskbar: Some(true),
resizable: false,
..Default::default()
},
)
.unwrap();

Ok(window)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ import {
NScrollbar,
NSwitch,
} from "naive-ui";
import { defineComponent, nextTick } from "vue";
import { computed, defineComponent, nextTick } from "vue";
import { getSettings, updateSettings, Theme, Settings } from "../../api";
import { useAsyncData } from "../../hooks/asyncData";
import { useI18n } from "../../hooks/i18n";
import { openUrl } from "../../utils/api";
import { openUrl, getPlatform } from "../../utils/api";
import { isTauri } from "../../utils/env";

export default defineComponent({
setup() {
const { t } = useI18n();

const platform = useAsyncData(getPlatform);
const isMacos = computed(() => platform.value === "darwin");

const model = useAsyncData(async () => {
return getSettings();
}, {});
Expand All @@ -38,7 +41,7 @@ export default defineComponent({
class="h-full p-8 flex flex-col justify-center"
>
<NScrollbar>
<div class="mt-8 pr-12">
<div class="grid place-items-center pr-12 h-full">
{model.value ? (
<NForm
model={model.value}
Expand Down Expand Up @@ -110,7 +113,7 @@ export default defineComponent({
></NSwitch>
</NFormItem>
) : null}
{isTauri ? (
{isTauri && !isMacos ? (
<NFormItem label={t("setting.hideTaskbar") + " :"}>
<NSwitch
v-model:value={model.value.hideTaskbar}
Expand Down
6 changes: 5 additions & 1 deletion web/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,18 @@ export default createRouter({
{
name: "setting",
path: "setting",
component: () => import("./pages/main/Setting"),
component: () => import("./pages/setting/Index"),
},
],
},
{
path: "casual-chat",
component: () => import("./pages/casualChat/Index"),
},
{
path: "setting",
component: () => import("./pages/setting/Index"),
},
{
path: "about",
component: () => import("./pages/about/Index"),
Expand Down

0 comments on commit 8af4c2b

Please sign in to comment.