Commit 30916668 authored by Allen Bauer's avatar Allen Bauer Committed by Commit Bot

Convert visible() to a property.

Updated usages to a visible changed callback.

Bug: 938501
Change-Id: I01fe4f9f21f3f5b40fedb05edde0331afce97604
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1505848
Commit-Queue: Allen Bauer <kylixrd@chromium.org>
Reviewed-by: default avatarPeter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#653559}
parent 60a9d69e
...@@ -119,7 +119,7 @@ IN_PROC_BROWSER_TEST_F(PresentationReceiverWindowViewBrowserTest, ...@@ -119,7 +119,7 @@ IN_PROC_BROWSER_TEST_F(PresentationReceiverWindowViewBrowserTest,
// It waits for the location bar visibility to change rather than simply using // It waits for the location bar visibility to change rather than simply using
// RunLoop::RunUntilIdle because in Mash, the fullscreen change takes place in // RunLoop::RunUntilIdle because in Mash, the fullscreen change takes place in
// another process. // another process.
class FullscreenWaiter final : public views::ViewObserver { class FullscreenWaiter final {
public: public:
enum class AwaitType { enum class AwaitType {
kOutOfFullscreen, kOutOfFullscreen,
...@@ -132,15 +132,16 @@ IN_PROC_BROWSER_TEST_F(PresentationReceiverWindowViewBrowserTest, ...@@ -132,15 +132,16 @@ IN_PROC_BROWSER_TEST_F(PresentationReceiverWindowViewBrowserTest,
: receiver_view_(receiver_view), : receiver_view_(receiver_view),
await_type_(await_type), await_type_(await_type),
fullscreen_callback_(std::move(fullscreen_callback)) { fullscreen_callback_(std::move(fullscreen_callback)) {
receiver_view_->location_bar_view()->AddObserver(this); auto* location_bar_view = receiver_view_->location_bar_view();
} subscription_ =
~FullscreenWaiter() final { location_bar_view->AddVisibleChangedCallback(base::BindRepeating(
receiver_view_->location_bar_view()->RemoveObserver(this); &FullscreenWaiter::OnViewVisibilityChanged,
base::Unretained(this), base::Unretained(location_bar_view)));
} }
~FullscreenWaiter() = default;
private: private:
void OnViewVisibilityChanged(views::View* observed_view, void OnViewVisibilityChanged(views::View* observed_view) {
views::View* starting_view) override {
bool fullscreen = !observed_view->visible(); bool fullscreen = !observed_view->visible();
EXPECT_EQ(fullscreen, receiver_view_->IsFullscreen()); EXPECT_EQ(fullscreen, receiver_view_->IsFullscreen());
if (fullscreen == (await_type_ == AwaitType::kIntoFullscreen)) if (fullscreen == (await_type_ == AwaitType::kIntoFullscreen))
...@@ -148,6 +149,7 @@ IN_PROC_BROWSER_TEST_F(PresentationReceiverWindowViewBrowserTest, ...@@ -148,6 +149,7 @@ IN_PROC_BROWSER_TEST_F(PresentationReceiverWindowViewBrowserTest,
} }
PresentationReceiverWindowView* const receiver_view_; PresentationReceiverWindowView* const receiver_view_;
views::PropertyChangedSubscription subscription_;
const AwaitType await_type_; const AwaitType await_type_;
base::OnceClosure fullscreen_callback_; base::OnceClosure fullscreen_callback_;
......
...@@ -424,12 +424,7 @@ void View::SetVisible(bool visible) { ...@@ -424,12 +424,7 @@ void View::SetVisible(bool visible) {
layout_manager->ViewVisibilitySet(parent_, this, visible); layout_manager->ViewVisibilitySet(parent_, this, visible);
} }
if (visible != visible_) { if (visible_ != visible) {
// If the View is currently visible, schedule paint to refresh parent.
// TODO(beng): not sure we should be doing this if we have a layer.
if (visible_)
SchedulePaint();
visible_ = visible; visible_ = visible;
AdvanceFocusIfNecessary(); AdvanceFocusIfNecessary();
...@@ -444,21 +439,25 @@ void View::SetVisible(bool visible) { ...@@ -444,21 +439,25 @@ void View::SetVisible(bool visible) {
PropagateVisibilityNotifications(this, visible_); PropagateVisibilityNotifications(this, visible_);
UpdateLayerVisibility(); UpdateLayerVisibility();
// If we are newly visible, schedule paint. // Notify all other subscriptions of the change.
if (visible_) OnPropertyChanged(&visible_, kPropertyEffectsLayout);
SchedulePaint();
} }
} }
PropertyChangedSubscription View::AddVisibleChangedCallback(
PropertyChangedCallback callback) {
return AddPropertyChangedCallback(&visible_, std::move(callback));
}
bool View::IsDrawn() const { bool View::IsDrawn() const {
return visible_ && parent_ ? parent_->IsDrawn() : false; return visible_ && parent_ ? parent_->IsDrawn() : false;
} }
void View::SetEnabled(bool is_enabled) { void View::SetEnabled(bool enabled) {
if (enabled_ == is_enabled) if (enabled_ == enabled)
return; return;
enabled_ = is_enabled; enabled_ = enabled;
AdvanceFocusIfNecessary(); AdvanceFocusIfNecessary();
OnPropertyChanged(&enabled_, kPropertyEffectsPaint); OnPropertyChanged(&enabled_, kPropertyEffectsPaint);
} }
...@@ -968,7 +967,7 @@ View* View::GetTooltipHandlerForPoint(const gfx::Point& point) { ...@@ -968,7 +967,7 @@ View* View::GetTooltipHandlerForPoint(const gfx::Point& point) {
View::Views children = GetChildrenInZOrder(); View::Views children = GetChildrenInZOrder();
DCHECK_EQ(children_.size(), children.size()); DCHECK_EQ(children_.size(), children.size());
for (auto* child : base::Reversed(children)) { for (auto* child : base::Reversed(children)) {
if (!child->visible()) if (!child->GetVisible())
continue; continue;
gfx::Point point_in_child_coords(point); gfx::Point point_in_child_coords(point);
...@@ -1610,7 +1609,7 @@ void View::MoveLayerToParent(ui::Layer* parent_layer, ...@@ -1610,7 +1609,7 @@ void View::MoveLayerToParent(ui::Layer* parent_layer,
void View::UpdateLayerVisibility() { void View::UpdateLayerVisibility() {
bool visible = visible_; bool visible = visible_;
for (const View* v = parent_; visible && v && !v->layer(); v = v->parent_) for (const View* v = parent_; visible && v && !v->layer(); v = v->parent_)
visible = v->visible(); visible = v->GetVisible();
UpdateChildLayerVisibility(visible); UpdateChildLayerVisibility(visible);
} }
...@@ -2066,7 +2065,7 @@ void View::AddChildViewAtImpl(View* view, int index) { ...@@ -2066,7 +2065,7 @@ void View::AddChildViewAtImpl(View* view, int index) {
if (widget) { if (widget) {
RegisterChildrenForVisibleBoundsNotification(view); RegisterChildrenForVisibleBoundsNotification(view);
if (view->visible()) if (view->GetVisible())
view->SchedulePaint(); view->SchedulePaint();
} }
...@@ -2102,7 +2101,7 @@ void View::DoRemoveChildView(View* view, ...@@ -2102,7 +2101,7 @@ void View::DoRemoveChildView(View* view,
bool is_removed_from_widget = false; bool is_removed_from_widget = false;
if (widget) { if (widget) {
UnregisterChildrenForVisibleBoundsNotification(view); UnregisterChildrenForVisibleBoundsNotification(view);
if (view->visible()) if (view->GetVisible())
view->SchedulePaint(); view->SchedulePaint();
is_removed_from_widget = !new_parent || new_parent->GetWidget() != widget; is_removed_from_widget = !new_parent || new_parent->GetWidget() != widget;
...@@ -2707,6 +2706,7 @@ bool View::DoDrag(const ui::LocatedEvent& event, ...@@ -2707,6 +2706,7 @@ bool View::DoDrag(const ui::LocatedEvent& event,
// declaration for View. // declaration for View.
BEGIN_METADATA(View) BEGIN_METADATA(View)
ADD_PROPERTY_METADATA(View, bool, Enabled) ADD_PROPERTY_METADATA(View, bool, Enabled)
ADD_PROPERTY_METADATA(View, bool, Visible)
END_METADATA() END_METADATA()
} // namespace views } // namespace views
...@@ -550,12 +550,23 @@ class VIEWS_EXPORT View : public ui::LayerDelegate, ...@@ -550,12 +550,23 @@ class VIEWS_EXPORT View : public ui::LayerDelegate,
// GetPreferredSize().height(). // GetPreferredSize().height().
virtual int GetHeightForWidth(int w) const; virtual int GetHeightForWidth(int w) const;
// The |Visible| property. See comment above for instructions on declaring and
// implementing a property.
//
// Sets whether this view is visible. Painting is scheduled as needed. Also, // Sets whether this view is visible. Painting is scheduled as needed. Also,
// clears focus if the focused view or one of its ancestors is set to be // clears focus if the focused view or one of its ancestors is set to be
// hidden. // hidden.
virtual void SetVisible(bool visible); virtual void SetVisible(bool visible);
// Return whether a view is visible.
bool GetVisible() const { return visible_; }
// Adds a callback subscription associated with the above Visible property.
// The callback will be invoked whenever the Visible property changes.
PropertyChangedSubscription AddVisibleChangedCallback(
PropertyChangedCallback callback) WARN_UNUSED_RESULT;
// Return whether a view is visible // NOTE: Deprecated. Please use GetVisible() which is the getter for the
// |Visible| property.
bool visible() const { return visible_; } bool visible() const { return visible_; }
// Returns true if this view is drawn on screen. // Returns true if this view is drawn on screen.
...@@ -567,7 +578,7 @@ class VIEWS_EXPORT View : public ui::LayerDelegate, ...@@ -567,7 +578,7 @@ class VIEWS_EXPORT View : public ui::LayerDelegate,
// Set whether this view is enabled. A disabled view does not receive keyboard // Set whether this view is enabled. A disabled view does not receive keyboard
// or mouse inputs. If |enabled| differs from the current value, SchedulePaint // or mouse inputs. If |enabled| differs from the current value, SchedulePaint
// is invoked. Also, clears focus if the focused view is disabled. // is invoked. Also, clears focus if the focused view is disabled.
void SetEnabled(bool is_enabled); void SetEnabled(bool enabled);
// Returns whether the view is enabled. // Returns whether the view is enabled.
bool GetEnabled() const { return enabled_; } bool GetEnabled() const { return enabled_; }
......
...@@ -5010,6 +5010,17 @@ TEST_F(ViewTest, TestEnabledChangedCallback) { ...@@ -5010,6 +5010,17 @@ TEST_F(ViewTest, TestEnabledChangedCallback) {
EXPECT_FALSE(test_view.GetEnabled()); EXPECT_FALSE(test_view.GetEnabled());
} }
TEST_F(ViewTest, TestVisibleChangedCallback) {
View test_view;
bool visibility_changed = false;
auto subscription = test_view.AddVisibleChangedCallback(base::BindRepeating(
[](bool* visibility_changed) { *visibility_changed = true; },
&visibility_changed));
test_view.SetVisible(false);
EXPECT_TRUE(visibility_changed);
EXPECT_FALSE(test_view.GetVisible());
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Observer tests. // Observer tests.
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment