Skip to content

Commit

Permalink
fix: add friend to monitor window focus change
Browse files Browse the repository at this point in the history
  • Loading branch information
brenca committed Oct 26, 2018
1 parent e1a44fa commit 5c729d2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
35 changes: 23 additions & 12 deletions atom/browser/ui/views/menu_bar.cc
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
21 changes: 18 additions & 3 deletions atom/browser/ui/views/menu_bar.h
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();

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

bool has_focus_ = true;

std::unique_ptr<MenuBarColorUpdater> color_updater_;

DISALLOW_COPY_AND_ASSIGN(MenuBar);
};

Expand Down

0 comments on commit 5c729d2

Please sign in to comment.