|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { SerializableDesktopCapturerSource } from '@proj-airi/electron-screen-capture' |
| 3 | +
|
| 4 | +import { useElectronScreenCapture } from '@proj-airi/electron-screen-capture/vue' |
| 5 | +import { Button } from '@proj-airi/ui' |
| 6 | +import { onBeforeUnmount, onMounted, ref } from 'vue' |
| 7 | +
|
| 8 | +const sources = ref<ScreenCaptureSource[]>([]) |
| 9 | +const isRefetching = ref(false) |
| 10 | +const activeStreams = ref<MediaStream[]>([]) |
| 11 | +
|
| 12 | +interface ScreenCaptureSource extends SerializableDesktopCapturerSource { |
| 13 | + appIconURL?: string |
| 14 | + thumbnailURL?: string |
| 15 | +} |
| 16 | +
|
| 17 | +const { getSources, selectWithSource } = useElectronScreenCapture(window.electron.ipcRenderer, { |
| 18 | + types: ['screen', 'window'], |
| 19 | + fetchWindowIcons: true, |
| 20 | +}) |
| 21 | +
|
| 22 | +function toObjectUrl(bytes: Uint8Array, mime: string) { |
| 23 | + return URL.createObjectURL(new Blob([bytes.slice().buffer], { type: mime })) |
| 24 | +} |
| 25 | +
|
| 26 | +async function startCapture(source: SerializableDesktopCapturerSource) { |
| 27 | + try { |
| 28 | + await selectWithSource(() => source.id, async () => { |
| 29 | + const stream = await navigator.mediaDevices.getDisplayMedia({ |
| 30 | + video: true, |
| 31 | + audio: true, |
| 32 | + }) |
| 33 | + activeStreams.value.push(stream) |
| 34 | + }) |
| 35 | + } |
| 36 | + catch (err) { |
| 37 | + console.error('Error selecting source:', err) |
| 38 | + } |
| 39 | +} |
| 40 | +
|
| 41 | +function stopStream(stream: MediaStream) { |
| 42 | + stream.getTracks().forEach(track => track.stop()) |
| 43 | + const index = activeStreams.value.indexOf(stream) |
| 44 | + if (index !== -1) { |
| 45 | + activeStreams.value.splice(index, 1) |
| 46 | + } |
| 47 | +} |
| 48 | +
|
| 49 | +async function refetchSources() { |
| 50 | + try { |
| 51 | + isRefetching.value = true |
| 52 | +
|
| 53 | + const nextSources = (await getSources()) |
| 54 | + .sort((a, b) => { |
| 55 | + if (a.id.startsWith('screen:') && b.id.startsWith('window:')) |
| 56 | + return -1 |
| 57 | + if (a.id.startsWith('window:') && b.id.startsWith('screen:')) |
| 58 | + return 1 |
| 59 | + return a.name.localeCompare(b.name) |
| 60 | + }) |
| 61 | +
|
| 62 | + sources.value.forEach((oldSource) => { |
| 63 | + if (oldSource.appIconURL) |
| 64 | + URL.revokeObjectURL(oldSource.appIconURL) |
| 65 | + if (oldSource.thumbnailURL) |
| 66 | + URL.revokeObjectURL(oldSource.thumbnailURL) |
| 67 | + }) |
| 68 | +
|
| 69 | + sources.value = nextSources.map(source => ({ |
| 70 | + ...source, |
| 71 | + appIconURL: source.appIcon && source.appIcon.length > 0 ? toObjectUrl(source.appIcon, 'image/png') : undefined, |
| 72 | + thumbnailURL: source.thumbnail && source.thumbnail.length > 0 ? toObjectUrl(source.thumbnail, 'image/jpeg') : undefined, |
| 73 | + })) |
| 74 | + } |
| 75 | + finally { |
| 76 | + isRefetching.value = false |
| 77 | + } |
| 78 | +} |
| 79 | +
|
| 80 | +onMounted(async () => { |
| 81 | + refetchSources() |
| 82 | +}) |
| 83 | +
|
| 84 | +onBeforeUnmount(() => { |
| 85 | + sources.value.forEach((source) => { |
| 86 | + if (source.appIconURL) |
| 87 | + URL.revokeObjectURL(source.appIconURL) |
| 88 | + if (source.thumbnailURL) |
| 89 | + URL.revokeObjectURL(source.thumbnailURL) |
| 90 | + }) |
| 91 | +}) |
| 92 | +</script> |
| 93 | + |
| 94 | +<template> |
| 95 | + <div |
| 96 | + flex="~ col gap-4 items-start" w-full |
| 97 | + text="neutral-500 dark:neutral-400" |
| 98 | + > |
| 99 | + <div |
| 100 | + v-if="activeStreams.length > 0" |
| 101 | + bg="primary-300/10" |
| 102 | + b="2 solid primary-400/70" |
| 103 | + w-full overflow-hidden rounded-2xl p-3 |
| 104 | + flex="~ col gap-2" |
| 105 | + > |
| 106 | + <div flex="~ row items-center gap-2"> |
| 107 | + <div class="i-solar:videocamera-record-line-duotone" /> |
| 108 | + <div>Capturing</div> |
| 109 | + </div> |
| 110 | + <div |
| 111 | + flex="~ row items-center gap-3" |
| 112 | + w-full overflow-x-auto |
| 113 | + > |
| 114 | + <div |
| 115 | + v-for="stream in activeStreams" :key="stream.id" |
| 116 | + relative overflow-hidden rounded-lg |
| 117 | + > |
| 118 | + <div |
| 119 | + flex="~ col items-center justify-center gap-1" |
| 120 | + absolute right-0 top-0 z-10 h-full w-full cursor-pointer |
| 121 | + rounded-lg op-0 backdrop-blur-sm hover:op-100 |
| 122 | + transition="all duration-200" |
| 123 | + text="light" |
| 124 | + bg="black/30" |
| 125 | + @click="stopStream(stream)" |
| 126 | + > |
| 127 | + <div class="i-solar:stop-line-duotone" /> |
| 128 | + <div text-sm> |
| 129 | + Stop |
| 130 | + </div> |
| 131 | + </div> |
| 132 | + <video |
| 133 | + autoplay |
| 134 | + muted |
| 135 | + playsinline |
| 136 | + :srcObject="stream" |
| 137 | + h-140px |
| 138 | + w-auto |
| 139 | + /> |
| 140 | + </div> |
| 141 | + </div> |
| 142 | + </div> |
| 143 | + |
| 144 | + <div |
| 145 | + flex="~ col gap-3" |
| 146 | + w-full pb-6 |
| 147 | + > |
| 148 | + <div flex="~ row items-center justify-between" w-full> |
| 149 | + <div>{{ sources.length }} source(s)</div> |
| 150 | + <Button |
| 151 | + :label="isRefetching ? 'Refetching...' : 'Refetch'" |
| 152 | + icon="i-solar:refresh-line-duotone" |
| 153 | + size="sm" |
| 154 | + :disabled="isRefetching" |
| 155 | + @click="refetchSources()" |
| 156 | + /> |
| 157 | + </div> |
| 158 | + <div grid="~ cols-1 sm:cols-2 md:cols-3 lg:cols-4 xl:cols-5 gap-3"> |
| 159 | + <div |
| 160 | + v-for="source in sources" |
| 161 | + :key="source.id" |
| 162 | + flex="~ col justify-between gap-3" |
| 163 | + w-full cursor-pointer rounded-2xl p-3 |
| 164 | + transition="all duration-200" |
| 165 | + border="2 solid neutral-200/60 dark:neutral-800/10 hover:primary-400/70" |
| 166 | + @click="startCapture(source)" |
| 167 | + > |
| 168 | + <div flex="~ col items-start w-full"> |
| 169 | + <div flex="~ row items-center gap-1"> |
| 170 | + <div class="h-16px w-16px"> |
| 171 | + <img |
| 172 | + v-if="source.appIconURL" |
| 173 | + :src="source.appIconURL" |
| 174 | + :alt="source.id.startsWith('screen:') ? 'Screen Icon' : 'Window Icon'" |
| 175 | + class="h-full w-full shrink-0" |
| 176 | + > |
| 177 | + <div |
| 178 | + v-else-if="source.id.startsWith('screen:')" |
| 179 | + h-full w-full |
| 180 | + class="i-solar:screencast-2-line-duotone" |
| 181 | + /> |
| 182 | + <div |
| 183 | + v-else |
| 184 | + h-full w-full |
| 185 | + class="i-solar:window-frame-line-duotone" |
| 186 | + /> |
| 187 | + </div> |
| 188 | + |
| 189 | + <div text-sm> |
| 190 | + {{ source.id.startsWith('screen:') ? 'Screen' : 'Window' }} |
| 191 | + </div> |
| 192 | + </div> |
| 193 | + |
| 194 | + <div text-ellipsis break-all> |
| 195 | + {{ source.name }} |
| 196 | + </div> |
| 197 | + <div text-sm font-mono text="neutral-400 dark:neutral-600"> |
| 198 | + {{ source.id }} |
| 199 | + </div> |
| 200 | + </div> |
| 201 | + <div |
| 202 | + h-200px w-full overflow-hidden rounded-2xl bg-black |
| 203 | + flex="~ items-center justify-center shrink-0" |
| 204 | + > |
| 205 | + <img |
| 206 | + v-if="source.thumbnailURL" |
| 207 | + :src="source.thumbnailURL" |
| 208 | + alt="Thumbnail" |
| 209 | + h-full |
| 210 | + w-full object-contain |
| 211 | + > |
| 212 | + <div |
| 213 | + v-else |
| 214 | + class="i-solar:forbidden-circle-line-duotone" |
| 215 | + h-10 w-10 bg-light |
| 216 | + /> |
| 217 | + </div> |
| 218 | + </div> |
| 219 | + </div> |
| 220 | + </div> |
| 221 | + </div> |
| 222 | +</template> |
| 223 | + |
| 224 | +<route lang="yaml"> |
| 225 | +meta: |
| 226 | + layout: settings |
| 227 | +</route> |
0 commit comments