Skip to content

Commit

Permalink
Refresh window before taking a screenshot
Browse files Browse the repository at this point in the history
  • Loading branch information
Blake-Madden committed Oct 8, 2023
1 parent 0e82f95 commit 388e7df
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/util/screenshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,8 @@ bool Screenshot::SaveScreenshotOfTextWindow(const wxString& filePath,
{ return false; }
}

PrepareWindowForScreenshot(windowToCapture);

if (highlightPoints.size())
{ dynamic_cast<wxTextCtrl*>(windowToCapture)->ShowPosition(highlightPoints[0].first); }

Expand Down Expand Up @@ -405,6 +407,8 @@ bool Screenshot::SaveScreenshotOfPropertyGrid(const wxString& filePath,
if (windowToCapture == nullptr)
{ return false; }

PrepareWindowForScreenshot(windowToCapture);

wxClientDC dc(windowToCapture);
wxMemoryDC memDC;

Expand Down Expand Up @@ -483,6 +487,8 @@ bool Screenshot::SaveScreenshot(const wxString& filePath,
if (windowToCapture == nullptr)
{ return false; }

PrepareWindowForScreenshot(windowToCapture);

wxClientDC dc(windowToCapture);
wxMemoryDC memDC;

Expand Down Expand Up @@ -566,6 +572,15 @@ bool Screenshot::SaveScreenshot(const wxString& filePath,
return bitmap.SaveFile(fn.GetFullPath(), wxBITMAP_TYPE_BMP);
}

//---------------------------------------------------
void Screenshot::PrepareWindowForScreenshot(wxWindow* windowToCapture)
{
windowToCapture->Refresh(true);
windowToCapture->UpdateWindowUI(wxUPDATE_UI_RECURSE);
wxTheApp->Yield();
::wxSleep(1);
}

//---------------------------------------------------
wxWindow* Screenshot::GetActiveDialogOrFrame()
{
Expand Down
7 changes: 5 additions & 2 deletions src/util/screenshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class Screenshot
@returns @c true if the image was successfully converted and saved.*/
static bool ConvertImageToPng(const wxString& filePath, const wxSize scaledSize,
const bool removeOriginalFile = false);
private:

/// @returns The active dialog or frame.
/// @note @c wxGetActiveWindow() always returns null on macOS, so this
/// uses @c wxWindow::FindFocus() and moves up to the parent dialog or frame.
Expand All @@ -114,7 +114,10 @@ class Screenshot
/// to get the app's main window.
[[nodiscard]]
static wxWindow* GetActiveDialogOrFrame();

private:
/// @brief Ensures that everything is repainted and ready for screenshot.
/// @param windowToCapture The window to prepare for the screenshot.
static void PrepareWindowForScreenshot(wxWindow* windowToCapture);
static void AddBorderToImage(wxBitmap& bmp);

static wxPen GetScreenshotHighlightPen(const int width)
Expand Down

0 comments on commit 388e7df

Please sign in to comment.