Window.svelte 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <script>
  2. import { appWindow, WebviewWindow, LogicalSize, LogicalPosition } from "@tauri-apps/api/window";
  3. import { open as openDialog } from "@tauri-apps/api/dialog";
  4. import { open } from "@tauri-apps/api/shell";
  5. const {
  6. setResizable,
  7. setTitle,
  8. maximize,
  9. unmaximize,
  10. minimize,
  11. unminimize,
  12. show,
  13. hide,
  14. setDecorations,
  15. setAlwaysOnTop,
  16. setSize,
  17. setMinSize,
  18. setMaxSize,
  19. setPosition,
  20. setFullscreen,
  21. setIcon,
  22. } = appWindow;
  23. export let onMessage;
  24. let urlValue = "https://tauri.studio";
  25. let resizable = true;
  26. let maximized = false;
  27. let transparent = false;
  28. let decorations = true;
  29. let alwaysOnTop = false;
  30. let fullscreen = false;
  31. let width = 900;
  32. let height = 700;
  33. let minWidth = 600;
  34. let minHeight = 600;
  35. let maxWidth = null;
  36. let maxHeight = null;
  37. let x = 100;
  38. let y = 100;
  39. let windowTitle = "Awesome Tauri Example!";
  40. function openUrl() {
  41. open(urlValue);
  42. }
  43. function setTitle_() {
  44. setTitle(windowTitle);
  45. }
  46. function hide_() {
  47. hide();
  48. setTimeout(show, 2000);
  49. }
  50. function minimize_() {
  51. minimize();
  52. setTimeout(unminimize, 2000);
  53. }
  54. function getIcon() {
  55. openDialog({
  56. multiple: false,
  57. }).then(setIcon);
  58. }
  59. function createWindow() {
  60. const webview = new WebviewWindow(Math.random().toString());
  61. webview.once('tauri://error', function () {
  62. onMessage("Error creating new webview")
  63. })
  64. }
  65. $: setResizable(resizable);
  66. $: maximized ? maximize() : unmaximize();
  67. $: setDecorations(decorations);
  68. $: setAlwaysOnTop(alwaysOnTop);
  69. $: setFullscreen(fullscreen);
  70. $: setSize(new LogicalSize(width, height));
  71. $: minWidth && minHeight ? setMinSize(new LogicalSize(minWidth, minHeight)) : setMinSize(null);
  72. $: maxWidth && maxHeight ? setMaxSize(new LogicalSize(maxWidth, maxHeight)) : setMaxSize(null);
  73. $: setPosition(new LogicalPosition(x, y));
  74. </script>
  75. <div class="flex col">
  76. <div>
  77. <label>
  78. <input type="checkbox" bind:checked={resizable} />
  79. Resizable
  80. </label>
  81. <label>
  82. <input type="checkbox" bind:checked={maximized} />
  83. Maximize
  84. </label>
  85. <button title="Unminimizes after 2 seconds" on:click={minimize_}>
  86. Minimize
  87. </button>
  88. <button title="Visible again after 2 seconds" on:click={hide_}>
  89. Hide
  90. </button>
  91. <label>
  92. <input type="checkbox" bind:checked={transparent} />
  93. Transparent
  94. </label>
  95. <label>
  96. <input type="checkbox" bind:checked={decorations} />
  97. Has decorations
  98. </label>
  99. <label>
  100. <input type="checkbox" bind:checked={alwaysOnTop} />
  101. Always on top
  102. </label>
  103. <label>
  104. <input type="checkbox" bind:checked={fullscreen} />
  105. Fullscreen
  106. </label>
  107. <button on:click={getIcon}> Change icon </button>
  108. </div>
  109. <div>
  110. <div class="window-controls flex flex-row">
  111. <div class="flex col grow">
  112. <div>
  113. X
  114. <input type="number" bind:value={x} min="0" />
  115. </div>
  116. <div>
  117. Y
  118. <input type="number" bind:value={y} min="0" />
  119. </div>
  120. </div>
  121. <div class="flex col grow">
  122. <div>
  123. Width
  124. <input type="number" bind:value={width} min="400" />
  125. </div>
  126. <div>
  127. Height
  128. <input type="number" bind:value={height} min="400" />
  129. </div>
  130. </div>
  131. <div class="flex col grow">
  132. <div>
  133. Min width
  134. <input type="number" bind:value={minWidth} />
  135. </div>
  136. <div>
  137. Min height
  138. <input type="number" bind:value={minHeight} />
  139. </div>
  140. </div>
  141. <div class="flex col grow">
  142. <div>
  143. Max width
  144. <input type="number" bind:value={maxWidth} min="400" />
  145. </div>
  146. <div>
  147. Max height
  148. <input type="number" bind:value={maxHeight} min="400" />
  149. </div>
  150. </div>
  151. </div>
  152. </div>
  153. </div>
  154. <form style="margin-top: 24px" on:submit|preventDefault={setTitle_}>
  155. <input id="title" bind:value={windowTitle} />
  156. <button class="button" type="submit">Set title</button>
  157. </form>
  158. <form style="margin-top: 24px" on:submit|preventDefault={openUrl}>
  159. <input id="url" bind:value={urlValue} />
  160. <button class="button" id="open-url"> Open URL </button>
  161. </form>
  162. <button class="button" on:click={createWindow}>New window</button>
  163. <style>
  164. .flex-row {
  165. flex-direction: row;
  166. }
  167. .grow {
  168. flex-grow: 1;
  169. }
  170. .window-controls input {
  171. width: 50px;
  172. }
  173. </style>