Skip to content

Commit

Permalink
Move preview screenshot logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementPasteau committed Nov 14, 2024
1 parent 0919d71 commit ae3be4d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 56 deletions.
17 changes: 1 addition & 16 deletions newIDE/app/src/MainFrame/Toolbar/PreviewAndShareButtons.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import FlatButtonWithSplitMenu from '../../UI/FlatButtonWithSplitMenu';
import { useResponsiveWindowSize } from '../../UI/Responsive/ResponsiveWindowMeasurer';
import ResponsiveRaisedButton from '../../UI/ResponsiveRaisedButton';
import PreferencesContext from '../../MainFrame/Preferences/PreferencesContext';
import { TIME_BETWEEN_PREVIEW_SCREENSHOTS } from '../UseCapturesManager';

export type PreviewAndShareButtonsProps = {|
onPreviewWithoutHotReload: (?{ numberOfWindows: number }) => Promise<void>,
Expand All @@ -21,8 +20,6 @@ export type PreviewAndShareButtonsProps = {|
onNetworkPreview: () => Promise<void>,
onHotReloadPreview: () => Promise<void>,
onLaunchPreviewWithDiagnosticReport: () => Promise<void>,
onLaunchPreviewWithScreenshot: () => Promise<void>,
lastPreviewScreenshotTakenAt: ?number,
setPreviewOverride: ({|
isPreviewOverriden: boolean,
overridenPreviewLayoutName: ?string,
Expand All @@ -43,8 +40,6 @@ const PreviewAndShareButtons = React.memo<PreviewAndShareButtonsProps>(
onOpenDebugger,
onHotReloadPreview,
onLaunchPreviewWithDiagnosticReport,
onLaunchPreviewWithScreenshot,
lastPreviewScreenshotTakenAt,
canDoNetworkPreview,
isPreviewEnabled,
hasPreviewsRunning,
Expand Down Expand Up @@ -186,21 +181,11 @@ const PreviewAndShareButtons = React.memo<PreviewAndShareButtonsProps>(
[openShareDialog]
);

// Do not use a memo, as Date.now() is always changing.
const shouldTakeScreenshotOnPreview =
preferences.values.takeScreenshotOnPreview &&
Date.now() >
(lastPreviewScreenshotTakenAt || 0) + TIME_BETWEEN_PREVIEW_SCREENSHOTS;

return (
<LineStackLayout noMargin>
<FlatButtonWithSplitMenu
primary
onClick={
shouldTakeScreenshotOnPreview
? onLaunchPreviewWithScreenshot
: onHotReloadPreview
}
onClick={onHotReloadPreview}
disabled={!isPreviewEnabled}
icon={hasPreviewsRunning ? <UpdateIcon /> : <PreviewIcon />}
label={
Expand Down
6 changes: 0 additions & 6 deletions newIDE/app/src/MainFrame/Toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,6 @@ export default React.forwardRef<MainFrameToolbarProps, ToolbarInterface>(
onLaunchPreviewWithDiagnosticReport={
props.onLaunchPreviewWithDiagnosticReport
}
onLaunchPreviewWithScreenshot={
props.onLaunchPreviewWithScreenshot
}
lastPreviewScreenshotTakenAt={
props.lastPreviewScreenshotTakenAt
}
setPreviewOverride={props.setPreviewOverride}
canDoNetworkPreview={props.canDoNetworkPreview}
isPreviewEnabled={props.isPreviewEnabled}
Expand Down
19 changes: 14 additions & 5 deletions newIDE/app/src/MainFrame/UseCapturesManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '../Utils/GDevelopServices/Game';
import { type GamesList } from '../GameDashboard/UseGamesList';
import AuthenticatedUserContext from '../Profile/AuthenticatedUserContext';
import PreferencesContext from './Preferences/PreferencesContext';

export const TIME_BETWEEN_PREVIEW_SCREENSHOTS = 1000 * 60 * 3; // 3 minutes

Expand All @@ -37,6 +38,7 @@ const useCapturesManager = ({
const { getAuthorizationHeader, profile } = React.useContext(
AuthenticatedUserContext
);
const preferences = React.useContext(PreferencesContext);

const createCaptureOptionsForPreview = React.useCallback(
async (
Expand Down Expand Up @@ -195,19 +197,26 @@ const useCapturesManager = ({
[project]
);

const getGameLastPreviewScreenshotTakenAt = React.useCallback(
(gameId: string): number => {
return lastPreviewScreenshotsTakenAt[gameId] || 0;
const getHotReloadPreviewLaunchCaptureOptions = React.useCallback(
(gameId: string): LaunchCaptureOptions | void => {
const shouldTakeScreenshotOnPreview =
preferences.values.takeScreenshotOnPreview &&
Date.now() >
(lastPreviewScreenshotsTakenAt[gameId] || 0) +
TIME_BETWEEN_PREVIEW_SCREENSHOTS;
return shouldTakeScreenshotOnPreview
? { screenshots: [{ delayTimeInSeconds: 3000 }] }
: undefined;
},
[lastPreviewScreenshotsTakenAt]
[preferences.values.takeScreenshotOnPreview, lastPreviewScreenshotsTakenAt]
);

return {
createCaptureOptionsForPreview,
onCaptureFinished,
getGameUnverifiedScreenshotUrls,
onGameScreenshotsClaimed,
getGameLastPreviewScreenshotTakenAt,
getHotReloadPreviewLaunchCaptureOptions,
};
};

Expand Down
39 changes: 12 additions & 27 deletions newIDE/app/src/MainFrame/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ const MainFrame = (props: Props) => {
onCaptureFinished,
onGameScreenshotsClaimed,
getGameUnverifiedScreenshotUrls,
getGameLastPreviewScreenshotTakenAt,
getHotReloadPreviewLaunchCaptureOptions,
} = useCapturesManager({ project: currentProject, gamesList });

/**
Expand Down Expand Up @@ -1729,27 +1729,20 @@ const MainFrame = (props: Props) => {
[launchPreview]
);

const launchPreviewWithScreenshot = React.useCallback(
() =>
launchPreview({
networkPreview: false,
hotReload: false,
launchCaptureOptions: {
screenshots: [
{ delayTimeInSeconds: 3000 }, // Take only 1 screenshot per preview.
],
},
}),
[launchPreview]
);

const launchHotReloadPreview = React.useCallback(
() =>
launchPreview({
async () => {
const launchCaptureOptions = currentProject
? getHotReloadPreviewLaunchCaptureOptions(
currentProject.getProjectUuid()
)
: undefined;
await launchPreview({
networkPreview: false,
hotReload: true,
}),
[launchPreview]
launchCaptureOptions,
});
},
[currentProject, launchPreview, getHotReloadPreviewLaunchCaptureOptions]
);

const launchNetworkPreview = React.useCallback(
Expand Down Expand Up @@ -3598,14 +3591,6 @@ const MainFrame = (props: Props) => {
onNetworkPreview={launchNetworkPreview}
onHotReloadPreview={launchHotReloadPreview}
onLaunchPreviewWithDiagnosticReport={launchPreviewWithDiagnosticReport}
onLaunchPreviewWithScreenshot={launchPreviewWithScreenshot}
lastPreviewScreenshotTakenAt={
currentProject
? getGameLastPreviewScreenshotTakenAt(
currentProject.getProjectUuid()
)
: null
}
canDoNetworkPreview={
!!_previewLauncher.current &&
_previewLauncher.current.canDoNetworkPreview()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ const defaultProps: MainFrameToolbarProps = {
onNetworkPreview: async () => {},
onHotReloadPreview: async () => {},
onLaunchPreviewWithDiagnosticReport: async () => {},
onLaunchPreviewWithScreenshot: async () => {},
lastPreviewScreenshotTakenAt: null,
setPreviewOverride: () => {},
canDoNetworkPreview: true,
isPreviewEnabled: false,
Expand Down

0 comments on commit ae3be4d

Please sign in to comment.