2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2014 GitHub, Inc.
|
2014-07-16 14:10:10 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/browser/ui/views/menu_bar.h"
|
|
|
|
|
2014-08-05 01:41:29 +00:00
|
|
|
#if defined(USE_X11)
|
|
|
|
#include "gtk/gtk.h"
|
|
|
|
#endif
|
|
|
|
|
2014-07-17 06:23:28 +00:00
|
|
|
#include "atom/browser/ui/views/menu_delegate.h"
|
2014-08-18 05:36:00 +00:00
|
|
|
#include "atom/browser/ui/views/submenu_button.h"
|
2014-07-17 06:23:28 +00:00
|
|
|
#include "ui/base/models/menu_model.h"
|
|
|
|
#include "ui/views/background.h"
|
|
|
|
#include "ui/views/layout/box_layout.h"
|
2014-07-16 14:10:10 +00:00
|
|
|
|
2014-07-25 07:20:25 +00:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
#include "ui/gfx/color_utils.h"
|
2014-08-05 01:41:29 +00:00
|
|
|
#elif defined(USE_X11)
|
2017-01-26 10:55:19 +00:00
|
|
|
#include "chrome/browser/ui/libgtkui/skia_utils_gtk.h"
|
2014-07-25 07:20:25 +00:00
|
|
|
#endif
|
|
|
|
|
2014-07-16 14:10:10 +00:00
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-02-10 01:04:59 +00:00
|
|
|
#if defined(USE_X11)
|
2014-07-16 14:10:10 +00:00
|
|
|
|
2018-02-10 01:04:59 +00:00
|
|
|
SkColor GdkRgbaToSkColor(const GdkRGBA& rgba) {
|
2018-02-15 21:20:55 +00:00
|
|
|
return SkColorSetARGB(rgba.alpha * 255, rgba.red * 255, rgba.green * 255,
|
|
|
|
rgba.blue * 255);
|
2018-02-10 01:04:59 +00:00
|
|
|
}
|
2014-07-16 14:10:10 +00:00
|
|
|
|
2018-02-10 01:04:59 +00:00
|
|
|
SkColor GetStyleContextFgColor(GtkStyleContext* style_context,
|
|
|
|
GtkStateFlags state) {
|
|
|
|
GdkRGBA rgba;
|
|
|
|
gtk_style_context_get_color(style_context, state, &rgba);
|
|
|
|
return GdkRgbaToSkColor(rgba);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkColor GetStyleContextBgColor(GtkStyleContext* style_context,
|
|
|
|
GtkStateFlags state) {
|
|
|
|
GdkRGBA rgba;
|
|
|
|
gtk_style_context_get_background_color(style_context, state, &rgba);
|
|
|
|
return GdkRgbaToSkColor(rgba);
|
|
|
|
}
|
|
|
|
|
2018-02-15 21:20:55 +00:00
|
|
|
void GetMenuBarColor(SkColor* enabled,
|
|
|
|
SkColor* disabled,
|
|
|
|
SkColor* highlight,
|
|
|
|
SkColor* hover,
|
|
|
|
SkColor* background) {
|
2015-12-07 21:27:05 +00:00
|
|
|
GtkWidget* menu_bar = gtk_menu_bar_new();
|
2018-02-10 01:34:37 +00:00
|
|
|
GtkStyleContext* sc = gtk_widget_get_style_context(menu_bar);
|
2018-02-15 21:20:55 +00:00
|
|
|
*enabled = GetStyleContextFgColor(sc, GTK_STATE_FLAG_NORMAL);
|
|
|
|
*disabled = GetStyleContextFgColor(sc, GTK_STATE_FLAG_INSENSITIVE);
|
|
|
|
*highlight = GetStyleContextFgColor(sc, GTK_STATE_FLAG_SELECTED);
|
|
|
|
*hover = GetStyleContextFgColor(sc, GTK_STATE_FLAG_PRELIGHT);
|
2018-02-10 01:34:37 +00:00
|
|
|
*background = GetStyleContextBgColor(sc, GTK_STATE_FLAG_NORMAL);
|
2018-02-10 01:04:59 +00:00
|
|
|
g_object_unref(G_OBJECT(menu_bar));
|
|
|
|
}
|
2014-08-05 01:41:29 +00:00
|
|
|
|
2018-02-10 01:04:59 +00:00
|
|
|
#endif // USE_X11
|
|
|
|
|
|
|
|
const char kViewClassName[] = "ElectronMenuBar";
|
|
|
|
|
|
|
|
// Default color of the menu bar.
|
|
|
|
const SkColor kDefaultColor = SkColorSetARGB(255, 233, 233, 233);
|
2014-08-05 01:41:29 +00:00
|
|
|
|
2014-07-16 14:10:10 +00:00
|
|
|
} // namespace
|
|
|
|
|
2016-10-24 02:42:15 +00:00
|
|
|
MenuBar::MenuBar(NativeWindow* window)
|
2018-02-15 21:20:55 +00:00
|
|
|
: background_color_(kDefaultColor), menu_model_(NULL), window_(window) {
|
2016-06-21 09:05:01 +00:00
|
|
|
UpdateMenuBarColor();
|
2017-09-13 20:57:24 +00:00
|
|
|
SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal));
|
2014-07-16 14:10:10 +00:00
|
|
|
}
|
|
|
|
|
2018-02-15 21:20:55 +00:00
|
|
|
MenuBar::~MenuBar() {}
|
2014-07-16 14:10:10 +00:00
|
|
|
|
2016-07-06 23:04:18 +00:00
|
|
|
void MenuBar::SetMenu(AtomMenuModel* model) {
|
2014-07-17 06:23:28 +00:00
|
|
|
menu_model_ = model;
|
|
|
|
RemoveAllChildViews(true);
|
|
|
|
|
|
|
|
for (int i = 0; i < model->GetItemCount(); ++i) {
|
2018-02-15 21:20:55 +00:00
|
|
|
SubmenuButton* button =
|
|
|
|
new SubmenuButton(model->GetLabelAt(i), this, background_color_);
|
2014-07-17 06:23:28 +00:00
|
|
|
button->set_tag(i);
|
2014-08-05 01:41:29 +00:00
|
|
|
|
|
|
|
#if defined(USE_X11)
|
2014-09-01 12:10:14 +00:00
|
|
|
button->SetTextColor(views::Button::STATE_NORMAL, enabled_color_);
|
|
|
|
button->SetTextColor(views::Button::STATE_DISABLED, disabled_color_);
|
|
|
|
button->SetTextColor(views::Button::STATE_PRESSED, highlight_color_);
|
|
|
|
button->SetTextColor(views::Button::STATE_HOVERED, hover_color_);
|
2014-08-18 05:36:00 +00:00
|
|
|
button->SetUnderlineColor(enabled_color_);
|
2014-08-18 07:41:57 +00:00
|
|
|
#elif defined(OS_WIN)
|
|
|
|
button->SetUnderlineColor(color_utils::GetSysSkColor(COLOR_GRAYTEXT));
|
2014-08-05 01:41:29 +00:00
|
|
|
#endif
|
|
|
|
|
2014-07-17 06:23:28 +00:00
|
|
|
AddChildView(button);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-18 06:12:12 +00:00
|
|
|
void MenuBar::SetAcceleratorVisibility(bool visible) {
|
|
|
|
for (int i = 0; i < child_count(); ++i)
|
|
|
|
static_cast<SubmenuButton*>(child_at(i))->SetAcceleratorVisibility(visible);
|
|
|
|
}
|
|
|
|
|
2014-08-18 06:52:44 +00:00
|
|
|
int MenuBar::GetAcceleratorIndex(base::char16 key) {
|
2014-08-18 06:42:21 +00:00
|
|
|
for (int i = 0; i < child_count(); ++i) {
|
|
|
|
SubmenuButton* button = static_cast<SubmenuButton*>(child_at(i));
|
2014-08-18 06:52:44 +00:00
|
|
|
if (button->accelerator() == key)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MenuBar::ActivateAccelerator(base::char16 key) {
|
|
|
|
int i = GetAcceleratorIndex(key);
|
2014-08-18 06:58:52 +00:00
|
|
|
if (i != -1)
|
2016-04-30 23:56:28 +00:00
|
|
|
static_cast<SubmenuButton*>(child_at(i))->Activate(nullptr);
|
2014-08-18 06:42:21 +00:00
|
|
|
}
|
|
|
|
|
2014-07-17 06:23:28 +00:00
|
|
|
int MenuBar::GetItemCount() const {
|
|
|
|
return menu_model_->GetItemCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MenuBar::GetMenuButtonFromScreenPoint(const gfx::Point& point,
|
2016-07-06 23:04:18 +00:00
|
|
|
AtomMenuModel** menu_model,
|
2014-07-17 06:23:28 +00:00
|
|
|
views::MenuButton** button) {
|
|
|
|
gfx::Point location(point);
|
|
|
|
views::View::ConvertPointFromScreen(this, &location);
|
|
|
|
|
|
|
|
if (location.x() < 0 || location.x() >= width() || location.y() < 0 ||
|
|
|
|
location.y() >= height())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (int i = 0; i < child_count(); ++i) {
|
|
|
|
views::View* view = child_at(i);
|
2017-05-21 20:36:10 +00:00
|
|
|
if (view->GetMirroredBounds().Contains(location) &&
|
2016-07-06 23:04:18 +00:00
|
|
|
(menu_model_->GetTypeAt(i) == AtomMenuModel::TYPE_SUBMENU)) {
|
2014-07-17 06:23:28 +00:00
|
|
|
*menu_model = menu_model_->GetSubmenuModelAt(i);
|
|
|
|
*button = static_cast<views::MenuButton*>(view);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-07-16 14:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* MenuBar::GetClassName() const {
|
|
|
|
return kViewClassName;
|
|
|
|
}
|
|
|
|
|
2016-04-30 23:56:28 +00:00
|
|
|
void MenuBar::OnMenuButtonClicked(views::MenuButton* source,
|
|
|
|
const gfx::Point& point,
|
|
|
|
const ui::Event* event) {
|
2014-08-18 06:58:52 +00:00
|
|
|
// Hide the accelerator when a submenu is activated.
|
|
|
|
SetAcceleratorVisibility(false);
|
|
|
|
|
2014-07-17 06:23:28 +00:00
|
|
|
if (!menu_model_)
|
|
|
|
return;
|
|
|
|
|
2016-10-24 02:42:15 +00:00
|
|
|
if (!window_->IsFocused())
|
|
|
|
window_->Focus(true);
|
|
|
|
|
2016-04-30 23:56:28 +00:00
|
|
|
int id = source->tag();
|
2016-07-06 23:04:18 +00:00
|
|
|
AtomMenuModel::ItemType type = menu_model_->GetTypeAt(id);
|
|
|
|
if (type != AtomMenuModel::TYPE_SUBMENU) {
|
2016-06-25 14:51:43 +00:00
|
|
|
menu_model_->ActivatedAt(id, 0);
|
2016-01-05 04:05:27 +00:00
|
|
|
return;
|
2016-06-25 14:51:43 +00:00
|
|
|
}
|
2016-01-05 04:05:27 +00:00
|
|
|
|
2017-07-03 19:47:02 +00:00
|
|
|
// Deleted in MenuDelegate::OnMenuClosed
|
|
|
|
MenuDelegate* menu_delegate = new MenuDelegate(this);
|
|
|
|
menu_delegate->RunMenu(menu_model_->GetSubmenuModelAt(id), source);
|
2014-07-17 06:23:28 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 09:05:01 +00:00
|
|
|
void MenuBar::OnNativeThemeChanged(const ui::NativeTheme* theme) {
|
|
|
|
UpdateMenuBarColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MenuBar::UpdateMenuBarColor() {
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
background_color_ = color_utils::GetSysSkColor(COLOR_MENUBAR);
|
|
|
|
#elif defined(USE_X11)
|
|
|
|
GetMenuBarColor(&enabled_color_, &disabled_color_, &highlight_color_,
|
|
|
|
&hover_color_, &background_color_);
|
|
|
|
#endif
|
2017-09-13 20:59:25 +00:00
|
|
|
SetBackground(views::CreateSolidBackground(background_color_));
|
2016-06-21 09:05:01 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 14:10:10 +00:00
|
|
|
} // namespace atom
|