Skip to content

Commit

Permalink
fix mouse cursor flickering in case of "hide mouse while typing"
Browse files Browse the repository at this point in the history
  • Loading branch information
RaiKoHoff committed May 20, 2024
1 parent f2c726d commit 963c5f2
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 136 deletions.
42 changes: 35 additions & 7 deletions scintilla/win32/ScintillaWin.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,9 @@ class ScintillaWin :
bool capturedMouse;
bool trackedMouseLeave;
BOOL typingWithoutCursor;
bool cursorIsHidden;
// >>>>>>>>>>>>>>> BEG NON STD SCI PATCH >>>>>>>>>>>>>>>
//bool cursorIsHidden;
// <<<<<<<<<<<<<<< END NON STD SCI PATCH <<<<<<<<<<<<<<<
SetCoalescableTimerSig SetCoalescableTimerFn;

unsigned int linesPerScroll; ///< Intellimouse support
Expand Down Expand Up @@ -494,6 +496,9 @@ class ScintillaWin :
void ClaimSelection() override;

void GetMouseParameters() noexcept;
// >>>>>>>>>>>>>>> BEG NON STD SCI PATCH >>>>>>>>>>>>>>>
bool IsMouseCursorHidden() noexcept;
// <<<<<<<<<<<<<<< END NON STD SCI PATCH <<<<<<<<<<<<<<<
void CopyToGlobal(GlobalMemory &gmUnicode, const SelectionText &selectedText);
void CopyToClipboard(const SelectionText &selectedText) override;
void ScrollMessage(WPARAM wParam);
Expand Down Expand Up @@ -579,7 +584,9 @@ ScintillaWin::ScintillaWin(HWND hwnd) {
capturedMouse = false;
trackedMouseLeave = false;
typingWithoutCursor = false;
cursorIsHidden = false;
// >>>>>>>>>>>>>>> BEG NON STD SCI PATCH >>>>>>>>>>>>>>>
//cursorIsHidden = false;
// <<<<<<<<<<<<<<< END NON STD SCI PATCH <<<<<<<<<<<<<<<
SetCoalescableTimerFn = nullptr;

linesPerScroll = 0;
Expand Down Expand Up @@ -1612,19 +1619,27 @@ sptr_t ScintillaWin::MouseMessage(unsigned int iMessage, uptr_t wParam, sptr_t l
break;

case WM_MOUSEMOVE: {
cursorIsHidden = false; // to be shown by ButtonMoveWithModifiers
// >>>>>>>>>>>>>>> BEG NON STD SCI PATCH >>>>>>>>>>>>>>>
const Point pt = PointFromLParam(lParam);

// Windows might send WM_MOUSEMOVE even though the mouse has not been moved:
// http:https://blogs.msdn.com/b/oldnewthing/archive/2003/10/01/55108.aspx
if (ptMouseLast != pt) {
if (IsMouseCursorHidden()) {
::ShowCursor(TRUE);
//cursorIsHidden = false; // to be shown by ButtonMoveWithModifiers
}
SetTrackMouseLeaveEvent(true);
ButtonMoveWithModifiers(pt, ::GetMessageTime(), MouseModifiers(wParam));
}
// <<<<<<<<<<<<<<< END NON STD SCI PATCH <<<<<<<<<<<<<<<
}
break;

case WM_MOUSELEAVE:
// >>>>>>>>>>>>>>> BEG NON STD SCI PATCH >>>>>>>>>>>>>>>
if (IsMouseCursorHidden()) { ::ShowCursor(TRUE); }
// <<<<<<<<<<<<<<< END NON STD SCI PATCH <<<<<<<<<<<<<<<
SetTrackMouseLeaveEvent(false);
MouseLeave();
return ::DefWindowProc(MainHWND(), iMessage, wParam, lParam);
Expand Down Expand Up @@ -2123,7 +2138,10 @@ sptr_t ScintillaWin::WndProc(Message iMessage, uptr_t wParam, sptr_t lParam) {

case WM_SETCURSOR:
if (LOWORD(lParam) == HTCLIENT) {
if (!cursorIsHidden) {
// >>>>>>>>>>>>>>> BEG NON STD SCI PATCH >>>>>>>>>>>>>>>
//if (!cursorIsHidden) {
if (!IsMouseCursorHidden()) {
// <<<<<<<<<<<<<<< END NON STD SCI PATCH <<<<<<<<<<<<<<<
POINT pt;
if (::GetCursorPos(&pt)) {
::ScreenToClient(MainHWND(), &pt);
Expand Down Expand Up @@ -2397,10 +2415,13 @@ void ScintillaWin::SetTrackMouseLeaveEvent(bool on) noexcept {

void ScintillaWin::HideCursorIfPreferred() noexcept {
// SPI_GETMOUSEVANISH from OS.
if (typingWithoutCursor && !cursorIsHidden) {
::SetCursor(NULL);
cursorIsHidden = true;
// >>>>>>>>>>>>>>> BEG NON STD SCI PATCH >>>>>>>>>>>>>>>
if (typingWithoutCursor && !IsMouseCursorHidden()) {
//::SetCursor(NULL);
::ShowCursor(FALSE);
//cursorIsHidden = true;
}
// <<<<<<<<<<<<<<< END NON STD SCI PATCH <<<<<<<<<<<<<<<
}

void ScintillaWin::UpdateBaseElements() {
Expand Down Expand Up @@ -3265,6 +3286,13 @@ void ScintillaWin::GetMouseParameters() noexcept {
::SystemParametersInfo(SPI_GETMOUSEVANISH, 0, &typingWithoutCursor, 0);
}

// >>>>>>>>>>>>>>> BEG NON STD SCI PATCH >>>>>>>>>>>>>>>
bool ScintillaWin::IsMouseCursorHidden() noexcept {
CURSORINFO curInfo = { sizeof(CURSORINFO) };
return GetCursorInfo(&curInfo) ? (curInfo.flags == 0UL) : false;
}
// <<<<<<<<<<<<<<< END NON STD SCI PATCH <<<<<<<<<<<<<<<

void ScintillaWin::CopyToGlobal(GlobalMemory &gmUnicode, const SelectionText &selectedText) {
const std::string_view svSelected(selectedText.Data(), selectedText.LengthWithTerminator());
if (IsUnicodeMode()) {
Expand Down
37 changes: 22 additions & 15 deletions src/Config/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2904,25 +2904,32 @@ extern "C" bool EditSetDocumentBuffer(const char* lpstrText, DocPosU lenText, bo
{
bool const bLargerThan2GB = (lenText >= ((DocPosU)INT32_MAX));
bool const bLargeFileLoaded = (lenText >= ((DocPosU)Settings2.FileLoadWarningMB << 20));
int const docOptions = bLargeFileLoaded ? (bLargerThan2GB ? SC_DOCUMENTOPTION_TEXT_LARGE : SC_DOCUMENTOPTION_STYLES_NONE)
: SC_DOCUMENTOPTION_DEFAULT;
int const docOptions = bLargeFileLoaded ? (bLargerThan2GB ? SC_DOCUMENTOPTION_TEXT_LARGE : SC_DOCUMENTOPTION_STYLES_NONE)
: SC_DOCUMENTOPTION_DEFAULT;
bool ok = true;
LimitNotifyEvents();
if (SciCall_GetDocumentOptions() != docOptions) {
// we have to create a new document with changed options
return CreateNewDocument(lpstrText, lenText, docOptions, reload);
ok = CreateNewDocument(lpstrText, lenText, docOptions, reload);
}
bool const bReadOnly = SciCall_GetReadOnly();
SciCall_SetReadOnly(false);
if (!lpstrText || (lenText == 0)) {
SciCall_ClearAll();
} else {
SciCall_TargetWholeDocument();
if (reload) {
SciCall_ReplaceTargetMinimal(lenText, lpstrText);
} else {
SciCall_ReplaceTarget(lenText, lpstrText);
else {
bool const bReadOnly = SciCall_GetReadOnly();
SciCall_SetReadOnly(false);
if (!lpstrText || (lenText == 0)) {
SciCall_ClearAll();
}
else {
SciCall_TargetWholeDocument();
if (reload) {
SciCall_ReplaceTargetMinimal(lenText, lpstrText);
}
else {
SciCall_ReplaceTarget(lenText, lpstrText);
}
}
SciCall_SetReadOnly(bReadOnly);
}
SciCall_SetReadOnly(bReadOnly);
return true;
RestoreNotifyEvents();
return ok;
}

13 changes: 9 additions & 4 deletions src/Edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ void EditSetNewText(HWND hwnd, const char* lpstrText, DocPosU lenText, bool bCle
}
s_bFreezeAppTitle = true;


// clear markers, flags and positions
if (FocusedView.HideNonMatchedLines) {
EditToggleView(hwnd);
Expand Down Expand Up @@ -497,6 +498,8 @@ bool EditConvertText(HWND hwnd, cpi_enc_t encSource, cpi_enc_t encDest)
Encoding_Current(encDest);
return true;
}
bool ok = false;
LimitNotifyEvents();

// moves the gap within Scintilla so that the text of the document is stored consecutively and
// ensure there is a NUL character after the text
Expand Down Expand Up @@ -532,7 +535,7 @@ bool EditConvertText(HWND hwnd, cpi_enc_t encSource, cpi_enc_t encDest)
SciCall_GotoPos(curPos);
SciCall_SetFirstVisibleLine(vis1stLine);
FreeMem(pchText);
return true;
ok = true;
}
else {
FreeMem(pwchText);
Expand All @@ -546,7 +549,8 @@ bool EditConvertText(HWND hwnd, cpi_enc_t encSource, cpi_enc_t encDest)
FreeMem(pwchText);
}
}
return false;
RestoreNotifyEvents();
return ok;
}


Expand Down Expand Up @@ -7545,7 +7549,6 @@ bool EditReplace(HWND hwnd, LPEDITFINDREPLACE lpefr)
// EditReplaceAllInRange()
//
//

DocPosU EditReplaceAllInRange(HWND hwnd, LPEDITFINDREPLACE lpefr, DocPos iStartPos, DocPos iEndPos, DocPos *enlargement)
{
if (iStartPos > iEndPos) {
Expand Down Expand Up @@ -7583,7 +7586,7 @@ DocPosU EditReplaceAllInRange(HWND hwnd, LPEDITFINDREPLACE lpefr, DocPos iStartP
}

DocPosU iCount = 0;

LimitNotifyEvents();
UndoTransActionBegin();

while ((iPos >= 0LL) && (start <= iEndPos)) {
Expand Down Expand Up @@ -7616,6 +7619,8 @@ DocPosU EditReplaceAllInRange(HWND hwnd, LPEDITFINDREPLACE lpefr, DocPos iStartP
*enlargement = (iEndPos - iOrigEndPos);
SciCall_SetTargetRange(_saveTargetBeg_, _saveTargetEnd_ + *enlargement); //restore

RestoreNotifyEvents();

return iCount;
}

Expand Down
25 changes: 25 additions & 0 deletions src/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,31 @@ static inline LONG64 GetTicks_ms() {

// ----------------------------------------------------------------------------

__forceinline POINT POINTFromLParam(LPARAM lParam)
{
POINT const pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
return pt;
};

// ----------------------------------------------------------------------------

#if 0
__forceinline bool IsCursorVisible()
{
CURSORINFO ci = { sizeof(CURSORINFO) };
return GetCursorInfo(&ci) ? (ci.flags != 0UL) : true;
}

__forceinline bool IsMouseVanish()
{
BOOL bMouseVanish = FALSE;
return SystemParametersInfoW(SPI_GETMOUSEVANISH, 0, &bMouseVanish, 0) ? bMouseVanish : false;
}

// ----------------------------------------------------------------------------
#endif


#endif //_NP3_HELPERS_H_

/// End of Helpers.h ///
Loading

0 comments on commit 963c5f2

Please sign in to comment.