Merge pull request #817 from atom/menubar-dynamic-api

Add APIs to show/hide menu bar dynamically
This commit is contained in:
Cheng Zhao 2014-11-12 21:02:13 +08:00
commit 5ee805e451
7 changed files with 176 additions and 97 deletions

View file

@ -378,6 +378,22 @@ void Window::SetProgressBar(double progress) {
window_->SetProgressBar(progress); window_->SetProgressBar(progress);
} }
void Window::SetAutoHideMenuBar(bool auto_hide) {
window_->SetAutoHideMenuBar(auto_hide);
}
bool Window::IsMenuBarAutoHide() {
return window_->IsMenuBarAutoHide();
}
void Window::SetMenuBarVisibility(bool visible) {
window_->SetMenuBarVisibility(visible);
}
bool Window::IsMenuBarVisible() {
return window_->IsMenuBarVisible();
}
mate::Handle<WebContents> Window::GetWebContents(v8::Isolate* isolate) const { mate::Handle<WebContents> Window::GetWebContents(v8::Isolate* isolate) const {
return WebContents::CreateFrom(isolate, window_->GetWebContents()); return WebContents::CreateFrom(isolate, window_->GetWebContents());
} }
@ -443,6 +459,10 @@ void Window::BuildPrototype(v8::Isolate* isolate,
.SetMethod("capturePage", &Window::CapturePage) .SetMethod("capturePage", &Window::CapturePage)
.SetMethod("print", &Window::Print) .SetMethod("print", &Window::Print)
.SetMethod("setProgressBar", &Window::SetProgressBar) .SetMethod("setProgressBar", &Window::SetProgressBar)
.SetMethod("setAutoHideMenuBar", &Window::SetAutoHideMenuBar)
.SetMethod("isMenuBarAutoHide", &Window::IsMenuBarAutoHide)
.SetMethod("setMenuBarVisibility", &Window::SetMenuBarVisibility)
.SetMethod("isMenuBarVisible", &Window::IsMenuBarVisible)
.SetMethod("_getWebContents", &Window::GetWebContents) .SetMethod("_getWebContents", &Window::GetWebContents)
.SetMethod("_getDevToolsWebContents", &Window::GetDevToolsWebContents); .SetMethod("_getDevToolsWebContents", &Window::GetDevToolsWebContents);
} }

View file

@ -111,6 +111,10 @@ class Window : public mate::EventEmitter,
void CapturePage(mate::Arguments* args); void CapturePage(mate::Arguments* args);
void Print(mate::Arguments* args); void Print(mate::Arguments* args);
void SetProgressBar(double progress); void SetProgressBar(double progress);
void SetAutoHideMenuBar(bool auto_hide);
bool IsMenuBarAutoHide();
void SetMenuBarVisibility(bool visible);
bool IsMenuBarVisible();
// APIs for WebContents. // APIs for WebContents.
mate::Handle<WebContents> GetWebContents(v8::Isolate* isolate) const; mate::Handle<WebContents> GetWebContents(v8::Isolate* isolate) const;

View file

@ -243,6 +243,20 @@ void NativeWindow::Print(bool silent, bool print_background) {
PrintNow(silent, print_background); PrintNow(silent, print_background);
} }
void NativeWindow::SetAutoHideMenuBar(bool auto_hide) {
}
bool NativeWindow::IsMenuBarAutoHide() {
return false;
}
void NativeWindow::SetMenuBarVisibility(bool visible) {
}
bool NativeWindow::IsMenuBarVisible() {
return true;
}
bool NativeWindow::HasModalDialog() { bool NativeWindow::HasModalDialog() {
return has_dialog_attached_; return has_dialog_attached_;
} }

View file

@ -161,6 +161,12 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate,
// Print current page. // Print current page.
virtual void Print(bool silent, bool print_background); virtual void Print(bool silent, bool print_background);
// Toggle the menu bar.
virtual void SetAutoHideMenuBar(bool auto_hide);
virtual bool IsMenuBarAutoHide();
virtual void SetMenuBarVisibility(bool visible);
virtual bool IsMenuBarVisible();
// The same with closing a tab in a real browser. // The same with closing a tab in a real browser.
// //
// Should be called by platform code when user want to close the window. // Should be called by platform code when user want to close the window.

View file

