From acabd053557a0be199774e9c484379819c2d7190 Mon Sep 17 00:00:00 2001 From: Ray Lin Date: Thu, 30 Mar 2017 17:38:59 +0800 Subject: [PATCH] Bug 1340483 - Part 4. Update input visibility accordingly. r=heycam MozReview-Commit-ID: 7eBhj7w4qhP --HG-- extra : rebase_source : 3610353fbb4e8d4c1abed57536a0bab3a537b134 --- dom/html/HTMLInputElement.cpp | 15 +++++++++++++-- dom/html/HTMLInputElement.h | 3 ++- dom/html/HTMLTextAreaElement.cpp | 10 ++++++++-- dom/html/HTMLTextAreaElement.h | 3 ++- dom/html/nsITextControlElement.h | 9 +++++++-- dom/html/nsTextEditorState.cpp | 14 ++++++++++---- dom/html/nsTextEditorState.h | 8 ++++++-- layout/forms/nsTextControlFrame.cpp | 26 ++++++++++++++------------ 8 files changed, 62 insertions(+), 26 deletions(-) diff --git a/dom/html/HTMLInputElement.cpp b/dom/html/HTMLInputElement.cpp index b77acc34db92..991cf9de0f8a 100644 --- a/dom/html/HTMLInputElement.cpp +++ b/dom/html/HTMLInputElement.cpp @@ -2873,11 +2873,11 @@ HTMLInputElement::GetPlaceholderNode() } NS_IMETHODIMP_(void) -HTMLInputElement::UpdatePlaceholderVisibility(bool aNotify) +HTMLInputElement::UpdateOverlayTextVisibility(bool aNotify) { nsTextEditorState* state = GetEditorState(); if (state) { - state->UpdatePlaceholderVisibility(aNotify); + state->UpdateOverlayTextVisibility(aNotify); } } @@ -2949,6 +2949,17 @@ HTMLInputElement::IsPreviewEnabled() return mIsPreviewEnabled; } +NS_IMETHODIMP_(bool) +HTMLInputElement::GetPreviewVisibility() +{ + nsTextEditorState* state = GetEditorState(); + if (!state) { + return false; + } + + return state->GetPreviewVisibility(); +} + void HTMLInputElement::GetDisplayFileName(nsAString& aValue) const { diff --git a/dom/html/HTMLInputElement.h b/dom/html/HTMLInputElement.h index 5020cc843a4e..5e7e6a0c2a10 100644 --- a/dom/html/HTMLInputElement.h +++ b/dom/html/HTMLInputElement.h @@ -236,12 +236,13 @@ public: NS_IMETHOD_(Element*) GetPlaceholderNode() override; NS_IMETHOD_(Element*) CreatePreviewNode() override; NS_IMETHOD_(Element*) GetPreviewNode() override; - NS_IMETHOD_(void) UpdatePlaceholderVisibility(bool aNotify) override; + NS_IMETHOD_(void) UpdateOverlayTextVisibility(bool aNotify) override; NS_IMETHOD_(void) SetPreviewValue(const nsAString& aValue) override; NS_IMETHOD_(void) GetPreviewValue(nsAString& aValue) override; NS_IMETHOD_(void) EnablePreview() override; NS_IMETHOD_(bool) IsPreviewEnabled() override; NS_IMETHOD_(bool) GetPlaceholderVisibility() override; + NS_IMETHOD_(bool) GetPreviewVisibility() override; NS_IMETHOD_(void) InitializeKeyboardEventListeners() override; NS_IMETHOD_(void) OnValueChanged(bool aNotify, bool aWasInteractiveUserChange) override; virtual void GetValueFromSetRangeText(nsAString& aValue) override; diff --git a/dom/html/HTMLTextAreaElement.cpp b/dom/html/HTMLTextAreaElement.cpp index 16b0f85ce4d6..538956f44ff0 100644 --- a/dom/html/HTMLTextAreaElement.cpp +++ b/dom/html/HTMLTextAreaElement.cpp @@ -310,9 +310,9 @@ HTMLTextAreaElement::GetPlaceholderNode() } NS_IMETHODIMP_(void) -HTMLTextAreaElement::UpdatePlaceholderVisibility(bool aNotify) +HTMLTextAreaElement::UpdateOverlayTextVisibility(bool aNotify) { - mState.UpdatePlaceholderVisibility(aNotify); + mState.UpdateOverlayTextVisibility(aNotify); } NS_IMETHODIMP_(bool) @@ -364,6 +364,12 @@ HTMLTextAreaElement::IsPreviewEnabled() return mIsPreviewEnabled; } +NS_IMETHODIMP_(bool) +HTMLTextAreaElement::GetPreviewVisibility() +{ + return mState.GetPreviewVisibility(); +} + nsresult HTMLTextAreaElement::SetValueInternal(const nsAString& aValue, uint32_t aFlags) diff --git a/dom/html/HTMLTextAreaElement.h b/dom/html/HTMLTextAreaElement.h index 5c9abfd96072..2e5b2fcda6e7 100644 --- a/dom/html/HTMLTextAreaElement.h +++ b/dom/html/HTMLTextAreaElement.h @@ -105,8 +105,9 @@ public: NS_IMETHOD_(Element*) GetPlaceholderNode() override; NS_IMETHOD_(Element*) CreatePreviewNode() override; NS_IMETHOD_(Element*) GetPreviewNode() override; - NS_IMETHOD_(void) UpdatePlaceholderVisibility(bool aNotify) override; + NS_IMETHOD_(void) UpdateOverlayTextVisibility(bool aNotify) override; NS_IMETHOD_(bool) GetPlaceholderVisibility() override; + NS_IMETHOD_(bool) GetPreviewVisibility() override; NS_IMETHOD_(void) SetPreviewValue(const nsAString& aValue) override; NS_IMETHOD_(void) GetPreviewValue(nsAString& aValue) override; NS_IMETHOD_(void) EnablePreview() override; diff --git a/dom/html/nsITextControlElement.h b/dom/html/nsITextControlElement.h index 08fa65b1f4b6..b548e88ad493 100644 --- a/dom/html/nsITextControlElement.h +++ b/dom/html/nsITextControlElement.h @@ -190,15 +190,20 @@ public: NS_IMETHOD_(void) InitializeKeyboardEventListeners() = 0; /** - * Update the placeholder visibility based on the element's state. + * Update the visibility of both the placholder and preview text based on the element's state. */ - NS_IMETHOD_(void) UpdatePlaceholderVisibility(bool aNotify) = 0; + NS_IMETHOD_(void) UpdateOverlayTextVisibility(bool aNotify) = 0; /** * Returns the current expected placeholder visibility state. */ NS_IMETHOD_(bool) GetPlaceholderVisibility() = 0; + /** + * Returns the current expected preview visibility state. + */ + NS_IMETHOD_(bool) GetPreviewVisibility() = 0; + /** * Callback called whenever the value is changed. */ diff --git a/dom/html/nsTextEditorState.cpp b/dom/html/nsTextEditorState.cpp index 8a9a013517dc..d08e9e2e26bd 100644 --- a/dom/html/nsTextEditorState.cpp +++ b/dom/html/nsTextEditorState.cpp @@ -1067,6 +1067,7 @@ nsTextEditorState::nsTextEditorState(nsITextControlElement* aOwningElement) , mSelectionCached(true) , mSelectionRestoreEagerInit(false) , mPlaceholderVisibility(false) + , mPreviewVisibility(false) , mIsCommittingComposition(false) // When adding more member variable initializations here, add the same // also to ::Construct. @@ -1091,6 +1092,7 @@ nsTextEditorState::Construct(nsITextControlElement* aOwningElement, state->mSelectionCached = true; state->mSelectionRestoreEagerInit = false; state->mPlaceholderVisibility = false; + state->mPreviewVisibility = false; state->mIsCommittingComposition = false; // When adding more member variable initializations here, add the same // also to the constructor. @@ -2700,7 +2702,7 @@ nsTextEditorState::InitializeKeyboardEventListeners() void nsTextEditorState::ValueWasChanged(bool aNotify) { - UpdatePlaceholderVisibility(aNotify); + UpdateOverlayTextVisibility(aNotify); } void @@ -2737,6 +2739,8 @@ nsTextEditorState::SetPreviewText(const nsAString& aValue, bool aNotify) nsContentUtils::RemoveNewlines(previewValue); MOZ_ASSERT(mPreviewDiv->GetFirstChild(), "preview div has no child"); mPreviewDiv->GetFirstChild()->SetText(previewValue, aNotify); + + UpdateOverlayTextVisibility(aNotify); } void @@ -2754,12 +2758,14 @@ nsTextEditorState::GetPreviewText(nsAString& aValue) } void -nsTextEditorState::UpdatePlaceholderVisibility(bool aNotify) +nsTextEditorState::UpdateOverlayTextVisibility(bool aNotify) { - nsAutoString value; + nsAutoString value, previewValue; GetValue(value, true); + GetPreviewText(previewValue); - mPlaceholderVisibility = value.IsEmpty(); + mPreviewVisibility = value.IsEmpty() && !previewValue.IsEmpty(); + mPlaceholderVisibility = value.IsEmpty() && previewValue.IsEmpty(); if (mPlaceholderVisibility && !Preferences::GetBool("dom.placeholder.show_on_focus", true)) { diff --git a/dom/html/nsTextEditorState.h b/dom/html/nsTextEditorState.h index 52a1f22afce3..1e143afe21e7 100644 --- a/dom/html/nsTextEditorState.h +++ b/dom/html/nsTextEditorState.h @@ -218,17 +218,20 @@ public: return mTextCtrlElement->GetRows(); } + void UpdateOverlayTextVisibility(bool aNotify); + // placeholder methods - void UpdatePlaceholderVisibility(bool aNotify); bool GetPlaceholderVisibility() { return mPlaceholderVisibility; } - void UpdatePlaceholderText(bool aNotify); // preview methods void SetPreviewText(const nsAString& aValue, bool aNotify); void GetPreviewText(nsAString& aValue); + bool GetPreviewVisibility() { + return mPreviewVisibility; + } /** * Get the maxlength attribute @@ -450,6 +453,7 @@ private: bool mSelectionCached; // Whether mSelectionProperties is valid mutable bool mSelectionRestoreEagerInit; // Whether we're eager initing because of selection restore bool mPlaceholderVisibility; + bool mPreviewVisibility; bool mIsCommittingComposition; }; diff --git a/layout/forms/nsTextControlFrame.cpp b/layout/forms/nsTextControlFrame.cpp index 499e0fea640b..33e967370c6a 100644 --- a/layout/forms/nsTextControlFrame.cpp +++ b/layout/forms/nsTextControlFrame.cpp @@ -364,7 +364,7 @@ nsTextControlFrame::CreateAnonymousContent(nsTArray& aElements) // For textareas, UpdateValueDisplay doesn't initialize the visibility // status of the placeholder because it returns early, so we have to // do that manually here. - txtCtrl->UpdatePlaceholderVisibility(true); + txtCtrl->UpdateOverlayTextVisibility(true); } } @@ -656,7 +656,7 @@ void nsTextControlFrame::SetFocus(bool aOn, bool aRepaint) // If 'dom.placeholeder.show_on_focus' preference is 'false', focusing or // blurring the frame can have an impact on the placeholder visibility. if (mUsePlaceholder) { - txtCtrl->UpdatePlaceholderVisibility(true); + txtCtrl->UpdateOverlayTextVisibility(true); } if (!aOn) { @@ -1183,7 +1183,7 @@ nsTextControlFrame::SetValueChanged(bool aValueChanged) if (mUsePlaceholder) { AutoWeakFrame weakFrame(this); - txtCtrl->UpdatePlaceholderVisibility(true); + txtCtrl->UpdateOverlayTextVisibility(true); if (!weakFrame.IsAlive()) { return; } @@ -1233,13 +1233,13 @@ nsTextControlFrame::UpdateValueDisplay(bool aNotify, txtCtrl->GetTextEditorValue(value, true); } - // Update the display of the placeholder value if needed. - // We don't need to do this if we're about to initialize the - // editor, since EnsureEditorInitialized takes care of this. - if (mUsePlaceholder && !aBeforeEditorInit) + // Update the display of the placeholder value and preview text if needed. + // We don't need to do this if we're about to initialize the editor, since + // EnsureEditorInitialized takes care of this. + if ((mUsePlaceholder || mUsePreview) && !aBeforeEditorInit) { AutoWeakFrame weakFrame(this); - txtCtrl->UpdatePlaceholderVisibility(aNotify); + txtCtrl->UpdateOverlayTextVisibility(aNotify); NS_ENSURE_STATE(weakFrame.IsAlive()); } @@ -1355,10 +1355,12 @@ nsTextControlFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder, nsDisplayListSet set(content, content, content, content, content, content); while (kid) { - // If the frame is the placeholder frame, we should only show it if the - // placeholder has to be visible. - if (kid->GetContent() != txtCtrl->GetPlaceholderNode() || - txtCtrl->GetPlaceholderVisibility()) { + // If the frame is the placeholder or preview frame, we should only show + // it if it has to be visible. + if (!((kid->GetContent() == txtCtrl->GetPlaceholderNode() && + !txtCtrl->GetPlaceholderVisibility()) || + (kid->GetContent() == txtCtrl->GetPreviewNode() && + !txtCtrl->GetPreviewVisibility()))) { BuildDisplayListForChild(aBuilder, kid, aDirtyRect, set, 0); } kid = kid->GetNextSibling();