Skip to content

Commit

Permalink
Bug 1694853 - Stop giving the nsMenuBarX a reference to the widget. r…
Browse files Browse the repository at this point in the history
…=harry

Differential Revision: https://phabricator.services.mozilla.com/D106379
  • Loading branch information
mstange committed Mar 2, 2021
1 parent 419b917 commit 168f75a
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 24 deletions.
6 changes: 5 additions & 1 deletion widget/cocoa/NativeMenuSupport.mm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "mozilla/widget/NativeMenuSupport.h"

#include "MainThreadUtils.h"
#include "nsCocoaWindow.h"
#include "nsMenuBarX.h"

namespace mozilla::widget {
Expand All @@ -15,8 +16,11 @@

RefPtr<nsMenuBarX> mb = new nsMenuBarX();

nsresult rv = mb->Create(aParent, aMenuBarElement);
nsresult rv = mb->Create(aMenuBarElement);
MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));

// Give the menubar to the parent window. The parent takes ownership.
static_cast<nsCocoaWindow*>(aParent)->SetMenuBar(std::move(mb));
}

} // namespace mozilla::widget
2 changes: 1 addition & 1 deletion widget/cocoa/nsCocoaWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ class nsCocoaWindow final : public nsBaseWidget, public nsPIWidgetCocoa {
bool HasModalDescendents() { return mNumModalDescendents > 0; }
NSWindow* GetCocoaWindow() { return mWindow; }

void SetMenuBar(nsMenuBarX* aMenuBar);
void SetMenuBar(RefPtr<nsMenuBarX>&& aMenuBar);
nsMenuBarX* GetMenuBar();

virtual void SetInputContext(const InputContext& aContext,
Expand Down
5 changes: 2 additions & 3 deletions widget/cocoa/nsCocoaWindow.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2130,13 +2130,12 @@ static nsSizeMode GetWindowSizeMode(NSWindow* aWindow, bool aFullScreen) {
Unused << bc->SetExplicitActive(ExplicitActiveStatus::Active);
}

void nsCocoaWindow::SetMenuBar(nsMenuBarX* aMenuBar) {
if (mMenuBar) mMenuBar->SetParent(nullptr);
void nsCocoaWindow::SetMenuBar(RefPtr<nsMenuBarX>&& aMenuBar) {
if (!mWindow) {
mMenuBar = nullptr;
return;
}
mMenuBar = aMenuBar;
mMenuBar = std::move(aMenuBar);

// Only paint for active windows, or paint the hidden window menu bar if no
// other menu bar has been painted yet so that some reasonable menu bar is
Expand Down
4 changes: 1 addition & 3 deletions widget/cocoa/nsMenuBarX.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ class nsMenuBarX : public nsMenuGroupOwnerX, public nsChangeObserver {
nsMenuObjectTypeX MenuObjectType() override { return eMenuBarObjectType; }

// nsMenuBarX
nsresult Create(nsIWidget* aParent, mozilla::dom::Element* aElement);
void SetParent(nsIWidget* aParent);
nsresult Create(mozilla::dom::Element* aElement);
uint32_t GetMenuCount();
bool MenuContainsAppMenu();
nsMenuX* GetMenuAt(uint32_t aIndex);
Expand All @@ -125,7 +124,6 @@ class nsMenuBarX : public nsMenuGroupOwnerX, public nsChangeObserver {
void CreateApplicationMenu(nsMenuX* aMenu);

nsTArray<mozilla::UniquePtr<nsMenuX>> mMenuArray;
nsIWidget* mParentWindow; // [weak]
GeckoNSMenu* mNativeMenu; // root menu, representing entire menu bar
bool mNeedsRebuild;
ApplicationMenuDelegate* mApplicationMenuDelegate;
Expand Down
19 changes: 3 additions & 16 deletions widget/cocoa/nsMenuBarX.mm
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "nsMenuItemX.h"
#include "nsMenuUtilsX.h"
#include "nsCocoaUtils.h"
#include "nsCocoaWindow.h"
#include "nsChildView.h"

#include "nsCOMPtr.h"
Expand All @@ -32,6 +31,7 @@
#include "mozilla/dom/Element.h"

using namespace mozilla;
using mozilla::dom::Element;

NativeMenuItemTarget* nsMenuBarX::sNativeEventTarget = nil;
nsMenuBarX* nsMenuBarX::sLastGeckoMenuBarPainted = nullptr;
Expand Down Expand Up @@ -73,10 +73,7 @@ - (void)menuDidClose:(NSMenu*)menu {
@end

nsMenuBarX::nsMenuBarX()
: nsMenuGroupOwnerX(),
mParentWindow(nullptr),
mNeedsRebuild(false),
mApplicationMenuDelegate(nil) {
: nsMenuGroupOwnerX(), mNeedsRebuild(false), mApplicationMenuDelegate(nil) {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;

mNativeMenu = [[GeckoNSMenu alloc] initWithTitle:@"MainMenuBar"];
Expand Down Expand Up @@ -123,14 +120,9 @@ - (void)menuDidClose:(NSMenu*)menu {
NS_OBJC_END_TRY_ABORT_BLOCK;
}

nsresult nsMenuBarX::Create(nsIWidget* aParent, Element* aContent) {
nsresult nsMenuBarX::Create(Element* aContent) {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;

if (!aParent) {
return NS_ERROR_INVALID_ARG;
}

mParentWindow = aParent;
mContent = aContent;

if (mContent) {
Expand All @@ -147,9 +139,6 @@ - (void)menuDidClose:(NSMenu*)menu {
ConstructFallbackNativeMenus();
}

// Give this to the parent window. The parent takes ownership.
static_cast<nsCocoaWindow*>(mParentWindow)->SetMenuBar(this);

return NS_OK;

NS_OBJC_END_TRY_ABORT_BLOCK;
Expand Down Expand Up @@ -793,8 +782,6 @@ developers need only add each node with a label and a key equivalent (if they
NS_OBJC_END_TRY_ABORT_BLOCK;
}

void nsMenuBarX::SetParent(nsIWidget* aParent) { mParentWindow = aParent; }

//
// Objective-C class used to allow us to have keyboard commands
// look like they are doing something but actually do nothing.
Expand Down

0 comments on commit 168f75a

Please sign in to comment.