-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Expand file tree
/
Copy pathsetup.rs
More file actions
203 lines (179 loc) · 8.04 KB
/
setup.rs
File metadata and controls
203 lines (179 loc) · 8.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use std::{
path::PathBuf,
sync::{Arc, Mutex},
};
use tauri::{
webview::DownloadEvent, App, LogicalPosition, Manager, PhysicalSize, WebviewBuilder,
WebviewUrl, WindowBuilder, WindowEvent,
};
use tauri_plugin_shell::ShellExt;
#[cfg(target_os = "macos")]
use tauri::TitleBarStyle;
use crate::core::{
conf::AppConf,
constant::{ASK_HEIGHT, INIT_SCRIPT, TITLEBAR_HEIGHT},
template,
};
pub fn init(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {
let handle = app.handle();
let conf = AppConf::load(handle)?;
let ask_mode_height = if conf.ask_mode { ASK_HEIGHT } else { 0.0 };
template::Template::new(AppConf::get_scripts_path(handle)?);
tauri::async_runtime::spawn({
let handle = handle.clone();
let scale_factor: f64;
async move {
let mut core_window = WindowBuilder::new(&handle, "core")
.title("ChatGPT")
.resizable(true)
.inner_size(800.0, 600.0)
.min_inner_size(300.0, 200.0)
.theme(Some(AppConf::get_theme(&handle)));
#[cfg(target_os = "macos")]
{
core_window = core_window
.title_bar_style(TitleBarStyle::Overlay)
.hidden_title(true);
}
let core_window = core_window
.build()
.expect("[core:window] Failed to build window");
let win_size = core_window.inner_size().expect("[core:window] Failed to get window size");
let window = Arc::new(Mutex::new(core_window));
scale_factor = window.lock().unwrap().scale_factor().unwrap();
let download_path = Arc::new(Mutex::new(PathBuf::new()));
let app_handle = handle.clone();
let main_view = WebviewBuilder::new("main", WebviewUrl::App("https://chatgpt.com".into()))
.auto_resize()
.on_download(move |_, event| handle_download_event(&app_handle, &download_path, event))
.initialization_script(&AppConf::load_script(&handle, "ask.js"))
.initialization_script(INIT_SCRIPT);
let titlebar_view = WebviewBuilder::new("titlebar", WebviewUrl::App("index.html".into()))
.auto_resize();
let ask_view = WebviewBuilder::new("ask", WebviewUrl::App("index.html".into()))
.auto_resize();
let win = window.lock().unwrap();
let titlebar_height = (scale_factor * TITLEBAR_HEIGHT).round() as u32;
let ask_height = (scale_factor * ask_mode_height).round() as u32;
#[cfg(target_os = "macos")]
setup_macos_views(&win, &main_view, &titlebar_view, &ask_view, win_size, titlebar_height, ask_height);
#[cfg(not(target_os = "macos"))]
setup_non_macos_views(&win, &main_view, &titlebar_view, &ask_view, win_size, titlebar_height, ask_height);
let window_clone = Arc::clone(&window);
win.on_window_event(move |event| {
if let WindowEvent::Resized(size) = event {
update_view_positions(&window_clone, size, scale_factor, titlebar_height, ask_height);
}
});
}
});
Ok(())
}
// Function to handle download events
fn handle_download_event(app_handle: &tauri::AppHandle, download_path: &Arc<Mutex<PathBuf>>, event: DownloadEvent) {
match event {
DownloadEvent::Requested { destination, .. } => {
let download_dir = app_handle.path().download_dir().expect("[view:download] Failed to get download directory");
let mut locked_path = download_path.lock().expect("[view:download] Failed to lock download path");
*locked_path = download_dir.join(&destination);
*destination = locked_path.clone();
}
DownloadEvent::Finished { success, .. } => {
let final_path = download_path.lock().expect("[view:download] Failed to lock download path").clone();
if success {
app_handle.shell().open(final_path.to_string_lossy(), None).expect("[view:download] Failed to open file");
}
}
_ => (),
}
}
// Setup views for macOS
#[cfg(target_os = "macos")]
fn setup_macos_views(win: &tauri::Window, main_view: &WebviewBuilder, titlebar_view: &WebviewBuilder, ask_view: &WebviewBuilder, win_size: PhysicalSize<u32>, titlebar_height: u32, ask_height: u32) {
win.add_child(
titlebar_view,
LogicalPosition::new(0, 0),
PhysicalSize::new(win_size.width, titlebar_height),
).unwrap();
win.add_child(
ask_view,
LogicalPosition::new(0.0, (win_size.height as f64 / win.scale_factor().unwrap()) - ASK_HEIGHT),
PhysicalSize::new(win_size.width, ask_height),
).unwrap();
win.add_child(
main_view,
LogicalPosition::new(0.0, TITLEBAR_HEIGHT),
PhysicalSize::new(win_size.width, win_size.height - titlebar_height - ask_height),
).unwrap();
}
// Setup views for non-macOS
#[cfg(not(target_os = "macos"))]
fn setup_non_macos_views(win: &tauri::Window, main_view: &WebviewBuilder, titlebar_view: &WebviewBuilder, ask_view: &WebviewBuilder, win_size: PhysicalSize<u32>, titlebar_height: u32, ask_height: u32) {
win.add_child(
ask_view,
LogicalPosition::new(0.0, (win_size.height as f64 / win.scale_factor().unwrap()) - ASK_HEIGHT),
PhysicalSize::new(win_size.width, ask_height),
).unwrap();
win.add_child(
titlebar_view,
LogicalPosition::new(0.0, (win_size.height as f64 / win.scale_factor().unwrap()) - ASK_HEIGHT - TITLEBAR_HEIGHT),
PhysicalSize::new(win_size.width, titlebar_height),
).unwrap();
win.add_child(
main_view,
LogicalPosition::new(0.0, 0.0),
PhysicalSize::new(win_size.width, win_size.height - (ask_height + titlebar_height)),
).unwrap();
}
// Update view positions based on window resize
fn update_view_positions(window_clone: &Arc<Mutex<tauri::Window>>, size: PhysicalSize<u32>, scale_factor: f64, titlebar_height: u32, ask_height: u32) {
let win = window_clone.lock().unwrap();
let main_view = win.get_webview("main").expect("[view:main] Failed to get webview window");
let titlebar_view = win.get_webview("titlebar").expect("[view:titlebar] Failed to get webview window");
let ask_view = win.get_webview("ask").expect("[view:ask] Failed to get webview window");
#[cfg(target_os = "macos")]
{
set_view_properties(
&main_view,
LogicalPosition::new(0.0, TITLEBAR_HEIGHT),
PhysicalSize::new(size.width, size.height - (titlebar_height + ask_height)),
);
set_view_properties(
&titlebar_view,
LogicalPosition::new(0.0, 0.0),
PhysicalSize::new(size.width, titlebar_height),
);
set_view_properties(
&ask_view,
LogicalPosition::new(0.0, (size.height as f64 / scale_factor) - ASK_HEIGHT),
PhysicalSize::new(size.width, ask_height),
);
}
#[cfg(not(target_os = "macos"))]
{
set_view_properties(
&main_view,
LogicalPosition::new(0.0, 0.0),
PhysicalSize::new(size.width, size.height - (ask_height + titlebar_height)),
);
set_view_properties(
&titlebar_view,
LogicalPosition::new(0.0, (size.height as f64 / scale_factor) - TITLEBAR_HEIGHT),
PhysicalSize::new(size.width, titlebar_height),
);
set_view_properties(
&ask_view,
LogicalPosition::new(0.0, (size.height as f64 / scale_factor) - ASK_HEIGHT - TITLEBAR_HEIGHT),
PhysicalSize::new(size.width, ask_height),
);
}
}
// Helper function to set view properties
fn set_view_properties(view: &tauri::Webview, position: LogicalPosition<f64>, size: PhysicalSize<u32>) {
if let Err(e) = view.set_position(position) {
eprintln!("[view:position] Failed to set view position: {}", e);
}
if let Err(e) = view.set_size(size) {
eprintln!("[view:size] Failed to set view size: {}", e);
}
}