Commit 726995ce authored by David Black's avatar David Black Committed by Commit Bot

Add subscription support to MenuItemView selected state.

MenuItemView supports being a thin wrapper around a sole child view but
the child view isn't notified of changes to the parent's selected state.
This means the child view doesn't know to update itself if it has some
state specific characteristics.

This CL makes selected state a subscribe-able property so that changes
can be observed and reacted to.

Bug: 1112888
Change-Id: I305eb1dd8cdb735d1bb14bd0f7c86ce8193b3670
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2393261Reviewed-by: default avatarMichael Wasserman <msw@chromium.org>
Commit-Queue: David Black <dmblack@google.com>
Cr-Commit-Position: refs/heads/master@{#804526}
parent 7ea61df9
......@@ -448,7 +448,12 @@ void MenuItemView::SetMinorIcon(const ui::ThemedVectorIcon& minor_icon) {
void MenuItemView::SetSelected(bool selected) {
selected_ = selected;
SchedulePaint();
OnPropertyChanged(&selected_, kPropertyEffectsPaint);
}
PropertyChangedSubscription MenuItemView::AddSelectedChangedCallback(
PropertyChangedCallback callback) {
return AddPropertyChangedCallback(&selected_, std::move(callback));
}
void MenuItemView::SetSelectionOfActionableSubmenu(
......
......@@ -237,6 +237,11 @@ class VIEWS_EXPORT MenuItemView : public View {
// Returns true if the item is selected.
bool IsSelected() const { return selected_; }
// Adds a callback subscription associated with the above selected property.
// The callback will be invoked whenever the selected property changes.
PropertyChangedSubscription AddSelectedChangedCallback(
PropertyChangedCallback callback) WARN_UNUSED_RESULT;
// Sets whether the submenu area of an ACTIONABLE_SUBMENU is selected.
void SetSelectionOfActionableSubmenu(
bool submenu_area_of_actionable_submenu_selected);
......
......@@ -9,6 +9,7 @@
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/compositor/canvas_painter.h"
......@@ -179,6 +180,31 @@ TEST_F(MenuItemViewUnitTest, UseMnemonicOnPlatform) {
}
}
TEST_F(MenuItemViewUnitTest, NotifiesSelectedChanged) {
views::TestMenuItemView root_menu;
// Append a MenuItemView.
views::MenuItemView* menu_item_view =
root_menu.AppendMenuItem(1, base::ASCIIToUTF16("item"));
// Verify initial selected state.
bool is_selected = menu_item_view->IsSelected();
EXPECT_FALSE(is_selected);
// Subscribe to be notified of changes to selected state.
auto subscription =
menu_item_view->AddSelectedChangedCallback(base::BindLambdaForTesting(
[&]() { is_selected = menu_item_view->IsSelected(); }));
// Verify we are notified when the MenuItemView becomes selected.
menu_item_view->SetSelected(true);
EXPECT_TRUE(is_selected);
// Verify we are notified when the MenuItemView becomes deselected.
menu_item_view->SetSelected(false);
EXPECT_FALSE(is_selected);
}
class MenuItemViewLayoutTest : public ViewsTestBase {
public:
MenuItemViewLayoutTest() : test_item_(root_menu_.AppendMenuItem(1)) {}
......
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