refactor: remove accessible_pane_view.patch (#29262)
This commit is contained in:
		
					parent
					
						
							
								4e2934a872
							
						
					
				
			
			
				commit
				
					
						259bf8c4f7
					
				
			
		
					 5 changed files with 63 additions and 217 deletions
				
			
		|  | @ -65,7 +65,6 @@ ignore_rc_check.patch | |||
| remove_usage_of_incognito_apis_in_the_spellchecker.patch | ||||
| chore_use_electron_resources_not_chrome_for_spellchecker.patch | ||||
| feat_allow_disabling_blink_scheduler_throttling_per_renderview.patch | ||||
| accessible_pane_view.patch | ||||
| hack_plugin_response_interceptor_to_point_to_electron.patch | ||||
| feat_add_support_for_overriding_the_base_spellchecker_download_url.patch | ||||
| feat_enable_offscreen_rendering_with_viz_compositor.patch | ||||
|  |  | |||
|  | @ -1,32 +0,0 @@ | |||
| From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 | ||||
| From: Cheng Zhao <zcbenz@gmail.com> | ||||
| Date: Thu, 4 Oct 2018 14:57:02 -0700 | ||||
| Subject: fix: add back virtual methods in AccessiblePaneView | ||||
| 
 | ||||
| Mark SetPaneFocus and RemovePaneFocus as virtual in AccessiblePaneView, as we | ||||
| need to override them in MenuBar. | ||||
| 
 | ||||
| Pending upstream patch: https://crrev.com/c/1959189 | ||||
| 
 | ||||
| diff --git a/ui/views/accessible_pane_view.h b/ui/views/accessible_pane_view.h
 | ||||
| index 813fd13860a863cd1e6e5bfec38d15f798418673..990c905e8f19dd015a625010ea30adfcb6f51ea6 100644
 | ||||
| --- a/ui/views/accessible_pane_view.h
 | ||||
| +++ b/ui/views/accessible_pane_view.h
 | ||||
| @@ -35,7 +35,7 @@ class VIEWS_EXPORT AccessiblePaneView : public View,
 | ||||
|    // If |initial_focus| is not NULL, that control will get | ||||
|    // the initial focus, if it's enabled and focusable. Returns true if | ||||
|    // the pane was able to receive focus. | ||||
| -  bool SetPaneFocus(View* initial_focus);
 | ||||
| +  virtual bool SetPaneFocus(View* initial_focus);
 | ||||
|   | ||||
|    bool pane_has_focus() const { return pane_has_focus_; } | ||||
|   | ||||
| @@ -83,7 +83,7 @@ class VIEWS_EXPORT AccessiblePaneView : public View,
 | ||||
|    bool ContainsForFocusSearch(View* root, const View* v); | ||||
|   | ||||
|    // Remove pane focus. | ||||
| -  void RemovePaneFocus();
 | ||||
| +  virtual void RemovePaneFocus();
 | ||||
|   | ||||
|    View* GetFirstFocusableChild(); | ||||
|    View* GetLastFocusableChild(); | ||||
|  | @ -5,17 +5,12 @@ | |||
| #include "shell/browser/ui/views/menu_bar.h" | ||||
| 
 | ||||
| #include <memory> | ||||
| #include <set> | ||||
| #include <sstream> | ||||
| 
 | ||||
| #include "shell/browser/native_window.h" | ||||
| #include "shell/browser/ui/views/submenu_button.h" | ||||
| #include "shell/common/keyboard_util.h" | ||||
| #include "ui/aura/window.h" | ||||
| #include "ui/base/models/menu_model.h" | ||||
| #include "ui/native_theme/common_theme.h" | ||||
| #include "ui/views/background.h" | ||||
| #include "ui/views/layout/box_layout.h" | ||||
| #include "ui/views/widget/widget.h" | ||||
| 
 | ||||
| #if defined(OS_LINUX) | ||||
| #include "ui/gtk/gtk_util.h" | ||||
|  | @ -36,36 +31,18 @@ const SkColor kDefaultColor = SkColorSetARGB(255, 233, 233, 233); | |||
| 
 | ||||
| const char MenuBar::kViewClassName[] = "ElectronMenuBar"; | ||||
| 
 | ||||
| MenuBarColorUpdater::MenuBarColorUpdater(MenuBar* menu_bar) | ||||
|     : menu_bar_(menu_bar) {} | ||||
| 
 | ||||
| MenuBarColorUpdater::~MenuBarColorUpdater() = default; | ||||
| 
 | ||||
| void MenuBarColorUpdater::OnDidChangeFocus(views::View* focused_before, | ||||
|                                            views::View* focused_now) { | ||||
|   if (menu_bar_) { | ||||
|     // if we've changed window focus, update menu bar colors
 | ||||
|     const auto had_focus = menu_bar_->has_focus_; | ||||
|     menu_bar_->has_focus_ = focused_now != nullptr; | ||||
|     if (menu_bar_->has_focus_ != had_focus) | ||||
|       menu_bar_->UpdateViewColors(); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| MenuBar::MenuBar(RootView* window) | ||||
|     : background_color_(kDefaultColor), | ||||
|       window_(window), | ||||
|       color_updater_(new MenuBarColorUpdater(this)) { | ||||
| MenuBar::MenuBar(NativeWindow* window, RootView* root_view) | ||||
|     : background_color_(kDefaultColor), window_(window), root_view_(root_view) { | ||||
|   RefreshColorCache(); | ||||
|   UpdateViewColors(); | ||||
|   SetFocusBehavior(FocusBehavior::ALWAYS); | ||||
|   SetLayoutManager(std::make_unique<views::BoxLayout>( | ||||
|       views::BoxLayout::Orientation::kHorizontal)); | ||||
|   window_->GetFocusManager()->AddFocusChangeListener(color_updater_.get()); | ||||
|   window_->AddObserver(this); | ||||
| } | ||||
| 
 | ||||
| MenuBar::~MenuBar() { | ||||
|   window_->GetFocusManager()->RemoveFocusChangeListener(color_updater_.get()); | ||||
|   window_->RemoveObserver(this); | ||||
| } | ||||
| 
 | ||||
| void MenuBar::SetMenu(ElectronMenuModel* model) { | ||||
|  | @ -123,131 +100,55 @@ void MenuBar::OnBeforeExecuteCommand() { | |||
|   if (GetPaneFocusTraversable() != nullptr) { | ||||
|     RemovePaneFocus(); | ||||
|   } | ||||
|   window_->RestoreFocus(); | ||||
|   root_view_->RestoreFocus(); | ||||
| } | ||||
| 
 | ||||
| void MenuBar::OnMenuClosed() { | ||||
|   SetAcceleratorVisibility(true); | ||||
|   SetAcceleratorVisibility(pane_has_focus()); | ||||
| } | ||||
| 
 | ||||
| void MenuBar::OnWindowBlur() { | ||||
|   UpdateViewColors(); | ||||
|   SetAcceleratorVisibility(pane_has_focus()); | ||||
| } | ||||
| 
 | ||||
| void MenuBar::OnWindowFocus() { | ||||
|   UpdateViewColors(); | ||||
|   SetAcceleratorVisibility(pane_has_focus()); | ||||
| } | ||||
| 
 | ||||
| bool MenuBar::AcceleratorPressed(const ui::Accelerator& accelerator) { | ||||
|   views::View* focused_view = GetFocusManager()->GetFocusedView(); | ||||
|   if (!ContainsForFocusSearch(this, focused_view)) | ||||
|     return false; | ||||
| 
 | ||||
|   switch (accelerator.key_code()) { | ||||
|     case ui::VKEY_MENU: | ||||
|     case ui::VKEY_ESCAPE: { | ||||
|       RemovePaneFocus(); | ||||
|       window_->RestoreFocus(); | ||||
|       return true; | ||||
|     } | ||||
|     case ui::VKEY_LEFT: | ||||
|       GetFocusManager()->AdvanceFocus(true); | ||||
|       return true; | ||||
|     case ui::VKEY_RIGHT: | ||||
|       GetFocusManager()->AdvanceFocus(false); | ||||
|       return true; | ||||
|     case ui::VKEY_HOME: | ||||
|       GetFocusManager()->SetFocusedViewWithReason( | ||||
|           GetFirstFocusableChild(), | ||||
|           views::FocusManager::FocusChangeReason::kFocusTraversal); | ||||
|       return true; | ||||
|     case ui::VKEY_END: | ||||
|       GetFocusManager()->SetFocusedViewWithReason( | ||||
|           GetLastFocusableChild(), | ||||
|           views::FocusManager::FocusChangeReason::kFocusTraversal); | ||||
|       return true; | ||||
|     default: { | ||||
|       for (auto* child : GetChildrenInZOrder()) { | ||||
|         auto* button = static_cast<SubmenuButton*>(child); | ||||
|         bool shifted = false; | ||||
|         auto keycode = | ||||
|             electron::KeyboardCodeFromCharCode(button->accelerator(), &shifted); | ||||
| 
 | ||||
|         if (keycode == accelerator.key_code()) { | ||||
|           auto event = accelerator.ToKeyEvent(); | ||||
|           ButtonPressed(button->tag(), event); | ||||
|           return true; | ||||
|         } | ||||
|   // Treat pressing Alt the same as pressing Esc.
 | ||||
|   const ui::Accelerator& translated = | ||||
|       accelerator.key_code() == ui::VKEY_MENU | ||||
|           ? ui::Accelerator(ui::VKEY_ESCAPE, accelerator.modifiers(), | ||||
|                             accelerator.key_state(), accelerator.time_stamp()) | ||||
|           : accelerator; | ||||
|   return views::AccessiblePaneView::AcceleratorPressed(translated); | ||||
| } | ||||
| 
 | ||||
|       return false; | ||||
|     } | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| bool MenuBar::SetPaneFocus(views::View* initial_focus) { | ||||
|   // TODO(zcbenz): Submit patch to upstream Chromium to fix the crash.
 | ||||
|   //
 | ||||
|   // Without this check, Electron would crash when running tests.
 | ||||
|   //
 | ||||
|   // Check failed: rules_->CanFocusWindow(window, nullptr).
 | ||||
|   //   logging::LogMessage::~LogMessage
 | ||||
|   //   wm::FocusController::SetFocusedWindow
 | ||||
|   //   wm::FocusController::ResetFocusWithinActiveWindow
 | ||||
|   //   views::View::OnFocus
 | ||||
|   //   views::Button::OnFocus
 | ||||
|   //   views::LabelButton::OnFocus
 | ||||
|   //   views::View::Focus
 | ||||
|   //   views::FocusManager::SetFocusedViewWithReason
 | ||||
|   //   views::AccessiblePaneView::SetPaneFocus
 | ||||
|   //   electron::MenuBar::SetPaneFocus
 | ||||
|   if (initial_focus && initial_focus->GetWidget()) { | ||||
|     aura::Window* window = initial_focus->GetWidget()->GetNativeWindow(); | ||||
|     if (!window || !window->GetRootWindow()) | ||||
|       return false; | ||||
|   } | ||||
| 
 | ||||
|   bool result = views::AccessiblePaneView::SetPaneFocus(initial_focus); | ||||
| 
 | ||||
|   if (result) { | ||||
|     std::set<ui::KeyboardCode> reg; | ||||
|     for (auto* child : GetChildrenInZOrder()) { | ||||
|       auto* button = static_cast<SubmenuButton*>(child); | ||||
|       bool shifted = false; | ||||
|       auto keycode = | ||||
|           electron::KeyboardCodeFromCharCode(button->accelerator(), &shifted); | ||||
| 
 | ||||
|       // We want the menu items to activate if the user presses the accelerator
 | ||||
|       // key, even without alt, since we are now focused on the menu bar
 | ||||
|       if (keycode != ui::VKEY_UNKNOWN && reg.find(keycode) != reg.end()) { | ||||
|         reg.insert(keycode); | ||||
|         focus_manager()->RegisterAccelerator( | ||||
|             ui::Accelerator(keycode, ui::EF_NONE), | ||||
|             ui::AcceleratorManager::kNormalPriority, this); | ||||
|       } | ||||
|     } | ||||
| 
 | ||||
|     // We want to remove focus / hide menu bar when alt is pressed again
 | ||||
| bool MenuBar::SetPaneFocusAndFocusDefault() { | ||||
|   bool result = views::AccessiblePaneView::SetPaneFocusAndFocusDefault(); | ||||
|   if (result && !accelerator_installed_) { | ||||
|     // Listen to Alt key events.
 | ||||
|     // Note that there is no need to unregister the accelerator.
 | ||||
|     accelerator_installed_ = true; | ||||
|     focus_manager()->RegisterAccelerator( | ||||
|         ui::Accelerator(ui::VKEY_MENU, ui::EF_ALT_DOWN), | ||||
|         ui::AcceleratorManager::kNormalPriority, this); | ||||
|   } | ||||
| 
 | ||||
|   return result; | ||||
| } | ||||
| 
 | ||||
| void MenuBar::RemovePaneFocus() { | ||||
|   views::AccessiblePaneView::RemovePaneFocus(); | ||||
|   SetAcceleratorVisibility(false); | ||||
| 
 | ||||
|   std::set<ui::KeyboardCode> unreg; | ||||
|   for (auto* child : GetChildrenInZOrder()) { | ||||
|     auto* button = static_cast<SubmenuButton*>(child); | ||||
|     bool shifted = false; | ||||
|     auto keycode = | ||||
|         electron::KeyboardCodeFromCharCode(button->accelerator(), &shifted); | ||||
| 
 | ||||
|     if (keycode != ui::VKEY_UNKNOWN && unreg.find(keycode) != unreg.end()) { | ||||
|       unreg.insert(keycode); | ||||
|       focus_manager()->UnregisterAccelerator( | ||||
|           ui::Accelerator(keycode, ui::EF_NONE), this); | ||||
|     } | ||||
| void MenuBar::OnThemeChanged() { | ||||
|   views::AccessiblePaneView::OnThemeChanged(); | ||||
|   RefreshColorCache(); | ||||
|   UpdateViewColors(); | ||||
| } | ||||
| 
 | ||||
|   focus_manager()->UnregisterAccelerator( | ||||
|       ui::Accelerator(ui::VKEY_MENU, ui::EF_ALT_DOWN), this); | ||||
| void MenuBar::OnDidChangeFocus(View* focused_before, View* focused_now) { | ||||
|   views::AccessiblePaneView::OnDidChangeFocus(focused_before, focused_now); | ||||
|   SetAcceleratorVisibility(pane_has_focus()); | ||||
| } | ||||
| 
 | ||||
| const char* MenuBar::GetClassName() const { | ||||
|  | @ -256,13 +157,13 @@ const char* MenuBar::GetClassName() const { | |||
| 
 | ||||
| void MenuBar::ButtonPressed(int id, const ui::Event& event) { | ||||
|   // Hide the accelerator when a submenu is activated.
 | ||||
|   SetAcceleratorVisibility(false); | ||||
|   SetAcceleratorVisibility(pane_has_focus()); | ||||
| 
 | ||||
|   if (!menu_model_) | ||||
|     return; | ||||
| 
 | ||||
|   if (!window_->HasFocus()) | ||||
|     window_->RequestFocus(); | ||||
|   if (!root_view_->HasFocus()) | ||||
|     root_view_->RequestFocus(); | ||||
| 
 | ||||
|   ElectronMenuModel::ItemType type = menu_model_->GetTypeAt(id); | ||||
|   if (type != ElectronMenuModel::TYPE_SUBMENU) { | ||||
|  | @ -304,12 +205,6 @@ void MenuBar::RefreshColorCache() { | |||
|   } | ||||
| } | ||||
| 
 | ||||
| void MenuBar::OnThemeChanged() { | ||||
|   views::AccessiblePaneView::OnThemeChanged(); | ||||
|   RefreshColorCache(); | ||||
|   UpdateViewColors(); | ||||
| } | ||||
| 
 | ||||
| void MenuBar::RebuildChildren() { | ||||
|   RemoveAllChildViews(true); | ||||
|   for (int i = 0, n = GetItemCount(); i < n; ++i) { | ||||
|  | @ -330,7 +225,8 @@ void MenuBar::UpdateViewColors() { | |||
|   if (menu_model_ == nullptr) | ||||
|     return; | ||||
| #if defined(OS_LINUX) | ||||
|   const auto& textColor = has_focus_ ? enabled_color_ : disabled_color_; | ||||
|   const auto& textColor = | ||||
|       window_->IsFocused() ? enabled_color_ : disabled_color_; | ||||
|   for (auto* child : GetChildrenInZOrder()) { | ||||
|     auto* button = static_cast<SubmenuButton*>(child); | ||||
|     button->SetTextColor(views::Button::STATE_NORMAL, textColor); | ||||
|  |  | |||
|  | @ -5,43 +5,25 @@ | |||
| #ifndef SHELL_BROWSER_UI_VIEWS_MENU_BAR_H_ | ||||
| #define SHELL_BROWSER_UI_VIEWS_MENU_BAR_H_ | ||||
| 
 | ||||
| #include <memory> | ||||
| 
 | ||||
| #include "shell/browser/native_window_observer.h" | ||||
| #include "shell/browser/ui/electron_menu_model.h" | ||||
| #include "shell/browser/ui/views/menu_delegate.h" | ||||
| #include "shell/browser/ui/views/root_view.h" | ||||
| #include "ui/views/accessible_pane_view.h" | ||||
| #include "ui/views/controls/button/button.h" | ||||
| #include "ui/views/focus/focus_manager.h" | ||||
| #include "ui/views/view.h" | ||||
| 
 | ||||
| namespace views { | ||||
| class Button; | ||||
| class MenuButton; | ||||
| }  // namespace views
 | ||||
| } | ||||
| 
 | ||||
| namespace electron { | ||||
| 
 | ||||
| class MenuBarColorUpdater : public views::FocusChangeListener { | ||||
|  public: | ||||
|   explicit MenuBarColorUpdater(MenuBar* menu_bar); | ||||
|   ~MenuBarColorUpdater() override; | ||||
| 
 | ||||
|   void OnDidChangeFocus(views::View* focused_before, | ||||
|                         views::View* focused_now) override; | ||||
|   void OnWillChangeFocus(views::View* focused_before, | ||||
|                          views::View* focused_now) override {} | ||||
| 
 | ||||
|  private: | ||||
|   MenuBar* menu_bar_; | ||||
| }; | ||||
| 
 | ||||
| class MenuBar : public views::AccessiblePaneView, | ||||
|                 public electron::MenuDelegate::Observer { | ||||
|                 public MenuDelegate::Observer, | ||||
|                 public NativeWindowObserver { | ||||
|  public: | ||||
|   static const char kViewClassName[]; | ||||
| 
 | ||||
|   explicit MenuBar(RootView* window); | ||||
|   MenuBar(NativeWindow* window, RootView* root_view); | ||||
|   ~MenuBar() override; | ||||
| 
 | ||||
|   // Replaces current menu with a new one.
 | ||||
|  | @ -64,18 +46,22 @@ class MenuBar : public views::AccessiblePaneView, | |||
|                                     ElectronMenuModel** menu_model, | ||||
|                                     views::MenuButton** button); | ||||
| 
 | ||||
|   // electron::MenuDelegate::Observer:
 | ||||
|  private: | ||||
|   // MenuDelegate::Observer:
 | ||||
|   void OnBeforeExecuteCommand() override; | ||||
|   void OnMenuClosed() override; | ||||
| 
 | ||||
|   // NativeWindowObserver:
 | ||||
|   void OnWindowBlur() override; | ||||
|   void OnWindowFocus() override; | ||||
| 
 | ||||
|   // views::AccessiblePaneView:
 | ||||
|   bool AcceleratorPressed(const ui::Accelerator& accelerator) override; | ||||
|   bool SetPaneFocus(views::View* initial_focus) override; | ||||
|   void RemovePaneFocus() override; | ||||
|   bool SetPaneFocusAndFocusDefault() override; | ||||
|   void OnThemeChanged() override; | ||||
| 
 | ||||
|  private: | ||||
|   friend class MenuBarColorUpdater; | ||||
|   // views::FocusChangeListener:
 | ||||
|   void OnDidChangeFocus(View* focused_before, View* focused_now) override; | ||||
| 
 | ||||
|   // views::View:
 | ||||
|   const char* GetClassName() const override; | ||||
|  | @ -84,22 +70,19 @@ class MenuBar : public views::AccessiblePaneView, | |||
| 
 | ||||
|   void RebuildChildren(); | ||||
|   void UpdateViewColors(); | ||||
| 
 | ||||
|   void RefreshColorCache(); | ||||
|   View* FindAccelChild(char16_t key); | ||||
| 
 | ||||
|   SkColor background_color_; | ||||
| #if defined(OS_LINUX) | ||||
|   SkColor enabled_color_; | ||||
|   SkColor disabled_color_; | ||||
| #endif | ||||
| 
 | ||||
|   RootView* window_ = nullptr; | ||||
|   NativeWindow* window_; | ||||
|   RootView* root_view_; | ||||
|   ElectronMenuModel* menu_model_ = nullptr; | ||||
| 
 | ||||
|   View* FindAccelChild(char16_t key); | ||||
| 
 | ||||
|   bool has_focus_ = true; | ||||
| 
 | ||||
|   std::unique_ptr<MenuBarColorUpdater> color_updater_; | ||||
|   bool accelerator_installed_ = false; | ||||
| 
 | ||||
|   DISALLOW_COPY_AND_ASSIGN(MenuBar); | ||||
| }; | ||||
|  |  | |||
|  | @ -62,7 +62,7 @@ void RootView::SetMenu(ElectronMenuModel* menu_model) { | |||
|     return; | ||||
| 
 | ||||
|   if (!menu_bar_) { | ||||
|     menu_bar_ = std::make_unique<MenuBar>(this); | ||||
|     menu_bar_ = std::make_unique<MenuBar>(window_, this); | ||||
|     menu_bar_->set_owned_by_client(); | ||||
|     if (!menu_bar_autohide_) | ||||
|       SetMenuBarVisibility(true); | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Cheng Zhao
				Cheng Zhao