Bug 1166436 part.2 mozilla::ContentCache should store a selection range and TabParent should use it r=m_kato

This commit is contained in:
Masayuki Nakano 2015-06-05 18:28:19 +09:00
Родитель 31dd7f05de
Коммит 28550f1773
4 изменённых файлов: 62 добавлений и 30 удалений

Просмотреть файл

@ -261,8 +261,6 @@ TabParent::TabParent(nsIContentParent* aManager,
uint32_t aChromeFlags)
: TabContext(aContext)
, mFrameElement(nullptr)
, mIMESelectionAnchor(0)
, mIMESelectionFocus(0)
, mWritingMode()
, mIMEComposing(false)
, mIMECompositionEnding(false)
@ -1898,8 +1896,7 @@ TabParent::RecvNotifyIMEFocus(const bool& aFocus,
}
mIMETabParent = aFocus ? this : nullptr;
mIMESelectionAnchor = 0;
mIMESelectionFocus = 0;
mContentCache.SetSelection(0);
widget->NotifyIME(IMENotification(aFocus ? NOTIFY_IME_OF_FOCUS :
NOTIFY_IME_OF_BLUR));
@ -1970,8 +1967,7 @@ TabParent::RecvNotifyIMESelection(const uint32_t& aAnchor,
if (!widget)
return true;
mIMESelectionAnchor = aAnchor;
mIMESelectionFocus = aFocus;
mContentCache.SetSelection(aAnchor, aFocus);
mWritingMode = aWritingMode;
const nsIMEUpdatePreference updatePreference =
widget->GetIMEUpdatePreference();
@ -1979,14 +1975,10 @@ TabParent::RecvNotifyIMESelection(const uint32_t& aAnchor,
(updatePreference.WantChangesCausedByComposition() ||
!aCausedByComposition)) {
IMENotification notification(NOTIFY_IME_OF_SELECTION_CHANGE);
notification.mSelectionChangeData.mOffset =
std::min(mIMESelectionAnchor, mIMESelectionFocus);
notification.mSelectionChangeData.mLength =
mIMESelectionAnchor > mIMESelectionFocus ?
mIMESelectionAnchor - mIMESelectionFocus :
mIMESelectionFocus - mIMESelectionAnchor;
notification.mSelectionChangeData.mOffset = mContentCache.SelectionStart();
notification.mSelectionChangeData.mLength = mContentCache.SelectionLength();
notification.mSelectionChangeData.mReversed =
mIMESelectionFocus < mIMESelectionAnchor;
mContentCache.SelectionReversed();
notification.mSelectionChangeData.SetWritingMode(mWritingMode);
notification.mSelectionChangeData.mCausedByComposition =
aCausedByComposition;
@ -2225,22 +2217,18 @@ TabParent::HandleQueryContentEvent(WidgetQueryContentEvent& aEvent)
{
case NS_QUERY_SELECTED_TEXT:
{
aEvent.mReply.mOffset = std::min(mIMESelectionAnchor, mIMESelectionFocus);
if (mIMESelectionAnchor == mIMESelectionFocus) {
aEvent.mReply.mOffset = mContentCache.SelectionStart();
if (mContentCache.SelectionCollapsed()) {
aEvent.mReply.mString.Truncate(0);
} else {
if (mIMESelectionAnchor > mContentCache.TextLength() ||
mIMESelectionFocus > mContentCache.TextLength()) {
if (NS_WARN_IF(mContentCache.SelectionEndIsGraterThanTextLength())) {
break;
}
uint32_t selLen = mIMESelectionAnchor > mIMESelectionFocus ?
mIMESelectionAnchor - mIMESelectionFocus :
mIMESelectionFocus - mIMESelectionAnchor;
aEvent.mReply.mString = Substring(mContentCache.Text(),
aEvent.mReply.mOffset,
selLen);
mContentCache.SelectionLength());
}
aEvent.mReply.mReversed = mIMESelectionFocus < mIMESelectionAnchor;
aEvent.mReply.mReversed = mContentCache.SelectionReversed();
aEvent.mReply.mHasSelection = true;
aEvent.mReply.mWritingMode = mWritingMode;
aEvent.mSucceeded = true;
@ -2328,7 +2316,7 @@ TabParent::SendCompositionEvent(WidgetCompositionEvent& event)
}
mIMEComposing = !event.CausesDOMCompositionEndEvent();
mIMECompositionStart = std::min(mIMESelectionAnchor, mIMESelectionFocus);
mIMECompositionStart = mContentCache.SelectionStart();
if (mIMECompositionEnding) {
mIMEEventCountAfterEnding++;
return true;
@ -2356,10 +2344,9 @@ TabParent::SendCompositionChangeEvent(WidgetCompositionEvent& event)
// We must be able to simulate the selection because
// we might not receive selection updates in time
if (!mIMEComposing) {
mIMECompositionStart = std::min(mIMESelectionAnchor, mIMESelectionFocus);
mIMECompositionStart = mContentCache.SelectionStart();
}
mIMESelectionAnchor = mIMESelectionFocus =
mIMECompositionStart + event.mData.Length();
mContentCache.SetSelection(mIMECompositionStart + event.mData.Length());
mIMEComposing = !event.CausesDOMCompositionEndEvent();
return PBrowserParent::SendCompositionEvent(event);
@ -2371,8 +2358,9 @@ TabParent::SendSelectionEvent(WidgetSelectionEvent& event)
if (mIsDestroyed) {
return false;
}
mIMESelectionAnchor = event.mOffset + (event.mReversed ? event.mLength : 0);
mIMESelectionFocus = event.mOffset + (!event.mReversed ? event.mLength : 0);
mContentCache.SetSelection(
event.mOffset + (event.mReversed ? event.mLength : 0),
event.mOffset + (!event.mReversed ? event.mLength : 0));
return PBrowserParent::SendSelectionEvent(event);
}

Просмотреть файл

@ -480,8 +480,6 @@ protected:
// IME
static TabParent *mIMETabParent;
ContentCache mContentCache;
uint32_t mIMESelectionAnchor;
uint32_t mIMESelectionFocus;
mozilla::WritingMode mWritingMode;
bool mIMEComposing;
bool mIMECompositionEnding;

Просмотреть файл

@ -21,4 +21,11 @@ ContentCache::SetText(const nsAString& aText)
mText = aText;
}
void
ContentCache::SetSelection(uint32_t aAnchorOffset, uint32_t aFocusOffset)
{
mSelection.mAnchor = aAnchorOffset;
mSelection.mFocus = aFocusOffset;
}
} // namespace mozilla

Просмотреть файл

@ -29,9 +29,48 @@ public:
const nsString& Text() const { return mText; }
uint32_t TextLength() const { return mText.Length(); }
void SetSelection(uint32_t aCaretOffset)
{
SetSelection(aCaretOffset, aCaretOffset);
}
void SetSelection(uint32_t aAnchorOffset, uint32_t aFocusOffset);
bool SelectionCollapsed() const { return mSelection.Collapsed(); }
bool SelectionReversed() const { return mSelection.Reversed(); }
bool SelectionEndIsGraterThanTextLength() const
{
return SelectionEnd() > TextLength();
}
uint32_t SelectionAnchor() const { return mSelection.mAnchor; }
uint32_t SelectionFocus() const { return mSelection.mFocus; }
uint32_t SelectionStart() const { return mSelection.StartOffset(); }
uint32_t SelectionEnd() const { return mSelection.EndOffset(); }
uint32_t SelectionLength() const { return mSelection.Length(); }
private:
// Whole text in the target
nsString mText;
struct Selection final
{
// Following values are offset in "flat text".
uint32_t mAnchor;
uint32_t mFocus;
Selection()
: mAnchor(0)
, mFocus(0)
{
}
bool Collapsed() const { return mFocus == mAnchor; }
bool Reversed() const { return mFocus < mAnchor; }
uint32_t StartOffset() const { return Reversed() ? mFocus : mAnchor; }
uint32_t EndOffset() const { return Reversed() ? mAnchor : mFocus; }
uint32_t Length() const
{
return Reversed() ? mAnchor - mFocus : mFocus - mAnchor;
}
} mSelection;
};
} // namespace mozilla