1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_IME_TEXT_COMPOSE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_IME_TEXT_COMPOSE_H 18 19 #include <cstdint> 20 21 #include "core/common/ime/constant.h" 22 23 namespace OHOS::Ace { 24 /** 25 * Stands for what text is selected and how the directional is. 26 * We use base/extent to indicate the start/end position because of uncertain direction. 27 */ 28 struct TextCompose { 29 TextCompose() = default; TextComposeTextCompose30 TextCompose(int32_t base, int32_t extent) : baseOffset(base), extentOffset(extent) {} UpdateTextCompose31 void Update(int32_t base, int32_t extent) 32 { 33 baseOffset = base; 34 extentOffset = extent; 35 } 36 // Usually called when none is selected. UpdateTextCompose37 void Update(int32_t both) 38 { 39 baseOffset = both; 40 extentOffset = both; 41 } 42 bool operator==(const TextCompose& other) const 43 { 44 return baseOffset == other.baseOffset && extentOffset == other.extentOffset; 45 } 46 bool operator!=(const TextCompose& other) const 47 { 48 return !operator==(other); 49 } 50 /** 51 * @brief Get first selected character in the text. 52 */ GetStartTextCompose53 inline int32_t GetStart() const 54 { 55 return std::min(baseOffset, extentOffset); 56 } 57 /** 58 * @brief Get last selected character in the text. 59 */ GetEndTextCompose60 inline int32_t GetEnd() const 61 { 62 return std::max(baseOffset, extentOffset); 63 } 64 /** 65 * @brief Whether selection is valid. 66 */ IsValidTextCompose67 inline bool IsValid() const 68 { 69 return baseOffset > -1 && extentOffset > -1; 70 } 71 // May larger than, smaller than or equal to extentOffset. 72 int32_t baseOffset = -1; 73 // When paints caret, this is where the caret position is. 74 int32_t extentOffset = -1; 75 }; 76 } // namespace OHOS::Ace 77 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_IME_TEXT_COMPOSE_H