Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: video player #225

Merged
merged 10 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default defineConfig(
},
rules: {
"unicorn/prefer-math-trunc": "off",
"@eslint-react/no-clone-element": 0,
"no-restricted-globals": [
"error",
{
Expand Down
1 change: 1 addition & 0 deletions icons/mgc/fullscreen_cute_re.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions icons/mgc/fullscreen_exit_cute_re.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions setup-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import { enableMapSet } from "immer"
globalThis.window = {
location: new URL("https://example.com"),
__dbIsReady: true,
}

if (!globalThis.navigator) {
globalThis.navigator = {
onLine: true,
}
}
enableMapSet()
29 changes: 28 additions & 1 deletion src/main/tipc/app.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { getRendererHandlers } from "@egoist/tipc/main"
import { callGlobalContextMethod } from "@shared/bridge"
import type { BrowserWindow } from "electron"
import { clipboard } from "electron"
import { clipboard, dialog } from "electron"

import { downloadFile } from "../lib/download"
import { cleanAuthSessionToken, cleanUser } from "../lib/user"
import type { RendererHandlers } from "../renderer-handlers"
import { quitAndInstall } from "../updater"
Expand Down Expand Up @@ -109,6 +111,31 @@ export const appRoute = {
webContents.stopFindInPage("keepSelection")
},
),

download: t.procedure
.input<string>()
.action(async ({ input, context: { sender } }) => {
const result = await dialog.showSaveDialog({
defaultPath: input.split("/").pop(),
})
if (result.canceled) return

// return result.filePath;
await downloadFile(input, result.filePath).catch((err) => {
const senderWindow = (sender as Sender).getOwnerBrowserWindow()
if (!senderWindow) return
callGlobalContextMethod(senderWindow, "toast.error", [
"Download failed!",
])
throw err
})

const senderWindow = (sender as Sender).getOwnerBrowserWindow()
if (!senderWindow) return
callGlobalContextMethod(senderWindow, "toast.success", [
"Download success!",
])
}),
}
interface Sender extends Electron.WebContents {
getOwnerBrowserWindow: () => Electron.BrowserWindow | null
Expand Down
22 changes: 0 additions & 22 deletions src/main/tipc/menu.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { callGlobalContextMethod } from "@shared/bridge"
import type { MenuItemConstructorOptions, MessageBoxOptions } from "electron"
import { dialog, Menu, ShareMenu } from "electron"

import { downloadFile } from "../lib/download"
import { getMainWindow } from "../window"
import { t } from "./_instance"

type MenuItem = ActionMenuItem | { type: "separator" }
Expand Down Expand Up @@ -87,23 +84,4 @@ export const menuRoute = {
},
})
}),

saveImage: t.procedure.input<string>().action(async ({ input }) => {
const result = await dialog.showSaveDialog({
defaultPath: input.split("/").pop(),
})
if (result.canceled) return

// return result.filePath;
await downloadFile(input, result.filePath).catch((err) => {
const mainWindow = getMainWindow()
if (!mainWindow) return
callGlobalContextMethod(mainWindow, "toast.error", ["Download failed!"])
throw err
})

const mainWindow = getMainWindow()
if (!mainWindow) return
callGlobalContextMethod(mainWindow, "toast.success", ["Download success!"])
}),
}
70 changes: 57 additions & 13 deletions src/renderer/src/components/ui/media.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { tipcClient } from "@renderer/lib/client"
import { nextFrame } from "@renderer/lib/dom"
import { getProxyUrl } from "@renderer/lib/img-proxy"
import { showNativeMenu } from "@renderer/lib/native-menu"
import { cn } from "@renderer/lib/utils"
import { saveImageDimensionsToDb } from "@renderer/store/image/db"
import { useForceUpdate } from "framer-motion"
import type { FC, ImgHTMLAttributes, VideoHTMLAttributes } from "react"
import { memo, useMemo, useState } from "react"
import { toast } from "sonner"
import { useEventCallback } from "usehooks-ts"

import { usePreviewMedia } from "./media/hooks"
import type { VideoPlayerRef } from "./media/VideoPlayer"
import { VideoPlayer } from "./media/VideoPlayer"

const failedList = new Set<string | undefined>()

Expand Down Expand Up @@ -164,18 +168,7 @@ const MediaImpl: FC<MediaProps> = ({
)}
onClick={handleClick}
>
{previewImageUrl ? (
<img src={previewImageUrl} className="size-full object-cover" />
) : (
<video
src={src}
muted
className="relative size-full object-cover"
/>
)}
<div className="absolute left-1/2 top-1/2 flex -translate-x-1/2 -translate-y-1/2 rounded-full bg-black/50 p-2 text-3xl text-white/80">
<i className="i-mgc-play-cute-fi" />
</div>
<VideoPreview src={src!} previewImageUrl={previewImageUrl} />
</div>
)
}
Expand All @@ -199,7 +192,9 @@ const MediaImpl: FC<MediaProps> = ({
type,
])

if (hidden && showFallback) { return <FallbackMedia {...props} /> }
if (hidden && showFallback) {
return <FallbackMedia {...props} />
}
return (
<div className={cn("overflow-hidden rounded", className)} style={style}>
{InnerContent}
Expand Down Expand Up @@ -245,3 +240,52 @@ const FallbackMedia: FC<MediaProps> = ({
</p>
</div>
)

const VideoPreview: FC<{
src: string
previewImageUrl?: string
}> = ({ src, previewImageUrl }) => {
const [isInitVideoPlayer, setIsInitVideoPlayer] = useState(!previewImageUrl)

const [videoRef, setVideoRef] = useState<VideoPlayerRef | null>(null)
const isPaused = videoRef?.getState().paused
const [forceUpdate] = useForceUpdate()
return (
<div
onMouseEnter={() => {
videoRef?.controls.play()?.then(forceUpdate)
}}
onMouseLeave={() => {
videoRef?.controls.pause()
nextFrame(forceUpdate)
}}
>
{isInitVideoPlayer ? (
<img
src={previewImageUrl}
className="size-full object-cover"
onMouseEnter={() => {
setIsInitVideoPlayer(true)
}}
/>
) : (
<VideoPlayer
controls={false}
src={src}
ref={setVideoRef}
muted
className="relative size-full object-cover"
/>
)}

<div
className={cn(
"absolute left-1/2 top-1/2 flex -translate-x-1/2 -translate-y-1/2 rounded-full bg-black/50 p-2 text-3xl text-white/80 duration-200",
isPaused ? "opacity-100" : "opacity-0",
)}
>
<i className="i-mgc-play-cute-fi" />
</div>
</div>
)
}
Loading
Loading