-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Window.svelte
296 lines (272 loc) · 8.66 KB
/
Window.svelte
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<script>
import { appWindow, WebviewWindow, LogicalSize, LogicalPosition, UserAttentionType, getCurrent, PhysicalSize, PhysicalPosition } from "@tauri-apps/api/window";
import { open as openDialog } from "@tauri-apps/api/dialog";
import { open } from "@tauri-apps/api/shell";
window.UserAttentionType = UserAttentionType;
let selectedWindow = getCurrent().label;
const windowMap = {
[selectedWindow]: appWindow
}
export let onMessage;
let urlValue = "https://tauri.studio";
let resizable = true;
let maximized = false;
let transparent = false;
let decorations = true;
let alwaysOnTop = false;
let fullscreen = false;
let width = 900;
let height = 700;
let minWidth = 600;
let minHeight = 600;
let maxWidth = null;
let maxHeight = null;
let x = 100;
let y = 100;
let scaleFactor = 1;
let innerPosition = new PhysicalPosition(x, y);
let outerPosition = new PhysicalPosition(x, y);
let innerSize = new PhysicalSize(width, height);
let outerSize = new PhysicalSize(width, height);
let resizeEventUnlisten;
let moveEventUnlisten;
let windowTitle = "Awesome Tauri Example!";
function openUrl() {
open(urlValue);
}
function setTitle_() {
windowMap[selectedWindow].setTitle(windowTitle);
}
function hide_() {
windowMap[selectedWindow].hide();
setTimeout(windowMap[selectedWindow].show, 2000);
}
function minimize_() {
windowMap[selectedWindow].minimize();
setTimeout(windowMap[selectedWindow].unminimize, 2000);
}
function getIcon() {
openDialog({
multiple: false,
}).then(windowMap[selectedWindow].setIcon);
}
function createWindow() {
const label = Math.random().toString();
const webview = new WebviewWindow(label);
windowMap[label] = webview;
webview.once('tauri://error', function () {
onMessage("Error creating new webview")
})
}
function handleWindowResize() {
windowMap[selectedWindow].innerSize().then(response => {
innerSize = response
width = innerSize.width
height = innerSize.height
});
windowMap[selectedWindow].outerSize().then(response => {
outerSize = response
});
}
function handleWindowMove() {
windowMap[selectedWindow].innerPosition().then(response => {
innerPosition = response
});
windowMap[selectedWindow].outerPosition().then(response => {
outerPosition = response
x = outerPosition.x
y = outerPosition.y
});
}
function addWindowEventListeners(window) {
if (resizeEventUnlisten) {
resizeEventUnlisten();
}
if(moveEventUnlisten) {
moveEventUnlisten();
}
moveEventUnlisten = window.listen('tauri://move', handleWindowMove);
resizeEventUnlisten = window.listen('tauri://resize', handleWindowResize);
}
async function requestUserAttention_() {
await windowMap[selectedWindow].minimize();
await windowMap[selectedWindow].requestUserAttention(UserAttentionType.Critical);
await new Promise(resolve => setTimeout(resolve, 3000));
await windowMap[selectedWindow].requestUserAttention(null);
}
$: windowMap[selectedWindow].setResizable(resizable);
$: maximized ? windowMap[selectedWindow].maximize() : windowMap[selectedWindow].unmaximize();
$: windowMap[selectedWindow].setDecorations(decorations);
$: windowMap[selectedWindow].setAlwaysOnTop(alwaysOnTop);
$: windowMap[selectedWindow].setFullscreen(fullscreen);
$: windowMap[selectedWindow].setSize(new LogicalSize(width, height));
$: minWidth && minHeight ? windowMap[selectedWindow].setMinSize(new LogicalSize(minWidth, minHeight)) : windowMap[selectedWindow].setMinSize(null);
$: maxWidth && maxHeight ? windowMap[selectedWindow].setMaxSize(new LogicalSize(maxWidth, maxHeight)) : windowMap[selectedWindow].setMaxSize(null);
$: windowMap[selectedWindow].setPosition(new LogicalPosition(x, y));
$: windowMap[selectedWindow].scaleFactor().then(factor => scaleFactor = factor);
$: addWindowEventListeners(windowMap[selectedWindow]);
</script>
<div class="flex col">
<select class="button" bind:value={selectedWindow}>
{#each Object.keys(windowMap) as label}
<option value={label}>{label}</option>
{/each}
</select>
<div>
<label>
<input type="checkbox" bind:checked={resizable} />
Resizable
</label>
<label>
<input type="checkbox" bind:checked={maximized} />
Maximize
</label>
<button title="Unminimizes after 2 seconds" on:click={() => windowMap[selectedWindow].center()}>
Center
</button>
<button title="Unminimizes after 2 seconds" on:click={minimize_}>
Minimize
</button>
<button title="Visible again after 2 seconds" on:click={hide_}>
Hide
</button>
<label>
<input type="checkbox" bind:checked={transparent} />
Transparent
</label>
<label>
<input type="checkbox" bind:checked={decorations} />
Has decorations
</label>
<label>
<input type="checkbox" bind:checked={alwaysOnTop} />
Always on top
</label>
<label>
<input type="checkbox" bind:checked={fullscreen} />
Fullscreen
</label>
<button on:click={getIcon}> Change icon </button>
</div>
<div>
<div class="window-controls flex flex-row">
<div class="flex col grow">
<div>
X
<input type="number" bind:value={x} min="0" />
</div>
<div>
Y
<input type="number" bind:value={y} min="0" />
</div>
</div>
<div class="flex col grow">
<div>
Width
<input type="number" bind:value={width} min="400" />
</div>
<div>
Height
<input type="number" bind:value={height} min="400" />
</div>
</div>
<div class="flex col grow">
<div>
Min width
<input type="number" bind:value={minWidth} />
</div>
<div>
Min height
<input type="number" bind:value={minHeight} />
</div>
</div>
<div class="flex col grow">
<div>
Max width
<input type="number" bind:value={maxWidth} min="400" />
</div>
<div>
Max height
<input type="number" bind:value={maxHeight} min="400" />
</div>
</div>
</div>
</div>
</div>
<div>
<div class="flex">
<div class="grow window-property">
<div>Inner Size</div>
<span>Width: {innerSize.width}</span>
<span>Height: {innerSize.height}</span>
</div>
<div class="grow window-property">
<div>Outer Size</div>
<span>Width: {outerSize.width}</span>
<span>Height: {outerSize.height}</span>
</div>
</div>
<div class="flex">
<div class="grow window-property">
<div>Inner Logical Size</div>
<span>Width: {innerSize.toLogical(scaleFactor).width}</span>
<span>Height: {innerSize.toLogical(scaleFactor).height}</span>
</div>
<div class="grow window-property">
<div>Outer Logical Size</div>
<span>Width: {outerSize.toLogical(scaleFactor).width}</span>
<span>Height: {outerSize.toLogical(scaleFactor).height}</span>
</div>
</div>
<div class="flex">
<div class="grow window-property">
<div>Inner Position</div>
<span>x: {innerPosition.x}</span>
<span>y: {innerPosition.y}</span>
</div>
<div class="grow window-property">
<div>Outer Position</div>
<span>x: {outerPosition.x}</span>
<span>y: {outerPosition.y}</span>
</div>
</div>
<div class="flex">
<div class="grow window-property">
<div>Inner Logical Position</div>
<span>x: {innerPosition.toLogical(scaleFactor).x}</span>
<span>y: {innerPosition.toLogical(scaleFactor).y}</span>
</div>
<div class="grow window-property">
<div>Outer Logical Position</div>
<span>x: {outerPosition.toLogical(scaleFactor).x}</span>
<span>y: {outerPosition.toLogical(scaleFactor).y}</span>
</div>
</div>
</div>
<form style="margin-top: 24px" on:submit|preventDefault={setTitle_}>
<input id="title" bind:value={windowTitle} />
<button class="button" type="submit">Set title</button>
</form>
<form style="margin-top: 24px" on:submit|preventDefault={openUrl}>
<input id="url" bind:value={urlValue} />
<button class="button" id="open-url"> Open URL </button>
</form>
<button class="button" on:click={requestUserAttention_} title="Minimizes the window, requests attention for 3s and then resets it">Request attention</button>
<button class="button" on:click={createWindow}>New window</button>
<style>
.flex-row {
flex-direction: row;
}
.grow {
flex-grow: 1;
}
.window-controls input {
width: 50px;
}
.window-property {
margin-top: 12px;
}
.window-property span {
font-size: 0.8rem;
}
</style>