Commit a701a64a authored by Avi Drissman's avatar Avi Drissman Committed by Commit Bot

Close Views menus when native menus are opened.

BUG=859779

Change-Id: I053e26086599ffb68f6379844bb20bf894e613e1
Reviewed-on: https://chromium-review.googlesource.com/1155663
Commit-Queue: Avi Drissman <avi@chromium.org>
Reviewed-by: default avatarTrent Apted <tapted@chromium.org>
Cr-Commit-Position: refs/heads/master@{#579881}
parent fe6f9c93
...@@ -121,6 +121,7 @@ jumbo_component("views") { ...@@ -121,6 +121,7 @@ jumbo_component("views") {
"controls/link.h", "controls/link.h",
"controls/link_listener.h", "controls/link_listener.h",
"controls/menu/menu_closure_animation_mac.h", "controls/menu/menu_closure_animation_mac.h",
"controls/menu/menu_cocoa_watcher_mac.h",
"controls/menu/menu_config.h", "controls/menu/menu_config.h",
"controls/menu/menu_controller.h", "controls/menu/menu_controller.h",
"controls/menu/menu_controller_delegate.h", "controls/menu/menu_controller_delegate.h",
...@@ -320,6 +321,7 @@ jumbo_component("views") { ...@@ -320,6 +321,7 @@ jumbo_component("views") {
"controls/link.cc", "controls/link.cc",
"controls/menu/display_change_listener_mac.cc", "controls/menu/display_change_listener_mac.cc",
"controls/menu/menu_closure_animation_mac.mm", "controls/menu/menu_closure_animation_mac.mm",
"controls/menu/menu_cocoa_watcher_mac.mm",
"controls/menu/menu_config.cc", "controls/menu/menu_config.cc",
"controls/menu/menu_config_chromeos.cc", "controls/menu/menu_config_chromeos.cc",
"controls/menu/menu_config_linux.cc", "controls/menu/menu_config_linux.cc",
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_VIEWS_CONTROLS_MENU_MENU_COCOA_WATCHER_MAC_H_
#define UI_VIEWS_CONTROLS_MENU_MENU_COCOA_WATCHER_MAC_H_
#include <objc/objc.h>
#include "base/callback.h"
#include "base/macros.h"
#include "ui/views/views_export.h"
namespace views {
// This class executes a callback when a native menu begins tracking. With
// native menus, each one automatically closes when a new one begins tracking.
// This allows Views menus to tie into this behavior.
class VIEWS_EXPORT MenuCocoaWatcherMac {
public:
explicit MenuCocoaWatcherMac(base::OnceClosure callback);
~MenuCocoaWatcherMac();
private:
// The closure to call when the notification comes in.
base::OnceClosure callback_;
// The token representing the notification observer.
id observer_token_;
DISALLOW_COPY_AND_ASSIGN(MenuCocoaWatcherMac);
};
} // namespace views
#endif // UI_VIEWS_CONTROLS_MENU_MENU_COCOA_WATCHER_MAC_H_
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/controls/menu/menu_cocoa_watcher_mac.h"
#import <Cocoa/Cocoa.h>
#import <utility>
namespace views {
MenuCocoaWatcherMac::MenuCocoaWatcherMac(base::OnceClosure callback)
: callback_(std::move(callback)) {
observer_token_ = [[NSNotificationCenter defaultCenter]
addObserverForName:NSMenuDidBeginTrackingNotification
object:[NSApp mainMenu]
queue:nil
usingBlock:^(NSNotification* notification) {
std::move(this->callback_).Run();
}];
}
MenuCocoaWatcherMac::~MenuCocoaWatcherMac() {
[[NSNotificationCenter defaultCenter] removeObserver:observer_token_];
}
} // namespace views
...@@ -464,10 +464,16 @@ void MenuController::Run(Widget* parent, ...@@ -464,10 +464,16 @@ void MenuController::Run(Widget* parent,
#if defined(USE_AURA) #if defined(USE_AURA)
// Only create a MenuPreTargetHandler for non-nested menus. Nested menus // Only create a MenuPreTargetHandler for non-nested menus. Nested menus
// will use the existing one. // will use the existing one.
menu_pre_target_handler_.reset(new MenuPreTargetHandler(this, owner_)); menu_pre_target_handler_ =
std::make_unique<MenuPreTargetHandler>(this, owner_);
#endif #endif
} }
#if defined(OS_MACOSX)
menu_cocoa_watcher_ = std::make_unique<MenuCocoaWatcherMac>(
base::BindOnce(&MenuController::CancelAll, base::Unretained(this)));
#endif
// Reset current state. // Reset current state.
pending_state_ = State(); pending_state_ = State();
state_ = State(); state_ = State();
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
#include "ui/views/controls/menu/menu_closure_animation_mac.h" #include "ui/views/controls/menu/menu_closure_animation_mac.h"
#include "ui/views/controls/menu/menu_cocoa_watcher_mac.h"
#endif #endif
namespace ui { namespace ui {
...@@ -727,6 +728,7 @@ class VIEWS_EXPORT MenuController ...@@ -727,6 +728,7 @@ class VIEWS_EXPORT MenuController
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
std::unique_ptr<MenuClosureAnimationMac> menu_closure_animation_; std::unique_ptr<MenuClosureAnimationMac> menu_closure_animation_;
std::unique_ptr<MenuCocoaWatcherMac> menu_cocoa_watcher_;
#endif #endif
#if defined(USE_AURA) #if defined(USE_AURA)
......
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