Exemplo n.º 1
0
        static async Task InvokeAfterCaptureAction(ScreenRecording recording, HSSettings settingsContext)
        {
            switch (settingsContext.ActionAfterVideoCapture)
            {
            case VideoCaptureHandlingAction.Upload:
                try
                {
                    var payload = new VideoUploadPayload(recording);

                    var result = await UploadDispatcher.InitiateUploadToDefaultUploader(payload, settingsContext, HolzShotsApplication.Instance.Uploaders, null).ConfigureAwait(true);

                    UploadHelper.InvokeUploadFinishedUI(result, settingsContext);
                }
                catch (UploadCanceledException)
                {
                    NotificationManager.ShowOperationCanceled();
                }
                catch (UploadException ex)
                {
                    await NotificationManager.UploadFailed(ex);
                }
                return;

            case VideoCaptureHandlingAction.CopyFile:
            {
                var success = ClipboardEx.SetFiles(recording.FilePath);
                if (settingsContext.ShowCopyConfirmation)
                {
                    if (success)
                    {
                        NotificationManager.ShowFileCopyConfirmation();
                    }
                    else
                    {
                        NotificationManager.CopyingFileFailed();
                    }
                }
            }
            break;

            case VideoCaptureHandlingAction.CopyFilePath:
            {
                var success = ClipboardEx.SetText(recording.FilePath);
                if (settingsContext.ShowCopyConfirmation)
                {
                    if (success)
                    {
                        NotificationManager.ShowFilePathCopyConfirmation();
                    }
                    else
                    {
                        NotificationManager.CopyingFilePathFailed();
                    }
                }
            }
            break;

            case VideoCaptureHandlingAction.ShowInExplorer:
                IO.HolzShotsPaths.OpenSelectedFileInExplorer(recording.FilePath);
                return;

            case VideoCaptureHandlingAction.OpenInDefaultApp:
                Process.Start(recording.FilePath);
                return;

            case VideoCaptureHandlingAction.None: return;

            default: throw new ArgumentException("Unhandled VideoCaptureHandlingAction: " + settingsContext.ActionAfterVideoCapture);
            }
        }
Exemplo n.º 2
0
        private static async Task <ScreenRecording?> PerformScreenRecording(HSSettings settingsContext)
        {
            _currentRecordingCts = new CancellationTokenSource();
            _throwAwayResult     = false;

            try
            {
                var ffmpegPath = EnsureAvailableFFmpegAndPotentiallyStartSetup();
                if (ffmpegPath == null)
                {
                    return(null); // We don't have ffmpeg available and the user didn't do anything to fix this. We act like it was aborted.
                }
                var effectiveFormat = settingsContext.VideoOutputFormat;
                if (effectiveFormat == VideoCaptureFormat.AskBeforeRecording)
                {
                    var formatToUse = VideoCaptureFormatSelection.PromptFormat();
                    if (formatToUse == null)
                    {
                        return(null); //Use clicked "cancel"
                    }
                    effectiveFormat = formatToUse.Value;

                    // TODO: It would be better to rewrite the settings context here, so other code can just work with that.
                    // For that to work, we should refactor HSSettings to be a record
                }

                var(selectionBackground, _) = Drawing.ScreenshotCreator.CaptureScreenshot(SystemInformation.VirtualScreen, settingsContext.CaptureCursor);
                using (selectionBackground)
                {
                    using var selector = Selection.AreaSelector.Create(selectionBackground, false, settingsContext);

                    var(selectedArea, windowInfo) = await selector.PromptSelectionAsync();

                    var recorder = ScreenRecorderSelector.CreateScreenRecorderForCurrentPlatform(ffmpegPath);

                    var tempRecordingDir = Path.Combine(Path.GetTempPath(), "hs-" + Path.GetRandomFileName());
                    Directory.CreateDirectory(tempRecordingDir);

                    var extension  = VideoUploadPayload.GetExtensionForVideoFormat(effectiveFormat);
                    var targetFile = Path.Combine(tempRecordingDir, "HS" + extension);

                    if (windowInfo != null)
                    {
                        // See GH#78
                        // The number 500 is just a guess. If it fails, it isn't such a problem and won't cause any harm
                        // As soon as we've got our own recording frame, we can be more precise on when to invoke the SetForegroundWindow
                        _ = Task.Delay(500).ContinueWith(t =>
                        {
                            Debug.WriteLine($"Set FG window to {windowInfo.Title ?? "<no title>"}");
                            Native.User32.SetForegroundWindow(windowInfo.Handle);
                        });
                    }

                    using var recordingControls = new RecordingControls(selectedArea, _currentRecordingCts);
                    recordingControls.Show();

                    var recording = await recorder.Invoke(selectedArea, targetFile, effectiveFormat, settingsContext, _currentRecordingCts.Token);

                    if (_throwAwayResult)
                    {
                        // The user cancelled the video recording
                        try
                        {
                            if (File.Exists(targetFile))
                            {
                                File.Delete(targetFile);
                            }
                        }
                        catch
                        {
                            // Not _that_ important to handle this because it's in a temp dir and willb e gone on reboot anyway
                        }
                        return(null);
                    }
                    return(recording);
                }
            }
            finally
            {
                _currentRecordingCts = null;
                _throwAwayResult     = false;
            }
        }