Merge pull request #558 from atom/menubar-autohide

Allow window menu bar to be hidden
This commit is contained in:
Cheng Zhao 2014-08-07 19:31:58 +08:00
Родитель 382dbb500c 8cc49ffa80
Коммит 3c078b4dab
6 изменённых файлов: 94 добавлений и 11 удалений

Просмотреть файл

@ -71,6 +71,22 @@ bool ShouldUseGlobalMenuBar() {
}
#endif
bool IsAltKey(const content::NativeWebKeyboardEvent& event) {
#if defined(USE_X11)
// 164 and 165 represent VK_LALT and VK_RALT.
return event.windowsKeyCode == 164 || event.windowsKeyCode == 165;
#else
return event.windowsKeyCode == ui::VKEY_MENU;
#endif
}
bool IsAltModifier(const content::NativeWebKeyboardEvent& event) {
typedef content::NativeWebKeyboardEvent::Modifiers Modifiers;
return (event.modifiers == Modifiers::AltKey) ||
(event.modifiers == (Modifiers::AltKey | Modifiers::IsLeft)) ||
(event.modifiers == (Modifiers::AltKey | Modifiers::IsRight));
}
class NativeWindowClientView : public views::ClientView {
public:
NativeWindowClientView(views::Widget* widget,
@ -94,13 +110,16 @@ NativeWindowViews::NativeWindowViews(content::WebContents* web_contents,
const mate::Dictionary& options)
: NativeWindow(web_contents, options),
window_(new views::Widget),
menu_bar_(NULL),
web_view_(inspectable_web_contents()->GetView()->GetView()),
menu_bar_autohide_(false),
menu_bar_visible_(false),
menu_bar_alt_pressed_(false),
keyboard_event_handler_(new views::UnhandledKeyboardEventHandler),
use_content_size_(false),
resizable_(true) {
options.Get(switches::kResizable, &resizable_);
options.Get(switches::kTitle, &title_);
options.Get(switches::kAutoHideMenuBar, &menu_bar_autohide_);
int width = 800, height = 600;
options.Get(switches::kWidth, &width);
@ -242,7 +261,7 @@ gfx::Size NativeWindowViews::GetContentSize() {
gfx::Size content_size =
window_->non_client_view()->frame_view()->GetBoundsForClientView().size();
if (menu_bar_)
if (menu_bar_ && menu_bar_visible_)
content_size.set_height(content_size.height() - kMenuBarHeight);
return content_size;
}
@ -383,12 +402,15 @@ void NativeWindowViews::SetMenu(ui::MenuModel* menu_model) {
if (!menu_bar_) {
gfx::Size content_size = GetContentSize();
menu_bar_ = new MenuBar;
AddChildViewAt(menu_bar_, 0);
menu_bar_.reset(new MenuBar);
menu_bar_->set_owned_by_client();
if (!menu_bar_autohide_) {
SetMenuBarVisibility(true);
if (use_content_size_)
SetContentSize(content_size);
}
}
menu_bar_->SetMenu(menu_model);
Layout();
@ -434,6 +456,15 @@ void NativeWindowViews::OnWidgetActivationChanged(
NotifyWindowFocus();
else
NotifyWindowBlur();
if (active && GetWebContents() && !IsDevToolsOpened())
GetWebContents()->Focus();
// Hide menu bar when window is blured.
if (!active && menu_bar_autohide_ && menu_bar_visible_) {
SetMenuBarVisibility(false);
Layout();
}
}
void NativeWindowViews::DeleteDelegate() {
@ -526,9 +557,33 @@ views::NonClientFrameView* NativeWindowViews::CreateNonClientFrameView(
return NULL;
}
void NativeWindowViews::HandleMouseDown() {
// Hide menu bar when web view is clicked.
if (menu_bar_autohide_ && menu_bar_visible_) {
SetMenuBarVisibility(false);
Layout();
}
}
void NativeWindowViews::HandleKeyboardEvent(
content::WebContents*,
const content::NativeWebKeyboardEvent& event) {
// Toggle the menu bar only when a single Alt is released.
if (event.type == blink::WebInputEvent::RawKeyDown && IsAltKey(event) &&
IsAltModifier(event)) {
// When a single Alt is pressed:
menu_bar_alt_pressed_ = true;
} else if (event.type == blink::WebInputEvent::KeyUp && IsAltKey(event) &&
event.modifiers == 0 && menu_bar_alt_pressed_) {
// When a single Alt is released right after a Alt is pressed:
menu_bar_alt_pressed_ = false;
SetMenuBarVisibility(!menu_bar_visible_);
Layout();
} else {
// When any other keys except single Alt have been pressed/released:
menu_bar_alt_pressed_ = false;
}
keyboard_event_handler_->HandleKeyboardEvent(event, GetFocusManager());
}
@ -558,11 +613,25 @@ gfx::Rect NativeWindowViews::ContentBoundsToWindowBounds(
const gfx::Rect& bounds) {
gfx::Rect window_bounds =
window_->non_client_view()->GetWindowBoundsForClientBounds(bounds);
if (menu_bar_)
if (menu_bar_ && menu_bar_visible_)
window_bounds.set_height(window_bounds.height() + kMenuBarHeight);
return window_bounds;
}
void NativeWindowViews::SetMenuBarVisibility(bool visible) {
if (!menu_bar_)
return;
menu_bar_visible_ = visible;
if (visible) {
DCHECK_EQ(child_count(), 1);
AddChildView(menu_bar_.get());
} else {
DCHECK_EQ(child_count(), 2);
RemoveChildView(menu_bar_.get());
}
}
// static
NativeWindow* NativeWindow::Create(content::WebContents* web_contents,
const mate::Dictionary& options) {

Просмотреть файл

@ -107,6 +107,7 @@ class NativeWindowViews : public NativeWindow,
views::Widget* widget) OVERRIDE;
// content::WebContentsDelegate:
virtual void HandleMouseDown() OVERRIDE;
virtual void HandleKeyboardEvent(
content::WebContents*,
const content::NativeWebKeyboardEvent& event) OVERRIDE;
@ -121,10 +122,17 @@ class NativeWindowViews : public NativeWindow,
// in client area we need to substract/add menu bar's height in convertions.
gfx::Rect ContentBoundsToWindowBounds(const gfx::Rect& content_bounds);
// Show/Hide the menu bar.
void SetMenuBarVisibility(bool visible);
scoped_ptr<views::Widget> window_;
MenuBar* menu_bar_;
views::View* web_view_; // Managed by inspectable_web_contents_.
scoped_ptr<MenuBar> menu_bar_;
bool menu_bar_autohide_;
bool menu_bar_visible_;
bool menu_bar_alt_pressed_;
#if defined(USE_X11)
scoped_ptr<GlobalMenuBarX11> global_menu_bar_;
#endif

Просмотреть файл

@ -24,10 +24,10 @@ void MenuLayout::Layout(views::View* host) {
gfx::Rect web_view_bounds = gfx::Rect(
0, menu_height_, size.width(), size.height() - menu_height_);
views::View* menu_bar = host->child_at(0);
views::View* web_view = host->child_at(1);
menu_bar->SetBoundsRect(menu_Bar_bounds);
views::View* web_view = host->child_at(0);
views::View* menu_bar = host->child_at(1);
web_view->SetBoundsRect(web_view_bounds);
menu_bar->SetBoundsRect(menu_Bar_bounds);
}
gfx::Size MenuLayout::GetPreferredSize(views::View* host) {

Просмотреть файл

@ -48,6 +48,9 @@ const char kWebPreferences[] = "web-preferences";
// The factor of which page should be zoomed.
const char kZoomFactor[] = "zoom-factor";
// The menu bar is hidden unless "Alt" is pressed.
const char kAutoHideMenuBar[] = "auto-hide-menu-bar";
} // namespace switches
} // namespace atom

Просмотреть файл

@ -32,6 +32,7 @@ extern const char kAcceptFirstMouse[];
extern const char kUseContentSize[];
extern const char kWebPreferences[];
extern const char kZoomFactor[];
extern const char kAutoHideMenuBar[];
} // namespace switches

Просмотреть файл

@ -55,6 +55,8 @@ You can also create a window without chrome by using
`manual-enable-iframe` or `disable`.
* `accept-first-mouse` Boolean - Whether the web view accepts a single
mouse-down event that simultaneously activates the window
* `auto-hide-menu-bar` Boolean - Auto hide the menu bar unless the `Alt`
key is pressed.
* `web-preferences` Object - Settings of web page's features
* `javascript` Boolean
* `web-security` Boolean