Repository: microsoft/terminal
File: src/host/getset.cpp (C++)

Code before:
            gci.SetInsertMode(WI_IsFlagSet(mode, ENABLE_INSERT_MODE));
            if (gci.GetInsertMode() != PreviousInsertMode)
            {
                gci.GetActiveOutputBuffer().SetCursorDBMode(false);
                if (gci.HasPendingCookedRead())
                {
                    gci.CookedReadData().SetInsertMode(gci.GetInsertMode());
                }
            }
        }
        else
        {
            WI_ClearFlag(gci.Flags, CONSOLE_USE_PRIVATE_FLAGS);
        }

        const auto oldMouseMode{ WI_IsFlagSet(context.InputMode, ENABLE_MOUSE_INPUT) };
        const auto newMouseMode{ WI_IsFlagSet(mode, ENABLE_MOUSE_INPUT) };
        const auto newQuickEditMode{ WI_IsFlagSet(gci.Flags, ENABLE_QUICK_EDIT_MODE) };

        // Whether we ask the terminal for mouse input is decided by 2 flags, ENABLE_MOUSE_INPUT
        // and ENABLE_QUICK_EDIT_MODE
        // If ENABLE_MOUSE_INPUT is set and ENABLE_QUICK_EDIT_MODE is not set, then we need
        // to ask the terminal to send us mouse events - in all other cases, terminal should
        // stop sending us mouse events
        if ((oldMouseMode != newMouseMode) ||
            (oldQuickEditMode != newQuickEditMode))
        {
            // At least one of ENABLE_MOUSE_INPUT or ENABLE_QUICK_EDIT_MODE changed, but we
            // only want to send the VT if the end result (whether we want mouse input or not)
            // changed, so check for that
            if ((newMouseMode && !newQuickEditMode))
            {
                gci.GetActiveInputBuffer()->PassThroughWin32MouseRequest(true);
            }
            else if (oldMouseMode && !oldQuickEditMode)
            {
                gci.GetActiveInputBuffer()->PassThroughWin32MouseRequest(false);
            }
        }

        context.InputMode = mode;
        WI_ClearAllFlags(context.InputMode, PRIVATE_MODES);

        // NOTE: For compatibility reasons, we need to set the modes and then return the error codes, not the other way around
        //       as might be expected.
        //       This is a bug from a long time ago and some applications depend on this functionality to operate properly.
        //       ---
        //       A prime example of this is that PSReadline module in Powershell will set the invalid mode 0x1e4
        //       which includes 0x4 for ECHO_INPUT but turns off 0x2 for LINE_INPUT. This is invalid, but PSReadline
        //       relies on it to properly receive the ^C printout and make a new line when the user presses Ctrl+C.
        {

Review comment (refactor):
I'm not sure this is quite right -- we only ever care about the current final status of quickedit when mouse mode is being toggled, right?

What we want to know is "*will user input generate mouse records in conhost*?"

You can probably simplify the logic here substantially:

```
const auto oldMouseMode{ !oldQuickEditMode && WI_IsFlagSet(old, mouse) };
const auto newMouseMode{ !newQuickEditMode && WI_IsFlagSet(new, mouse) };
```

"The mouse was enabled prior if quickedit was off and mouse was on" "the mouse is newly-enabled if quickedit is off and mouse is on".

Code after:
            gci.SetInsertMode(WI_IsFlagSet(mode, ENABLE_INSERT_MODE));
            if (gci.GetInsertMode() != PreviousInsertMode)
            {
                gci.GetActiveOutputBuffer().SetCursorDBMode(false);
                if (gci.HasPendingCookedRead())
                {
                    gci.CookedReadData().SetInsertMode(gci.GetInsertMode());
                }
            }
        }
        else
        {
            WI_ClearFlag(gci.Flags, CONSOLE_USE_PRIVATE_FLAGS);
        }

        const auto newQuickEditMode{ WI_IsFlagSet(gci.Flags, CONSOLE_QUICK_EDIT_MODE) };

        // Mouse input should be received when mouse mode is on and quick edit mode is off
        // (for more information regarding the quirks of mouse mode and why/how it relates
        //  to quick edit mode, see GH#9970)
        const auto oldMouseMode{ !oldQuickEditMode && WI_IsFlagSet(context.InputMode, ENABLE_MOUSE_INPUT) };
        const auto newMouseMode{ !newQuickEditMode && WI_IsFlagSet(mode, ENABLE_MOUSE_INPUT) };

        if (oldMouseMode != newMouseMode)
        {
            gci.GetActiveInputBuffer()->PassThroughWin32MouseRequest(newMouseMode);
        }

        context.InputMode = mode;
        WI_ClearAllFlags(context.InputMode, PRIVATE_MODES);

        // NOTE: For compatibility reasons, we need to set the modes and then return the error codes, not the other way around
        //       as might be expected.
        //       This is a bug from a long time ago and some applications depend on this functionality to operate properly.
        //       ---
        //       A prime example of this is that PSReadline module in Powershell will set the invalid mode 0x1e4
        //       which includes 0x4 for ECHO_INPUT but turns off 0x2 for LINE_INPUT. This is invalid, but PSReadline
        //       relies on it to properly receive the ^C printout and make a new line when the user presses Ctrl+C.
        {
            // Flags we don't understand are invalid.
            RETURN_HR_IF(E_INVALIDARG, WI_IsAnyFlagSet(mode, ~(INPUT_MODES | PRIVATE_MODES)));

            // ECHO on with LINE off is invalid.
            RETURN_HR_IF(E_INVALIDARG, WI_IsFlagSet(mode, ENABLE_ECHO_INPUT) && WI_IsFlagClear(mode, ENABLE_LINE_INPUT));
        }

        return S_OK;
    }
    CATCH_RETURN();
}