views: Implement menubar APIs

This commit is contained in:
Cheng Zhao 2014-11-12 20:32:14 +08:00
parent cb8f975528
commit 1cd3918494
2 changed files with 41 additions and 31 deletions

View file

@ -237,6 +237,8 @@ NativeWindowViews::NativeWindowViews(content::WebContents* web_contents,
}
#endif
// TODO(zcbenz): This was used to force using native frame on Windows 2003, we
// should check whether setting it in InitParams can work.
if (has_frame_) {
window_->set_frame_type(views::Widget::FrameType::FRAME_TYPE_FORCE_NATIVE);
window_->FrameTypeChanged();
@ -559,6 +561,38 @@ void NativeWindowViews::SetProgressBar(double progress) {
#endif
}
void NativeWindowViews::SetAutoHideMenuBar(bool auto_hide) {
menu_bar_autohide_ = auto_hide;
}
bool NativeWindowViews::IsMenuBarAutoHide() {
return menu_bar_autohide_;
}
void NativeWindowViews::SetMenuBarVisibility(bool visible) {
if (!menu_bar_ || menu_bar_visible_ == visible)
return;
// Always show the accelerator when the auto-hide menu bar shows.
if (menu_bar_autohide_)
menu_bar_->SetAcceleratorVisibility(visible);
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());
}
Layout();
}
bool NativeWindowViews::IsMenuBarVisible() {
return menu_bar_visible_;
}
gfx::AcceleratedWidget NativeWindowViews::GetAcceleratedWidget() {
return GetNativeWindow()->GetHost()->GetAcceleratedWidget();
}
@ -600,10 +634,8 @@ void NativeWindowViews::OnWidgetActivationChanged(
GetWebContents()->Focus();
// Hide menu bar when window is blured.
if (!active && menu_bar_autohide_ && menu_bar_visible_) {
if (!active && menu_bar_autohide_ && menu_bar_visible_)
SetMenuBarVisibility(false);
Layout();
}
}
void NativeWindowViews::DeleteDelegate() {
@ -695,10 +727,8 @@ gfx::ImageSkia NativeWindowViews::GetDevToolsWindowIcon() {
void NativeWindowViews::HandleMouseDown() {
// Hide menu bar when web view is clicked.
if (menu_bar_autohide_ && menu_bar_visible_) {
if (menu_bar_autohide_ && menu_bar_visible_)
SetMenuBarVisibility(false);
Layout();
}
}
void NativeWindowViews::HandleKeyboardEvent(
@ -718,10 +748,8 @@ void NativeWindowViews::HandleKeyboardEvent(
if (event.type == blink::WebInputEvent::RawKeyDown && !IsAltKey(event) &&
IsAltModifier(event)) {
if (!menu_bar_visible_ &&
(menu_bar_->GetAcceleratorIndex(event.windowsKeyCode) != -1)) {
(menu_bar_->GetAcceleratorIndex(event.windowsKeyCode) != -1))
SetMenuBarVisibility(true);
Layout();
}
menu_bar_->ActivateAccelerator(event.windowsKeyCode);
return;
}
@ -741,7 +769,6 @@ void NativeWindowViews::HandleKeyboardEvent(
// 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;
@ -779,24 +806,6 @@ gfx::Rect NativeWindowViews::ContentBoundsToWindowBounds(
return window_bounds;
}
void NativeWindowViews::SetMenuBarVisibility(bool visible) {
if (!menu_bar_)
return;
// Always show the accelerator when the auto-hide menu bar shows.
if (menu_bar_autohide_)
menu_bar_->SetAcceleratorVisibility(visible);
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) {

View file

@ -73,6 +73,10 @@ class NativeWindowViews : public NativeWindow,
void SetMenu(ui::MenuModel* menu_model) override;
gfx::NativeWindow GetNativeWindow() override;
void SetProgressBar(double value) override;
void SetAutoHideMenuBar(bool auto_hide) override;
bool IsMenuBarAutoHide() override;
void SetMenuBarVisibility(bool visible) override;
bool IsMenuBarVisible() override;
gfx::AcceleratedWidget GetAcceleratedWidget();
@ -126,9 +130,6 @@ 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_;
views::View* web_view_; // Managed by inspectable_web_contents_.