Skip to content

Commit b283683

Browse files
authored
refactor: app dir move logic (#8281)
1 parent ecf116d commit b283683

44 files changed

Lines changed: 1355 additions & 380 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ lettre = { version = "0.11.23", default-features = false, features = [
138138
"tokio1",
139139
"tokio1-rustls",
140140
] }
141+
libc = "0.2.191"
141142
lingua = "=1.8.0"
142143
linkify = "0.11.0"
143144
lz4_flex = { version = "0.14.0", default-features = false, features = [

‎apps/app-frontend/src/components/ui/SplashScreen.vue‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@
6767
</g>
6868
</g>
6969
</svg>
70-
<ProgressBar class="loading-bar" :progress="Math.min(loadingProgress, 100)" />
71-
<span v-if="message">{{ message }}</span>
70+
<ProgressBar class="loading-bar" :progress="progress" />
71+
<span v-if="message" class="text-secondary">{{ message }}</span>
7272
</div>
7373
<div class="gradient-bg" data-tauri-drag-region></div>
7474
<div class="cube-bg"></div>
@@ -78,19 +78,36 @@
7878
</template>
7979

8080
<script setup>
81-
import { injectLoadingState } from '@modrinth/ui'
82-
import { onMounted, ref, watch } from 'vue'
81+
import { defineMessages, injectLoadingState, useVIntl } from '@modrinth/ui'
82+
import { computed, onMounted, ref, watch } from 'vue'
8383
8484
import ProgressBar from '@/components/ui/ProgressBar.vue'
8585
import { useAppEvent } from '@/composables/use-app-event'
8686
import { useTheme } from '@/composables/use-theme.ts'
8787
import { debugStartup } from '@/helpers/startup-debug'
8888
8989
const theme = useTheme()
90+
const { formatMessage } = useVIntl()
91+
92+
const messages = defineMessages({
93+
movingAppDirectory: {
94+
id: 'app.splash.moving-app-directory',
95+
defaultMessage: 'Moving your app directory...',
96+
},
97+
})
9098
9199
const doneLoading = ref(false)
92100
const loadingProgress = ref(0)
93-
const message = ref()
101+
const directoryMoveFraction = ref(null)
102+
103+
const progress = computed(() =>
104+
directoryMoveFraction.value === null
105+
? Math.min(loadingProgress.value, 100)
106+
: directoryMoveFraction.value * 100,
107+
)
108+
const message = computed(() =>
109+
directoryMoveFraction.value === null ? null : formatMessage(messages.movingAppDirectory),
110+
)
94111
95112
const MIN_DISPLAY_MS = 500
96113
const mountedAt = Date.now()
@@ -144,8 +161,7 @@ function fakeLoadingIncrease() {
144161
145162
useAppEvent('loading', (e) => {
146163
if (e.event.type === 'directory_move') {
147-
loadingProgress.value = 100 * (e.fraction ?? 1)
148-
message.value = 'Updating app directory...'
164+
directoryMoveFraction.value = e.fraction ?? null
149165
}
150166
})
151167
</script>

‎apps/app-frontend/src/features/library/shared-instances/shared-instance-modals.vue‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ async function install(
5757
async () => {
5858
if (loadingIds.value.has(sharedInstanceId)) return
5959
setLoading(sharedInstanceId, true)
60+
let jobId: string
6061
try {
6162
const job = await install_shared_instance(
6263
sharedInstanceId,
@@ -67,12 +68,16 @@ async function install(
6768
instance.icon_path ?? null,
6869
instance.icon_path ? (instance.icon_config ?? null) : null,
6970
)
71+
jobId = job.job_id
7072
await queryClient.invalidateQueries({ queryKey: ['instances'] })
7173
options.onInstalled?.()
72-
await wait_for_install_job(appEvents, job.job_id).catch(() => {})
73-
} finally {
74+
} catch (error) {
7475
setLoading(sharedInstanceId, false)
76+
throw error
7577
}
78+
void wait_for_install_job(appEvents, jobId)
79+
.catch(() => {})
80+
.finally(() => setLoading(sharedInstanceId, false))
7681
},
7782
manager
7883
? { id: manager.id, username: manager.username, avatarUrl: manager.avatar_url ?? null }
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
<script setup lang="ts">
2+
import {
3+
ClipboardCopyIcon,
4+
ClockIcon,
5+
FolderSearchIcon,
6+
RefreshCwIcon,
7+
XIcon,
8+
} from '@modrinth/assets'
9+
import {
10+
Admonition,
11+
Button,
12+
type ButtonMenuOption,
13+
commonMessages,
14+
defineMessages,
15+
NewModal,
16+
SplitButton,
17+
useVIntl,
18+
} from '@modrinth/ui'
19+
import { useQuery } from '@tanstack/vue-query'
20+
import { computed, ref } from 'vue'
21+
22+
import { get_all as getRunningProcesses } from '@/helpers/process.js'
23+
import { pickDirectory } from '@/platform/dialogs'
24+
25+
import { isDriveRoot, isNestedPath, normalizePath } from './paths'
26+
import { useCopyPath } from './use-copy-path'
27+
28+
const props = defineProps<{
29+
currentDir: string
30+
caseInsensitive: boolean
31+
}>()
32+
33+
const emit = defineEmits<{
34+
confirm: [directory: string, restartNow: boolean]
35+
}>()
36+
37+
const { formatMessage } = useVIntl()
38+
const copyPath = useCopyPath()
39+
40+
const messages = defineMessages({
41+
header: {
42+
id: 'app.settings.resource-management.app-directory.modal.header',
43+
defaultMessage: 'Change app directory',
44+
},
45+
admonitionHeader: {
46+
id: 'app.settings.resource-management.app-directory.modal.admonition-header',
47+
defaultMessage: 'All your files will be moved',
48+
},
49+
admonitionBody: {
50+
id: 'app.settings.resource-management.app-directory.modal.admonition-body',
51+
defaultMessage:
52+
"You won't be able to use Modrinth App while we move your data. This may take a while.",
53+
},
54+
currentLabel: {
55+
id: 'app.settings.resource-management.app-directory.modal.current',
56+
defaultMessage: 'Current',
57+
},
58+
newLabel: {
59+
id: 'app.settings.resource-management.app-directory.modal.new',
60+
defaultMessage: 'New',
61+
},
62+
choose: {
63+
id: 'app.settings.resource-management.app-directory.modal.choose',
64+
defaultMessage: 'Choose a folder',
65+
},
66+
pickerTitle: {
67+
id: 'app.settings.resource-management.app-directory.modal.picker-title',
68+
defaultMessage: 'Select a new app directory',
69+
},
70+
instancesRunning: {
71+
id: 'app.settings.resource-management.app-directory.modal.instances-running',
72+
defaultMessage: 'Close all running instances before changing the app directory.',
73+
},
74+
nested: {
75+
id: 'app.settings.resource-management.app-directory.modal.nested',
76+
defaultMessage: "The new folder can't be inside your current app directory, or contain it.",
77+
},
78+
driveRoot: {
79+
id: 'app.settings.resource-management.app-directory.modal.drive-root',
80+
defaultMessage:
81+
"Choose a folder rather than the top of a drive, so your files don't mix with everything else on it.",
82+
},
83+
moveAndRestart: {
84+
id: 'app.settings.resource-management.app-directory.modal.move-and-restart',
85+
defaultMessage: 'Move and restart',
86+
},
87+
moveLater: {
88+
id: 'app.settings.resource-management.app-directory.modal.move-later',
89+
defaultMessage: 'Move on next launch',
90+
},
91+
moreOptions: {
92+
id: 'app.settings.resource-management.app-directory.modal.more-options',
93+
defaultMessage: 'More move options',
94+
},
95+
})
96+
97+
const modal = ref<InstanceType<typeof NewModal>>()
98+
const newDir = ref<string | null>(null)
99+
const locked = ref(false)
100+
101+
const processesQuery = useQuery({
102+
queryKey: ['processes', 'list'],
103+
networkMode: 'always',
104+
queryFn: (): Promise<unknown[]> => getRunningProcesses(),
105+
staleTime: 0,
106+
})
107+
const instancesRunning = computed(() => (processesQuery.data.value?.length ?? 0) > 0)
108+
109+
const normalizedNew = computed(() =>
110+
newDir.value === null ? null : normalizePath(newDir.value, props.caseInsensitive),
111+
)
112+
const normalizedCurrent = computed(() => normalizePath(props.currentDir, props.caseInsensitive))
113+
const sameAsCurrent = computed(() => normalizedNew.value === normalizedCurrent.value)
114+
const nested = computed(
115+
() =>
116+
normalizedNew.value !== null &&
117+
!sameAsCurrent.value &&
118+
isNestedPath(normalizedNew.value, normalizedCurrent.value),
119+
)
120+
const driveRoot = computed(() => newDir.value !== null && isDriveRoot(newDir.value))
121+
const canMove = computed(
122+
() => newDir.value !== null && !sameAsCurrent.value && !nested.value && !instancesRunning.value,
123+
)
124+
125+
const moveOptions = computed<ButtonMenuOption[]>(() => [
126+
{
127+
id: 'move-later',
128+
label: formatMessage(messages.moveLater),
129+
icon: ClockIcon,
130+
action: () => confirm(false),
131+
},
132+
])
133+
134+
function show(event?: MouseEvent, directory?: string) {
135+
newDir.value = directory ?? null
136+
locked.value = directory !== undefined
137+
void processesQuery.refetch()
138+
modal.value?.show(event)
139+
}
140+
141+
async function browse() {
142+
const directory = await pickDirectory(formatMessage(messages.pickerTitle))
143+
if (directory) {
144+
newDir.value = directory
145+
}
146+
}
147+
148+
async function confirm(restartNow: boolean) {
149+
await processesQuery.refetch()
150+
if (!canMove.value || newDir.value === null) return
151+
modal.value?.hide()
152+
emit('confirm', newDir.value, restartNow)
153+
}
154+
155+
defineExpose({ show })
156+
</script>
157+
158+
<template>
159+
<NewModal ref="modal" :header="formatMessage(messages.header)" fade="danger" max-width="560px">
160+
<div class="flex flex-col gap-6">
161+
<Admonition
162+
type="critical"
163+
:header="formatMessage(messages.admonitionHeader)"
164+
:body="formatMessage(messages.admonitionBody)"
165+
/>
166+
167+
<div class="flex flex-col gap-2">
168+
<span class="font-semibold text-contrast">{{ formatMessage(messages.currentLabel) }}</span>
169+
<Button size="lg" class="w-full !justify-between text-left" @click="copyPath(currentDir)">
170+
<span class="min-w-0 truncate text-base font-semibold text-primary">
171+
{{ currentDir }}
172+
</span>
173+
<ClipboardCopyIcon class="size-5 shrink-0 text-secondary" aria-hidden="true" />
174+
</Button>
175+
</div>
176+
177+
<div class="flex flex-col gap-2">
178+
<span class="font-semibold text-contrast">{{ formatMessage(messages.newLabel) }}</span>
179+
<Button
180+
size="lg"
181+
class="w-full !justify-between text-left"
182+
:disabled="locked"
183+
@click="browse"
184+
>
185+
<span
186+
class="min-w-0 truncate font-semibold"
187+
:class="newDir ? 'text-primary' : 'text-secondary'"
188+
:title="newDir ?? undefined"
189+
>
190+
{{ newDir ?? formatMessage(messages.choose) }}
191+
</span>
192+
<FolderSearchIcon class="size-5 shrink-0 text-secondary" aria-hidden="true" />
193+
</Button>
194+
<div aria-live="polite" class="flex flex-col gap-1 empty:hidden">
195+
<p v-if="instancesRunning" class="m-0 text-sm text-red">
196+
{{ formatMessage(messages.instancesRunning) }}
197+
</p>
198+
<p v-if="nested" class="m-0 text-sm text-red">
199+
{{ formatMessage(messages.nested) }}
200+
</p>
201+
<p v-else-if="driveRoot" class="m-0 text-sm text-orange">
202+
{{ formatMessage(messages.driveRoot) }}
203+
</p>
204+
</div>
205+
</div>
206+
</div>
207+
208+
<template #actions>
209+
<div class="flex justify-end gap-2">
210+
<Button type="outlined" @click="modal?.hide()">
211+
<XIcon aria-hidden="true" />
212+
{{ formatMessage(commonMessages.cancelButton) }}
213+
</Button>
214+
<SplitButton
215+
type="colored"
216+
color="red"
217+
:menu-label="formatMessage(messages.moreOptions)"
218+
:options="moveOptions"
219+
:disabled="!canMove"
220+
@click="confirm(true)"
221+
>
222+
<RefreshCwIcon aria-hidden="true" />
223+
{{ formatMessage(messages.moveAndRestart) }}
224+
</SplitButton>
225+
</div>
226+
</template>
227+
</NewModal>
228+
</template>

0 commit comments

Comments
 (0)