Skip to content

Commit

Permalink
feat: support config home page
Browse files Browse the repository at this point in the history
support config home page

fix lisiur#60
  • Loading branch information
lisiur committed Aug 1, 2023
1 parent db84e6d commit 34cbb72
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 7 deletions.
17 changes: 11 additions & 6 deletions gui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub struct SchemaPort(u16);
pub struct EventBus {
pub sender: Sender<CommandEvent>,
}
#[derive(Clone)]
pub struct AppSetting(Arc<Mutex<Setting>>);

impl EventBus {
Expand Down Expand Up @@ -71,20 +72,22 @@ async fn main() {
let app_setting = AppSetting(Arc::new(Mutex::new(setting)));

tauri::Builder::default()
.manage(conn.clone())
.manage(CommandExecutor::new())
.manage(app_setting.clone())
.plugin(tauri_plugin_positioner::init())
.plugin(tauri_plugin_single_instance::init(|handle, _argv, _cwd| {
let handle = handle.clone();
let app_handle = handle.clone();
tokio::spawn(async move {
show_or_create_main_window(&handle, "index.html")
let app_setting = app_handle.state::<AppSetting>();
let home_page_url = app_setting.0.lock().await.home_page_url();
show_or_create_main_window(&app_handle, &home_page_url)
.await
.unwrap();
});
}))
.system_tray(tray::system_tray())
.on_system_tray_event(tray::on_system_tray_event)
.manage(conn.clone())
.manage(CommandExecutor::new())
.manage(app_setting)
.setup(move |app| {
let app_handle = app.handle();
app.manage(EventBus::new(app_handle.clone()));
Expand Down Expand Up @@ -154,7 +157,9 @@ async fn main() {
if !hide_main_window {
let handle = app_handle.clone();
tokio::spawn(async move {
show_or_create_main_window(&handle, "index.html")
let app_setting = handle.state::<AppSetting>();
let home_page_url = app_setting.0.lock().await.home_page_url();
show_or_create_main_window(&handle, &home_page_url)
.await
.unwrap();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
ALTER TABLE settings DROP COLUMN home_page;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Your SQL goes here
ALTER TABLE settings ADD COLUMN home_page TEXT NOT NULL DEFAULT 'casual';
43 changes: 43 additions & 0 deletions service/src/models/setting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct Setting {
pub enable_web_server: bool,
pub hide_main_window: bool,
pub hide_taskbar: bool,
pub home_page: TextWrapper<HomePage>,
}

impl Setting {
Expand Down Expand Up @@ -75,6 +76,47 @@ impl Setting {

OpenAIChatApi::new(client, host)
}

pub fn home_page_url(&self) -> String {
self.home_page.as_ref().url().to_string()
}
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub enum HomePage {
Casual,
Chats,
}

impl AsRef<str> for HomePage {
fn as_ref(&self) -> &str {
match self {
Self::Casual => "casual",
Self::Chats => "chats",
}
}
}

impl FromStr for HomePage {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"casual" => Ok(Self::Casual),
"chats" => Ok(Self::Chats),
_ => Err("Invalid home page".into()),
}
}
}

impl HomePage {
pub fn url(&self) -> &str {
match self {
Self::Casual => "index.html",
Self::Chats => "index.html#/main/chat",
}
}
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down Expand Up @@ -135,4 +177,5 @@ pub struct PatchSetting {
pub hide_main_window: Option<bool>,
pub hide_taskbar: Option<bool>,
pub enable_web_server: Option<bool>,
pub home_page: Option<TextWrapper<HomePage>>,
}
1 change: 1 addition & 0 deletions service/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ diesel::table! {
enable_web_server -> Bool,
hide_main_window -> Bool,
hide_taskbar -> Bool,
home_page -> Text,
}
}

Expand Down
4 changes: 3 additions & 1 deletion service/src/services/setting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::Deserialize;

use crate::result::Result;
use crate::{models::setting::Setting, repositories::setting::SettingRepo, DbConn, Id};
use crate::{PatchSetting, Theme};
use crate::{HomePage, PatchSetting, Theme};

#[derive(Clone)]
pub struct SettingService {
Expand Down Expand Up @@ -43,6 +43,7 @@ impl SettingService {
hide_main_window: payload.hide_main_window,
hide_taskbar: payload.hide_taskbar,
enable_web_server: payload.enable_web_server,
home_page: payload.home_page.map(|h| h.into()),
})?;

Ok(())
Expand All @@ -62,4 +63,5 @@ pub struct UpdateSettingPayload {
pub hide_main_window: Option<bool>,
pub hide_taskbar: Option<bool>,
pub enable_web_server: Option<bool>,
pub home_page: Option<HomePage>,
}
6 changes: 6 additions & 0 deletions web/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ export interface Settings {
enableWebServer?: boolean;
hideMainWindow?: boolean;
hideTaskbar?: boolean;
homePage?: HomePage;
}

export enum HomePage {
Casual = "casual",
Chats = "chats",
}

export enum Theme {
Expand Down
1 change: 1 addition & 0 deletions web/src/i18n/enUS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export const messages = {
"setting.hideMainWindow": "Hide Main Window",
"setting.needRestart.hint":
"The following settings will take effect after restarting the app",
"setting.homePage": "Home Page",
};

export default messages;
Expand Down
1 change: 1 addition & 0 deletions web/src/i18n/ruRU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ const messages: Messages = {
"setting.hideMainWindow": "Скрыть главное окно",
"setting.needRestart.hint":
"Следующие настройки вступят в силу после перезапуска приложения",
"setting.homePage": "Домашняя страница",
};

export default messages;
1 change: 1 addition & 0 deletions web/src/i18n/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ const messages: Messages = {
"setting.hideMainWindow": "Hide Main Window",
"setting.needRestart.hint":
"The following settings will take effect after restarting the app",
"setting.homePage": "Home Page",
};

export default messages;
1 change: 1 addition & 0 deletions web/src/i18n/zhCN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ const messages: Messages = {
"setting.hideTaskbar": "隐藏任务栏图标",
"setting.hideMainWindow": "隐藏主窗口",
"setting.needRestart.hint": "下面的设置需要重启应用才能生效",
"setting.homePage": "主页",
};

export default messages;
18 changes: 18 additions & 0 deletions web/src/pages/setting/Index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@ export default defineComponent({
onUpdateValue={() => updateSettingHandler("forwardApiKey")}
></NSwitch>
</NFormItem>
{isTauri ? (
<NFormItem label={t("setting.homePage") + " :"}>
<NSelect
v-model:value={model.value.homePage}
onUpdateValue={() => updateSettingHandler("homePage")}
options={[
{
label: t("chat.casual.title"),
value: "casual",
},
{
label: t("chat.conversations"),
value: "chats",
},
]}
></NSelect>
</NFormItem>
) : null}
{isTauri ? (
<NDivider>{t("setting.needRestart.hint")}</NDivider>
) : null}
Expand Down

0 comments on commit 34cbb72

Please sign in to comment.