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
--- 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];