Skip to content

Commit 52c6a80

Browse files
committed
feat: tauri shell adpater and window size and positioning logic
1 parent 125d1dd commit 52c6a80

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

src/phoenix/shell.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import init from "./init_vfs.js";
2929
import ERR_CODES from "./errno.js";
3030
import getBrowserDetails from "./browserDetails.js";
31+
import initTauriShell from "./tauriShell.js";
3132

3233
function _getBaseURL() {
3334
// strip query string
@@ -77,3 +78,8 @@ Phoenix.app = {
7778
if(!window.appshell){
7879
window.appshell = Phoenix;
7980
}
81+
82+
if(window.__TAURI__) {
83+
// the __TAURI__ object will be present if we are in a tauri window
84+
initTauriShell();
85+
}

src/phoenix/tauriShell.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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

Comments
 (0)