File size: 9,703 Bytes
2d8be8f | 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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | <script>
import { onMount, tick } from 'svelte'
import { writable } from 'svelte/store'
import { invoke } from '@tauri-apps/api/core'
import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow'
import { setTheme } from '@tauri-apps/api/app'
import Welcome from './views/Welcome.svelte'
import Communication from './views/Communication.svelte'
import Window from './views/Window.svelte'
import WebRTC from './views/WebRTC.svelte'
import App from './views/App.svelte'
import Menu from './views/Menu.svelte'
import Tray from './views/Tray.svelte'
document.addEventListener('keydown', (event) => {
if (event.ctrlKey && event.key === 'b') {
invoke('plugin:app-menu|toggle')
}
})
const appWindow = getCurrentWebviewWindow()
appWindow.onDragDropEvent((event) => {
onMessage(event.payload)
})
const userAgent = navigator.userAgent.toLowerCase()
const isMobile = userAgent.includes('android') || userAgent.includes('iphone')
const desktopViews = [
{
label: 'App',
component: App,
icon: 'i-codicon-hubot'
},
{
label: 'Window',
component: Window,
icon: 'i-codicon-window'
},
{
label: 'Menu',
component: Menu,
icon: 'i-ph-list'
},
{
label: 'Tray',
component: Tray,
icon: 'i-ph-tray'
}
]
const views = [
{
label: 'Welcome',
component: Welcome,
icon: 'i-ph-hand-waving'
},
{
label: 'Communication',
component: Communication,
icon: 'i-codicon-radio-tower'
},
...(isMobile ? [] : desktopViews),
{
label: 'WebRTC',
component: WebRTC,
icon: 'i-ph-broadcast'
}
]
let selected = $state.raw(views[0])
function select(view) {
selected = view
}
// dark/light
let isDark = $state()
onMount(() => {
isDark = localStorage && localStorage.getItem('theme') == 'dark'
applyTheme(isDark)
})
function applyTheme(isDark) {
const html = document.querySelector('html')
isDark ? html.classList.add('dark') : html.classList.remove('dark')
localStorage && localStorage.setItem('theme', isDark ? 'dark' : '')
}
function toggleDark() {
isDark = !isDark
applyTheme(isDark)
setTheme(isDark ? 'dark' : 'light')
}
// Console
let messages = writable([])
let consoleTextEl = $state()
// this function is renders HTML without sanitizing it so it's insecure
// we only use it with our own input data
async function insecureRenderHtml(html) {
messages.update((r) => [
...r,
{
html: `<pre><strong class="text-accent dark:text-darkAccent">[${new Date().toLocaleTimeString()} ${html}</pre>`
}
])
await tick()
if (consoleTextEl) consoleTextEl.scrollTop = consoleTextEl.scrollHeight
}
async function onMessage(value) {
const valueStr =
typeof value === 'string'
? value
: JSON.stringify(
value instanceof ArrayBuffer
? Array.from(new Uint8Array(value))
: value,
null,
1
)
insecureRenderHtml(valueStr)
}
function clear() {
messages.update(() => [])
}
let consoleEl = $state(),
consoleH,
cStartY
let minConsoleHeight = 50
function startResizingConsole(e) {
cStartY = e.clientY
const styles = window.getComputedStyle(consoleEl)
consoleH = parseInt(styles.height, 10)
const moveHandler = (e) => {
const dy = e.clientY - cStartY
const newH = consoleH - dy
consoleEl.style.height = `${
newH < minConsoleHeight ? minConsoleHeight : newH
}px`
}
const upHandler = () => {
document.removeEventListener('mouseup', upHandler)
document.removeEventListener('mousemove', moveHandler)
}
document.addEventListener('mouseup', upHandler)
document.addEventListener('mousemove', moveHandler)
}
// mobile
let isSideBarOpen = $state(false)
let sidebar
let sidebarToggle
let isDraggingSideBar = false
let draggingStartPosX = 0
let draggingEndPosX = 0
const clamp = (min, num, max) => Math.min(Math.max(num, min), max)
function toggleSidebar(sidebar, isSideBarOpen) {
sidebar.style.setProperty(
'--translate-x',
`${isSideBarOpen ? '0' : '-18.75'}rem`
)
}
onMount(() => {
sidebar = document.querySelector('#sidebar')
sidebarToggle = document.querySelector('#sidebarToggle')
document.addEventListener('click', (e) => {
if (sidebarToggle.contains(e.target)) {
isSideBarOpen = !isSideBarOpen
} else if (isSideBarOpen && !sidebar.contains(e.target)) {
isSideBarOpen = false
}
})
document.addEventListener('touchstart', (e) => {
if (sidebarToggle.contains(e.target)) return
const x = e.touches[0].clientX
if ((0 < x && x < 20 && !isSideBarOpen) || isSideBarOpen) {
isDraggingSideBar = true
draggingStartPosX = x
}
})
document.addEventListener('touchmove', (e) => {
if (isDraggingSideBar) {
const x = e.touches[0].clientX
draggingEndPosX = x
const delta = (x - draggingStartPosX) / 10
sidebar.style.setProperty(
'--translate-x',
`-${clamp(0, isSideBarOpen ? 0 - delta : 18.75 - delta, 18.75)}rem`
)
}
})
document.addEventListener('touchend', () => {
if (isDraggingSideBar) {
const delta = (draggingEndPosX - draggingStartPosX) / 10
isSideBarOpen = isSideBarOpen ? delta > -(18.75 / 2) : delta > 18.75 / 2
}
isDraggingSideBar = false
})
})
$effect(() => {
const sidebar = document.querySelector('#sidebar')
if (sidebar) {
toggleSidebar(sidebar, isSideBarOpen)
}
})
</script>
<!-- Sidebar toggle, only visible on small screens -->
<div
id="sidebarToggle"
class="z-2000 hidden lt-sm:flex justify-center items-center absolute top-2 left-2 w-8 h-8 rd-8
bg-accent dark:bg-darkAccent active:bg-accentDark dark:active:bg-darkAccentDark"
>
{#if isSideBarOpen}
{:else}
{/if}
</div>
<div
class="flex h-screen w-screen overflow-hidden children-pt4 children-pb-2 text-primaryText dark:text-darkPrimaryText"
>
<aside
id="sidebar"
class="lt-sm:h-screen lt-sm:shadow-lg lt-sm:shadow lt-sm:transition-transform lt-sm:absolute lt-sm:z-1999
bg-darkPrimaryLighter transition-colors-250 overflow-hidden grid grid-rows-[min-content_auto] select-none px-2"
>
<img
class="self-center p-7 cursor-pointer"
src="tauri_logo.png"
alt="Tauri logo"
/>
<a href="##" class="nv justify-between" onclick={toggleDark}>
{#if isDark}
Switch to Light mode
{:else}
Switch to Dark mode
{/if}
</a>
<br />
<br />
<a
class="nv justify-between"
target="_blank"
href="https://v2.tauri.app/start/"
>
Documentation
</a>
<a
class="nv justify-between"
target="_blank"
href="https://github.com/tauri-apps/tauri"
>
GitHub
</a>
<a
class="nv justify-between"
target="_blank"
href="https://github.com/tauri-apps/tauri/tree/dev/examples/api"
>
Source
</a>
<br />
<br />
<div class="flex flex-col overflow-y-auto children-flex-none gap-1">
{#each views as view}
<a
href="##"
class="nv {selected === view ? 'nv_selected' : ''}"
onclick={() => {
select(view)
isSideBarOpen = false
}}
>
{view.label}
{/each}
</div>
</aside>
<main
class="flex-1 bg-primary dark:bg-darkPrimary transition-transform transition-colors-250 grid grid-rows-[2fr_auto]"
>
<div class="px-5 overflow-hidden grid grid-rows-[auto_1fr]">
{selected.label}
<div class="overflow-y-auto">
<div class="mr-2">
<selected.component {onMessage}
<div
bind:this={consoleEl}
id="console"
class="select-none h-15rem grid grid-rows-[2px_2rem_1fr] gap-1 overflow-hidden"
>
<div
role="button"
tabindex="0"
onmousedown={startResizingConsole}
class="bg-black/20 h-4px cursor-ns-resize"
></div>
<div class="flex justify-between items-center px-2">
{(e) => (e.key === 'Enter' ? clear() : {})}
onclick={clear}
>
</div>
<div
bind:this={consoleTextEl}
class="px-2 overflow-y-auto all:font-mono code-block all:text-xs select-text mr-2"
>
{#each $messages as r}
{@html r.html}
{/each}
</div>
</div>
</main>
</div>
|