Skip to content

Commit

Permalink
chore: window
Browse files Browse the repository at this point in the history
  • Loading branch information
lencx committed Jul 15, 2024
1 parent ca63adb commit ef74c4a
Show file tree
Hide file tree
Showing 15 changed files with 510 additions and 532 deletions.
758 changes: 365 additions & 393 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"tauri": "tauri"
},
"dependencies": {
"@tauri-apps/api": "2.0.0-beta.11",
"@tauri-apps/plugin-os": "2.0.0-beta.3",
"@tauri-apps/plugin-shell": "2.0.0-beta.3",
"@tauri-apps/api": "2.0.0-beta.15",
"@tauri-apps/plugin-os": "2.0.0-beta.7",
"@tauri-apps/plugin-shell": "2.0.0-beta.8",
"clsx": "^2.1.1",
"lodash": "^4.17.21",
"react": "^18.3.1",
Expand All @@ -22,7 +22,7 @@
},
"devDependencies": {
"@tailwindcss/typography": "^0.5.13",
"@tauri-apps/cli": "2.0.0-beta.15",
"@tauri-apps/cli": "2.0.0-beta.22",
"@types/lodash": "^4.17.1",
"@types/node": "^20.12.12",
"@types/react": "^18.3.1",
Expand Down
126 changes: 63 additions & 63 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 8 additions & 14 deletions src-tauri/src/core/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ pub fn ask_send(app: AppHandle) {

win.get_webview("main")
.unwrap()
.eval(r#"
.eval(
r#"
ChatAsk.submit();
setTimeout(() => {
__TAURI__.webview.Webview.getByLabel('ask')?.setFocus();
}, 500);
"#)
"#,
)
.unwrap();
}

Expand Down Expand Up @@ -112,20 +114,12 @@ pub fn set_view_ask(app: AppHandle, enabled: bool) {
let ask_mode_height = if enabled { ASK_HEIGHT } else { 0.0 };
let scale_factor = core_window.scale_factor().unwrap();
let titlebar_height = (scale_factor * TITLEBAR_HEIGHT).round() as u32;
let win_size = core_window
.inner_size()
.unwrap();
let win_size = core_window.inner_size().unwrap();
let ask_height = (scale_factor * ask_mode_height).round() as u32;

let main_view = core_window
.get_webview("main")
.unwrap();
let titlebar_view = core_window
.get_webview("titlebar")
.unwrap();
let ask_view = core_window
.get_webview("ask")
.unwrap();
let main_view = core_window.get_webview("main").unwrap();
let titlebar_view = core_window.get_webview("titlebar").unwrap();
let ask_view = core_window.get_webview("ask").unwrap();

if enabled {
ask_view.set_focus().unwrap();
Expand Down
2 changes: 2 additions & 0 deletions src-tauri/src/core/constant.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pub static TITLEBAR_HEIGHT: f64 = 28.0;
pub static ASK_HEIGHT: f64 = 120.0;

pub static WINDOW_SETTINGS: &str = "settings";

pub static INIT_SCRIPT: &str = r#"
window.addEventListener('DOMContentLoaded', function() {
function handleUrlChange() {
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod conf;
pub mod constant;
pub mod setup;
pub mod template;
pub mod window;
5 changes: 3 additions & 2 deletions src-tauri/src/core/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ pub fn init(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {

let titlebar_view = WebviewBuilder::new(
"titlebar",
WebviewUrl::App("index.html?type=titlebar".into()),
WebviewUrl::App("index.html".into()),
)
.auto_resize();

let ask_view =
WebviewBuilder::new("ask", WebviewUrl::App("index.html?type=ask".into()))
WebviewBuilder::new("ask", WebviewUrl::App("index.html".into()))
.auto_resize();

let win = window.lock().unwrap();
Expand All @@ -111,6 +111,7 @@ pub fn init(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {
#[cfg(target_os = "macos")]
{
let main_area_height = win_size.height - titlebar_height;

win.add_child(
titlebar_view,
LogicalPosition::new(0, 0),
Expand Down
17 changes: 17 additions & 0 deletions src-tauri/src/core/window.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use tauri::{command, AppHandle, Manager, WebviewUrl, WebviewWindowBuilder};

use crate::core::constant::WINDOW_SETTINGS;

#[command]
pub fn open_settings(app: AppHandle) {
match app.get_webview_window(WINDOW_SETTINGS) {
Some(window) => {
window.show().unwrap();
}
None => {
WebviewWindowBuilder::new(&app, WINDOW_SETTINGS, WebviewUrl::App("index.html".into()))
.build()
.unwrap();
}
}
}
3 changes: 2 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

mod core;
use core::{cmd, setup};
use core::{cmd, setup, window};

fn main() {
tauri::Builder::default()
Expand All @@ -20,6 +20,7 @@ fn main() {
cmd::ask_sync,
cmd::ask_send,
cmd::set_theme,
window::open_settings,
])
.setup(setup::init)
.run(tauri::generate_context!())
Expand Down
16 changes: 16 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { getCurrentWebview } from '@tauri-apps/api/webview';

import Titlebar from '~view/Titlebar';
import Ask from '~view/Ask';
import Settings from '~view/Settings';

const viewMap = {
titlebar: <Titlebar />,
ask: <Ask />,
settings: <Settings />,
};

export default function App() {
const webview = getCurrentWebview();
return viewMap[webview.label as keyof typeof viewMap] || null;
}
7 changes: 4 additions & 3 deletions src/hooks/useTheme.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { useState, useEffect } from 'react';
import { getCurrent } from '@tauri-apps/api/window';
import { getCurrentWindow } from '@tauri-apps/api/window';

export default function useTheme() {
const [theme, setTheme] = useState<string | null>('light'); // ['light', 'dark']

useEffect(() => {
let unlisten: Function;
(async () => {
setTheme(await getCurrent().theme() || '');
unlisten = await getCurrent().onThemeChanged(({ payload: newTheme }) => {
let win = getCurrentWindow();
setTheme(await win.theme() || '');
unlisten = await win.onThemeChanged(({ payload: newTheme }) => {
setTheme(newTheme);
});
})()
Expand Down
Loading

0 comments on commit ef74c4a

Please sign in to comment.