1+ /*
2+ * GNU AGPL-3.0 License
3+ *
4+ * Copyright (c) 2021 - present core.ai . All rights reserved.
5+ *
6+ * This program is free software: you can redistribute it and/or modify it under
7+ * the terms of the GNU Affero General Public License as published by the Free
8+ * Software Foundation, either version 3 of the License, or (at your option) any later version.
9+ *
10+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+ * See the GNU Affero General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU Affero General Public License along
15+ * with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
16+ *
17+ */
18+
19+ // jshint ignore: start
20+ /*globals __TAURI__*/
21+
22+ const TAURI_KEYS = {
23+ LAST_WINDOW_WIDTH : "tauri.LAST_WINDOW_WIDTH" ,
24+ LAST_WINDOW_HEIGHT : "tauri.LAST_WINDOW_HEIGHT"
25+ } ;
26+
27+ const appWindow = __TAURI__ . window . appWindow ;
28+
29+ function _setupWindowResizeListeners ( ) {
30+ appWindow . onResized ( async ( { payload : size } ) => {
31+ const maximized = await appWindow . isMaximized ( ) ;
32+ if ( ! maximized ) {
33+ localStorage . setItem ( TAURI_KEYS . LAST_WINDOW_HEIGHT , size . height ) ;
34+ localStorage . setItem ( TAURI_KEYS . LAST_WINDOW_WIDTH , size . width ) ;
35+ }
36+ } ) ;
37+ }
38+
39+ async function positionWindow ( ) {
40+ const phoenixAspectRatio = 1.6 , // phoenix looks good in aspect ratio 1.6w:1h
41+ minWidth = 800 ,
42+ minHeight = 600 ;
43+ let monitorSize = ( await __TAURI__ . window . currentMonitor ( ) ) . size ,
44+ targetWindowHeight = monitorSize . height * 2 / 3 ,
45+ targetWindowWidth = targetWindowHeight * phoenixAspectRatio ;
46+ let targetHeight = parseInt ( localStorage . getItem ( TAURI_KEYS . LAST_WINDOW_HEIGHT ) || `${ targetWindowHeight } ` ) ,
47+ targetWidth = parseInt ( localStorage . getItem ( TAURI_KEYS . LAST_WINDOW_WIDTH ) || `${ targetWindowWidth } ` ) ;
48+ if ( targetHeight . height > monitorSize . height || targetWidth . width > monitorSize . width ) {
49+ // our window is larger than the monitor, so just maximise to fit to monitor
50+ appWindow . maximize ( ) ;
51+ _setupWindowResizeListeners ( ) ;
52+ return ;
53+ }
54+ if ( targetHeight < minHeight ) {
55+ targetHeight = minHeight ;
56+ }
57+ if ( targetWidth < minWidth ) {
58+ targetWidth = minWidth ;
59+ }
60+ await appWindow . setSize ( new __TAURI__ . window . PhysicalSize ( targetWidth , targetHeight ) ) ;
61+ _setupWindowResizeListeners ( ) ;
62+ }
63+
64+ function initTauriShell ( ) {
65+ positionWindow ( ) ;
66+ }
67+
68+ export default initTauriShell ;
0 commit comments