Skip to content
Permalink
Browse files

fix: add friend to monitor window focus change

  • Loading branch information...
brenca committed Oct 26, 2018
1 parent e1a44fa commit 5c729d2092692041a86e0cc6dc0a5fa652444b35
Showing with 41 additions and 15 deletions.
  1. +23 −12 atom/browser/ui/views/menu_bar.cc
  2. +18 −3 atom/browser/ui/views/menu_bar.h
@@ -35,16 +35,37 @@ const SkColor kDefaultColor = SkColorSetARGB(255, 233, 233, 233);

const char MenuBar::kViewClassName[] = "ElectronMenuBar";

MenuBarColorUpdater::MenuBarColorUpdater(MenuBar* menu_bar)
: menu_bar_(menu_bar) {}

MenuBarColorUpdater::~MenuBarColorUpdater() {}

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) {
: background_color_(kDefaultColor),
window_(window),
color_updater_(new MenuBarColorUpdater(this)) {
RefreshColorCache();
UpdateViewColors();
SetFocusBehavior(FocusBehavior::ALWAYS);
SetLayoutManager(
std::make_unique<views::BoxLayout>(views::BoxLayout::kHorizontal));
window_->GetFocusManager()->AddFocusChangeListener(color_updater_.get());
}

MenuBar::~MenuBar() {}
MenuBar::~MenuBar() {
window_->GetFocusManager()->RemoveFocusChangeListener(color_updater_.get());
}

void MenuBar::SetMenu(AtomMenuModel* model) {
menu_model_ = model;
@@ -269,16 +290,6 @@ void MenuBar::OnNativeThemeChanged(const ui::NativeTheme* theme) {
UpdateViewColors();
}

void MenuBar::OnDidChangeFocus(View* focused_before, View* focused_now) {
views::AccessiblePaneView::OnDidChangeFocus(focused_before, focused_now);

// if we've changed focus, update our view
const auto had_focus = has_focus_;
has_focus_ = focused_now != nullptr;
if (has_focus_ != had_focus)
UpdateViewColors();
}

void MenuBar::RebuildChildren() {
RemoveAllChildViews(true);
for (int i = 0, n = GetItemCount(); i < n; ++i) {
@@ -19,6 +19,20 @@ class MenuButton;

namespace atom {

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 views::MenuButtonListener,
public atom::MenuDelegate::Observer {
@@ -67,10 +81,9 @@ class MenuBar : public views::AccessiblePaneView,
const ui::Event* event) override;
void OnNativeThemeChanged(const ui::NativeTheme* theme) override;

// views::FocusChangeListener:
void OnDidChangeFocus(View* focused_before, View* focused_now) override;

private:
friend class MenuBarColorUpdater;

void RebuildChildren();
void UpdateViewColors();

@@ -88,6 +101,8 @@ class MenuBar : public views::AccessiblePaneView,

bool has_focus_ = true;

std::unique_ptr<MenuBarColorUpdater> color_updater_;

DISALLOW_COPY_AND_ASSIGN(MenuBar);
};

0 comments on commit 5c729d2

Please sign in to comment.
You can’t perform that action at this time.