Skip to content

refactor CCIMEDispatcher #19688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions cocos/base/CCIMEDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ extern const std::string CC_DLL STD_STRING_EMPTY;
/**
* Keyboard notification event type.
*/
typedef struct
struct IMEKeyboardNotificationInfo
{
Rect begin; // the soft keyboard rectangle when animation begins
Rect end; // the soft keyboard rectangle when animation ends
float duration; // the soft keyboard animation duration
} IMEKeyboardNotificationInfo;
};

/**
*@brief Input method editor delegate.
Expand All @@ -60,21 +60,21 @@ class CC_DLL IMEDelegate
{
public:
/**
* Default constructor.
* Destructor.
* @js NA
* @lua NA
*/
virtual ~IMEDelegate();

/**
* Default destructor.
* Attach the delegate to IME. Return true if succesful.
* @js NA
* @lua NA
*/
virtual bool attachWithIME();

/**
* Determine whether the IME is detached or not.
* Detach the delegate from IME. Return true if succesful.
* @js NA
* @lua NA
*/
Expand Down Expand Up @@ -166,6 +166,7 @@ class CC_DLL IMEDelegate

protected:
/**
* Default constructor.
* @js NA
* @lua NA
*/
Expand Down
62 changes: 10 additions & 52 deletions cocos/base/CCIMEDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,32 +63,19 @@ typedef std::list< IMEDelegate * >::iterator DelegateIter;
// Delegate List manage class
//////////////////////////////////////////////////////////////////////////

class IMEDispatcher::Impl
struct IMEDispatcher::Impl
{
public:
Impl()
Impl() : _delegateWithIme(nullptr)
{
}

~Impl()
{

}

void init()
{
_delegateWithIme = 0;
}

DelegateIter findDelegate(IMEDelegate* delegate)
{
DelegateIter end = _delegateList.end();
for (DelegateIter iter = _delegateList.begin(); iter != end; ++iter)
{
if (delegate == *iter)
{
return iter;
}
}
return end;
}
Expand All @@ -104,7 +91,6 @@ class IMEDispatcher::Impl
IMEDispatcher::IMEDispatcher()
: _impl(new IMEDispatcher::Impl)
{
_impl->init();
}

IMEDispatcher::~IMEDispatcher()
Expand Down Expand Up @@ -137,11 +123,8 @@ bool IMEDispatcher::attachDelegateWithIME(IMEDelegate * delegate)
{
CC_BREAK_IF(! _impl || ! delegate);

DelegateIter end = _impl->_delegateList.end();
DelegateIter iter = _impl->findDelegate(delegate);

// if pDelegate is not in delegate list, return
CC_BREAK_IF(end == iter);
CC_BREAK_IF(_impl->findDelegate(delegate) == _impl->_delegateList.end());

if (_impl->_delegateWithIme)
{
Expand All @@ -155,10 +138,9 @@ bool IMEDispatcher::attachDelegateWithIME(IMEDelegate * delegate)

// detach first
IMEDelegate * oldDelegate = _impl->_delegateWithIme;
_impl->_delegateWithIme = 0;
oldDelegate->didDetachWithIME();

_impl->_delegateWithIme = *iter;
_impl->_delegateWithIme = delegate;
delegate->didAttachWithIME();
}
ret = true;
Expand All @@ -168,7 +150,7 @@ bool IMEDispatcher::attachDelegateWithIME(IMEDelegate * delegate)
// delegate hasn't attached to IME yet
CC_BREAK_IF(! delegate->canAttachWithIME());

_impl->_delegateWithIme = *iter;
_impl->_delegateWithIme = delegate;
delegate->didAttachWithIME();
ret = true;
} while (0);
Expand Down Expand Up @@ -204,8 +186,6 @@ void IMEDispatcher::removeDelegate(IMEDelegate* delegate)
DelegateIter end = _impl->_delegateList.end();
CC_BREAK_IF(end == iter);

if (_impl->_delegateWithIme)

if (*iter == _impl->_delegateWithIme)
{
_impl->_delegateWithIme = 0;
Expand Down Expand Up @@ -268,9 +248,7 @@ const std::string& IMEDispatcher::getContentText()

bool IMEDispatcher::isAnyDelegateAttachedWithIME() const
{
if (!_impl)
return false;
return _impl->_delegateWithIme != nullptr;
return _impl ? _impl->_delegateWithIme != nullptr : false;
}

//////////////////////////////////////////////////////////////////////////
Expand All @@ -281,15 +259,10 @@ void IMEDispatcher::dispatchKeyboardWillShow(IMEKeyboardNotificationInfo& info)
{
if (_impl)
{
IMEDelegate * delegate = nullptr;
DelegateIter last = _impl->_delegateList.end();
for (DelegateIter first = _impl->_delegateList.begin(); first != last; ++first)
for (IMEDelegate *delegate : _impl->_delegateList)
{
delegate = *(first);
if (delegate)
{
delegate->keyboardWillShow(info);
}
}
}
}
Expand All @@ -298,15 +271,10 @@ void IMEDispatcher::dispatchKeyboardDidShow(IMEKeyboardNotificationInfo& info)
{
if (_impl)
{
IMEDelegate * delegate = nullptr;
DelegateIter last = _impl->_delegateList.end();
for (DelegateIter first = _impl->_delegateList.begin(); first != last; ++first)
for (IMEDelegate *delegate : _impl->_delegateList)
{
delegate = *(first);
if (delegate)
{
delegate->keyboardDidShow(info);
}
}
}
}
Expand All @@ -315,15 +283,10 @@ void IMEDispatcher::dispatchKeyboardWillHide(IMEKeyboardNotificationInfo& info)
{
if (_impl)
{
IMEDelegate * delegate = nullptr;
DelegateIter last = _impl->_delegateList.end();
for (DelegateIter first = _impl->_delegateList.begin(); first != last; ++first)
for (IMEDelegate *delegate : _impl->_delegateList)
{
delegate = *(first);
if (delegate)
{
delegate->keyboardWillHide(info);
}
}
}
}
Expand All @@ -332,15 +295,10 @@ void IMEDispatcher::dispatchKeyboardDidHide(IMEKeyboardNotificationInfo& info)
{
if (_impl)
{
IMEDelegate * delegate = nullptr;
DelegateIter last = _impl->_delegateList.end();
for (DelegateIter first = _impl->_delegateList.begin(); first != last; ++first)
for (IMEDelegate *delegate : _impl->_delegateList)
{
delegate = *(first);
if (delegate)
{
delegate->keyboardDidHide(info);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion cocos/base/CCIMEDispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class CC_DLL IMEDispatcher
private:
IMEDispatcher();

class Impl;
struct Impl;
Impl * _impl;
};

Expand Down