Skip to content

Commit e79a9ab

Browse files
committed
newwin: menu support
1 parent a062900 commit e79a9ab

File tree

3 files changed

+93
-39
lines changed

3 files changed

+93
-39
lines changed

src/api/nw_current_window_internal.idl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ namespace nw.currentWindowInternal {
3535
static void toggleKioskMode();
3636
static bool isKioskInternal();
3737
static void capturePageInternal(optional CapturePageOptions options, optional CapturePageCallback callback);
38-
static void clearMenu();
39-
static MenuPatch[] setMenu(long id);
38+
static void clearMenu(optional long win);
39+
static MenuPatch[] setMenu(long id, optional long wid);
4040
static void reloadIgnoringCache();
4141
static double getZoom();
4242
static void setZoom(double level);

src/api/nw_window_api.cc

Lines changed: 89 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include "chrome/browser/ui/browser.h"
1414
#include "chrome/browser/ui/browser_finder.h"
1515
#include "chrome/browser/ui/browser_window.h"
16+
#include "chrome/browser/ui/views/frame/browser_view.h"
17+
#include "chrome/browser/ui/views/frame/browser_view_layout.h"
1618
#include "components/zoom/zoom_controller.h"
1719
#include "content/nw/src/api/menu/menu.h"
1820
#include "content/nw/src/api/object_manager.h"
@@ -54,7 +56,6 @@
5456
#if defined(OS_LINUX) || defined(OS_WIN)
5557
#include "content/nw/src/browser/menubar_view.h"
5658
#include "content/nw/src/browser/browser_view_layout.h"
57-
using nw::BrowserViewLayout;
5859
#endif
5960

6061
#if defined(OS_MACOSX)
@@ -416,31 +417,57 @@ NwCurrentWindowInternalClearMenuFunction::~NwCurrentWindowInternalClearMenuFunct
416417
ExtensionFunction::ResponseAction
417418
NwCurrentWindowInternalClearMenuFunction::Run() {
418419
AppWindow* window = getAppWindow(this);
419-
if (!window) {
420-
error_ = kNoAssociatedAppWindow;
421-
return RespondNow(Error(error_));
420+
Browser* browser = nullptr;
421+
if (!base::FeatureList::IsEnabled(::features::kNWNewWin)) {
422+
if (!window) {
423+
error_ = kNoAssociatedAppWindow;
424+
return RespondNow(Error(error_));
425+
}
426+
} else {
427+
int wid = 0;
428+
args_->GetInteger(0, &wid);
429+
browser = getBrowser(this, wid);
430+
if (!browser) {
431+
error_ = kNoAssociatedAppWindow;
432+
return RespondNow(Error(error_));
433+
}
422434
}
423435

424436
#if defined(OS_MACOSX)
425437
NWChangeAppMenu(NULL);
426438
#endif
427439

428440
#if defined(OS_LINUX) || defined(OS_WIN)
429-
native_app_window::NativeAppWindowViews* native_app_window_views =
441+
if (!base::FeatureList::IsEnabled(::features::kNWNewWin)) {
442+
native_app_window::NativeAppWindowViews* native_app_window_views =
430443
static_cast<native_app_window::NativeAppWindowViews*>(
431444
window->GetBaseWindow());
432445

433-
BrowserViewLayout *browser_view_layout = static_cast<BrowserViewLayout*>(native_app_window_views->GetLayoutManager());
434-
views::View* menubar = browser_view_layout->menu_bar();
435-
if (menubar) {
436-
native_app_window_views->RemoveChildView(menubar);
437-
}
438-
browser_view_layout->set_menu_bar(NULL);
439-
native_app_window_views->layout_();
440-
native_app_window_views->SchedulePaint();
441-
if (window->menu_) {
442-
window->menu_->RemoveKeys();
443-
window->menu_ = NULL;
446+
nw::BrowserViewLayout *browser_view_layout = static_cast<nw::BrowserViewLayout*>(native_app_window_views->GetLayoutManager());
447+
views::View* menubar = browser_view_layout->menu_bar();
448+
if (menubar) {
449+
native_app_window_views->RemoveChildView(menubar);
450+
}
451+
browser_view_layout->set_menu_bar(NULL);
452+
native_app_window_views->layout_();
453+
native_app_window_views->SchedulePaint();
454+
if (window->menu_) {
455+
window->menu_->RemoveKeys();
456+
window->menu_ = nullptr;
457+
}
458+
} else {
459+
BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser);
460+
BrowserViewLayout* layout = browser_view->GetBrowserViewLayout();
461+
views::View* menubar = layout->menu_bar();
462+
if (menubar)
463+
browser_view->RemoveChildView(menubar);
464+
layout->set_menu_bar(nullptr);
465+
browser_view->Layout();
466+
browser_view->SchedulePaint();
467+
if (browser->nw_menu_) {
468+
browser->nw_menu_->RemoveKeys();
469+
browser->nw_menu_ = nullptr;
470+
}
444471
}
445472
#endif
446473
return RespondNow(NoArguments());
@@ -455,42 +482,69 @@ NwCurrentWindowInternalSetMenuFunction::~NwCurrentWindowInternalSetMenuFunction(
455482
bool NwCurrentWindowInternalSetMenuFunction::RunNWSync(base::ListValue* response, std::string* error) {
456483
int id = 0;
457484
EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &id));
458-
AppWindow* window = getAppWindow(this);
459-
if (!window) {
460-
error_ = kNoAssociatedAppWindow;
461-
return false;
462-
}
463485
nw::ObjectManager* obj_manager = nw::ObjectManager::Get(browser_context());
464486
Menu* menu = (Menu*)obj_manager->GetApiObject(id);
465-
487+
Browser* browser = nullptr;
488+
AppWindow* window = nullptr;
466489
#if defined(OS_LINUX) || defined(OS_WIN)
467-
Menu* old_menu = window->menu_;
490+
Menu* old_menu = nullptr;
468491
#endif
492+
if (base::FeatureList::IsEnabled(::features::kNWNewWin)) {
493+
int wid = 0;
494+
args_->GetInteger(1, &wid);
495+
browser = getBrowser(this, wid);
496+
if (!browser)
497+
return false;
498+
#if defined(OS_LINUX) || defined(OS_WIN)
499+
old_menu = browser->nw_menu_;
500+
#endif
501+
browser->nw_menu_ = menu;
502+
} else {
503+
window = getAppWindow(this);
504+
if (!window) {
505+
error_ = kNoAssociatedAppWindow;
506+
return false;
507+
}
508+
#if defined(OS_LINUX) || defined(OS_WIN)
509+
old_menu = window->menu_;
510+
#endif
511+
window->menu_ = menu;
512+
}
513+
469514

470-
window->menu_ = menu;
471-
472515
#if defined(OS_MACOSX)
473516
response->Append(NWChangeAppMenu(menu));
474517
#endif
475518

476519
#if defined(OS_LINUX) || defined(OS_WIN)
477-
native_app_window::NativeAppWindowViews* native_app_window_views =
520+
MenuBarView* menubar = new MenuBarView();
521+
if (base::FeatureList::IsEnabled(::features::kNWNewWin)) {
522+
BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser);
523+
browser_view->GetBrowserViewLayout()->set_menu_bar(menubar);
524+
browser_view->AddChildView(menubar);
525+
menubar->UpdateMenu(menu->model());
526+
browser_view->Layout();
527+
browser_view->SchedulePaint();
528+
if (old_menu) old_menu->RemoveKeys();
529+
menu->UpdateKeys(browser_view->GetWidget()->GetFocusManager());
530+
} else {
531+
native_app_window::NativeAppWindowViews* native_app_window_views =
478532
static_cast<native_app_window::NativeAppWindowViews*>(
479533
window->GetBaseWindow());
480534

481-
MenuBarView* menubar = new MenuBarView();
482-
static_cast<BrowserViewLayout*>(native_app_window_views->GetLayoutManager())->set_menu_bar(menubar);
483-
native_app_window_views->AddChildView(menubar);
484-
menubar->UpdateMenu(menu->model());
485-
native_app_window_views->layout_();
486-
native_app_window_views->SchedulePaint();
487-
if (old_menu) old_menu->RemoveKeys();
488-
menu->UpdateKeys( native_app_window_views->widget()->GetFocusManager() );
535+
static_cast<nw::BrowserViewLayout*>(native_app_window_views->GetLayoutManager())->set_menu_bar(menubar);
536+
native_app_window_views->AddChildView(menubar);
537+
menubar->UpdateMenu(menu->model());
538+
native_app_window_views->layout_();
539+
native_app_window_views->SchedulePaint();
540+
if (old_menu) old_menu->RemoveKeys();
541+
menu->UpdateKeys( native_app_window_views->widget()->GetFocusManager() );
542+
}
489543
response->Append(std::unique_ptr<base::ListValue>(new base::ListValue()));
490544
#endif
491545
return true;
492546
}
493-
547+
494548
#if defined(OS_WIN)
495549
static base::win::ScopedHICON createBadgeIcon(const HWND hWnd, const TCHAR *value, const int sizeX, const int sizeY) {
496550
// canvas for the overlay icon

src/resources/api_nw_newwin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,14 +616,14 @@ Object.defineProperty(NWWindow.prototype, 'menu', {
616616
set: function(menu) {
617617
if(!menu) {
618618
privates(this).menu = null;
619-
currentNWWindowInternal.clearMenu();
619+
currentNWWindowInternal.clearMenu(this.cWindow.id);
620620
return;
621621
}
622622
if (menu.type != 'menubar')
623623
throw new TypeError('Only menu of type "menubar" can be used as this.window menu');
624624

625625
privates(this).menu = menu;
626-
var menuPatch = currentNWWindowInternal.setMenu(menu.id);
626+
var menuPatch = currentNWWindowInternal.setMenu(menu.id, this.cWindow.id);
627627
if (menuPatch.length) {
628628
menuPatch.forEach((patch)=>{
629629
let menuIndex = patch.menu;

0 commit comments

Comments
 (0)