Skip to content

Commit

Permalink
refactor: add useViewTransition
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanSalt committed Dec 4, 2024
1 parent c2cbf11 commit 0fb5958
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
13 changes: 6 additions & 7 deletions addons/launcher/src/renderer/LauncherList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ const launchers = $(useLaunchers())
let isCollapsed = $ref(false)
const isLauncherCollapsed = $(commas.ui.useViewTransition($$(isCollapsed)))
const filteredLaunchers = $computed(() => {
if (!isCollapsed) return launchers
if (!isLauncherCollapsed) return launchers
return launchers.filter(launcher => {
const launcherTabs = getTerminalTabsByLauncher(launcher)
return launcherTabs.length > 0
Expand Down Expand Up @@ -72,10 +74,7 @@ const launcherItems = $computed(() => {
})
function toggleCollapsing() {
const transition = document.startViewTransition(() => {
isCollapsed = !isCollapsed
})
return transition.updateCallbackDone
isCollapsed = !isCollapsed
}
function createLauncher(data: LauncherInfo, index: number) {
Expand Down Expand Up @@ -263,11 +262,11 @@ function openLauncherMenu(launcher: Launcher, tab: TerminalTab | undefined, even
>
<div
:ref="mount"
:class="['launcher-folder', { collapsed: isCollapsed }]"
:class="['launcher-folder', { collapsed: isLauncherCollapsed }]"
@click="toggleCollapsing"
>
<div class="group-name">
<VisualIcon :name="isCollapsed ? 'lucide-list-filter' : 'lucide-list-video'" />
<VisualIcon :name="isLauncherCollapsed ? 'lucide-list-filter' : 'lucide-list-video'" />
</div>
<DropIndicator
v-if="draggingEdges.get(FOLDER_ID)"
Expand Down
2 changes: 1 addition & 1 deletion addons/launcher/src/renderer/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const settings = commas.remote.useSettings()
let launchers = $(commas.ipcRenderer.inject('launchers', []))

export function useLaunchers() {
return $$(launchers)
return commas.ui.useViewTransition($$(launchers))
}

const launcherCharacters = $computed(() => {
Expand Down
2 changes: 2 additions & 0 deletions src/api/modules/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import VisualIcon from '../../renderer/components/basic/VisualIcon.vue'
import { openFolder, openItem, openLink } from '../../renderer/compositions/terminal'
import { RendererWebContentsView } from '../../renderer/compositions/web-contents'
import { createContextMenu, openContextMenu } from '../../renderer/utils/frame'
import { useViewTransition } from '../../renderer/utils/helper'
import { vI18n } from '../../renderer/utils/i18n'
import type { RendererAPIContext } from '../types'

Expand Down Expand Up @@ -62,4 +63,5 @@ export {
openLink,
openFolder,
openItem,
useViewTransition,
}
12 changes: 12 additions & 0 deletions src/renderer/utils/helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { memoize } from 'lodash'
import type { Ref } from 'vue'
import { toValue, watch } from 'vue'

interface MousePressingOptions {
element?: Window | HTMLElement,
Expand Down Expand Up @@ -42,3 +44,13 @@ export function escapeHTML(text: string) {
wrapper.textContent = text
return wrapper.innerHTML
}

export function useViewTransition<T>(refOrGetter: Ref<T> | (() => T)) {
let value = $shallowRef(toValue(refOrGetter))
watch(refOrGetter, newValue => {
document.startViewTransition(() => {
value = newValue as typeof value
})
})
return $$(value) as Ref<T> & { readonly value: T }
}

0 comments on commit 0fb5958

Please sign in to comment.