feature/tauri-v2
svelte 272 lines 7.94 KB
Raw
1 <script lang="ts" context="module">
2 export const _openPopup = writable(false);
3 const popupProps = writable({});
4 const popup = writable(null);
5 export const fullscreen = writable(false);
6
7 type Button = {
8 name: string;
9 action;
10 disabled?: boolean;
11 style?: "primary" | "secondary" | "accent" | "danger";
12 type?: "button" | "submit" | "reset";
13 cancel?: boolean;
14 };
15
16 export function openInputModal(
17 title: string,
18 description: string,
19 buttons: Button[],
20 options = undefined,
21 path = ""
22 ) {
23 popup.set(InputModal);
24 popupProps.set({
25 title: title,
26 description: description,
27 buttons: buttons,
28 options: options,
29 open: true,
30 path: path,
31 });
32 _openPopup.set(true);
33 }
34 export function openRenameModal(
35 title: string,
36 description: string,
37 buttons: Button[],
38 path: string
39 ) {
40 popup.set(RenameModal);
41 popupProps.set({
42 title: title,
43 description: description,
44 buttons: buttons,
45 open: true,
46 path: path,
47 });
48 _openPopup.set(true);
49 }
50 </script>
51
52 <script lang="ts">
53 import Header from "./lib/Header.svelte";
54 import Sidebar, { showsidebarview, tool } from "./lib/Sidebar.svelte";
55 import { Pane, Splitpanes } from "svelte-splitpanes";
56 import SidebarView from "./lib/SidebarView.svelte";
57 import Statusbar, {
58 showBottomPanel,
59 editortool,
60 } from "./lib/Statusbar.svelte";
61 import EditorTabList, {
62 hidden,
63 updateTablistWidth,
64 } from "./lib/EditorTabList.svelte";
65 import { onMount } from "svelte";
66 import { Window } from "@tauri-apps/api/window";
67 import { writable } from "svelte/store";
68 import InputModal from "./lib/Modals/InputModal.svelte";
69 import RenameModal from "./lib/Modals/RenameModal.svelte";
70 import { loadDefaultSettings } from "./config/config";
71 import { error } from "tauri-plugin-log-api";
72 import ToolView from "./lib/ToolView.svelte";
73 import { getExtensions } from "./config/extensionhandler";
74 import { fitTerminal } from "./lib/Terminal.svelte";
75 import { loadDir } from "./lib/File";
76 import NotificationToasts from "./lib/Notifications/NotificationToasts.svelte";
77 import {
78 NotifType,
79 addNotification,
80 toasts,
81 } from "./lib/Notifications/notifications";
82 import { addTab } from "./lib/EditorTabList.svelte";
83 import Welcome from "./lib/Welcome.svelte";
84 import { openUrl } from '@tauri-apps/plugin-opener';
85
86 let resolution = writable(0);
87
88 let minPanelSize = 10;
89 let panelSize = 15;
90 let bottomPanelSize = 20;
91 let appWindow = new Window("main");
92
93 onMount(async () => {
94 await getExtensions();
95 await loadDefaultSettings();
96 let dir = localStorage.getItem("lastDir");
97 if (dir) {
98 loadDir(dir);
99 } else {
100 addTab(
101 "Welcome",
102 "Welcome",
103 new Welcome({ target: document.getElementById("tabview") })
104 );
105 }
106
107 let size = await appWindow.innerSize();
108 resolution.set(size.width);
109 updateMinPanelSize();
110
111 addNotification(
112 NotifType.Message,
113 "Welcome to Svara",
114 [
115 {
116 label: "Learn More",
117 action: () => {
118 openUrl("https://github.com/setoelkahfi/svara");
119 },
120 },
121 {
122 label: "Create Issue",
123 action: () => {
124 openUrl("https://github.com/setoelkahfi/svara/issues/new/choose");
125 },
126 },
127 ],
128 " is in alpha. If there are any bugs present or features you want to add, create an issue below."
129 );
130
131 appWindow.onResized((e) => {
132 resolution.set(e.payload.width);
133 updateMinPanelSize();
134 });
135
136 window.onunhandledrejection = (e) => {
137 error(e.reason);
138 };
139 });
140
141 function updatePanelSize(e) {
142 if ($showsidebarview) {
143 panelSize = e.detail[0].size;
144 }
145 }
146
147 async function updateMinPanelSize() {
148 fitTerminal();
149 updateTablistWidth();
150 if (!(await appWindow.isFocused())) {
151 return;
152 }
153 // TODO: make this better
154 if ($resolution <= 700) {
155 minPanelSize = 30;
156 } else if ($resolution <= 1040) {
157 minPanelSize = 20;
158 } else if ($resolution <= 1120) {
159 minPanelSize = 17;
160 } else if ($resolution <= 1300) {
161 minPanelSize = 14;
162 } else if ($resolution > 1300) {
163 minPanelSize = 10;
164 }
165
166 if (panelSize < minPanelSize) {
167 panelSize = minPanelSize;
168 }
169 }
170 </script>
171
172 <svelte:window on:contextmenu|preventDefault />
173
174 <div id="_" inert={$_openPopup}>
175 {#if !$fullscreen}
176 <Header />
177 {/if}
178 <div id="main" class:fullscreen={$fullscreen}>
179 <Sidebar />
180 <Splitpanes
181 on:resized={updatePanelSize}
182 on:resize={updateMinPanelSize}
183 theme="editor-panes"
184 >
185 {#if $showsidebarview}
186 <Pane
187 bind:size={panelSize}
188 bind:minSize={minPanelSize}
189 maxSize={60}
190 >
191 <SidebarView content={$tool.content}></SidebarView>
192 </Pane>
193 {/if}
194 <Pane>
195 <div class="__">
196 <Splitpanes
197 horizontal
198 theme="editor-panes"
199 on:resize={fitTerminal}
200 class={!$showBottomPanel ? "hidden" : ""}
201 >
202 <Pane
203 size={$showBottomPanel
204 ? 100 - bottomPanelSize
205 : 100}
206 >
207 <div id="container">
208 <EditorTabList />
209 <div id="tabview" class:hidden={$hidden}></div>
210 </div>
211 </Pane>
212 {#if $editortool}
213 <Pane
214 bind:size={bottomPanelSize}
215 maxSize={90}
216 minSize={10}
217 class="view-bottom-pane"
218 >
219 <ToolView
220 name={$editortool.name}
221 options={$editortool.options}
222 buttons={$editortool.buttons}
223 ></ToolView>
224 </Pane>
225 {/if}
226 </Splitpanes>
227 </div>
228 </Pane>
229 </Splitpanes>
230 </div>
231 <Statusbar />
232 {#if $toasts.length != 0}
233 <NotificationToasts />
234 {/if}
235 </div>
236
237 {#if $_openPopup}
238 <svelte:component this={$popup} {...$popupProps}></svelte:component>
239 {/if}
240
241 <style lang="scss">
242 .__ {
243 height: 100%;
244 overflow: hidden;
245 :global(.hidden) {
246 :global(.splitpanes__splitter),
247 :global(.view-bottom-pane) {
248 display: none;
249 }
250 }
251 }
252 #main {
253 display: flex;
254 }
255 #container {
256 height: 100%;
257 overflow: hidden;
258 background-image: url("/assets/images/Icon.png");
259 background-repeat: no-repeat;
260 background-position: center;
261 }
262 #tabview {
263 width: -webkit-fill-available;
264 z-index: 1;
265 }
266 .hidden {
267 visibility: hidden;
268 }
269 :global(.view-bottom-pane) {
270 z-index: 1;
271 }
272 </style>