Skip to content

Commit 04a0954

Browse files
authored
chore(example): Improve dialog/fs mobile examples (#2410)
1 parent 9e4e859 commit 04a0954

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

examples/api/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="UTF-8" />
55
<meta
66
name="viewport"
7-
content="width=device-width, initial-scale=1.0, viewport-fit=cover"
7+
content="width=device-width, initial-scale=1.0, viewport-fit=cover, user-scalable=0"
88
/>
99
<title>Svelte + Vite App</title>
1010
</head>

examples/api/src-tauri/capabilities/base.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,19 @@
6868
"fs:allow-rename",
6969
"fs:allow-mkdir",
7070
"fs:allow-remove",
71+
"fs:allow-stat",
72+
"fs:allow-fstat",
73+
"fs:allow-lstat",
7174
"fs:allow-write-text-file",
7275
"fs:read-meta",
7376
"fs:scope-download-recursive",
7477
"fs:scope-resource-recursive",
7578
{
7679
"identifier": "fs:scope-appdata-recursive",
7780
"allow": [
81+
{
82+
"path": "$APPDATA/db/"
83+
},
7884
{
7985
"path": "$APPDATA/db/**"
8086
}

examples/api/src/views/FileSystem.svelte

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<script>
22
import * as fs from '@tauri-apps/plugin-fs'
3+
import * as os from '@tauri-apps/plugin-os'
34
import { convertFileSrc } from '@tauri-apps/api/core'
45
import { arrayBufferToBase64 } from '../lib/utils'
5-
import { onDestroy } from 'svelte'
6+
import { onDestroy, onMount } from 'svelte'
67
78
const { onMessage, insecureRenderHtml } = $props()
89
@@ -18,6 +19,12 @@
1819
let baseDir = $state()
1920
let unwatchFn
2021
let unwatchPath = ''
22+
let isMobile = $state(false)
23+
24+
onMount(() => {
25+
let platform = os.platform()
26+
isMobile = platform === 'android' || platform === 'ios'
27+
})
2128
2229
const dirOptions = Object.keys(fs.BaseDirectory).filter((key) =>
2330
isNaN(parseInt(key))
@@ -38,7 +45,7 @@
3845
}
3946
4047
function mkdir() {
41-
fs.mkdir(path, { baseDir })
48+
fs.mkdir(path, { baseDir, recursive: true })
4249
.then(() => {
4350
onMessage(`Created dir ${path}`)
4451
})
@@ -73,6 +80,16 @@
7380
.catch(onMessage)
7481
}
7582
83+
function write() {
84+
const encoder = new TextEncoder()
85+
file
86+
.write(encoder.encode('Hello from Tauri :)'))
87+
.then(() => {
88+
onMessage(`wrote to file`)
89+
})
90+
.catch(onMessage)
91+
}
92+
7693
function stat() {
7794
file
7895
.stat()
@@ -180,6 +197,13 @@
180197
</script>
181198

182199
<div class="flex flex-col">
200+
{#if isMobile}
201+
<div>
202+
On mobile, paths outside of App* paths require the use of dialogs
203+
regardless of Tauri's scope mechanism.
204+
</div>
205+
<br />
206+
{/if}
183207
<div class="flex gap-1">
184208
<select class="input" bind:value={baseDir}>
185209
<option value={undefined} selected>None</option>
@@ -207,6 +231,7 @@
207231
</div>
208232
{#if file}
209233
<div>
234+
<button class="btn" onclick={write}>Write</button>
210235
<button class="btn" onclick={truncate}>Truncate</button>
211236
<button class="btn" onclick={stat}>Stat</button>
212237
</div>

0 commit comments

Comments
 (0)