Bug 1212527 - Set background color to NSColor before calling showDefinitionForAttributedString on 10.11. r?mstange draft
authorMakoto Kato <m_kato@ga2.so-net.ne.jp>
Tue, 24 May 2016 18:32:04 +0900
changeset 370569 995ee4a1df3136532d5a7e1a47ccfefe9bc97370
parent 370474 5511d54a3f172c1d68f98cc55dce4de1d0ba1b51
child 521789 39490d97a07c5afaa07e3a2488d29c335c27abc3
push id19105
push userm_kato@ga2.so-net.ne.jp
push dateWed, 25 May 2016 01:14:33 +0000
reviewersmstange
bugs1212527
milestone49.0a1
Bug 1212527 - Set background color to NSColor before calling showDefinitionForAttributedString on 10.11. r?mstange When calling showDefinitionforAttributedString on OSX 10.11, it always return error due to the following exception. Exception: decodeObjectForKey: class "TitlebarAndBackgroundColor" not loaded or does not exist So before calling it, we set temporary color value, then restore color after calling it. MozReview-Commit-ID: 1YeY6X1W6AV
widget/cocoa/nsChildView.mm
widget/cocoa/nsCocoaWindow.h
widget/cocoa/nsCocoaWindow.mm
--- a/widget/cocoa/nsChildView.mm
+++ b/widget/cocoa/nsChildView.mm
@@ -2861,16 +2861,46 @@ nsChildView::DispatchAPZWheelInputEvent(
       return;
   }
   if (event.mMessage == eWheel &&
       (event.mDeltaX != 0 || event.mDeltaY != 0)) {
     DispatchEvent(&event, status);
   }
 }
 
+// When using 10.11, calling showDefinitionForAttributedString causes the
+// following exception on LookupViewService.
+//
+// Exception: decodeObjectForKey: class "TitlebarAndBackgroundColor" not
+// loaded or does not exist
+//
+// So we set temporary color that is NSColor before calling it.
+
+class MOZ_RAII AutoBackgroundSetter final {
+public:
+  AutoBackgroundSetter(NSView* aView) {
+    if (nsCocoaFeatures::OnElCapitanOrLater() &&
+        [[aView window] isKindOfClass:[ToolbarWindow class]]) {
+      mWindow = (ToolbarWindow*)[aView window];
+      [mWindow setTemporaryBackgroundColor];
+    } else {
+      mWindow = nullptr;
+    }
+  }
+
+  ~AutoBackgroundSetter() {
+    if (mWindow) {
+      [mWindow restoreBackgroundColor];
+    }
+  }
+
+private:
+  ToolbarWindow* mWindow;
+};
+
 void
 nsChildView::LookUpDictionary(
                const nsAString& aText,
                const nsTArray<mozilla::FontRange>& aFontRangeArray,
                const bool aIsVertical,
                const LayoutDeviceIntPoint& aPoint)
 {
   NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
@@ -2885,16 +2915,18 @@ nsChildView::LookUpDictionary(
   NSFont* font = [attributes objectForKey:NSFontAttributeName];
   if (font) {
     if (aIsVertical) {
       pt.x -= [font descender];
     } else {
       pt.y += [font ascender];
     }
   }
+
+  AutoBackgroundSetter setter(mView);
   [mView showDefinitionForAttributedString:attrStr atPoint:pt];
 
   NS_OBJC_END_TRY_ABORT_BLOCK;
 }
 
 #ifdef ACCESSIBILITY
 already_AddRefed<a11y::Accessible>
 nsChildView::GetDocumentAccessible()
--- a/widget/cocoa/nsCocoaWindow.h
+++ b/widget/cocoa/nsCocoaWindow.h
@@ -236,16 +236,18 @@ typedef struct _nsCocoaWindowList {
 - (void)setTitlebarNeedsDisplayInRect:(NSRect)aRect sync:(BOOL)aSync;
 - (void)setTitlebarNeedsDisplayInRect:(NSRect)aRect;
 - (void)setDrawsContentsIntoWindowFrame:(BOOL)aState;
 - (void)setSheetAttachmentPosition:(CGFloat)aY;
 - (void)placeWindowButtons:(NSRect)aRect;
 - (void)placeFullScreenButton:(NSRect)aRect;
 - (NSPoint)windowButtonsPositionWithDefaultPosition:(NSPoint)aDefaultPosition;
 - (NSPoint)fullScreenButtonPositionWithDefaultPosition:(NSPoint)aDefaultPosition;
+- (void)setTemporaryBackgroundColor;
+- (void)restoreBackgroundColor;
 @end
 
 class nsCocoaWindow : public nsBaseWidget, public nsPIWidgetCocoa
 {
 private:
   typedef nsBaseWidget Inherited;
 
 public:
--- a/widget/cocoa/nsCocoaWindow.mm
+++ b/widget/cocoa/nsCocoaWindow.mm
@@ -3396,16 +3396,26 @@ static const NSString* kStateCollectionB
   mBackgroundColor = aColor;
 }
 
 - (NSColor*)windowBackgroundColor
 {
   return mBackgroundColor;
 }
 
+- (void)setTemporaryBackgroundColor
+{
+  [super setBackgroundColor:[NSColor clearColor]];
+}
+
+- (void)restoreBackgroundColor
+{
+  [super setBackgroundColor:mBackgroundColor];
+}
+
 - (void)setTitlebarNeedsDisplayInRect:(NSRect)aRect
 {
   [self setTitlebarNeedsDisplayInRect:aRect sync:NO];
 }
 
 - (void)setTitlebarNeedsDisplayInRect:(NSRect)aRect sync:(BOOL)aSync
 {
   NSRect titlebarRect = [self titlebarRect];