Repository: microsoft/terminal
File: src/cascadia/TerminalControl/ControlCore.cpp (C++)

Code before:
            {
                conpty.ShowHide(showOrHide);
            }
        }
    }

    // Method Description:
    // - When the control gains focus, it needs to tell ConPTY about this.
    //   Usually, these sequences are reserved for applications that
    //   specifically request SET_FOCUS_EVENT_MOUSE, ?1004h. ConPTY uses this
    //   sequence REGARDLESS to communicate if the control was focused or not.
    // - Even if a client application disables this mode, the Terminal & conpty
    //   should always request this from the hosting terminal (and just ignore
    //   internally to ConPTY).
    // - Full support for this sequence is tracked in GH#11682.
    // - This is related to work done for GH#2988.
    void ControlCore::GotFocus()
    {
        // GH#13461 - temporarily turn off read-only mode, send the focus event,
        // then turn it back on. Even in focus mode, focus events are fine to
        // send. We don't want to pop a warning every time the control is
        // focused.
        const auto tmp = _isReadOnly;
        _isReadOnly = false;
        _terminal->FocusChanged(true);
        _isReadOnly = tmp;
    }

    // See GotFocus.
    void ControlCore::LostFocus()
    {
        const auto tmp = _isReadOnly;
        _isReadOnly = false;
        _terminal->FocusChanged(false);
        _isReadOnly = tmp;
    }

    bool ControlCore::_isBackgroundTransparent()
    {
        // If we're:
        // * Not fully opaque
        // * On an acrylic background (of any opacity)
        // * rendering on top of an image
        //
        // then the renderer should not render "default background" text with a
        // fully opaque background. Doing that would cover up our nice
        // transparency, or our acrylic, or our image.
        return Opacity() < 1.0f || UseAcrylic() || !_settings->BackgroundImage().empty() || _settings->UseBackgroundImageForWindow();
    }

    uint64_t ControlCore::OwningHwnd()

Review comment (refactor):
I believe we should use `wil::scope_exit` here so that this is exception safe. You can even abstract this away as:
```cpp
void ControlCore::GotFocus()
{
    _focusChanged(true);
}

void ControlCore::LostFocus()
{
    _focusChanged(false);
}

void ControlCore::_focusChanged(bool focused)
{
    // GH#13461 - temporarily turn off read-only mode, send the focus event,
    // then turn it back on. Even in focus mode, focus events are fine to
    // send. We don't want to pop a warning every time the control is
    // focused.
    const auto previous = std::exchange(_isReadOnly, false);
    const auto restore = wil::scope_exit([&]() { _isReadOnly = tmp; });
    _terminal->FocusChanged(focused);
}
```

Code after:
            {
                conpty.ShowHide(showOrHide);
            }
        }
    }

    // Method Description:
    // - When the control gains focus, it needs to tell ConPTY about this.
    //   Usually, these sequences are reserved for applications that
    //   specifically request SET_FOCUS_EVENT_MOUSE, ?1004h. ConPTY uses this
    //   sequence REGARDLESS to communicate if the control was focused or not.
    // - Even if a client application disables this mode, the Terminal & conpty
    //   should always request this from the hosting terminal (and just ignore
    //   internally to ConPTY).
    // - Full support for this sequence is tracked in GH#11682.
    // - This is related to work done for GH#2988.
    void ControlCore::GotFocus()
    {
        _focusChanged(true);
    }

    // See GotFocus.
    void ControlCore::LostFocus()
    {
        _focusChanged(false);
    }

    void ControlCore::_focusChanged(bool focused)
    {
        // GH#13461 - temporarily turn off read-only mode, send the focus event,
        // then turn it back on. Even in focus mode, focus events are fine to
        // send. We don't want to pop a warning every time the control is
        // focused.
        const auto previous = std::exchange(_isReadOnly, false);
        const auto restore = wil::scope_exit([&]() { _isReadOnly = previous; });
        _terminal->FocusChanged(focused);
    }

    bool ControlCore::_isBackgroundTransparent()
    {
        // If we're:
        // * Not fully opaque
        // * On an acrylic background (of any opacity)
        // * rendering on top of an image
        //
        // then the renderer should not render "default background" text with a
        // fully opaque background. Doing that would cover up our nice
        // transparency, or our acrylic, or our image.
        return Opacity() < 1.0f || UseAcrylic() || !_settings->BackgroundImage().empty() || _settings->UseBackgroundImageForWindow();
    }