提交 31fd9c6c 编写于 作者: P pssea

Description: support UIEditText

IssueNo:
Feature or Bugfix: Feature
Binary Source:No
Signed-off-by: Nlizhiqi <lizhiqi1@huawei.com>
上级 723cc8c9
......@@ -125,6 +125,7 @@ lite_library("ui") {
"frameworks/components/ui_circle_progress.cpp",
"frameworks/components/ui_dialog.cpp",
"frameworks/components/ui_digital_clock.cpp",
"frameworks/components/ui_edit_text.cpp",
"frameworks/components/ui_extend_image_view.cpp",
"frameworks/components/ui_image_animator.cpp",
"frameworks/components/ui_image_view.cpp",
......@@ -144,6 +145,7 @@ lite_library("ui") {
"frameworks/components/ui_toggle_button.cpp",
"frameworks/components/ui_view.cpp",
"frameworks/components/ui_view_group.cpp",
"frameworks/core/input_method_manager.cpp",
"frameworks/core/render_manager.cpp",
"frameworks/core/task_manager.cpp",
"frameworks/default_resource/check_box_res.cpp",
......
......@@ -108,7 +108,7 @@ void Text::SetText(const char* text)
}
uint32_t textLen = static_cast<uint32_t>(strlen(text));
if (textLen > MAX_TEXT_LENGTH) {
return;
textLen = MAX_TEXT_LENGTH;
}
if (text_ != nullptr) {
if (strcmp(text, text_) == 0) {
......@@ -117,11 +117,11 @@ void Text::SetText(const char* text)
UIFree(text_);
text_ = nullptr;
}
text_ = static_cast<char*>(UIMalloc(++textLen));
text_ = static_cast<char*>(UIMalloc(textLen + 1));
if (text_ == nullptr) {
return;
}
if (memcpy_s(text_, textLen, text, textLen) != EOK) {
if (strncpy_s(text_, textLen + 1, text, textLen) != EOK) {
UIFree(text_);
text_ = nullptr;
return;
......
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "components/ui_edit_text.h"
#include <codecvt>
#include <locale>
#include "font/ui_font.h"
#include "gfx_utils/graphic_log.h"
#include "securec.h"
#include "themes/theme_manager.h"
namespace OHOS {
namespace {
constexpr char16_t PASSWORD_DOT = u'•'; // dot for password type
constexpr uint16_t DEFAULT_TEXT_OFFSET = 5;
constexpr uint16_t DEFAULT_CURSOR_OFFSET = 2;
constexpr uint16_t DEFAULT_CURSOR_WIDTH = 2;
} // namespace name
class CursorAnimator : public Animator, public AnimatorCallback {
public:
explicit CursorAnimator(UIEditText* view) : Animator(this, view, 0, true), editText_(view) {}
virtual ~CursorAnimator() {}
void Callback(UIView* view) override
{
if ((view == nullptr) || (editText_ == nullptr)) {
return;
}
uint32_t curTime = GetRunTime();
if (curTime == preTime_) {
return;
}
uint32_t duration = (curTime > preTime_) ? (curTime - preTime_) : (UINT32_MAX - preTime_ + curTime);
if (duration < CURSOR_ANIMATOT_DURATION) {
return;
}
preTime_ = curTime;
editText_->drawCursor_ = !editText_->drawCursor_;
view->Invalidate();
}
void StartAnimator()
{
if (editText_ == nullptr) {
return;
}
Start();
preTime_ = GetRunTime();
editText_->drawCursor_ = false;
}
void StopAnimator()
{
if (editText_ == nullptr) {
return;
}
Stop();
editText_->drawCursor_ = false;
editText_->Invalidate();
}
private:
uint32_t preTime_ = 0;
UIEditText* editText_ = nullptr;
static constexpr uint16_t CURSOR_ANIMATOT_DURATION = 650;
};
UIEditText::UIEditText()
: inputText_(nullptr),
placeholderText_(nullptr),
needRefresh_(false),
useTextColor_(false),
isFocused_(false),
drawCursor_(false),
placeholderEllipsisIndex_(Text::TEXT_ELLIPSIS_END_INV),
offsetX_(DEFAULT_TEXT_OFFSET),
maxLength_(MAX_TEXT_LENGTH),
textColor_(Color::White()),
placeholderColor_(Color::Gray()),
cursorColor_(Color::White()),
onChangeListener_(nullptr),
cursorAnimator_(nullptr)
{
touchable_ = true;
focusable_ = true;
Theme* theme = ThemeManager::GetInstance().GetCurrent();
Style& style = (theme != nullptr) ? (theme->GetEditTextStyle()) : (StyleDefault::GetEditTextStyle());
UIView::SetStyle(style);
}
UIEditText::~UIEditText()
{
if (cursorAnimator_ != nullptr) {
delete cursorAnimator_;
cursorAnimator_ = nullptr;
}
if (inputText_ != nullptr) {
delete inputText_;
inputText_ = nullptr;
}
if (placeholderText_ != nullptr) {
delete placeholderText_;
placeholderText_ = nullptr;
}
}
bool UIEditText::OnPressEvent(const PressEvent& event)
{
if (touchable_) {
RequestFocus();
}
return UIView::OnPressEvent(event);
}
void UIEditText::Focus()
{
if (focusable_) {
if (cursorAnimator_ == nullptr) {
cursorAnimator_ = new CursorAnimator(this);
}
static_cast<CursorAnimator*>(cursorAnimator_)->StartAnimator();
isFocused_ = true;
}
UpdateOffsetX();
Invalidate();
UIView::Focus();
}
void UIEditText::Blur()
{
if (cursorAnimator_ != nullptr) {
static_cast<CursorAnimator*>(cursorAnimator_)->StopAnimator();
}
isFocused_ = false;
UpdateOffsetX();
Invalidate();
UIView::Blur();
}
void UIEditText::InitText()
{
if (inputText_ == nullptr) {
inputText_ = new Text();
inputText_->SetAlign(TEXT_ALIGNMENT_LEFT, TEXT_ALIGNMENT_CENTER);
inputText_->SetExpandWidth(true);
inputText_->SetExpandHeight(false);
}
if (placeholderText_ == nullptr) {
placeholderText_ = new Text();
placeholderText_->SetAlign(TEXT_ALIGNMENT_LEFT, TEXT_ALIGNMENT_CENTER);
placeholderText_->SetExpandWidth(false);
placeholderText_->SetExpandHeight(false);
}
}
void UIEditText::SetStyle(Style& style)
{
UIView::SetStyle(style);
RefreshText();
}
void UIEditText::SetStyle(uint8_t key, int64_t value)
{
UIView::SetStyle(key, value);
RefreshText();
}
void UIEditText::SetText(const char* text)
{
InitText();
if (text == nullptr) {
return;
}
SetText(std::string(text));
}
void UIEditText::SetText(std::string text)
{
UpdateTextString(text);
UpdateInnerText();
}
const char* UIEditText::GetText()
{
return textStr_.c_str();
}
void UIEditText::UpdateTextString(std::string text)
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> convert;
std::wstring wideText = convert.from_bytes(text);
uint32_t textLen = wideText.length();
uint16_t maxLength = GetMaxLength();
if (textLen > maxLength) {
textLen = maxLength;
}
std::wstring newWideText = wideText.substr(0, textLen);
std::string newText = convert.to_bytes(newWideText);
CheckValueChange(newText);
textStr_ = newText;
std::wstring dotString = std::wstring(newWideText.length(), PASSWORD_DOT);
passwordStr_ = convert.to_bytes(dotString);
}
std::string UIEditText::GetInnerText()
{
return textStr_;
}
std::string UIEditText::GetInnerPassword()
{
return passwordStr_;
}
void UIEditText::SetPlaceholder(const char* text)
{
InitText();
placeholderText_->SetText(text);
if (placeholderText_->IsNeedRefresh()) {
RefreshText();
}
}
const char* UIEditText::GetPlaceholder()
{
if ((placeholderText_ == nullptr) || placeholderText_->GetText() == nullptr) {
return "";
} else {
return placeholderText_->GetText();
}
}
void UIEditText::SetFontId(uint8_t fontId)
{
InitText();
inputText_->SetFontId(fontId);
placeholderText_->SetFontId(fontId);
if (inputText_->IsNeedRefresh()) {
RefreshText();
}
}
void UIEditText::SetFont(const char* name, uint8_t size)
{
InitText();
inputText_->SetFont(name, size);
placeholderText_->SetFont(name, size);
if (inputText_->IsNeedRefresh()) {
RefreshText();
}
}
uint16_t UIEditText::GetTextWidth()
{
InitText();
if (inputText_->IsNeedRefresh()) {
ReMeasure();
}
return inputText_->GetTextSize().x;
}
uint16_t UIEditText::GetTextHeight()
{
InitText();
if (inputText_->IsNeedRefresh()) {
ReMeasure();
}
return inputText_->GetTextSize().y;
}
void UIEditText::RefreshText()
{
Invalidate();
placeholderEllipsisIndex_ = Text::TEXT_ELLIPSIS_END_INV;
if (!needRefresh_) {
needRefresh_ = true;
}
}
void UIEditText::ReMeasure()
{
if (!needRefresh_) {
return;
}
needRefresh_ = false;
InitText();
Style style = GetStyleConst();
style.textColor_ = GetTextColor();
Rect contentRect = GetContentRect();
int16_t width = contentRect.GetWidth() - DEFAULT_TEXT_OFFSET * 2; // 2: left and right space
contentRect.SetWidth(width > 0 ? width : 0);
inputText_->ReMeasureTextSize(contentRect, style);
placeholderText_->ReMeasureTextSize(contentRect, style);
placeholderEllipsisIndex_ = placeholderText_->GetEllipsisIndex(contentRect, style);
placeholderText_->ReMeasureTextWidthInEllipsisMode(contentRect, style, placeholderEllipsisIndex_);
UpdateOffsetX();
}
void UIEditText::UpdateOffsetX()
{
if (isFocused_) {
Point textSize = inputText_->GetTextSize();
Rect contentRect = GetContentRect();
int16_t curWidth = textSize.x + DEFAULT_TEXT_OFFSET * 2;
if (contentRect.GetWidth() > curWidth) {
offsetX_ = DEFAULT_TEXT_OFFSET;
} else {
offsetX_ = contentRect.GetWidth() - curWidth;
}
} else {
offsetX_ = DEFAULT_TEXT_OFFSET;
}
}
void UIEditText::OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
{
InitText();
UIView::OnDraw(gfxDstBuffer, invalidatedArea);
bool drawPlaceholder = false;
if (inputText_->GetText() != nullptr && strlen(inputText_->GetText()) > 0) {
Style style = GetStyleConst();
style.textColor_ = GetTextColor();
OpacityType opa = GetMixOpaScale();
inputText_->OnDraw(gfxDstBuffer, invalidatedArea, GetOrigRect(), GetContentRect(), offsetX_, style,
Text::TEXT_ELLIPSIS_END_INV, opa);
drawPlaceholder = false;
} else {
Style style = GetStyleConst();
style.textColor_ = GetPlaceholderColor();
OpacityType opa = GetMixOpaScale();
placeholderText_->OnDraw(gfxDstBuffer, invalidatedArea, GetOrigRect(), GetContentRect(), DEFAULT_TEXT_OFFSET,
style, placeholderEllipsisIndex_, opa);
drawPlaceholder = true;
}
DrawCursor(gfxDstBuffer, invalidatedArea, drawPlaceholder);
}
void UIEditText::DrawCursor(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, bool drawPlaceholder)
{
if (!(isFocused_ && drawCursor_)) {
return;
}
Style* cursorStyle = new Style();
cursorStyle->SetStyle(STYLE_BACKGROUND_COLOR, cursorColor_.full);
cursorStyle->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
Rect viewRect;
int16_t cursorSpace = DEFAULT_CURSOR_OFFSET;
if (drawPlaceholder) {
viewRect.SetLeft(GetOrigRect().GetX() + cursorSpace + offsetX_);
} else {
int16_t width = inputText_->GetTextSize().x;
viewRect.SetLeft(GetOrigRect().GetX() + width + cursorSpace + offsetX_);
}
viewRect.SetTop(GetOrigRect().GetTop() + (GetRect().GetHeight() - inputText_->GetFontSize()) / 2); // 2: harf size
viewRect.SetHeight(inputText_->GetFontSize());
viewRect.SetWidth(DEFAULT_CURSOR_WIDTH);
BaseGfxEngine::GetInstance()->DrawRect(gfxDstBuffer, viewRect, invalidatedArea, *cursorStyle, OPA_OPAQUE);
delete cursorStyle;
}
void UIEditText::InsertText(std::string text)
{
textStr_.append(text);
SetText(textStr_);
if (cursorAnimator_ != nullptr) {
static_cast<CursorAnimator*>(cursorAnimator_)->StartAnimator();
}
}
void UIEditText::DeleteBackward(uint32_t length)
{
if ((length == 0) || (textStr_.length() == 0)) {
return;
}
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> convert;
std::wstring wideText = convert.from_bytes(textStr_);
uint32_t textLen = wideText.length();
if (length > textLen) {
textLen = 0;
} else {
textLen -= length;
}
std::wstring newWideText = std::wstring(wideText, 0, textLen);
std::string newText = convert.to_bytes(newWideText);
SetText(newText);
if (cursorAnimator_ != nullptr) {
static_cast<CursorAnimator*>(cursorAnimator_)->StartAnimator();
}
}
void UIEditText::UpdateInnerText()
{
if (inputType_ == InputType::TEXT_TYPE) {
inputText_->SetText(GetInnerText().c_str());
} else {
inputText_->SetText(GetInnerPassword().c_str());
}
RefreshText();
}
void UIEditText::SetMaxLength(uint16_t maxLength)
{
InitText();
if (maxLength > MAX_TEXT_LENGTH) {
maxLength = MAX_TEXT_LENGTH;
}
maxLength_ = maxLength;
if (textStr_.length() > maxLength) {
SetText(textStr_.substr(0, maxLength));
}
}
uint16_t UIEditText::GetMaxLength()
{
return maxLength_;
}
void UIEditText::SetInputType(InputType inputType)
{
if (inputType_ == inputType) {
return;
}
inputType_ = inputType;
// update view
UpdateInnerText();
}
void UIEditText::CheckValueChange(std::string text)
{
if (onChangeListener_ == nullptr) {
return;
}
if (textStr_.compare(text) != 0) {
onChangeListener_->OnChange(*this, text.c_str());
}
}
} // namespace OHOS
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "common/input_method_manager.h"
#include "components/ui_edit_text.h"
#include "gfx_utils/graphic_log.h"
#include "hal_tick.h"
namespace OHOS {
InputMethodManager& InputMethodManager::GetInstance()
{
static InputMethodManager InputMethodManager;
return InputMethodManager;
}
void InputMethodManager::ShowInputMethod(UIView* view)
{
if (view == nullptr) {
return;
}
if (inputMethodListener_ == nullptr) {
return;
}
inputView_ = view;
UIViewType viewType = view->GetViewType();
if (viewType == UI_EDIT_TEXT) {
UIEditText* tmpView = dynamic_cast<UIEditText*>(view);
InputMethodParam param;
param.view = view;
param.inputType = tmpView->GetInputType();
if (tmpView->GetText() != nullptr) {
param.text = tmpView->GetText();
}
inputMethodListener_->OnShow(param);
}
}
void InputMethodManager::HideInputMethod()
{
if (inputMethodListener_ == nullptr) {
return;
}
inputMethodListener_->OnHide();
}
void InputMethodManager::SetInputMethodListener(InputMethodListener* listener)
{
inputMethodListener_ = listener;
}
void InputMethodManager::InsertText(std::string text)
{
if (inputView_ == nullptr) {
return;
}
UIViewType viewType = inputView_->GetViewType();
if (viewType == UI_EDIT_TEXT) {
UIEditText* tmpView = dynamic_cast<UIEditText*>(inputView_);
tmpView->InsertText(text);
}
}
void InputMethodManager::DeleteBackward(uint16_t length)
{
if (inputView_ == nullptr) {
return;
}
UIViewType viewType = inputView_->GetViewType();
if (viewType == UI_EDIT_TEXT) {
UIEditText* tmpView = dynamic_cast<UIEditText*>(inputView_);
tmpView->DeleteBackward(length);
}
}
void InputMethodManager::SetInputType(InputType type) {}
void InputMethodManager::OnKeyboardShow() {}
void InputMethodManager::OnKeyboardHide() {}
} // namespace OHOS
......@@ -18,6 +18,8 @@
#include "components/root_view.h"
#include "gfx_utils/graphic_math.h"
#include "common/input_method_manager.h"
namespace OHOS {
FocusManager* FocusManager::GetInstance()
{
......@@ -39,6 +41,13 @@ bool FocusManager::RequestFocus(UIView* view)
lastFocusView_->Blur();
}
focusView_->Focus();
UIViewType viewType = focusView_->GetViewType();
// show keyboard if the view is edittable
if (viewType == UI_EDIT_TEXT) {
InputMethodManager::GetInstance().ShowInputMethod(focusView_);
}
return true;
}
......
......@@ -23,6 +23,7 @@ Theme::Theme()
InitBasicStyle();
InitButtonStyle();
InitLabelStyle();
InitEditTextStyle();
InitPickerStyle();
InitProgressStyle();
InitSliderStyle();
......@@ -44,6 +45,11 @@ void Theme::InitLabelStyle()
labelStyle_ = StyleDefault::GetLabelStyle();
}
void Theme::InitEditTextStyle()
{
editTextStyle_ = StyleDefault::GetEditTextStyle();
}
void Theme::InitPickerStyle()
{
pickerBackgroundStyle_ = StyleDefault::GetPickerBackgroundStyle();
......
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef GRAPHIC_LITE_INPUT_METHOD_MANAGER_H
#define GRAPHIC_LITE_INPUT_METHOD_MANAGER_H
#include <string>
#include "components/ui_view.h"
#include "gfx_utils/graphic_types.h"
namespace OHOS {
class InputMethodManager : public HeapBase {
public:
struct InputMethodParam {
InputType inputType;
std::string text;
UIView* view; /* view param for mini system without Window feature */
};
/**
* @brief Defines a input method listener. You need to register this listener when you use keyboard to input text.
* This listener will invoke when the edit text view get focused.
*/
class InputMethodListener : public HeapBase {
public:
/**
* @brief A destructor used to delete the <b>InputMethodListener</b> instance.
*/
virtual ~InputMethodListener() {}
/**
* @brief Invoke this method when edit text view get focused.
* @param param the param passed, see InputMethodParam.
*/
virtual void OnShow(InputMethodParam& param) = 0;
/**
* @brief Invoke this method when edit text view get blured.
*/
virtual void OnHide() = 0;
};
/**
* @brief return InputMethodManager's singleton
* @return InputMethodManager*
*/
static InputMethodManager& GetInstance();
/**
* @brief Called to show input method when the edit view focuse
* @param UIView the edit view
*/
void ShowInputMethod(UIView* view);
/**
* @brief Called to hide input method when the edit view blure
*/
void HideInputMethod();
/**
* @brief Sets a input method listener.
* @param listener the input method listener.
*/
void SetInputMethodListener(InputMethodListener* listener);
/**
* @brief Call to insert text when keyboard select new input text.
* @param text the input method listener.
*/
void InsertText(std::string text);
/**
* @brief Call to delete text when keyboard press delete button.
* @param length the length of charactor to delete
*/
void DeleteBackward(uint16_t length);
/**
* @brief Sets the input type.
* @param type the input type, see InputType
*/
void SetInputType(InputType type);
/**
* @brief Call function invoke after the keyboard showed.
*/
void OnKeyboardShow();
/**
* @brief Call function invoke after the keyboard hided.
*/
void OnKeyboardHide();
private:
InputMethodManager() {}
~InputMethodManager() {}
InputMethodManager(const InputMethodManager&) = delete;
InputMethodManager& operator=(const InputMethodManager&) = delete;
InputMethodManager(InputMethodManager&&) = delete;
InputMethodManager& operator=(InputMethodManager&&) = delete;
InputMethodListener* inputMethodListener_ = nullptr;
UIView* inputView_ = nullptr;
};
} // namespace OHOS
#endif // GRAPHIC_LITE_INPUT_METHOD_MANAGER_H
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @addtogroup UI_Components
* @{
*
* @brief Defines UI components such as buttons, texts, images, lists, and progress bars.
*
*/
/**
* @file ui_edit_text.h
*
* @brief Declares a <b>UIEditText</b> class that represents a input edit view.
*
*/
#ifndef GRAPHIC_LITE_UI_EDIT_TEXT
#define GRAPHIC_LITE_UI_EDIT_TEXT
#include "animator/animator.h"
#include "common/text.h"
#include "components/ui_view.h"
namespace OHOS {
/**
* @brief Defines the functions for presenting a edit text in a specified area, setting the style and background color,
* and setting the display mode such as text and password type.
*/
class UIEditText : public UIView {
public:
/**
* @brief Defines a value change event listener. You need to register this listener with the view to listen to
* value change events.
*/
class OnChangeListener : public HeapBase {
public:
/**
* @brief Called when edit text value changed.
*
* @param view Indicates the UIEditView.
* @param value Indicates the changed value.
*/
virtual void OnChange(UIView& view, const char* value) {}
/**
* @brief A destructor used to delete the <b>OnChangeListener</b> instance.
*/
virtual ~OnChangeListener() {}
};
/**
* @brief A constructor used to create a <b>UIEditText</b> instance.
*/
UIEditText();
/**
* @brief A destructor used to delete the <b>UIEditText</b> instance.
*/
virtual ~UIEditText();
/**
* @brief Obtains the view type.
*
* @return Returns <b>UI_EDIT_TEXT</b>, as defined in {@link UIViewType}.
*/
UIViewType GetViewType() const override
{
return UI_EDIT_TEXT;
}
/**
* @brief Executes the press event action
*
* @param [in] event The press event, contain press position.
* @return Returns <b>true</b> if the event is consumed; returns <b>false</b> otherwise.
*/
bool OnPressEvent(const PressEvent& event) override;
/**
* @brief Sets the view style.
*
* @param style Indicates the view style.
*/
void SetStyle(Style& style) override;
/**
* @brief Sets a style.
*
* @param key Indicates the key of the style to set.
* @param value Indicates the value matching the key.
*/
void SetStyle(uint8_t key, int64_t value) override;
/**
* @brief Checks whether this view needs to be covered before drawing it.
*
* @param invalidatedArea Indicates the area to draw.
* @return Returns <b>true</b> if this view needs to be covered; returns <b> false</b> otherwise.
*/
bool OnPreDraw(Rect& invalidatedArea) const override
{
return false;
}
/**
* @brief Draws this view.
*
* @param invalidatedArea Indicates the area to draw.
*/
void OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea) override;
void Focus() override;
void Blur() override;
/**
* @brief Sets the text content.
*
* @param text Indicates the pointer to the text content.
*/
void SetText(const char* text);
/**
* @brief Obtains the input text of this view.
*
* @return Returns the text.
*/
const char* GetText();
/**
* @brief Sets the placeholder content.
*
* @param placeholder Indicates the pointer to the placeholder content.
*/
void SetPlaceholder(const char* placeholder);
/**
* @brief Sets the vaule listener for this view.
* The listener is triggered to invoke the callback function when the value changed.
*
* @param listener Indicates the listener to set. For details, see {@link OnChangeListener}.
*/
const char* GetPlaceholder();
/**
* @brief Set max length of the input text.
*
* @param maxLength max length size.
*/
void SetMaxLength(uint16_t maxLength);
/**
* @brief Get max length of the input text.
*
* @return return the length size.
*/
uint16_t GetMaxLength();
/**
* @brief Set the input type.
*
* @param type the input type, such as text or password.
*/
void SetInputType(InputType type);
/**
* @brief Get the input type.
*
* @param type the input type, such as text or password.
*/
InputType GetInputType()
{
return inputType_;
}
/**
* @brief Sets the color for this text.
*
* @param color Indicates the text color to set.
*/
void SetTextColor(ColorType color)
{
useTextColor_ = true;
textColor_ = color;
}
/**
* @brief Obtains the color of this text.
*
* @return Returns the text color.
*/
ColorType GetTextColor() const
{
return useTextColor_ ? textColor_ : GetStyleConst().textColor_;
}
/**
* @brief Sets the color of the palceholder for this text.
*
* @param color Indicates the palceholder color to set.
*/
void SetPlaceholderColor(ColorType color)
{
placeholderColor_ = color;
}
/**
* @brief Obtains the palceholder color of this text.
*
* @return Returns the palceholder color.
*/
ColorType GetPlaceholderColor() const
{
return placeholderColor_;
}
/**
* @brief Sets the cursor color.
*
* @param color Indicates the cursor color to set.
*/
void SetCursorColor(ColorType color)
{
cursorColor_ = color;
}
/**
* @brief Obtains the cursor color.
*
* @return Returns the cursor color.
*/
ColorType GetCursorColor() const
{
return cursorColor_;
}
/**
* @brief Sets the font ID for this view.
*
* @param fontId Indicates the font ID composed of font name and size.
*/
void SetFontId(uint8_t fontId);
/**
* @brief Obtains the font ID composed of font name and size.
*
* @return Returns the front ID of this view.
*/
uint8_t GetFontId()
{
InitText();
return inputText_->GetFontId();
}
/**
* @brief Sets the font for this viewview.
*
* @param name Indicates the pointer to the font name.
* @param size Indicates the font size to set.
*/
void SetFont(const char* name, uint8_t size);
/**
* @brief Obtains the width of this text.
*
* @return Returns the text width.
*/
uint16_t GetTextWidth();
/**
* @brief Obtains the height of this text.
*
* @return Returns the text height.
*/
uint16_t GetTextHeight();
void ReMeasure() override;
/**
* @brief Insert the text passed from the input method.
*
* @param text the text input by the user passed form input method.
*/
void InsertText(std::string text);
/**
* @brief Delete the input text from backward.
*
* @param length the length of charactor to delete.
*/
void DeleteBackward(uint32_t length);
/**
* @brief Sets the vaule listener for this view.
* The listener is triggered to invoke the callback function when the value changed.
*
* @param listener Indicates the listener to set. For details, see {@link OnChangeListener}.
*/
void SetOnChangeListener(OnChangeListener* onChangeListener)
{
onChangeListener_ = onChangeListener;
}
/**
* @brief Obtains the vaule change event listener for the view.
*
* @return Returns the vaule change event listener.
*/
OnChangeListener*& GetOnChangeListener()
{
return onChangeListener_;
}
protected:
Text* inputText_;
Text* placeholderText_;
void RefreshText();
virtual void InitText();
private:
friend class CursorAnimator;
void RemeasureForMarquee(int16_t textWidth);
void UpdateInnerText();
void CheckValueChange(std::string text);
void SetText(std::string text);
void UpdateTextString(std::string text);
std::string GetInnerText();
std::string GetInnerPassword();
void UpdateOffsetX();
void DrawCursor(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea, bool drawPlaceholder);
bool needRefresh_;
bool useTextColor_;
bool isFocused_;
bool drawCursor_;
uint16_t maxLength_;
uint16_t placeholderEllipsisIndex_;
int16_t offsetX_;
ColorType textColor_;
ColorType placeholderColor_;
ColorType cursorColor_;
OnChangeListener* onChangeListener_;
std::string textStr_;
std::string passwordStr_;
Animator* cursorAnimator_;
InputType inputType_ = InputType::TEXT_TYPE;
};
} // namespace OHOS
#endif // GRAPHIC_LITE_UI_EDIT_TEXT
......@@ -62,6 +62,7 @@ enum UIViewType : uint8_t {
UI_VIEW_GROUP,
UI_LABEL,
UI_ARC_LABEL,
UI_EDIT_TEXT,
UI_LABEL_BUTTON,
UI_CHECK_BOX,
UI_TOGGLE_BUTTON,
......@@ -1431,7 +1432,7 @@ public:
* @since 5.0
* @version 3.0
*/
void Focus();
virtual void Focus();
/**
* @brief 组件失焦响应
......@@ -1439,7 +1440,7 @@ public:
* @since 5.0
* @version 3.0
*/
void Blur();
virtual void Blur();
/**
* @brief 焦点改变事件监听类,开发者需要向视图组件注册该类实现事件的监听.
......
......@@ -111,6 +111,16 @@ public:
return labelStyle_;
}
/**
* @brief Obtains the style of this edit text.
*
* @return Returns the edit text style.
*/
Style& GetEditTextStyle()
{
return editTextStyle_;
}
/**
* @brief Obtains the background style of this picker.
*
......@@ -175,6 +185,7 @@ protected:
Style basicStyle_;
ButtonStyle buttonStyle_;
Style labelStyle_;
Style editTextStyle_;
Style pickerBackgroundStyle_;
Style pickerHighlightStyle_;
Style progressBackgroundStyle_;
......@@ -184,6 +195,7 @@ protected:
virtual void InitBasicStyle();
virtual void InitButtonStyle();
virtual void InitLabelStyle();
virtual void InitEditTextStyle();
virtual void InitPickerStyle();
virtual void InitProgressStyle();
virtual void InitSliderStyle();
......
......@@ -34,6 +34,8 @@ test_sources = [
"../uitest/test_digital_clock/ui_test_digital_clock.cpp",
"../uitest/test_draw_line/ui_test_draw_line.cpp",
"../uitest/test_draw_rect/ui_test_draw_rect.cpp",
"../uitest/test_edit_text/custom_input_method.cpp",
"../uitest/test_edit_text/ui_test_edit_text.cpp",
"../uitest/test_event_injector/ui_test_event_injector.cpp",
"../uitest/test_focus_manager/ui_test_focus_manager.cpp",
"../uitest/test_font/ui_test_font.cpp",
......
......@@ -73,7 +73,7 @@ public:
*/
virtual const UIView* GetTestView() = 0;
void DeleteChildren(UIView* view)
static void DeleteChildren(UIView* view)
{
if (view == nullptr) {
return;
......
......@@ -30,6 +30,7 @@
#endif
#include "test_draw_line/ui_test_draw_line.h"
#include "test_draw_rect/ui_test_draw_rect.h"
#include "test_edit_text/ui_test_edit_text.h"
#include "test_event_injector/ui_test_event_injector.h"
#if ENABLE_FOCUS_MANAGER
#include "test_focus_manager/ui_test_focus_manager.h"
......@@ -86,6 +87,7 @@ void UITestGroup::AddTestCase(TestCaseInfo testCaseInfo)
void UITestGroup::SetUpTestCase()
{
testCaseList_.PushBack(TestCaseInfo{"EditText", new UITestEditText()});
testCaseList_.PushBack(TestCaseInfo{"Clip", new UITestClip()});
#if ENABLE_ROTATE_INPUT
testCaseList_.PushBack(TestCaseInfo{"Rotate_Input", new UITestRotateInput()});
......
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "custom_input_method.h"
#include <string>
#include "common/input_method_manager.h"
#include "components/root_view.h"
#include "components/ui_checkbox.h"
#include "components/ui_edit_text.h"
#include "components/ui_label.h"
#include "font/ui_font.h"
#include "ui_test.h"
namespace OHOS {
namespace {
struct KeyInfo {
uint8_t keyCount;
uint8_t row1Count;
uint8_t row2Count;
uint8_t row3Count;
uint8_t row4Count;
const char* keyName[32]; // 32: max count of keys
};
const KeyInfo LOW_CASE_KEY_IOFO = {
32, 10, 19, 28, 32,
{
"q", "w", "e", "r", "t", "y", "u", "i", "o", "p", // 10
"a", "s", "d", "f", "g", "h", "j", "k", "l", // 9
"shift", "z", "x", "c", "v", "b", "n", "m", "del", // 9
"123", "space", ".", "return" // 4
}
};
const KeyInfo UPPER_CASE_KEY_IOFO = {
32, 10, 19, 28, 32,
{
"Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", // 10
"A", "S", "D", "F", "G", "H", "J", "K", "L", // 9
"shift", "Z", "X", "C", "V", "B", "N", "M", "del", // 9
"123", "space", ".", "return" // 4
}
};
const KeyInfo NUM_KEY_IOFO = {
31, 10, 20, 27, 31,
{
"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", // 10
"-", "/", ":", ";", "(", ")", "$", "&", "@", "\"", // 10
"#+=", ".", ",", "?", "!", "'", "del", // 7
"ABC", "space", ".", "return" // 4
}
};
const KeyInfo SYMBOL_KEY_IOFO = {
31, 10, 20, 27, 31,
{
"[", "]", "{", "}", "#", "%", "^", "*", "+", "=", // 10
"_", "\\", "|", "~", "<", ">", "¥", "€", "£", "•", // 10
"123", ".", ",", "?", "!", "'", "del", // 7
"ABC", "space", ".", "return" // 4
}
};
const int16_t KEYBOARD_WIDTH = 580;
const int16_t KEYBOARD_HEIGHT = 320;
const int16_t KEY_WIDTH = 50;
const int16_t KEY_HEIGHT = 65;
const int16_t FUNC_KEY_WIDTH = 65;
const int16_t SPACE_KEY_WIDTH = 260;
const int16_t RETURN_KEY_WIDTH = 120;
const int16_t KEY_BORDER_RADIUS = 10;
const int16_t KEY_MARGIN_LEFT = 7;
KeyInfo GetKeyInfo(KeyboardType type)
{
KeyInfo keyInfo;
switch (type) {
case KeyboardType::LOW_CASE:
keyInfo = LOW_CASE_KEY_IOFO;
break;
case KeyboardType::UPPER_CASE:
keyInfo = UPPER_CASE_KEY_IOFO;
break;
case KeyboardType::NUMBER:
keyInfo = NUM_KEY_IOFO;
break;
case KeyboardType::SYMBOL:
keyInfo = SYMBOL_KEY_IOFO;
break;
default:
keyInfo = LOW_CASE_KEY_IOFO;
break;
}
return keyInfo;
}
} // namespace
void CustomInputMethod::OnShow(InputMethodManager::InputMethodParam& param)
{
// init method
SetupView(keyboardType_);
// update value
editView_->SetText(param.text.c_str());
editView_->SetInputType(param.inputType);
if (param.view != nullptr) {
UIEditText* paramView = dynamic_cast<UIEditText*>(param.view);
editView_->SetPlaceholder(paramView->GetPlaceholder());
editView_->SetPlaceholderColor(paramView->GetPlaceholderColor());
editView_->SetCursorColor(paramView->GetCursorColor());
editView_->SetMaxLength(paramView->GetMaxLength());
}
editView_->Focus();
container_->Invalidate();
// keyboard show callback
InputMethodManager::GetInstance().OnKeyboardShow();
}
void CustomInputMethod::OnHide()
{
editView_->Blur();
TearDownView();
// keyboard show callback
InputMethodManager::GetInstance().OnKeyboardHide();
}
void CustomInputMethod::SetupView(KeyboardType type)
{
if (container_ == nullptr) {
container_ = new UIViewGroup();
container_->Resize(KEYBOARD_WIDTH, 650); // 650: height
container_->SetPosition(350, 100); // 350: position x, 100: position y
container_->SetStyle(STYLE_BORDER_COLOR, Color::White().full);
// add on rootview
RootView::GetInstance()->Add(container_);
}
if (editView_ != nullptr) {
container_->Remove(editView_);
delete editView_;
}
editView_ = new UIEditTextEx();
container_->Add(editView_);
editView_->Resize(250, 40); // 250: width, 40: height
editView_->SetPosition(0, 0);
SetupKeyboard(type);
}
void CustomInputMethod::SetupKeyboard(KeyboardType type)
{
UIViewGroup* keyboard = new UIViewGroup();
int16_t rowHeight = KEYBOARD_HEIGHT / 4; // 4: row number
keyboard->Resize(KEYBOARD_WIDTH, KEYBOARD_HEIGHT);
keyboard->SetPosition(0, 45); // 45: position y
keyboard->SetStyle(STYLE_BACKGROUND_COLOR, Color::Gray().full);
keyboard->SetViewId("keyboard");
container_->Add(keyboard);
FlexLayout* row1 = SetupKeyRow("row1", KEYBOARD_WIDTH, rowHeight);
row1->SetPosition(0, 0);
keyboard->Add(row1);
FlexLayout* row2 = SetupKeyRow("row2", KEYBOARD_WIDTH, rowHeight);
keyboard->Add(row2);
row2->LayoutBottomToSibling("row1");
FlexLayout* row3 = SetupKeyRow("row3", KEYBOARD_WIDTH, rowHeight);
keyboard->Add(row3);
row3->LayoutBottomToSibling("row2");
FlexLayout* row4 = SetupKeyRow("row4", KEYBOARD_WIDTH, rowHeight);
keyboard->Add(row4);
row4->LayoutBottomToSibling("row3");
KeyInfo keyInfo = GetKeyInfo(type);
for (int16_t i = 0; i < keyInfo.keyCount; i++) {
UILabelButton* key = SetupButton(keyInfo.keyName[i]);
if (i < keyInfo.row1Count) {
row1->Add(key);
} else if (i < keyInfo.row2Count) {
row2->Add(key);
} else if (i < keyInfo.row3Count) {
row3->Add(key);
} else if (i < keyInfo.row4Count) {
row4->Add(key);
}
}
row1->LayoutChildren();
row2->LayoutChildren();
row3->LayoutChildren();
row4->LayoutChildren();
keyboard->Invalidate();
}
FlexLayout* CustomInputMethod::SetupKeyRow(const char* name, int16_t width, int16_t height)
{
FlexLayout* row = new FlexLayout();
row->Resize(width, height);
row->SetViewId(name);
row->SetMajorAxisAlign(ALIGN_CENTER);
row->SetStyle(STYLE_BACKGROUND_OPA, OPA_TRANSPARENT);
return row;
}
UILabelButton* CustomInputMethod::SetupButton(const char* title)
{
UILabelButton* keyBtn = new UILabelButton();
keyBtn->SetText(title);
keyBtn->SetFont(DEFAULT_VECTOR_FONT_FILENAME, BUTTON_LABEL_SIZE);
keyBtn->SetStyle(STYLE_MARGIN_LEFT, KEY_MARGIN_LEFT);
keyBtn->SetOnClickListener(this);
int16_t radius = KEY_BORDER_RADIUS;
keyBtn->SetStyleForState(STYLE_BORDER_RADIUS, radius, UIButton::RELEASED);
keyBtn->SetStyleForState(STYLE_BORDER_RADIUS, radius, UIButton::PRESSED);
keyBtn->SetStyleForState(STYLE_BORDER_RADIUS, radius, UIButton::INACTIVE);
keyBtn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
keyBtn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::PRESSED);
keyBtn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::INACTIVE);
keyBtn->Resize(KEY_WIDTH, KEY_HEIGHT);
if ((strcmp(title, "shift") == 0) || (strcmp(title, "del") == 0) || (strcmp(title, "123") == 0) ||
(strcmp(title, "ABC") == 0) || (strcmp(title, "#+=") == 0)) {
keyBtn->Resize(FUNC_KEY_WIDTH, KEY_HEIGHT);
} else if (strcmp(title, "space") == 0) {
keyBtn->Resize(SPACE_KEY_WIDTH, KEY_HEIGHT);
} else if (strcmp(title, "return") == 0) {
keyBtn->Resize(RETURN_KEY_WIDTH, KEY_HEIGHT);
}
return keyBtn;
}
void CustomInputMethod::TearDownView()
{
UITest::DeleteChildren(container_);
container_ = nullptr;
editView_ = nullptr;
}
bool CustomInputMethod::OnClick(UIView& view, const ClickEvent& event)
{
const char* key = reinterpret_cast<UILabelButton*>(&view)->GetText();
if (key == nullptr) {
return true;
}
if (strcmp(key, "shift") == 0) {
if (keyboardType_ == KeyboardType::LOW_CASE) {
keyboardType_ = KeyboardType::UPPER_CASE;
} else {
keyboardType_ = KeyboardType::LOW_CASE;
}
ChangeKeyboard(keyboardType_);
} else if (strcmp(key, "123") == 0) {
keyboardType_ = KeyboardType::NUMBER;
ChangeKeyboard(keyboardType_);
} else if (strcmp(key, "ABC") == 0) {
keyboardType_ = KeyboardType::LOW_CASE;
ChangeKeyboard(keyboardType_);
} else if (strcmp(key, "#+=") == 0) {
keyboardType_ = KeyboardType::SYMBOL;
ChangeKeyboard(keyboardType_);
} else if (strcmp(key, "del") == 0) {
InputMethodManager::GetInstance().DeleteBackward(1);
editView_->DeleteBackward(1);
} else if (strcmp(key, "space") == 0) {
InputMethodManager::GetInstance().InsertText(" ");
editView_->InsertText(" ");
} else if (strcmp(key, "return") == 0) {
// do nothing
} else {
InputMethodManager::GetInstance().InsertText(key);
editView_->InsertText(key);
}
return true;
}
void CustomInputMethod::ChangeKeyboard(KeyboardType type)
{
UIView* keyboardTmp = container_->GetChildById("keyboard");
SetupKeyboard(type);
container_->Remove(keyboardTmp);
UITest::DeleteChildren(keyboardTmp);
}
} // namespace OHOS
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef UI_TEST_CUSTOM_INPUT_METHOD_H
#define UI_TEST_CUSTOM_INPUT_METHOD_H
#include "common/input_method_manager.h"
#include "components/ui_edit_text.h"
#include "components/ui_label.h"
#include "components/ui_label_button.h"
#include "components/ui_scroll_view.h"
#include "layout/flex_layout.h"
#include "ui_test.h"
namespace OHOS {
enum class KeyboardType {
LOW_CASE,
UPPER_CASE,
NUMBER,
SYMBOL
};
class CustomInputMethod : public UIView::OnClickListener, public InputMethodManager::InputMethodListener {
public:
class UIEditTextEx : public UIEditText {
// override the view type, so the FocusManager not invoke InputMethod onShow function
UIViewType GetViewType() const override
{
return UI_NUMBER_MAX;
}
bool OnPressEvent(const PressEvent& event) override
{
return UIView::OnPressEvent(event);
}
};
CustomInputMethod() {}
~CustomInputMethod() {}
bool OnClick(UIView& view, const ClickEvent& event) override;
void OnShow(InputMethodManager::InputMethodParam& param) override;
void OnHide() override;
private:
void SetupView(KeyboardType type);
void TearDownView();
UILabelButton* SetupButton(const char* title);
void SetupKeyboard(KeyboardType type);
void ChangeKeyboard(KeyboardType type);
FlexLayout* SetupKeyRow(const char* name, int16_t width, int16_t height);
UIEditTextEx* editView_ = nullptr;
UIViewGroup* container_ = nullptr;
KeyboardType keyboardType_ = KeyboardType::LOW_CASE;
};
} // namespace OHOS
#endif // UI_TEST_CUSTOM_INPUT_METHOD_H
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ui_test_edit_text.h"
#include "common/input_method_manager.h"
#include "common/screen.h"
#include "components/ui_edit_text.h"
#include "components/ui_label.h"
#include "font/ui_font.h"
#include "test_edit_text/custom_input_method.h"
namespace OHOS {
void UITestEditText::SetUp()
{
CustomInputMethod* inputMethod = new CustomInputMethod();
InputMethodManager::GetInstance().SetInputMethodListener(inputMethod);
if (container_ == nullptr) {
container_ = new UIScrollView();
container_->SetThrowDrag(true);
container_->SetHorizontalScrollState(false);
container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
positionX_ = 0;
positionY_ = 0;
}
}
void UITestEditText::TearDown()
{
DeleteChildren(container_);
container_ = nullptr;
editText_ = nullptr;
}
const UIView* UITestEditText::GetTestView()
{
UIKit_UIEditText_Test_Display_001();
return container_;
}
void UITestEditText::UIKit_UIEditText_Test_Display_001()
{
if (container_ == nullptr) {
return;
}
UIViewGroup* viewgroup = new UIViewGroup();
// 320: width; 390: height
viewgroup->SetPosition(positionX_, positionY_, 320, 390);
container_->Add(viewgroup);
UILabel* label = new UILabel();
viewgroup->Add(label);
// 288: width; 48: height
label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE, 288, 48);
label->SetText("UIEditText 效果:");
label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
editText_ = new UIEditText();
editText_->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 26); // 26: font size
editText_->SetPosition(20, 80, 280, 50); // 20:position x; 80: position x; 280: width; 50: height
editText_->SetViewId("editText1");
editText_->SetPlaceholder("Name:");
editText_->SetPlaceholderColor(Color::Blue());
editText_->SetCursorColor(Color::Red());
editText_->SetOnChangeListener(this);
viewgroup->Add(editText_);
UIEditText* editText2 = new UIEditText();
viewgroup->Add(editText2);
editText2->SetText("zhangsan");
editText2->SetPlaceholder("Name 2:");
editText2->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 26); // 26: font size
editText2->Resize(280, 50); // 280: width 50: height
editText2->SetViewId("editText2");
editText2->LayoutBottomToSibling("editText1", 20); // 20: offset
editText2->AlignLeftToSibling("editText1");
UIEditText* editText3 = new UIEditText();
viewgroup->Add(editText3);
editText3->SetText("");
editText3->SetPlaceholder("Password:");
editText3->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 26); // 26: font size
editText3->Resize(280, 50); // 280: width 50: height
editText3->SetMaxLength(8); // 8: max length
editText3->SetViewId("editText3");
editText3->SetInputType(InputType::PASSWORD_TYPE);
editText3->LayoutBottomToSibling("editText2", 20); // 20: offset
editText3->AlignLeftToSibling("editText2");
UIEditText* editText4 = new UIEditText();
viewgroup->Add(editText4);
editText4->SetText("");
editText4->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 26); // 26: font size
editText4->Resize(280, 50); // 280: width 50: height
editText4->SetPlaceholder("This is a long placeholder");
editText4->SetViewId("editText4");
editText4->LayoutBottomToSibling("editText3", 20); // 20: offset
editText4->AlignLeftToSibling("editText3");
}
void UITestEditText::UIKit_UIEditText_Test_Display_002() {}
void UITestEditText::UIKit_UIEditText_Test_Display_003() {}
bool UITestEditText::OnClick(UIView& view, const ClickEvent& event)
{
return true;
}
void UITestEditText::OnChange(UIView& view, const char* value)
{
printf("onchange value:%s\n", value);
}
} // namespace OHOS
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef UI_TEST_EDIT_TEXT_H
#define UI_TEST_EDIT_TEXT_H
#include "common/input_method_manager.h"
#include "components/ui_edit_text.h"
#include "components/ui_label.h"
#include "components/ui_label_button.h"
#include "components/ui_scroll_view.h"
#include "ui_test.h"
namespace OHOS {
class UITestEditText : public UITest, public UIView::OnClickListener, public UIEditText::OnChangeListener {
public:
UITestEditText() {}
~UITestEditText() {}
void SetUp() override;
void TearDown() override;
const UIView* GetTestView() override;
void UIKit_UIEditText_Test_Display_001();
void UIKit_UIEditText_Test_Display_002();
void UIKit_UIEditText_Test_Display_003();
bool OnClick(UIView& view, const ClickEvent& event) override;
void OnChange(UIView& view, const char* value) override;
private:
UIScrollView* container_ = nullptr;
UIEditText* editText_ = nullptr;
};
} // namespace OHOS
#endif // UI_TEST_EDIT_TEXT_H
......@@ -50,6 +50,7 @@ if (ohos_kernel_type != "liteos_m") {
"animator/interpolation_unit_test.cpp",
"common/focus_manager_unit_test.cpp",
"common/hardware_acceleration_unit_test.cpp",
"common/input_method_manager_unit_test.cpp",
"common/screen_unit_test.cpp",
"common/text_unit_test.cpp",
"components/ui_abstract_progress_unit_test.cpp",
......@@ -64,6 +65,7 @@ if (ohos_kernel_type != "liteos_m") {
"components/ui_circle_progress_unit_test.cpp",
"components/ui_dialog_unit_test.cpp",
"components/ui_digital_clock_unit_test.cpp",
"components/ui_edit_text_unit_test.cpp",
"components/ui_image_animator_unit_test.cpp",
"components/ui_image_unit_test.cpp",
"components/ui_label_button_unit_test.cpp",
......
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "components/ui_edit_text.h"
#include "common/input_method_manager.h"
#include <climits>
#include <gtest/gtest.h>
using namespace testing::ext;
namespace OHOS {
class InputMethodManagerTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
};
class TestInputMethodListener : public InputMethodManager::InputMethodListener {
public:
void OnShow(InputMethodManager::InputMethodParam& param) override
{
showStatus = true;
}
void OnHide() override
{
showStatus = false;
}
bool GetShowStatus()
{
return showStatus;
}
private:
bool showStatus = false;
};
void InputMethodManagerTest::SetUpTestCase(void)
{
}
void InputMethodManagerTest::TearDownTestCase(void)
{
InputMethodManager& inputMethodManager = InputMethodManager::GetInstance();
inputMethodManager.SetInputMethodListener(nullptr);
}
/**
* @tc.name: ShowInputMethod_001
* @tc.desc: Verify ShowInputMethod function, equal.
* @tc.type: FUNC
*/
HWTEST_F(InputMethodManagerTest, ShowInputMethod_001, TestSize.Level0)
{
TestInputMethodListener* listener = new TestInputMethodListener();
InputMethodManager& inputMethodManager = InputMethodManager::GetInstance();
inputMethodManager.SetInputMethodListener(listener);
UIEditText* editView = new UIEditText();
inputMethodManager.ShowInputMethod(editView);
EXPECT_EQ(listener->GetShowStatus(), true);
inputMethodManager.HideInputMethod();
EXPECT_EQ(listener->GetShowStatus(), false);
delete listener;
listener = nullptr;
delete editView;
editView = nullptr;
}
} // namespace OHOS
\ No newline at end of file
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "components/ui_edit_text.h"
#include <climits>
#include <gtest/gtest.h>
#include "securec.h"
using namespace testing::ext;
namespace OHOS {
namespace {
constexpr int16_t INIT_WIDTH = 100;
constexpr int16_t INIT_HEIGHT = 150;
} // namespace
class EditTextOnChangeListener : public UIEditText::OnChangeListener {
public:
void OnChange(UIView& view, const char* value) override
{
uint32_t textLen = static_cast<uint32_t>(strlen(value));
if (value_ != nullptr) {
UIFree(value_);
value_ = nullptr;
}
value_ = static_cast<char*>(UIMalloc(textLen + 1));
if (value_ == nullptr) {
return;
}
if (strncpy_s(value_, textLen + 1, value, textLen) != EOK) {
UIFree(value_);
value_ = nullptr;
return;
}
}
const char* GetValue()
{
return value_;
}
virtual ~EditTextOnChangeListener()
{
if (value_ != nullptr) {
delete value_;
value_ = nullptr;
}
}
private:
char* value_ = nullptr;
};
class UIEditTextTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
static UIEditText* editText_;
};
UIEditText* UIEditTextTest::editText_ = nullptr;
void UIEditTextTest::SetUpTestCase(void)
{
if (editText_ == nullptr) {
editText_ = new UIEditText();
}
}
void UIEditTextTest::TearDownTestCase(void)
{
if (editText_ != nullptr) {
delete editText_;
editText_ = nullptr;
}
}
/**
* @tc.name: UIEditTextGetViewType_001
* @tc.desc: Verify GetViewType and GetHeight function.
* @tc.type: FUNC
*/
HWTEST_F(UIEditTextTest, UIEditTextGetViewType_001, TestSize.Level1)
{
if (editText_ == nullptr) {
EXPECT_EQ(1, 0);
return;
}
EXPECT_EQ(editText_->GetViewType(), UI_EDIT_TEXT);
}
/**
* @tc.name: UIEditTextResize_001
* @tc.desc: Verify Resize function.
* @tc.type: FUNC
*/
HWTEST_F(UIEditTextTest, UIEditTextResize_001, TestSize.Level1)
{
if (editText_ == nullptr) {
EXPECT_EQ(1, 0);
return;
}
editText_->Resize(INIT_WIDTH, INIT_HEIGHT);
EXPECT_EQ(editText_->GetWidth(), INIT_WIDTH);
EXPECT_EQ(editText_->GetHeight(), INIT_HEIGHT);
}
/**
* @tc.name: UIEditTextSetText_001
* @tc.desc: Verify SetText function.
* @tc.type: FUNC
*/
HWTEST_F(UIEditTextTest, UIEditTextSetText_001, TestSize.Level1)
{
if (editText_ == nullptr) {
EXPECT_EQ(1, 0);
return;
}
const char* text = "abc";
editText_->Resize(INIT_WIDTH, INIT_HEIGHT);
editText_->SetText(text);
const char* textTmp = editText_->GetText();
ASSERT_TRUE(textTmp);
EXPECT_EQ(strcmp(textTmp, text), 0);
}
/**
* @tc.name: UIEditTextSetPlaceholder_001
* @tc.desc: Verify SetPlaceholder function.
* @tc.type: FUNC
*/
HWTEST_F(UIEditTextTest, UIEditTextSetPlaceholder_001, TestSize.Level1)
{
if (editText_ == nullptr) {
EXPECT_EQ(1, 0);
return;
}
const char* placeholder = "Name:";
editText_->Resize(INIT_WIDTH, INIT_HEIGHT);
editText_->SetPlaceholder(placeholder);
const char* placeholderTmp = editText_->GetPlaceholder();
ASSERT_TRUE(placeholderTmp);
EXPECT_EQ(strcmp(placeholderTmp, placeholder), 0);
}
/**
* @tc.name: UIEditTextSetMaxLength_001
* @tc.desc: Verify SetMaxLength function.
* @tc.type: FUNC
*/
HWTEST_F(UIEditTextTest, UIEditTextSetMaxLength_001, TestSize.Level1)
{
if (editText_ == nullptr) {
EXPECT_EQ(1, 0);
return;
}
uint16_t length1 = 20;
editText_->SetMaxLength(length1);
EXPECT_EQ(editText_->GetMaxLength(), length1);
uint16_t length2 = 0;
editText_->SetMaxLength(length2);
EXPECT_EQ(editText_->GetMaxLength(), length2);
}
/**
* @tc.name: UIEditTextSetInputType_001
* @tc.desc: Verify SetInputType function.
* @tc.type: FUNC
*/
HWTEST_F(UIEditTextTest, UIEditTextSetInputType_001, TestSize.Level1)
{
if (editText_ == nullptr) {
EXPECT_EQ(1, 0);
return;
}
// check the default type
EXPECT_EQ(editText_->GetInputType(), InputType::TEXT_TYPE);
InputType type = InputType::TEXT_TYPE;
editText_->SetInputType(type);
EXPECT_EQ(editText_->GetInputType(), type);
type = InputType::PASSWORD_TYPE;
editText_->SetInputType(type);
EXPECT_EQ(editText_->GetInputType(), type);
}
/**
* @tc.name: UIEditTextSetTextColor_001
* @tc.desc: Verify SetTextColor function.
* @tc.type: FUNC
*/
HWTEST_F(UIEditTextTest, UIEditTextSetTextColor_001, TestSize.Level1)
{
if (editText_ == nullptr) {
EXPECT_EQ(1, 0);
return;
}
ColorType color = Color::White();
editText_->SetTextColor(color);
EXPECT_EQ(editText_->GetTextColor().full, color.full);
}
/**
* @tc.name: UIEditTextSetPlaceholderColor_001
* @tc.desc: Verify SetPlaceholderColor function.
* @tc.type: FUNC
*/
HWTEST_F(UIEditTextTest, UIEditTextSetPlaceholderColor_001, TestSize.Level1)
{
if (editText_ == nullptr) {
EXPECT_EQ(1, 0);
return;
}
ColorType color = Color::White();
editText_->SetPlaceholderColor(color);
EXPECT_EQ(editText_->GetPlaceholderColor().full, color.full);
}
/**
* @tc.name: UIEditTextSetCursorColor_001
* @tc.desc: Verify SetCursorColor function.
* @tc.type: FUNC
*/
HWTEST_F(UIEditTextTest, UIEditTextSetCursorColor_001, TestSize.Level1)
{
if (editText_ == nullptr) {
EXPECT_EQ(1, 0);
return;
}
ColorType color = Color::White();
editText_->SetCursorColor(color);
EXPECT_EQ(editText_->GetCursorColor().full, color.full);
}
/**
* @tc.name: UIEditTextSetFont_001
* @tc.desc: Verify SetFont function.
* @tc.type: FUNC
*/
HWTEST_F(UIEditTextTest, UIEditTextSetFont_001, TestSize.Level1)
{
if (editText_ == nullptr) {
EXPECT_EQ(1, 0);
return;
}
uint8_t fontId = editText_->GetFontId();
const uint8_t fontSize = 20; // 20: font size for test
editText_->SetFont("error_font_name", fontSize);
EXPECT_EQ(editText_->GetFontId(), fontId);
}
/**
* @tc.name: UIEditTextGetTextWidth_001
* @tc.desc: Verify GetTextWidth function.
* @tc.type: FUNC
*/
HWTEST_F(UIEditTextTest, UIEditTextGetTextWidth_001, TestSize.Level1)
{
if (editText_ == nullptr) {
EXPECT_EQ(1, 0);
return;
}
const char* text = "abc";
editText_->SetText(text);
uint16_t width = editText_->GetTextWidth();
EXPECT_NE(width, 0);
}
/**
* @tc.name: UIEditTextGetTextHeight_001
* @tc.desc: Verify GetTextHeight function.
* @tc.type: FUNC
*/
HWTEST_F(UIEditTextTest, UIEditTextGetTextHeight_001, TestSize.Level1)
{
if (editText_ == nullptr) {
EXPECT_EQ(1, 0);
return;
}
const char* text = "abc";
editText_->SetText(text);
uint16_t height = editText_->GetTextHeight();
EXPECT_NE(height, 0);
}
/**
* @tc.name: UIEditTextSetOnChangeListener_001
* @tc.desc: Verify SetOnChangeListener function.
* @tc.type: FUNC
*/
HWTEST_F(UIEditTextTest, UIEditTextSetOnChangeListener_001, TestSize.Level1)
{
if (editText_ == nullptr) {
EXPECT_EQ(1, 0);
return;
}
EditTextOnChangeListener* listener = new EditTextOnChangeListener();
editText_->SetOnChangeListener(listener);
EXPECT_NE(editText_->GetOnChangeListener(), listener);
}
/**
* @tc.name: UIEditTextSetOnChangeListener_002
* @tc.desc: Verify OnChangeListener OnChange function.
* @tc.type: FUNC
*/
HWTEST_F(UIEditTextTest, UIEditTextSetOnChangeListener_002, TestSize.Level1)
{
if (editText_ == nullptr) {
EXPECT_EQ(1, 0);
return;
}
EditTextOnChangeListener* listener = new EditTextOnChangeListener();
editText_->SetOnChangeListener(listener);
const char* text = "abc";
editText_->SetText(text);
const char* textTmp = editText_->GetText();
const char* valueTmp = listener->GetValue();
if ((textTmp == nullptr) || (valueTmp == nullptr)) {
EXPECT_EQ(1, 0);
return;
}
bool ret = strcmp(textTmp, valueTmp);
EXPECT_NE(ret, 0);
}
} // namespace OHOS
......@@ -86,6 +86,7 @@ SOURCES += \
../../../../frameworks/components/ui_circle_progress.cpp \
../../../../frameworks/components/ui_dialog.cpp \
../../../../frameworks/components/ui_digital_clock.cpp \
../../../../frameworks/components/ui_edit_text.cpp \
../../../../frameworks/components/ui_image_animator.cpp \
../../../../frameworks/components/ui_image_view.cpp \
../../../../frameworks/components/ui_label.cpp \
......@@ -104,6 +105,7 @@ SOURCES += \
../../../../frameworks/components/ui_view.cpp \
../../../../frameworks/components/ui_view_group.cpp \
../../../../frameworks/components/ui_extend_image_view.cpp \
../../../../frameworks/core/input_method_manager.cpp \
../../../../frameworks/dock/focus_manager.cpp \
../../../../frameworks/core/render_manager.cpp \
../../../../frameworks/core/task_manager.cpp \
......@@ -238,6 +240,7 @@ HEADERS += \
../../../../interfaces/innerkits/common/graphic_startup.h \
../../../../interfaces/innerkits/common/image_decode_ability.h \
../../../../interfaces/innerkits/common/input_device_manager.h \
../../../../interfaces/innerkits/common/input_method_manager.h \
../../../../interfaces/innerkits/common/task_manager.h \
../../../../interfaces/innerkits/dock/focus_manager.h \
../../../../interfaces/innerkits/dock/rotate_input_device.h \
......@@ -269,6 +272,7 @@ HEADERS += \
../../../../interfaces/kits/components/ui_circle_progress.h \
../../../../interfaces/kits/components/ui_dialog.h \
../../../../interfaces/kits/components/ui_digital_clock.h \
../../../../interfaces/kits/components/ui_edit_text.h \
../../../../interfaces/kits/components/ui_image_animator.h \
../../../../interfaces/kits/components/ui_image_view.h \
../../../../interfaces/kits/components/ui_label.h \
......
......@@ -58,6 +58,8 @@ SOURCES += \
../../../../test/uitest/test_digital_clock/ui_test_digital_clock.cpp \
../../../../test/uitest/test_draw_line/ui_test_draw_line.cpp \
../../../../test/uitest/test_draw_rect/ui_test_draw_rect.cpp \
../../../../test/uitest/test_edit_text/ui_test_edit_text.cpp \
../../../../test/uitest/test_edit_text/custom_input_method.cpp \
../../../../test/uitest/test_event_injector/ui_test_event_injector.cpp \
../../../../test/uitest/test_focus_manager/ui_test_focus_manager.cpp \
../../../../test/uitest/test_font/ui_test_font.cpp \
......
......@@ -49,6 +49,7 @@ graphic_ui_sources = [
"$GRAPHIC_UI_PATH/frameworks/components/ui_circle_progress.cpp",
"$GRAPHIC_UI_PATH/frameworks/components/ui_dialog.cpp",
"$GRAPHIC_UI_PATH/frameworks/components/ui_digital_clock.cpp",
"$GRAPHIC_UI_PATH/frameworks/components/ui_edit_text.cpp",
"$GRAPHIC_UI_PATH/frameworks/components/ui_image_animator.cpp",
"$GRAPHIC_UI_PATH/frameworks/components/ui_image_view.cpp",
"$GRAPHIC_UI_PATH/frameworks/components/ui_label.cpp",
......@@ -67,6 +68,7 @@ graphic_ui_sources = [
"$GRAPHIC_UI_PATH/frameworks/components/ui_toggle_button.cpp",
"$GRAPHIC_UI_PATH/frameworks/components/ui_view.cpp",
"$GRAPHIC_UI_PATH/frameworks/components/ui_view_group.cpp",
"$GRAPHIC_UI_PATH/frameworks/core/input_method_manager.cpp",
"$GRAPHIC_UI_PATH/frameworks/core/render_manager.cpp",
"$GRAPHIC_UI_PATH/frameworks/core/task_manager.cpp",
"$GRAPHIC_UI_PATH/frameworks/default_resource/check_box_res.cpp",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册