terminal/src/host/conimeinfo.h
James Holderness 8752054f5b
Make sure cursor visibility is restored after using an IME (#6207)
## Summary of the Pull Request

When using an _Input Method Editor_ in conhost for East Asian languages, the text cursor is temporarily hidden while the characters are being composed. When the composition is complete, the cursor visibility is meant to be restored, but that doesn't always happen if the IME composition is cancelled. This PR makes sure the cursor visibility is always restored, regardless of how the IME is closed.

## PR Checklist
* [x] Closes #810
* [x] CLA signed.
* [ ] Tests added/passed
* [ ] Requires documentation to be updated
* [ ] I've discussed this with core contributors already. If not checked, I'm ready to accept this work might be rejected in favor of a different grand plan.

## Detailed Description of the Pull Request / Additional comments

The original implementation hid the cursor whenever `ConsoleImeInfo::WriteCompMessage` was called (which could be multiple times in the course of a composition), and then only restored the visibility when `ConsoleImeInfo::WriteResultMessage` was called. If a composition is cancelled, though, `WriteResultMessage` would never be called, so the cursor visibility wouldn't be restored.

I've now made the `SaveCursorVisibility` and `RestoreCursorVisibility` methods public, so they can instead be called from the `ImeStartComposition` and `ImeEndComposition` functions. This makes sure `RestoreCursorVisibility` is always called, regardless of how the composition ended, and `SaveCursorVisibility` is only called once at the start of the composition (which isn't essential, but seems cleaner to me).

## Validation Steps Performed

I've manually tested opening and closing the IME, both while submitting characters and while cancelling a composition, and in all cases the cursor visibility was correctly restored.
2020-05-27 16:31:09 +00:00

92 lines
3.2 KiB
C++

/*++
Copyright (c) Microsoft Corporation
Licensed under the MIT license.
Module Name:
- conimeinfo.h
Abstract:
- This module contains the structures for the console IME entrypoints
for overall control
Author:
- Michael Niksa (MiNiksa) 10-May-2018
Revision History:
- From pieces of convarea.cpp originally authored by KazuM
--*/
#pragma once
#include "../inc/conime.h"
#include "../buffer/out/OutputCell.hpp"
#include "../buffer/out/TextAttribute.hpp"
#include "../renderer/inc/FontInfo.hpp"
#include "../types/inc/viewport.hpp"
#include "conareainfo.h"
class SCREEN_INFORMATION;
class ConsoleImeInfo final
{
public:
// IME composition string information
// There is one "composition string" per line that must be rendered on the screen
std::vector<ConversionAreaInfo> ConvAreaCompStr;
ConsoleImeInfo();
~ConsoleImeInfo() = default;
ConsoleImeInfo(const ConsoleImeInfo&) = delete;
ConsoleImeInfo(ConsoleImeInfo&&) = delete;
ConsoleImeInfo& operator=(const ConsoleImeInfo&) & = delete;
ConsoleImeInfo& operator=(ConsoleImeInfo&&) & = delete;
void RefreshAreaAttributes();
void ClearAllAreas();
[[nodiscard]] HRESULT ResizeAllAreas(const COORD newSize);
void WriteCompMessage(const std::wstring_view text,
const std::basic_string_view<BYTE> attributes,
const std::basic_string_view<WORD> colorArray);
void WriteResultMessage(const std::wstring_view text);
void RedrawCompMessage();
void SaveCursorVisibility();
void RestoreCursorVisibility();
private:
[[nodiscard]] HRESULT _AddConversionArea();
void _ClearComposition();
void _WriteUndeterminedChars(const std::wstring_view text,
const std::basic_string_view<BYTE> attributes,
const std::basic_string_view<WORD> colorArray);
void _InsertConvertedString(const std::wstring_view text);
static TextAttribute s_RetrieveAttributeAt(const size_t pos,
const std::basic_string_view<BYTE> attributes,
const std::basic_string_view<WORD> colorArray);
static std::vector<OutputCell> s_ConvertToCells(const std::wstring_view text,
const std::basic_string_view<BYTE> attributes,
const std::basic_string_view<WORD> colorArray);
std::vector<OutputCell>::const_iterator _WriteConversionArea(const std::vector<OutputCell>::const_iterator begin,
const std::vector<OutputCell>::const_iterator end,
COORD& pos,
const Microsoft::Console::Types::Viewport view,
SCREEN_INFORMATION& screenInfo);
bool _isSavedCursorVisible;
std::wstring _text;
std::basic_string<BYTE> _attributes;
std::basic_string<WORD> _colorArray;
};