@ -237,6 +237,8 @@ NativeWindowViews::NativeWindowViews(content::WebContents* web_contents,
} }
#endif #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_) { if (has_frame_) {
window_->set_frame_type(views::Widget::FrameType::FRAME_TYPE_FORCE_NATIVE); window_->set_frame_type(views::Widget::FrameType::FRAME_TYPE_FORCE_NATIVE);
window_->FrameTypeChanged(); window_->FrameTypeChanged();
@ -559,6 +561,38 @@ void NativeWindowViews::SetProgressBar(double progress) {
#endif #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() { gfx::AcceleratedWidget NativeWindowViews::GetAcceleratedWidget() {
return GetNativeWindow()->GetHost()->GetAcceleratedWidget(); return GetNativeWindow()->GetHost()->GetAcceleratedWidget();
} }
@ -600,10 +634,8 @@ void NativeWindowViews::OnWidgetActivationChanged(
GetWebContents()->Focus(); GetWebContents()->Focus();
// Hide menu bar when window is blured. // 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); SetMenuBarVisibility(false);
Layout();
}
} }
void NativeWindowViews::DeleteDelegate() { void NativeWindowViews::DeleteDelegate() {
@ -695,10 +727,8 @@ gfx::ImageSkia NativeWindowViews::GetDevToolsWindowIcon() {
void NativeWindowViews::HandleMouseDown() { void NativeWindowViews::HandleMouseDown() {
// Hide menu bar when web view is clicked. // Hide menu bar when web view is clicked.
if (menu_bar_autohide_ && menu_bar_visible_) { if (menu_bar_autohide_ && menu_bar_visible_)
SetMenuBarVisibility(false); SetMenuBarVisibility(false);
Layout();
}
} }
void NativeWindowViews::HandleKeyboardEvent( void NativeWindowViews::HandleKeyboardEvent(
@ -718,10 +748,8 @@ void NativeWindowViews::HandleKeyboardEvent(
if (event.type == blink::WebInputEvent::RawKeyDown && !IsAltKey(event) && if (event.type == blink::WebInputEvent::RawKeyDown && !IsAltKey(event) &&
IsAltModifier(event)) { IsAltModifier(event)) {
if (!menu_bar_visible_ && if (!menu_bar_visible_ &&
(menu_bar_->GetAcceleratorIndex(event.windowsKeyCode) != -1)) { (menu_bar_->GetAcceleratorIndex(event.windowsKeyCode) != -1))
SetMenuBarVisibility(true); SetMenuBarVisibility(true);
Layout();
}
menu_bar_->ActivateAccelerator(event.windowsKeyCode); menu_bar_->ActivateAccelerator(event.windowsKeyCode);
return; return;
} }
@ -741,7 +769,6 @@ void NativeWindowViews::HandleKeyboardEvent(
// When a single Alt is released right after a Alt is pressed: // When a single Alt is released right after a Alt is pressed:
menu_bar_alt_pressed_ = false; menu_bar_alt_pressed_ = false;
SetMenuBarVisibility(!menu_bar_visible_); SetMenuBarVisibility(!menu_bar_visible_);
Layout();
} else { } else {
// When any other keys except single Alt have been pressed/released: // When any other keys except single Alt have been pressed/released:
menu_bar_alt_pressed_ = false; menu_bar_alt_pressed_ = false;
@ -779,24 +806,6 @@ gfx::Rect NativeWindowViews::ContentBoundsToWindowBounds(
return window_bounds; 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 // static
NativeWindow* NativeWindow::Create(content::WebContents* web_contents, NativeWindow* NativeWindow::Create(content::WebContents* web_contents,
const mate::Dictionary& options) { const mate::Dictionary& options) {

View file

@ -32,47 +32,51 @@ class NativeWindowViews : public NativeWindow,
virtual ~NativeWindowViews(); virtual ~NativeWindowViews();
// NativeWindow: // NativeWindow:
virtual void Close() OVERRIDE; void Close() override;
virtual void CloseImmediately() OVERRIDE; void CloseImmediately() override;
virtual void Move(const gfx::Rect& pos) OVERRIDE; void Move(const gfx::Rect& pos) override;
virtual void Focus(bool focus) OVERRIDE; void Focus(bool focus) override;
virtual bool IsFocused() OVERRIDE; bool IsFocused() override;
virtual void Show() OVERRIDE; void Show() override;
virtual void ShowInactive() OVERRIDE; void ShowInactive() override;
virtual void Hide() OVERRIDE; void Hide() override;
virtual bool IsVisible() OVERRIDE; bool IsVisible() override;
virtual void Maximize() OVERRIDE; void Maximize() override;
virtual void Unmaximize() OVERRIDE; void Unmaximize() override;
virtual bool IsMaximized() OVERRIDE; bool IsMaximized() override;
virtual void Minimize() OVERRIDE; void Minimize() override;
virtual void Restore() OVERRIDE; void Restore() override;
virtual bool IsMinimized() OVERRIDE; bool IsMinimized() override;
virtual void SetFullscreen(bool fullscreen) OVERRIDE; void SetFullscreen(bool fullscreen) override;
virtual bool IsFullscreen() OVERRIDE; bool IsFullscreen() override;
virtual void SetSize(const gfx::Size& size) OVERRIDE; void SetSize(const gfx::Size& size) override;
virtual gfx::Size GetSize() OVERRIDE; gfx::Size GetSize() override;
virtual void SetContentSize(const gfx::Size& size) OVERRIDE; void SetContentSize(const gfx::Size& size) override;
virtual gfx::Size GetContentSize() OVERRIDE; gfx::Size GetContentSize() override;
virtual void SetMinimumSize(const gfx::Size& size) OVERRIDE; void SetMinimumSize(const gfx::Size& size) override;
virtual gfx::Size GetMinimumSize() OVERRIDE; gfx::Size GetMinimumSize() override;
virtual void SetMaximumSize(const gfx::Size& size) OVERRIDE; void SetMaximumSize(const gfx::Size& size) override;
virtual gfx::Size GetMaximumSize() OVERRIDE; gfx::Size GetMaximumSize() override;
virtual void SetResizable(bool resizable) OVERRIDE; void SetResizable(bool resizable) override;
virtual bool IsResizable() OVERRIDE; bool IsResizable() override;
virtual void SetAlwaysOnTop(bool top) OVERRIDE; void SetAlwaysOnTop(bool top) override;
virtual bool IsAlwaysOnTop() OVERRIDE; bool IsAlwaysOnTop() override;
virtual void Center() OVERRIDE; void Center() override;
virtual void SetPosition(const gfx::Point& position) OVERRIDE; void SetPosition(const gfx::Point& position) override;
virtual gfx::Point GetPosition() OVERRIDE; gfx::Point GetPosition() override;
virtual void SetTitle(const std::string& title) OVERRIDE; void SetTitle(const std::string& title) override;
virtual std::string GetTitle() OVERRIDE; std::string GetTitle() override;
virtual void FlashFrame(bool flash) OVERRIDE; void FlashFrame(bool flash) override;
virtual void SetSkipTaskbar(bool skip) OVERRIDE; void SetSkipTaskbar(bool skip) override;
virtual void SetKiosk(bool kiosk) OVERRIDE; void SetKiosk(bool kiosk) override;
virtual bool IsKiosk() OVERRIDE; bool IsKiosk() override;
virtual void SetMenu(ui::MenuModel* menu_model) OVERRIDE; void SetMenu(ui::MenuModel* menu_model) override;
virtual gfx::NativeWindow GetNativeWindow() OVERRIDE; gfx::NativeWindow GetNativeWindow() override;
virtual void SetProgressBar(double value) 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(); gfx::AcceleratedWidget GetAcceleratedWidget();
@ -81,43 +85,43 @@ class NativeWindowViews : public NativeWindow,
private: private:
// NativeWindow: // NativeWindow:
virtual void UpdateDraggableRegions( void UpdateDraggableRegions(
const std::vector<DraggableRegion>& regions) OVERRIDE; const std::vector<DraggableRegion>& regions) override;
// views::WidgetObserver: // views::WidgetObserver:
virtual void OnWidgetActivationChanged( void OnWidgetActivationChanged(
views::Widget* widget, bool active) OVERRIDE; views::Widget* widget, bool active) override;
// views::WidgetDelegate: // views::WidgetDelegate:
virtual void DeleteDelegate() OVERRIDE; void DeleteDelegate() override;
virtual views::View* GetInitiallyFocusedView() OVERRIDE; views::View* GetInitiallyFocusedView() override;
virtual bool CanResize() const OVERRIDE; bool CanResize() const override;
virtual bool CanMaximize() const OVERRIDE; bool CanMaximize() const override;
virtual base::string16 GetWindowTitle() const OVERRIDE; base::string16 GetWindowTitle() const override;
virtual bool ShouldHandleSystemCommands() const OVERRIDE; bool ShouldHandleSystemCommands() const override;
virtual gfx::ImageSkia GetWindowAppIcon() OVERRIDE; gfx::ImageSkia GetWindowAppIcon() override;
virtual gfx::ImageSkia GetWindowIcon() OVERRIDE; gfx::ImageSkia GetWindowIcon() override;
virtual views::Widget* GetWidget() OVERRIDE; views::Widget* GetWidget() override;
virtual const views::Widget* GetWidget() const OVERRIDE; const views::Widget* GetWidget() const override;
virtual views::View* GetContentsView() OVERRIDE; views::View* GetContentsView() override;
virtual bool ShouldDescendIntoChildForEventHandling( bool ShouldDescendIntoChildForEventHandling(
gfx::NativeView child, gfx::NativeView child,
const gfx::Point& location) OVERRIDE; const gfx::Point& location) override;
virtual views::ClientView* CreateClientView(views::Widget* widget) OVERRIDE; views::ClientView* CreateClientView(views::Widget* widget) override;
virtual views::NonClientFrameView* CreateNonClientFrameView( views::NonClientFrameView* CreateNonClientFrameView(
views::Widget* widget) OVERRIDE; views::Widget* widget) override;
// brightray::InspectableWebContentsDelegate: // brightray::InspectableWebContentsDelegate:
virtual gfx::ImageSkia GetDevToolsWindowIcon() OVERRIDE; gfx::ImageSkia GetDevToolsWindowIcon() override;
// content::WebContentsDelegate: // content::WebContentsDelegate:
virtual void HandleMouseDown() OVERRIDE; void HandleMouseDown() override;
virtual void HandleKeyboardEvent( void HandleKeyboardEvent(
content::WebContents*, content::WebContents*,
const content::NativeWebKeyboardEvent& event) OVERRIDE; const content::NativeWebKeyboardEvent& event) override;
// views::View: // views::View:
virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE; bool AcceleratorPressed(const ui::Accelerator& accelerator) override;
// Register accelerators supported by the menu model. // Register accelerators supported by the menu model.
void RegisterAccelerators(ui::MenuModel* menu_model); void RegisterAccelerators(ui::MenuModel* menu_model);
@ -126,9 +130,6 @@ class NativeWindowViews : public NativeWindow,
// in client area we need to substract/add menu bar's height in convertions. // in client area we need to substract/add menu bar's height in convertions.
gfx::Rect ContentBoundsToWindowBounds(const gfx::Rect& content_bounds); gfx::Rect ContentBoundsToWindowBounds(const gfx::Rect& content_bounds);
// Show/Hide the menu bar.
void SetMenuBarVisibility(bool visible);
scoped_ptr<views::Widget> window_; scoped_ptr<views::Widget> window_;
views::View* web_view_; // Managed by inspectable_web_contents_. views::View* web_view_; // Managed by inspectable_web_contents_.

View file

@ -506,6 +506,31 @@ On Linux platform, only supports Unity desktop environment, you need to specify
the `*.desktop` file name to `desktopName` field in `package.json`. By default, the `*.desktop` file name to `desktopName` field in `package.json`. By default,
it will assume `app.getName().desktop`. it will assume `app.getName().desktop`.
### BrowserWindow.setAutoHideMenuBar(hide)
* `hide` Boolean
Sets whether the window menu bar should hide itself automatically. Once set the
menu bar will only show when users press the single `Alt` key.
If the menu bar is already visible, calling `setAutoHideMenuBar(true)` won't
hide it immediately.
### BrowserWindow.isMenuBarAutoHide()
Returns whether menu bar automatically hides itself.
### BrowserWindow.setMenuBarVisibility(visible)
* `visible` Boolean
Sets whether the menu bar should be visible. If the menu bar is auto-hide, users
can still bring up the menu bar by pressing the single `Alt` key.
### BrowserWindow.isMenuBarVisible()
Returns whether the menu bar is visible.
## Class: WebContents ## Class: WebContents
A `WebContents` is responsible for rendering and controlling a web page. A `WebContents` is responsible for rendering and controlling a web page.