2 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
23 #import "RenderThemeMac.h"
25 #import "BitmapImage.h"
26 #import "CSSValueKeywords.h"
27 #import "CSSValueList.h"
29 #import "CoreGraphicsSPI.h"
32 #import "ExceptionCodePlaceholder.h"
34 #import "FloatRoundedRect.h"
35 #import "FocusController.h"
37 #import "FrameSelection.h"
39 #import "GraphicsContextCG.h"
40 #import "HTMLAttachmentElement.h"
41 #import "HTMLAudioElement.h"
42 #import "HTMLInputElement.h"
43 #import "HTMLMediaElement.h"
45 #import "HTMLPlugInImageElement.h"
48 #import "ImageBuffer.h"
49 #import "LocalCurrentGraphicsContext.h"
50 #import "LocalizedStrings.h"
51 #import "MediaControlElements.h"
52 #import "NSColorSPI.h"
53 #import "NSSharingServicePickerSPI.h"
56 #import "PathUtilities.h"
57 #import "RenderAttachment.h"
58 #import "RenderLayer.h"
59 #import "RenderMedia.h"
60 #import "RenderMediaControlElements.h"
61 #import "RenderMediaControls.h"
62 #import "RenderProgress.h"
63 #import "RenderSlider.h"
64 #import "RenderSnapshottedPlugIn.h"
65 #import "RenderView.h"
66 #import "SharedBuffer.h"
67 #import "StringTruncator.h"
68 #import "StyleResolver.h"
70 #import "TimeRanges.h"
71 #import "UserAgentScripts.h"
72 #import "UserAgentStyleSheets.h"
73 #import "WebCoreSystemInterface.h"
74 #import <wtf/RetainPtr.h>
75 #import <wtf/RetainPtr.h>
76 #import <wtf/StdLibExtras.h>
77 #import <wtf/text/StringBuilder.h>
78 #import <Carbon/Carbon.h>
79 #import <Cocoa/Cocoa.h>
82 #if ENABLE(METER_ELEMENT)
83 #import "RenderMeter.h"
84 #import "HTMLMeterElement.h"
87 #if defined(__LP64__) && __LP64__
88 #define HAVE_APPKIT_SERVICE_CONTROLS_SUPPORT 1
90 #define HAVE_APPKIT_SERVICE_CONTROLS_SUPPORT 0
93 #if ENABLE(SERVICE_CONTROLS) && HAVE(APPKIT_SERVICE_CONTROLS_SUPPORT)
95 #if USE(APPLE_INTERNAL_SDK)
96 #import <AppKit/AppKitDefines_Private.h>
97 #import <AppKit/NSServicesRolloverButtonCell.h>
99 #define APPKIT_PRIVATE_CLASS
100 @interface NSServicesRolloverButtonCell : NSButtonCell
104 @interface NSServicesRolloverButtonCell ()
105 + (NSServicesRolloverButtonCell *)serviceRolloverButtonCellForStyle:(NSSharingServicePickerStyle)style;
106 - (NSRect)rectForBounds:(NSRect)bounds preferredEdge:(NSRectEdge)preferredEdge;
109 #endif // ENABLE(SERVICE_CONTROLS)
111 // The methods in this file are specific to the Mac OS X platform.
113 // We estimate the animation rate of a Mac OS X progress bar is 33 fps.
114 // Hard code the value here because we haven't found API for it.
115 const double progressAnimationFrameRate = 0.033;
117 // Mac OS X progress bar animation seems to have 256 frames.
118 const double progressAnimationNumFrames = 256;
120 @interface WebCoreRenderThemeNotificationObserver : NSObject
122 WebCore::RenderTheme *_theme;
125 - (id)initWithTheme:(WebCore::RenderTheme *)theme;
126 - (void)systemColorsDidChange:(NSNotification *)notification;
130 @implementation WebCoreRenderThemeNotificationObserver
132 - (id)initWithTheme:(WebCore::RenderTheme *)theme
134 if (!(self = [super init]))
141 - (void)systemColorsDidChange:(NSNotification *)unusedNotification
143 ASSERT_UNUSED(unusedNotification, [[unusedNotification name] isEqualToString:NSSystemColorsDidChangeNotification]);
144 _theme->platformColorsDidChange();
149 @interface NSTextFieldCell (WKDetails)
150 - (CFDictionaryRef)_coreUIDrawOptionsWithFrame:(NSRect)cellFrame inView:(NSView *)controlView includeFocus:(BOOL)includeFocus;
154 @interface WebCoreTextFieldCell : NSTextFieldCell
155 - (CFDictionaryRef)_coreUIDrawOptionsWithFrame:(NSRect)cellFrame inView:(NSView *)controlView includeFocus:(BOOL)includeFocus;
158 @implementation WebCoreTextFieldCell
159 - (CFDictionaryRef)_coreUIDrawOptionsWithFrame:(NSRect)cellFrame inView:(NSView *)controlView includeFocus:(BOOL)includeFocus
161 // FIXME: This is a post-Lion-only workaround for <rdar://problem/11385461>. When that bug is resolved, we should remove this code.
162 CFMutableDictionaryRef coreUIDrawOptions = CFDictionaryCreateMutableCopy(NULL, 0, [super _coreUIDrawOptionsWithFrame:cellFrame inView:controlView includeFocus:includeFocus]);
163 CFDictionarySetValue(coreUIDrawOptions, @"borders only", kCFBooleanTrue);
164 return (CFDictionaryRef)[NSMakeCollectable(coreUIDrawOptions) autorelease];
168 @interface WebCoreRenderThemeBundle : NSObject
171 @implementation WebCoreRenderThemeBundle
174 @interface NSSearchFieldCell()
175 @property (getter=isCenteredLook) BOOL centeredLook;
180 using namespace HTMLNames;
196 PassRefPtr<RenderTheme> RenderTheme::themeForPage(Page*)
198 static RenderTheme& rt = RenderThemeMac::create().leakRef();
202 Ref<RenderTheme> RenderThemeMac::create()
204 return adoptRef(*new RenderThemeMac);
207 RenderThemeMac::RenderThemeMac()
208 : m_isSliderThumbHorizontalPressed(false)
209 , m_isSliderThumbVerticalPressed(false)
210 , m_notificationObserver(adoptNS([[WebCoreRenderThemeNotificationObserver alloc] initWithTheme:this]))
212 [[NSNotificationCenter defaultCenter] addObserver:m_notificationObserver.get()
213 selector:@selector(systemColorsDidChange:)
214 name:NSSystemColorsDidChangeNotification
218 RenderThemeMac::~RenderThemeMac()
220 [[NSNotificationCenter defaultCenter] removeObserver:m_notificationObserver.get()];
223 NSView* RenderThemeMac::documentViewFor(const RenderObject& o) const
225 ControlStates states(extractControlStatesForRenderer(o));
226 return ThemeMac::ensuredView(&o.view().frameView(), states);
230 String RenderThemeMac::mediaControlsStyleSheet()
232 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
233 if (m_mediaControlsStyleSheet.isEmpty())
234 m_mediaControlsStyleSheet = [NSString stringWithContentsOfFile:[[NSBundle bundleForClass:[WebCoreRenderThemeBundle class]] pathForResource:@"mediaControlsApple" ofType:@"css"] encoding:NSUTF8StringEncoding error:nil];
235 return m_mediaControlsStyleSheet;
237 return emptyString();
241 String RenderThemeMac::mediaControlsScript()
243 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
244 if (m_mediaControlsScript.isEmpty()) {
245 StringBuilder scriptBuilder;
246 scriptBuilder.append([NSString stringWithContentsOfFile:[[NSBundle bundleForClass:[WebCoreRenderThemeBundle class]] pathForResource:@"mediaControlsLocalizedStrings" ofType:@"js"] encoding:NSUTF8StringEncoding error:nil]);
247 scriptBuilder.append([NSString stringWithContentsOfFile:[[NSBundle bundleForClass:[WebCoreRenderThemeBundle class]] pathForResource:@"mediaControlsApple" ofType:@"js"] encoding:NSUTF8StringEncoding error:nil]);
248 m_mediaControlsScript = scriptBuilder.toString();
250 return m_mediaControlsScript;
252 return emptyString();
256 #endif // ENABLE(VIDEO)
259 #if ENABLE(SERVICE_CONTROLS)
260 String RenderThemeMac::imageControlsStyleSheet() const
262 return String(imageControlsMacUserAgentStyleSheet, sizeof(imageControlsMacUserAgentStyleSheet));
266 Color RenderThemeMac::platformActiveSelectionBackgroundColor() const
268 NSColor* color = [[NSColor selectedTextBackgroundColor] colorUsingColorSpaceName:NSDeviceRGBColorSpace];
269 return Color(static_cast<int>(255.0 * [color redComponent]), static_cast<int>(255.0 * [color greenComponent]), static_cast<int>(255.0 * [color blueComponent]));
272 Color RenderThemeMac::platformInactiveSelectionBackgroundColor() const
274 NSColor* color = [[NSColor secondarySelectedControlColor] colorUsingColorSpaceName:NSDeviceRGBColorSpace];
275 return Color(static_cast<int>(255.0 * [color redComponent]), static_cast<int>(255.0 * [color greenComponent]), static_cast<int>(255.0 * [color blueComponent]));
278 Color RenderThemeMac::platformActiveListBoxSelectionBackgroundColor() const
280 NSColor* color = [[NSColor alternateSelectedControlColor] colorUsingColorSpaceName:NSDeviceRGBColorSpace];
281 return Color(static_cast<int>(255.0 * [color redComponent]), static_cast<int>(255.0 * [color greenComponent]), static_cast<int>(255.0 * [color blueComponent]));
284 Color RenderThemeMac::platformActiveListBoxSelectionForegroundColor() const
289 Color RenderThemeMac::platformInactiveListBoxSelectionForegroundColor() const
294 Color RenderThemeMac::platformFocusRingColor() const
296 if (usesTestModeFocusRingColor())
297 return oldAquaFocusRingColor();
299 return systemColor(CSSValueWebkitFocusRingColor);
302 Color RenderThemeMac::platformInactiveListBoxSelectionBackgroundColor() const
304 return platformInactiveSelectionBackgroundColor();
307 static FontWeight toFontWeight(NSInteger appKitFontWeight)
309 ASSERT(appKitFontWeight > 0 && appKitFontWeight < 15);
310 if (appKitFontWeight > 14)
311 appKitFontWeight = 14;
312 else if (appKitFontWeight < 1)
313 appKitFontWeight = 1;
315 static const FontWeight fontWeights[] = {
331 return fontWeights[appKitFontWeight - 1];
334 void RenderThemeMac::updateCachedSystemFontDescription(CSSValueID cssValueId, FontCascadeDescription& fontDescription) const
337 // System-font-ness can't be encapsulated by simply a font name. Instead, we must use a token
338 // which FontCache will look for.
339 // Make sure we keep this list of possible tokens in sync with FontCascade::primaryFontIsSystemFont()
340 AtomicString fontName;
341 switch (cssValueId) {
342 case CSSValueSmallCaption:
343 font = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];
346 font = [NSFont menuFontOfSize:[NSFont systemFontSize]];
347 fontName = AtomicString("-apple-menu", AtomicString::ConstructFromLiteral);
349 case CSSValueStatusBar:
350 font = [NSFont labelFontOfSize:[NSFont labelFontSize]];
351 fontName = AtomicString("-apple-status-bar", AtomicString::ConstructFromLiteral);
353 case CSSValueWebkitMiniControl:
354 #pragma clang diagnostic push
355 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
356 font = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSMiniControlSize]];
357 #pragma clang diagnostic pop
359 case CSSValueWebkitSmallControl:
360 #pragma clang diagnostic push
361 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
362 font = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]];
363 #pragma clang diagnostic pop
365 case CSSValueWebkitControl:
366 #pragma clang diagnostic push
367 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
368 font = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSRegularControlSize]];
369 #pragma clang diagnostic pop
372 font = [NSFont systemFontOfSize:[NSFont systemFontSize]];
378 if (fontName.isNull())
379 fontName = AtomicString("-apple-system", AtomicString::ConstructFromLiteral);
381 NSFontManager *fontManager = [NSFontManager sharedFontManager];
382 fontDescription.setIsAbsoluteSize(true);
383 fontDescription.setOneFamily(fontName);
384 fontDescription.setSpecifiedSize([font pointSize]);
385 fontDescription.setWeight(toFontWeight([fontManager weightOfFont:font]));
386 fontDescription.setIsItalic([fontManager traitsOfFont:font] & NSItalicFontMask);
389 static RGBA32 convertNSColorToColor(NSColor *color)
391 NSColor *colorInColorSpace = [color colorUsingColorSpaceName:NSDeviceRGBColorSpace];
392 if (colorInColorSpace) {
393 static const double scaleFactor = nextafter(256.0, 0.0);
394 return makeRGB(static_cast<int>(scaleFactor * [colorInColorSpace redComponent]),
395 static_cast<int>(scaleFactor * [colorInColorSpace greenComponent]),
396 static_cast<int>(scaleFactor * [colorInColorSpace blueComponent]));
399 // This conversion above can fail if the NSColor in question is an NSPatternColor
400 // (as many system colors are). These colors are actually a repeating pattern
401 // not just a solid color. To work around this we simply draw a 1x1 image of
402 // the color and use that pixel's color. It might be better to use an average of
403 // the colors in the pattern instead.
404 NSBitmapImageRep *offscreenRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:nil
411 colorSpaceName:NSDeviceRGBColorSpace
415 [NSGraphicsContext saveGraphicsState];
416 [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithBitmapImageRep:offscreenRep]];
417 NSEraseRect(NSMakeRect(0, 0, 1, 1));
418 [color drawSwatchInRect:NSMakeRect(0, 0, 1, 1)];
419 [NSGraphicsContext restoreGraphicsState];
422 [offscreenRep getPixel:pixel atX:0 y:0];
424 [offscreenRep release];
426 return makeRGB(pixel[0], pixel[1], pixel[2]);
429 static RGBA32 menuBackgroundColor()
431 NSBitmapImageRep *offscreenRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:nil
438 colorSpaceName:NSDeviceRGBColorSpace
442 CGContextRef context = static_cast<CGContextRef>([[NSGraphicsContext graphicsContextWithBitmapImageRep:offscreenRep] graphicsPort]);
443 CGRect rect = CGRectMake(0, 0, 1, 1);
444 HIThemeMenuDrawInfo drawInfo;
445 drawInfo.version = 0;
446 drawInfo.menuType = kThemeMenuTypePopUp;
447 HIThemeDrawMenuBackground(&rect, &drawInfo, context, kHIThemeOrientationInverted);
450 [offscreenRep getPixel:pixel atX:0 y:0];
452 [offscreenRep release];
454 return makeRGB(pixel[0], pixel[1], pixel[2]);
457 void RenderThemeMac::platformColorsDidChange()
459 m_systemColorCache.clear();
460 RenderTheme::platformColorsDidChange();
463 Color RenderThemeMac::systemColor(CSSValueID cssValueID) const
465 auto addResult = m_systemColorCache.add(cssValueID, Color());
466 if (!addResult.isNewEntry)
467 return addResult.iterator->value;
470 switch (cssValueID) {
471 case CSSValueActiveborder:
472 color = convertNSColorToColor([NSColor keyboardFocusIndicatorColor]);
474 case CSSValueActivebuttontext:
475 // There is no corresponding NSColor for this so we use a hard coded value.
476 color = Color::white;
478 case CSSValueActivecaption:
479 color = convertNSColorToColor([NSColor windowFrameTextColor]);
481 case CSSValueAppworkspace:
482 color = convertNSColorToColor([NSColor headerColor]);
484 case CSSValueBackground:
485 // Use theme independent default
487 case CSSValueButtonface:
488 // We use this value instead of NSColor's controlColor to avoid website incompatibilities.
489 // We may want to change this to use the NSColor in future.
492 case CSSValueButtonhighlight:
493 color = convertNSColorToColor([NSColor controlHighlightColor]);
495 case CSSValueButtonshadow:
496 color = convertNSColorToColor([NSColor controlShadowColor]);
498 case CSSValueButtontext:
499 color = convertNSColorToColor([NSColor controlTextColor]);
501 case CSSValueCaptiontext:
502 color = convertNSColorToColor([NSColor textColor]);
504 case CSSValueGraytext:
505 color = convertNSColorToColor([NSColor disabledControlTextColor]);
507 case CSSValueHighlight:
508 color = convertNSColorToColor([NSColor selectedTextBackgroundColor]);
510 case CSSValueHighlighttext:
511 color = convertNSColorToColor([NSColor selectedTextColor]);
513 case CSSValueInactiveborder:
514 color = convertNSColorToColor([NSColor controlBackgroundColor]);
516 case CSSValueInactivecaption:
517 color = convertNSColorToColor([NSColor controlBackgroundColor]);
519 case CSSValueInactivecaptiontext:
520 color = convertNSColorToColor([NSColor textColor]);
522 case CSSValueInfobackground:
523 // There is no corresponding NSColor for this so we use a hard coded value.
526 case CSSValueInfotext:
527 color = convertNSColorToColor([NSColor textColor]);
530 color = menuBackgroundColor();
532 case CSSValueMenutext:
533 color = convertNSColorToColor([NSColor selectedMenuItemTextColor]);
535 case CSSValueScrollbar:
536 color = convertNSColorToColor([NSColor scrollBarColor]);
539 color = convertNSColorToColor([NSColor textColor]);
541 case CSSValueThreeddarkshadow:
542 color = convertNSColorToColor([NSColor controlDarkShadowColor]);
544 case CSSValueThreedshadow:
545 color = convertNSColorToColor([NSColor shadowColor]);
547 case CSSValueThreedface:
548 // We use this value instead of NSColor's controlColor to avoid website incompatibilities.
549 // We may want to change this to use the NSColor in future.
552 case CSSValueThreedhighlight:
553 color = convertNSColorToColor([NSColor highlightColor]);
555 case CSSValueThreedlightshadow:
556 color = convertNSColorToColor([NSColor controlLightHighlightColor]);
558 case CSSValueWebkitFocusRingColor:
559 color = convertNSColorToColor([NSColor keyboardFocusIndicatorColor]);
562 color = convertNSColorToColor([NSColor windowBackgroundColor]);
564 case CSSValueWindowframe:
565 color = convertNSColorToColor([NSColor windowFrameColor]);
567 case CSSValueWindowtext:
568 color = convertNSColorToColor([NSColor windowFrameTextColor]);
570 case CSSValueAppleWirelessPlaybackTargetActive:
571 color = convertNSColorToColor([NSColor systemBlueColor]);
573 case CSSValueAppleSystemBlue:
574 color = convertNSColorToColor([NSColor systemBlueColor]);
576 case CSSValueAppleSystemBrown:
577 color = convertNSColorToColor([NSColor systemBrownColor]);
579 case CSSValueAppleSystemGray:
580 color = convertNSColorToColor([NSColor systemGrayColor]);
582 case CSSValueAppleSystemGreen:
583 color = convertNSColorToColor([NSColor systemGreenColor]);
585 case CSSValueAppleSystemOrange:
586 color = convertNSColorToColor([NSColor systemOrangeColor]);
588 case CSSValueAppleSystemPink:
589 color = convertNSColorToColor([NSColor systemPinkColor]);
591 case CSSValueAppleSystemPurple:
592 color = convertNSColorToColor([NSColor systemPurpleColor]);
594 case CSSValueAppleSystemRed:
595 color = convertNSColorToColor([NSColor systemRedColor]);
597 case CSSValueAppleSystemYellow:
598 color = convertNSColorToColor([NSColor systemYellowColor]);
604 if (!color.isValid())
605 color = RenderTheme::systemColor(cssValueID);
607 addResult.iterator->value = color;
609 return addResult.iterator->value;
612 bool RenderThemeMac::usesTestModeFocusRingColor() const
614 return WebCore::usesTestModeFocusRingColor();
617 bool RenderThemeMac::isControlStyled(const RenderStyle& style, const BorderData& border,
618 const FillLayer& background, const Color& backgroundColor) const
620 if (style.appearance() == TextFieldPart || style.appearance() == TextAreaPart || style.appearance() == ListboxPart)
621 return style.border() != border;
623 // FIXME: This is horrible, but there is not much else that can be done. Menu lists cannot draw properly when
624 // scaled. They can't really draw properly when transformed either. We can't detect the transform case at style
625 // adjustment time so that will just have to stay broken. We can however detect that we're zooming. If zooming
626 // is in effect we treat it like the control is styled.
627 if (style.appearance() == MenulistPart && style.effectiveZoom() != 1.0f)
630 return RenderTheme::isControlStyled(style, border, background, backgroundColor);
633 static FloatRect inflateRect(const FloatRect& rect, const IntSize& size, const int* margins, float zoomLevel)
635 // Only do the inflation if the available width/height are too small. Otherwise try to
636 // fit the glow/check space into the available box's width/height.
637 int widthDelta = rect.width() - (size.width() + margins[leftMargin] * zoomLevel + margins[rightMargin] * zoomLevel);
638 int heightDelta = rect.height() - (size.height() + margins[topMargin] * zoomLevel + margins[bottomMargin] * zoomLevel);
639 FloatRect result(rect);
640 if (widthDelta < 0) {
641 result.setX(result.x() - margins[leftMargin] * zoomLevel);
642 result.setWidth(result.width() - widthDelta);
644 if (heightDelta < 0) {
645 result.setY(result.y() - margins[topMargin] * zoomLevel);
646 result.setHeight(result.height() - heightDelta);
651 void RenderThemeMac::adjustRepaintRect(const RenderObject& renderer, FloatRect& rect)
653 ControlPart part = renderer.style().appearance();
660 case SquareButtonPart:
661 case DefaultButtonPart:
663 case InnerSpinButtonPart:
664 return RenderTheme::adjustRepaintRect(renderer, rect);
670 float zoomLevel = renderer.style().effectiveZoom();
672 if (part == MenulistPart) {
673 setPopupButtonCellState(renderer, IntSize(rect.size()));
674 IntSize size = popupButtonSizes()[[popupButton() controlSize]];
675 size.setHeight(size.height() * zoomLevel);
676 size.setWidth(rect.width());
677 rect = inflateRect(rect, size, popupButtonMargins(), zoomLevel);
681 static FloatPoint convertToPaintingPosition(const RenderBox& inputRenderer, const RenderBox& customButtonRenderer, const FloatPoint& customButtonLocalPosition,
682 const IntPoint& paintOffset)
684 IntPoint offsetFromInputRenderer = roundedIntPoint(customButtonRenderer.localToContainerPoint(customButtonRenderer.contentBoxRect().location(), &inputRenderer));
685 FloatPoint paintingPosition = customButtonLocalPosition;
686 paintingPosition.moveBy(-offsetFromInputRenderer);
687 paintingPosition.moveBy(paintOffset);
688 return paintingPosition;
691 void RenderThemeMac::updateCheckedState(NSCell* cell, const RenderObject& o)
693 bool oldIndeterminate = [cell state] == NSMixedState;
694 bool indeterminate = isIndeterminate(o);
695 bool checked = isChecked(o);
697 if (oldIndeterminate != indeterminate) {
698 [cell setState:indeterminate ? NSMixedState : (checked ? NSOnState : NSOffState)];
702 bool oldChecked = [cell state] == NSOnState;
703 if (checked != oldChecked)
704 [cell setState:checked ? NSOnState : NSOffState];
707 void RenderThemeMac::updateEnabledState(NSCell* cell, const RenderObject& o)
709 bool oldEnabled = [cell isEnabled];
710 bool enabled = isEnabled(o);
711 if (enabled != oldEnabled)
712 [cell setEnabled:enabled];
715 void RenderThemeMac::updateFocusedState(NSCell* cell, const RenderObject& o)
717 bool oldFocused = [cell showsFirstResponder];
718 bool focused = isFocused(o) && o.style().outlineStyleIsAuto();
719 if (focused != oldFocused)
720 [cell setShowsFirstResponder:focused];
723 void RenderThemeMac::updatePressedState(NSCell* cell, const RenderObject& o)
725 bool oldPressed = [cell isHighlighted];
726 bool pressed = is<Element>(o.node()) && downcast<Element>(*o.node()).active();
727 if (pressed != oldPressed)
728 [cell setHighlighted:pressed];
731 bool RenderThemeMac::controlSupportsTints(const RenderObject& o) const
733 // An alternate way to implement this would be to get the appropriate cell object
734 // and call the private _needRedrawOnWindowChangedKeyState method. An advantage of
735 // that would be that we would match AppKit behavior more closely, but a disadvantage
736 // would be that we would rely on an AppKit SPI method.
741 // Checkboxes only have tint when checked.
742 if (o.style().appearance() == CheckboxPart)
745 // For now assume other controls have tint if enabled.
749 NSControlSize RenderThemeMac::controlSizeForFont(const RenderStyle& style) const
751 int fontSize = style.fontSize();
752 #pragma clang diagnostic push
753 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
755 return NSRegularControlSize;
757 return NSSmallControlSize;
758 return NSMiniControlSize;
759 #pragma clang diagnostic pop
762 NSControlSize RenderThemeMac::controlSizeForCell(NSCell*, const IntSize* sizes, const IntSize& minSize, float zoomLevel) const
764 #pragma clang diagnostic push
765 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
766 if (minSize.width() >= static_cast<int>(sizes[NSRegularControlSize].width() * zoomLevel)
767 && minSize.height() >= static_cast<int>(sizes[NSRegularControlSize].height() * zoomLevel))
768 return NSRegularControlSize;
770 if (minSize.width() >= static_cast<int>(sizes[NSSmallControlSize].width() * zoomLevel)
771 && minSize.height() >= static_cast<int>(sizes[NSSmallControlSize].height() * zoomLevel))
772 return NSSmallControlSize;
774 return NSMiniControlSize;
775 #pragma clang diagnostic pop
778 void RenderThemeMac::setControlSize(NSCell* cell, const IntSize* sizes, const IntSize& minSize, float zoomLevel)
780 NSControlSize size = controlSizeForCell(cell, sizes, minSize, zoomLevel);
781 if (size != [cell controlSize]) // Only update if we have to, since AppKit does work even if the size is the same.
782 [cell setControlSize:size];
785 IntSize RenderThemeMac::sizeForFont(const RenderStyle& style, const IntSize* sizes) const
787 if (style.effectiveZoom() != 1.0f) {
788 IntSize result = sizes[controlSizeForFont(style)];
789 return IntSize(result.width() * style.effectiveZoom(), result.height() * style.effectiveZoom());
791 return sizes[controlSizeForFont(style)];
794 IntSize RenderThemeMac::sizeForSystemFont(const RenderStyle& style, const IntSize* sizes) const
796 if (style.effectiveZoom() != 1.0f) {
797 IntSize result = sizes[controlSizeForSystemFont(style)];
798 return IntSize(result.width() * style.effectiveZoom(), result.height() * style.effectiveZoom());
800 return sizes[controlSizeForSystemFont(style)];
803 void RenderThemeMac::setSizeFromFont(RenderStyle& style, const IntSize* sizes) const
805 // FIXME: Check is flawed, since it doesn't take min-width/max-width into account.
806 IntSize size = sizeForFont(style, sizes);
807 if (style.width().isIntrinsicOrAuto() && size.width() > 0)
808 style.setWidth(Length(size.width(), Fixed));
809 if (style.height().isAuto() && size.height() > 0)
810 style.setHeight(Length(size.height(), Fixed));
813 void RenderThemeMac::setFontFromControlSize(StyleResolver&, RenderStyle& style, NSControlSize controlSize) const
815 FontCascadeDescription fontDescription;
816 fontDescription.setIsAbsoluteSize(true);
818 NSFont* font = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:controlSize]];
819 fontDescription.setOneFamily(AtomicString("-apple-system", AtomicString::ConstructFromLiteral));
820 fontDescription.setComputedSize([font pointSize] * style.effectiveZoom());
821 fontDescription.setSpecifiedSize([font pointSize] * style.effectiveZoom());
824 style.setLineHeight(RenderStyle::initialLineHeight());
826 if (style.setFontDescription(fontDescription))
827 style.fontCascade().update(0);
830 NSControlSize RenderThemeMac::controlSizeForSystemFont(const RenderStyle& style) const
832 #pragma clang diagnostic push
833 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
834 int fontSize = style.fontSize();
835 if (fontSize >= [NSFont systemFontSizeForControlSize:NSRegularControlSize])
836 return NSRegularControlSize;
837 if (fontSize >= [NSFont systemFontSizeForControlSize:NSSmallControlSize])
838 return NSSmallControlSize;
839 return NSMiniControlSize;
840 #pragma clang diagnostic pop
843 bool RenderThemeMac::paintTextField(const RenderObject& o, const PaintInfo& paintInfo, const FloatRect& r)
845 LocalCurrentGraphicsContext localContext(paintInfo.context());
847 // <rdar://problem/22896977> We adjust the paint rect here to account for how AppKit draws the text
848 // field cell slightly smaller than the rect we pass to drawWithFrame.
849 FloatRect adjustedPaintRect(r);
850 AffineTransform transform = paintInfo.context().getCTM();
851 if (transform.xScale() > 1 || transform.yScale() > 1) {
852 adjustedPaintRect.inflateX(1 / transform.xScale());
853 adjustedPaintRect.inflateY(1 / transform.yScale());
855 NSTextFieldCell *textField = this->textField();
857 GraphicsContextStateSaver stateSaver(paintInfo.context());
859 [textField setEnabled:(isEnabled(o) && !isReadOnlyControl(o))];
860 [textField drawWithFrame:NSRect(adjustedPaintRect) inView:documentViewFor(o)];
862 [textField setControlView:nil];
867 void RenderThemeMac::adjustTextFieldStyle(StyleResolver&, RenderStyle&, Element*) const
871 bool RenderThemeMac::paintTextArea(const RenderObject& o, const PaintInfo& paintInfo, const FloatRect& r)
873 LocalCurrentGraphicsContext localContext(paintInfo.context());
874 wkDrawBezeledTextArea(r, isEnabled(o) && !isReadOnlyControl(o));
878 void RenderThemeMac::adjustTextAreaStyle(StyleResolver&, RenderStyle&, Element*) const
882 const int* RenderThemeMac::popupButtonMargins() const
884 static const int margins[3][4] =
890 return margins[[popupButton() controlSize]];
893 const IntSize* RenderThemeMac::popupButtonSizes() const
895 static const IntSize sizes[3] = { IntSize(0, 21), IntSize(0, 18), IntSize(0, 15) };
899 const int* RenderThemeMac::popupButtonPadding(NSControlSize size) const
901 static const int padding[3][4] =
907 return padding[size];
910 bool RenderThemeMac::paintMenuList(const RenderObject& renderer, const PaintInfo& paintInfo, const FloatRect& rect)
912 LocalCurrentGraphicsContext localContext(paintInfo.context());
913 setPopupButtonCellState(renderer, IntSize(rect.size()));
915 NSPopUpButtonCell* popupButton = this->popupButton();
917 float zoomLevel = renderer.style().effectiveZoom();
918 IntSize size = popupButtonSizes()[[popupButton controlSize]];
919 size.setHeight(size.height() * zoomLevel);
920 size.setWidth(rect.width());
922 // Now inflate it to account for the shadow.
923 FloatRect inflatedRect = rect;
924 if (rect.width() >= minimumMenuListSize(renderer.style()))
925 inflatedRect = inflateRect(rect, size, popupButtonMargins(), zoomLevel);
927 GraphicsContextStateSaver stateSaver(paintInfo.context());
929 if (zoomLevel != 1.0f) {
930 inflatedRect.setWidth(inflatedRect.width() / zoomLevel);
931 inflatedRect.setHeight(inflatedRect.height() / zoomLevel);
932 paintInfo.context().translate(inflatedRect.x(), inflatedRect.y());
933 paintInfo.context().scale(FloatSize(zoomLevel, zoomLevel));
934 paintInfo.context().translate(-inflatedRect.x(), -inflatedRect.y());
937 paintCellAndSetFocusedElementNeedsRepaintIfNecessary(popupButton, renderer, paintInfo, inflatedRect);
938 [popupButton setControlView:nil];
943 #if ENABLE(METER_ELEMENT)
945 IntSize RenderThemeMac::meterSizeForBounds(const RenderMeter& renderMeter, const IntRect& bounds) const
947 if (NoControlPart == renderMeter.style().appearance())
948 return bounds.size();
950 NSLevelIndicatorCell* cell = levelIndicatorFor(renderMeter);
951 // Makes enough room for cell's intrinsic size.
952 NSSize cellSize = [cell cellSizeForBounds:IntRect(IntPoint(), bounds.size())];
953 return IntSize(bounds.width() < cellSize.width ? cellSize.width : bounds.width(),
954 bounds.height() < cellSize.height ? cellSize.height : bounds.height());
957 bool RenderThemeMac::paintMeter(const RenderObject& renderObject, const PaintInfo& paintInfo, const IntRect& rect)
959 if (!is<RenderMeter>(renderObject))
962 LocalCurrentGraphicsContext localContext(paintInfo.context());
964 NSLevelIndicatorCell* cell = levelIndicatorFor(downcast<RenderMeter>(renderObject));
965 GraphicsContextStateSaver stateSaver(paintInfo.context());
967 [cell drawWithFrame:rect inView:documentViewFor(renderObject)];
968 [cell setControlView:nil];
972 bool RenderThemeMac::supportsMeter(ControlPart part) const
975 case RelevancyLevelIndicatorPart:
976 case DiscreteCapacityLevelIndicatorPart:
977 case RatingLevelIndicatorPart:
979 case ContinuousCapacityLevelIndicatorPart:
986 NSLevelIndicatorStyle RenderThemeMac::levelIndicatorStyleFor(ControlPart part) const
989 case RelevancyLevelIndicatorPart:
990 return NSRelevancyLevelIndicatorStyle;
991 case DiscreteCapacityLevelIndicatorPart:
992 return NSDiscreteCapacityLevelIndicatorStyle;
993 case RatingLevelIndicatorPart:
994 return NSRatingLevelIndicatorStyle;
996 case ContinuousCapacityLevelIndicatorPart:
998 return NSContinuousCapacityLevelIndicatorStyle;
1003 NSLevelIndicatorCell* RenderThemeMac::levelIndicatorFor(const RenderMeter& renderMeter) const
1005 const RenderStyle& style = renderMeter.style();
1006 ASSERT(style.appearance() != NoControlPart);
1008 if (!m_levelIndicator)
1009 m_levelIndicator = adoptNS([[NSLevelIndicatorCell alloc] initWithLevelIndicatorStyle:NSContinuousCapacityLevelIndicatorStyle]);
1010 NSLevelIndicatorCell* cell = m_levelIndicator.get();
1012 HTMLMeterElement* element = renderMeter.meterElement();
1013 double value = element->value();
1015 // Because NSLevelIndicatorCell does not support optimum-in-the-middle type coloring,
1016 // we explicitly control the color instead giving low and high value to NSLevelIndicatorCell as is.
1017 switch (element->gaugeRegion()) {
1018 case HTMLMeterElement::GaugeRegionOptimum:
1019 // Make meter the green
1020 [cell setWarningValue:value + 1];
1021 [cell setCriticalValue:value + 2];
1023 case HTMLMeterElement::GaugeRegionSuboptimal:
1024 // Make the meter yellow
1025 [cell setWarningValue:value - 1];
1026 [cell setCriticalValue:value + 1];
1028 case HTMLMeterElement::GaugeRegionEvenLessGood:
1029 // Make the meter red
1030 [cell setWarningValue:value - 2];
1031 [cell setCriticalValue:value - 1];
1035 [cell setLevelIndicatorStyle:levelIndicatorStyleFor(style.appearance())];
1036 [cell setBaseWritingDirection:style.isLeftToRightDirection() ? NSWritingDirectionLeftToRight : NSWritingDirectionRightToLeft];
1037 [cell setMinValue:element->min()];
1038 [cell setMaxValue:element->max()];
1039 RetainPtr<NSNumber> valueObject = [NSNumber numberWithDouble:value];
1040 [cell setObjectValue:valueObject.get()];
1047 const IntSize* RenderThemeMac::progressBarSizes() const
1049 static const IntSize sizes[3] = { IntSize(0, 20), IntSize(0, 12), IntSize(0, 12) };
1053 const int* RenderThemeMac::progressBarMargins(NSControlSize controlSize) const
1055 static const int margins[3][4] =
1061 return margins[controlSize];
1064 IntRect RenderThemeMac::progressBarRectForBounds(const RenderObject& renderObject, const IntRect& bounds) const
1066 // Workaround until <rdar://problem/15855086> is fixed.
1067 int maxDimension = static_cast<int>(std::numeric_limits<ushort>::max());
1068 IntRect progressBarBounds(bounds.x(), bounds.y(), std::min(bounds.width(), maxDimension), std::min(bounds.height(), maxDimension));
1069 if (NoControlPart == renderObject.style().appearance())
1070 return progressBarBounds;
1072 float zoomLevel = renderObject.style().effectiveZoom();
1073 NSControlSize controlSize = controlSizeForFont(renderObject.style());
1074 IntSize size = progressBarSizes()[controlSize];
1075 size.setHeight(size.height() * zoomLevel);
1076 size.setWidth(progressBarBounds.width());
1078 // Now inflate it to account for the shadow.
1079 IntRect inflatedRect = progressBarBounds;
1080 if (progressBarBounds.height() <= minimumProgressBarHeight(renderObject.style()))
1081 inflatedRect = IntRect(inflateRect(inflatedRect, size, progressBarMargins(controlSize), zoomLevel));
1083 return inflatedRect;
1086 int RenderThemeMac::minimumProgressBarHeight(const RenderStyle& style) const
1088 return sizeForSystemFont(style, progressBarSizes()).height();
1091 double RenderThemeMac::animationRepeatIntervalForProgressBar(RenderProgress&) const
1093 return progressAnimationFrameRate;
1096 double RenderThemeMac::animationDurationForProgressBar(RenderProgress&) const
1098 return progressAnimationNumFrames * progressAnimationFrameRate;
1101 void RenderThemeMac::adjustProgressBarStyle(StyleResolver&, RenderStyle&, Element*) const
1105 bool RenderThemeMac::paintProgressBar(const RenderObject& renderObject, const PaintInfo& paintInfo, const IntRect& rect)
1107 if (!is<RenderProgress>(renderObject))
1110 IntRect inflatedRect = progressBarRectForBounds(renderObject, rect);
1111 NSControlSize controlSize = controlSizeForFont(renderObject.style());
1113 const auto& renderProgress = downcast<RenderProgress>(renderObject);
1114 HIThemeTrackDrawInfo trackInfo;
1115 trackInfo.version = 0;
1116 #pragma clang diagnostic push
1117 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
1118 if (controlSize == NSRegularControlSize)
1119 #pragma clang diagnostic pop
1120 trackInfo.kind = renderProgress.position() < 0 ? kThemeLargeIndeterminateBar : kThemeLargeProgressBar;
1122 trackInfo.kind = renderProgress.position() < 0 ? kThemeMediumIndeterminateBar : kThemeMediumProgressBar;
1124 float deviceScaleFactor = renderObject.document().deviceScaleFactor();
1125 trackInfo.bounds = IntRect(IntPoint(), inflatedRect.size());
1127 trackInfo.max = std::numeric_limits<SInt32>::max();
1128 trackInfo.value = lround(renderProgress.position() * nextafter(trackInfo.max, 0));
1129 trackInfo.trackInfo.progress.phase = lround(renderProgress.animationProgress() * nextafter(progressAnimationNumFrames, 0) * deviceScaleFactor);
1130 trackInfo.attributes = kThemeTrackHorizontal;
1131 trackInfo.enableState = isActive(renderObject) ? kThemeTrackActive : kThemeTrackInactive;
1132 trackInfo.reserved = 0;
1133 trackInfo.filler1 = 0;
1135 std::unique_ptr<ImageBuffer> imageBuffer = ImageBuffer::createCompatibleBuffer(inflatedRect.size(), deviceScaleFactor, ColorSpaceSRGB, paintInfo.context(), true);
1139 ContextContainer cgContextContainer(imageBuffer->context());
1140 CGContextRef cgContext = cgContextContainer.context();
1141 HIThemeDrawTrack(&trackInfo, 0, cgContext, kHIThemeOrientationNormal);
1143 GraphicsContextStateSaver stateSaver(paintInfo.context());
1145 if (!renderProgress.style().isLeftToRightDirection()) {
1146 paintInfo.context().translate(2 * inflatedRect.x() + inflatedRect.width(), 0);
1147 paintInfo.context().scale(FloatSize(-1, 1));
1150 paintInfo.context().drawConsumingImageBuffer(WTFMove(imageBuffer), inflatedRect.location());
1154 const float baseFontSize = 11.0f;
1155 const float baseArrowHeight = 4.0f;
1156 const float baseArrowWidth = 5.0f;
1157 const float baseSpaceBetweenArrows = 2.0f;
1158 const int arrowPaddingLeft = 6;
1159 const int arrowPaddingRight = 6;
1160 const int paddingBeforeSeparator = 4;
1161 const int baseBorderRadius = 5;
1162 const int styledPopupPaddingLeft = 8;
1163 const int styledPopupPaddingTop = 1;
1164 const int styledPopupPaddingBottom = 2;
1166 static void TopGradientInterpolate(void*, const CGFloat* inData, CGFloat* outData)
1168 static const float dark[4] = { 1.0f, 1.0f, 1.0f, 0.4f };
1169 static const float light[4] = { 1.0f, 1.0f, 1.0f, 0.15f };
1170 float a = inData[0];
1172 for (i = 0; i < 4; i++)
1173 outData[i] = (1.0f - a) * dark[i] + a * light[i];
1176 static void BottomGradientInterpolate(void*, const CGFloat* inData, CGFloat* outData)
1178 static const float dark[4] = { 1.0f, 1.0f, 1.0f, 0.0f };
1179 static const float light[4] = { 1.0f, 1.0f, 1.0f, 0.3f };
1180 float a = inData[0];
1182 for (i = 0; i < 4; i++)
1183 outData[i] = (1.0f - a) * dark[i] + a * light[i];
1186 static void MainGradientInterpolate(void*, const CGFloat* inData, CGFloat* outData)
1188 static const float dark[4] = { 0.0f, 0.0f, 0.0f, 0.15f };
1189 static const float light[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
1190 float a = inData[0];
1192 for (i = 0; i < 4; i++)
1193 outData[i] = (1.0f - a) * dark[i] + a * light[i];
1196 static void TrackGradientInterpolate(void*, const CGFloat* inData, CGFloat* outData)
1198 static const float dark[4] = { 0.0f, 0.0f, 0.0f, 0.678f };
1199 static const float light[4] = { 0.0f, 0.0f, 0.0f, 0.13f };
1200 float a = inData[0];
1202 for (i = 0; i < 4; i++)
1203 outData[i] = (1.0f - a) * dark[i] + a * light[i];
1206 void RenderThemeMac::paintMenuListButtonGradients(const RenderObject& o, const PaintInfo& paintInfo, const IntRect& r)
1211 ContextContainer cgContextContainer(paintInfo.context());
1212 CGContextRef context = cgContextContainer.context();
1214 GraphicsContextStateSaver stateSaver(paintInfo.context());
1216 FloatRoundedRect border = FloatRoundedRect(o.style().getRoundedBorderFor(r));
1217 int radius = border.radii().topLeft().width();
1219 CGColorSpaceRef cspace = sRGBColorSpaceRef();
1221 FloatRect topGradient(r.x(), r.y(), r.width(), r.height() / 2.0f);
1222 struct CGFunctionCallbacks topCallbacks = { 0, TopGradientInterpolate, NULL };
1223 RetainPtr<CGFunctionRef> topFunction = adoptCF(CGFunctionCreate(NULL, 1, NULL, 4, NULL, &topCallbacks));
1224 RetainPtr<CGShadingRef> topShading = adoptCF(CGShadingCreateAxial(cspace, CGPointMake(topGradient.x(), topGradient.y()), CGPointMake(topGradient.x(), topGradient.maxY()), topFunction.get(), false, false));
1226 FloatRect bottomGradient(r.x() + radius, r.y() + r.height() / 2.0f, r.width() - 2.0f * radius, r.height() / 2.0f);
1227 struct CGFunctionCallbacks bottomCallbacks = { 0, BottomGradientInterpolate, NULL };
1228 RetainPtr<CGFunctionRef> bottomFunction = adoptCF(CGFunctionCreate(NULL, 1, NULL, 4, NULL, &bottomCallbacks));
1229 RetainPtr<CGShadingRef> bottomShading = adoptCF(CGShadingCreateAxial(cspace, CGPointMake(bottomGradient.x(), bottomGradient.y()), CGPointMake(bottomGradient.x(), bottomGradient.maxY()), bottomFunction.get(), false, false));
1231 struct CGFunctionCallbacks mainCallbacks = { 0, MainGradientInterpolate, NULL };
1232 RetainPtr<CGFunctionRef> mainFunction = adoptCF(CGFunctionCreate(NULL, 1, NULL, 4, NULL, &mainCallbacks));
1233 RetainPtr<CGShadingRef> mainShading = adoptCF(CGShadingCreateAxial(cspace, CGPointMake(r.x(), r.y()), CGPointMake(r.x(), r.maxY()), mainFunction.get(), false, false));
1235 RetainPtr<CGShadingRef> leftShading = adoptCF(CGShadingCreateAxial(cspace, CGPointMake(r.x(), r.y()), CGPointMake(r.x() + radius, r.y()), mainFunction.get(), false, false));
1237 RetainPtr<CGShadingRef> rightShading = adoptCF(CGShadingCreateAxial(cspace, CGPointMake(r.maxX(), r.y()), CGPointMake(r.maxX() - radius, r.y()), mainFunction.get(), false, false));
1240 GraphicsContextStateSaver stateSaver(paintInfo.context());
1241 CGContextClipToRect(context, r);
1242 paintInfo.context().clipRoundedRect(border);
1243 context = cgContextContainer.context();
1244 CGContextDrawShading(context, mainShading.get());
1248 GraphicsContextStateSaver stateSaver(paintInfo.context());
1249 CGContextClipToRect(context, topGradient);
1250 paintInfo.context().clipRoundedRect(FloatRoundedRect(enclosingIntRect(topGradient), border.radii().topLeft(), border.radii().topRight(), IntSize(), IntSize()));
1251 context = cgContextContainer.context();
1252 CGContextDrawShading(context, topShading.get());
1255 if (!bottomGradient.isEmpty()) {
1256 GraphicsContextStateSaver stateSaver(paintInfo.context());
1257 CGContextClipToRect(context, bottomGradient);
1258 paintInfo.context().clipRoundedRect(FloatRoundedRect(enclosingIntRect(bottomGradient), IntSize(), IntSize(), border.radii().bottomLeft(), border.radii().bottomRight()));
1259 context = cgContextContainer.context();
1260 CGContextDrawShading(context, bottomShading.get());
1264 GraphicsContextStateSaver stateSaver(paintInfo.context());
1265 CGContextClipToRect(context, r);
1266 paintInfo.context().clipRoundedRect(border);
1267 context = cgContextContainer.context();
1268 CGContextDrawShading(context, leftShading.get());
1269 CGContextDrawShading(context, rightShading.get());
1273 bool RenderThemeMac::paintMenuListButtonDecorations(const RenderBox& renderer, const PaintInfo& paintInfo, const FloatRect& rect)
1275 IntRect bounds = IntRect(rect.x() + renderer.style().borderLeftWidth(),
1276 rect.y() + renderer.style().borderTopWidth(),
1277 rect.width() - renderer.style().borderLeftWidth() - renderer.style().borderRightWidth(),
1278 rect.height() - renderer.style().borderTopWidth() - renderer.style().borderBottomWidth());
1279 // Draw the gradients to give the styled popup menu a button appearance
1280 paintMenuListButtonGradients(renderer, paintInfo, bounds);
1282 // Since we actually know the size of the control here, we restrict the font scale to make sure the arrows will fit vertically in the bounds
1283 float fontScale = std::min(renderer.style().fontSize() / baseFontSize, bounds.height() / (baseArrowHeight * 2 + baseSpaceBetweenArrows));
1284 float centerY = bounds.y() + bounds.height() / 2.0f;
1285 float arrowHeight = baseArrowHeight * fontScale;
1286 float arrowWidth = baseArrowWidth * fontScale;
1287 float leftEdge = bounds.maxX() - arrowPaddingRight * renderer.style().effectiveZoom() - arrowWidth;
1288 float spaceBetweenArrows = baseSpaceBetweenArrows * fontScale;
1290 if (bounds.width() < arrowWidth + arrowPaddingLeft * renderer.style().effectiveZoom())
1293 GraphicsContextStateSaver stateSaver(paintInfo.context());
1295 paintInfo.context().setFillColor(renderer.style().visitedDependentColor(CSSPropertyColor));
1296 paintInfo.context().setStrokeStyle(NoStroke);
1298 // Draw the top arrow
1299 Vector<FloatPoint> arrow1 = {
1300 { leftEdge, centerY - spaceBetweenArrows / 2.0f },
1301 { leftEdge + arrowWidth, centerY - spaceBetweenArrows / 2.0f },
1302 { leftEdge + arrowWidth / 2.0f, centerY - spaceBetweenArrows / 2.0f - arrowHeight }
1304 paintInfo.context().fillPath(Path::polygonPathFromPoints(arrow1));
1306 // Draw the bottom arrow
1307 Vector<FloatPoint> arrow2 = {
1308 { leftEdge, centerY + spaceBetweenArrows / 2.0f },
1309 { leftEdge + arrowWidth, centerY + spaceBetweenArrows / 2.0f },
1310 { leftEdge + arrowWidth / 2.0f, centerY + spaceBetweenArrows / 2.0f + arrowHeight }
1312 paintInfo.context().fillPath(Path::polygonPathFromPoints(arrow2));
1314 Color leftSeparatorColor(0, 0, 0, 40);
1315 Color rightSeparatorColor(255, 255, 255, 40);
1317 // FIXME: Should the separator thickness and space be scaled up by fontScale?
1318 int separatorSpace = 2; // Deliberately ignores zoom since it looks nicer if it stays thin.
1319 int leftEdgeOfSeparator = static_cast<int>(leftEdge - arrowPaddingLeft * renderer.style().effectiveZoom()); // FIXME: Round?
1321 // Draw the separator to the left of the arrows
1322 paintInfo.context().setStrokeThickness(1); // Deliberately ignores zoom since it looks nicer if it stays thin.
1323 paintInfo.context().setStrokeStyle(SolidStroke);
1324 paintInfo.context().setStrokeColor(leftSeparatorColor);
1325 paintInfo.context().drawLine(IntPoint(leftEdgeOfSeparator, bounds.y()),
1326 IntPoint(leftEdgeOfSeparator, bounds.maxY()));
1328 paintInfo.context().setStrokeColor(rightSeparatorColor);
1329 paintInfo.context().drawLine(IntPoint(leftEdgeOfSeparator + separatorSpace, bounds.y()),
1330 IntPoint(leftEdgeOfSeparator + separatorSpace, bounds.maxY()));
1334 static const IntSize* menuListButtonSizes()
1336 static const IntSize sizes[3] = { IntSize(0, 21), IntSize(0, 18), IntSize(0, 15) };
1340 void RenderThemeMac::adjustMenuListStyle(StyleResolver& styleResolver, RenderStyle& style, Element* e) const
1342 NSControlSize controlSize = controlSizeForFont(style);
1344 style.resetBorder();
1345 style.resetPadding();
1347 // Height is locked to auto.
1348 style.setHeight(Length(Auto));
1350 // White-space is locked to pre
1351 style.setWhiteSpace(PRE);
1353 // Set the foreground color to black or gray when we have the aqua look.
1354 // Cast to RGB32 is to work around a compiler bug.
1355 style.setColor(e && !e->isDisabledFormControl() ? static_cast<RGBA32>(Color::black) : Color::darkGray);
1357 // Set the button's vertical size.
1358 setSizeFromFont(style, menuListButtonSizes());
1360 // Our font is locked to the appropriate system font size for the control. To clarify, we first use the CSS-specified font to figure out
1361 // a reasonable control size, but once that control size is determined, we throw that font away and use the appropriate
1362 // system font for the control size instead.
1363 setFontFromControlSize(styleResolver, style, controlSize);
1365 style.setBoxShadow(nullptr);
1368 int RenderThemeMac::popupInternalPaddingLeft(const RenderStyle& style) const
1370 if (style.appearance() == MenulistPart)
1371 return popupButtonPadding(controlSizeForFont(style))[leftPadding] * style.effectiveZoom();
1372 if (style.appearance() == MenulistButtonPart)
1373 return styledPopupPaddingLeft * style.effectiveZoom();
1377 int RenderThemeMac::popupInternalPaddingRight(const RenderStyle& style) const
1379 if (style.appearance() == MenulistPart)
1380 return popupButtonPadding(controlSizeForFont(style))[rightPadding] * style.effectiveZoom();
1381 if (style.appearance() == MenulistButtonPart) {
1382 float fontScale = style.fontSize() / baseFontSize;
1383 float arrowWidth = baseArrowWidth * fontScale;
1384 return static_cast<int>(ceilf(arrowWidth + (arrowPaddingLeft + arrowPaddingRight + paddingBeforeSeparator) * style.effectiveZoom()));
1389 int RenderThemeMac::popupInternalPaddingTop(const RenderStyle& style) const
1391 if (style.appearance() == MenulistPart)
1392 return popupButtonPadding(controlSizeForFont(style))[topPadding] * style.effectiveZoom();
1393 if (style.appearance() == MenulistButtonPart)
1394 return styledPopupPaddingTop * style.effectiveZoom();
1398 int RenderThemeMac::popupInternalPaddingBottom(const RenderStyle& style) const
1400 if (style.appearance() == MenulistPart)
1401 return popupButtonPadding(controlSizeForFont(style))[bottomPadding] * style.effectiveZoom();
1402 if (style.appearance() == MenulistButtonPart)
1403 return styledPopupPaddingBottom * style.effectiveZoom();
1407 PopupMenuStyle::PopupMenuSize RenderThemeMac::popupMenuSize(const RenderStyle& style, IntRect& rect) const
1409 NSPopUpButtonCell* popupButton = this->popupButton();
1410 NSControlSize size = controlSizeForCell(popupButton, popupButtonSizes(), rect.size(), style.effectiveZoom());
1412 #pragma clang diagnostic push
1413 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
1414 case NSRegularControlSize:
1415 return PopupMenuStyle::PopupMenuSizeNormal;
1416 case NSSmallControlSize:
1417 return PopupMenuStyle::PopupMenuSizeSmall;
1418 case NSMiniControlSize:
1419 return PopupMenuStyle::PopupMenuSizeMini;
1420 #pragma clang diagnostic pop
1422 return PopupMenuStyle::PopupMenuSizeNormal;
1426 void RenderThemeMac::adjustMenuListButtonStyle(StyleResolver&, RenderStyle& style, Element*) const
1428 float fontScale = style.fontSize() / baseFontSize;
1430 style.resetPadding();
1431 style.setBorderRadius(IntSize(int(baseBorderRadius + fontScale - 1), int(baseBorderRadius + fontScale - 1))); // FIXME: Round up?
1433 const int minHeight = 15;
1434 style.setMinHeight(Length(minHeight, Fixed));
1436 style.setLineHeight(RenderStyle::initialLineHeight());
1439 void RenderThemeMac::setPopupButtonCellState(const RenderObject& o, const IntSize& buttonSize)
1441 NSPopUpButtonCell* popupButton = this->popupButton();
1443 // Set the control size based off the rectangle we're painting into.
1444 setControlSize(popupButton, popupButtonSizes(), buttonSize, o.style().effectiveZoom());
1446 // Update the various states we respond to.
1447 updateCheckedState(popupButton, o);
1448 updateEnabledState(popupButton, o);
1449 updatePressedState(popupButton, o);
1452 void RenderThemeMac::paintCellAndSetFocusedElementNeedsRepaintIfNecessary(NSCell* cell, const RenderObject& renderer, const PaintInfo& paintInfo, const FloatRect& rect)
1454 Page* page = renderer.document().page();
1455 bool shouldDrawFocusRing = isFocused(renderer) && renderer.style().outlineStyleIsAuto();
1456 bool shouldUseImageBuffer = renderer.style().effectiveZoom() != 1 || page->pageScaleFactor() != 1;
1457 bool shouldDrawCell = true;
1458 if (ThemeMac::drawCellOrFocusRingWithViewIntoContext(cell, paintInfo.context(), rect, documentViewFor(renderer), shouldDrawCell, shouldDrawFocusRing, shouldUseImageBuffer, page->deviceScaleFactor()))
1459 page->focusController().setFocusedElementNeedsRepaint();
1462 const IntSize* RenderThemeMac::menuListSizes() const
1464 static const IntSize sizes[3] = { IntSize(9, 0), IntSize(5, 0), IntSize(0, 0) };
1468 int RenderThemeMac::minimumMenuListSize(const RenderStyle& style) const
1470 return sizeForSystemFont(style, menuListSizes()).width();
1473 const int trackWidth = 5;
1474 const int trackRadius = 2;
1476 void RenderThemeMac::adjustSliderTrackStyle(StyleResolver&, RenderStyle& style, Element*) const
1478 style.setBoxShadow(nullptr);
1481 bool RenderThemeMac::paintSliderTrack(const RenderObject& o, const PaintInfo& paintInfo, const IntRect& r)
1484 float zoomLevel = o.style().effectiveZoom();
1485 float zoomedTrackWidth = trackWidth * zoomLevel;
1487 if (o.style().appearance() == SliderHorizontalPart || o.style().appearance() == MediaSliderPart) {
1488 bounds.setHeight(zoomedTrackWidth);
1489 bounds.setY(r.y() + r.height() / 2 - zoomedTrackWidth / 2);
1490 } else if (o.style().appearance() == SliderVerticalPart) {
1491 bounds.setWidth(zoomedTrackWidth);
1492 bounds.setX(r.x() + r.width() / 2 - zoomedTrackWidth / 2);
1495 LocalCurrentGraphicsContext localContext(paintInfo.context());
1496 CGContextRef context = localContext.cgContext();
1497 CGColorSpaceRef cspace = sRGBColorSpaceRef();
1499 #if ENABLE(DATALIST_ELEMENT)
1500 paintSliderTicks(o, paintInfo, r);
1503 GraphicsContextStateSaver stateSaver(paintInfo.context());
1504 CGContextClipToRect(context, bounds);
1506 struct CGFunctionCallbacks mainCallbacks = { 0, TrackGradientInterpolate, NULL };
1507 RetainPtr<CGFunctionRef> mainFunction = adoptCF(CGFunctionCreate(NULL, 1, NULL, 4, NULL, &mainCallbacks));
1508 RetainPtr<CGShadingRef> mainShading;
1509 if (o.style().appearance() == SliderVerticalPart)
1510 mainShading = adoptCF(CGShadingCreateAxial(cspace, CGPointMake(bounds.x(), bounds.maxY()), CGPointMake(bounds.maxX(), bounds.maxY()), mainFunction.get(), false, false));
1512 mainShading = adoptCF(CGShadingCreateAxial(cspace, CGPointMake(bounds.x(), bounds.y()), CGPointMake(bounds.x(), bounds.maxY()), mainFunction.get(), false, false));
1514 IntSize radius(trackRadius, trackRadius);
1515 paintInfo.context().clipRoundedRect(FloatRoundedRect(bounds, radius, radius, radius, radius));
1516 context = localContext.cgContext();
1517 CGContextDrawShading(context, mainShading.get());
1522 void RenderThemeMac::adjustSliderThumbStyle(StyleResolver& styleResolver, RenderStyle& style, Element* element) const
1524 RenderTheme::adjustSliderThumbStyle(styleResolver, style, element);
1525 style.setBoxShadow(nullptr);
1528 const float verticalSliderHeightPadding = 0.1f;
1530 bool RenderThemeMac::paintSliderThumb(const RenderObject& o, const PaintInfo& paintInfo, const IntRect& r)
1532 NSSliderCell* sliderThumbCell = o.style().appearance() == SliderThumbVerticalPart
1533 ? sliderThumbVertical()
1534 : sliderThumbHorizontal();
1536 LocalCurrentGraphicsContext localContext(paintInfo.context());
1538 // Update the various states we respond to.
1539 updateEnabledState(sliderThumbCell, o);
1540 Element* focusDelegate = is<Element>(o.node()) ? downcast<Element>(*o.node()).focusDelegate() : nullptr;
1542 updateFocusedState(sliderThumbCell, *focusDelegate->renderer());
1544 // Update the pressed state using the NSCell tracking methods, since that's how NSSliderCell keeps track of it.
1546 if (o.style().appearance() == SliderThumbVerticalPart)
1547 oldPressed = m_isSliderThumbVerticalPressed;
1549 oldPressed = m_isSliderThumbHorizontalPressed;
1551 bool pressed = isPressed(o);
1553 if (o.style().appearance() == SliderThumbVerticalPart)
1554 m_isSliderThumbVerticalPressed = pressed;
1556 m_isSliderThumbHorizontalPressed = pressed;
1558 NSView *view = documentViewFor(o);
1560 if (pressed != oldPressed) {
1562 [sliderThumbCell startTrackingAt:NSPoint() inView:view];
1564 [sliderThumbCell stopTracking:NSPoint() at:NSPoint() inView:view mouseIsUp:YES];
1567 FloatRect bounds = r;
1568 // Make the height of the vertical slider slightly larger so NSSliderCell will draw a vertical slider.
1569 if (o.style().appearance() == SliderThumbVerticalPart)
1570 bounds.setHeight(bounds.height() + verticalSliderHeightPadding * o.style().effectiveZoom());
1572 GraphicsContextStateSaver stateSaver(paintInfo.context());
1573 float zoomLevel = o.style().effectiveZoom();
1575 FloatRect unzoomedRect = bounds;
1576 if (zoomLevel != 1.0f) {
1577 unzoomedRect.setWidth(unzoomedRect.width() / zoomLevel);
1578 unzoomedRect.setHeight(unzoomedRect.height() / zoomLevel);
1579 paintInfo.context().translate(unzoomedRect.x(), unzoomedRect.y());
1580 paintInfo.context().scale(FloatSize(zoomLevel, zoomLevel));
1581 paintInfo.context().translate(-unzoomedRect.x(), -unzoomedRect.y());
1584 bool shouldDrawCell = true;
1585 bool shouldDrawFocusRing = false;
1586 float deviceScaleFactor = o.document().page()->deviceScaleFactor();
1587 bool shouldUseImageBuffer = deviceScaleFactor != 1 || zoomLevel != 1;
1588 ThemeMac::drawCellOrFocusRingWithViewIntoContext(sliderThumbCell, paintInfo.context(), unzoomedRect, view, shouldDrawCell, shouldDrawFocusRing, shouldUseImageBuffer, deviceScaleFactor);
1589 [sliderThumbCell setControlView:nil];
1594 bool RenderThemeMac::paintSearchField(const RenderObject& o, const PaintInfo& paintInfo, const IntRect& r)
1596 LocalCurrentGraphicsContext localContext(paintInfo.context());
1597 NSSearchFieldCell* search = this->search();
1599 setSearchCellState(o, r);
1601 GraphicsContextStateSaver stateSaver(paintInfo.context());
1603 float zoomLevel = o.style().effectiveZoom();
1605 IntRect unzoomedRect = r;
1607 if (zoomLevel != 1.0f) {
1608 unzoomedRect.setWidth(unzoomedRect.width() / zoomLevel);
1609 unzoomedRect.setHeight(unzoomedRect.height() / zoomLevel);
1610 paintInfo.context().translate(unzoomedRect.x(), unzoomedRect.y());
1611 paintInfo.context().scale(FloatSize(zoomLevel, zoomLevel));
1612 paintInfo.context().translate(-unzoomedRect.x(), -unzoomedRect.y());
1615 // Set the search button to nil before drawing. Then reset it so we can draw it later.
1616 [search setSearchButtonCell:nil];
1618 paintCellAndSetFocusedElementNeedsRepaintIfNecessary(search, o, paintInfo, unzoomedRect);
1619 [search setControlView:nil];
1620 [search resetSearchButtonCell];
1625 void RenderThemeMac::setSearchCellState(const RenderObject& o, const IntRect&)
1627 NSSearchFieldCell* search = this->search();
1629 [search setPlaceholderString:@""];
1630 [search setControlSize:controlSizeForFont(o.style())];
1632 // Update the various states we respond to.
1633 updateEnabledState(search, o);
1634 updateFocusedState(search, o);
1637 const IntSize* RenderThemeMac::searchFieldSizes() const
1639 static const IntSize sizes[3] = { IntSize(0, 22), IntSize(0, 19), IntSize(0, 17) };
1643 void RenderThemeMac::setSearchFieldSize(RenderStyle& style) const
1645 // If the width and height are both specified, then we have nothing to do.
1646 if (!style.width().isIntrinsicOrAuto() && !style.height().isAuto())
1649 // Use the font size to determine the intrinsic width of the control.
1650 setSizeFromFont(style, searchFieldSizes());
1653 void RenderThemeMac::adjustSearchFieldStyle(StyleResolver& styleResolver, RenderStyle& style, Element*) const
1656 style.resetBorder();
1657 const short borderWidth = 2 * style.effectiveZoom();
1658 style.setBorderLeftWidth(borderWidth);
1659 style.setBorderLeftStyle(INSET);
1660 style.setBorderRightWidth(borderWidth);
1661 style.setBorderRightStyle(INSET);
1662 style.setBorderBottomWidth(borderWidth);
1663 style.setBorderBottomStyle(INSET);
1664 style.setBorderTopWidth(borderWidth);
1665 style.setBorderTopStyle(INSET);
1668 style.setHeight(Length(Auto));
1669 setSearchFieldSize(style);
1671 // Override padding size to match AppKit text positioning.
1672 const int padding = 1 * style.effectiveZoom();
1673 style.setPaddingLeft(Length(padding, Fixed));
1674 style.setPaddingRight(Length(padding, Fixed));
1675 style.setPaddingTop(Length(padding, Fixed));
1676 style.setPaddingBottom(Length(padding, Fixed));
1678 NSControlSize controlSize = controlSizeForFont(style);
1679 setFontFromControlSize(styleResolver, style, controlSize);
1681 style.setBoxShadow(nullptr);
1684 bool RenderThemeMac::paintSearchFieldCancelButton(const RenderBox& box, const PaintInfo& paintInfo, const IntRect& r)
1686 auto adjustedCancelButtonRect = [this, &box] (const FloatRect& localBoundsForCancelButton) -> FloatRect
1688 IntSize cancelButtonSizeBasedOnFontSize = sizeForSystemFont(box.style(), cancelButtonSizes());
1689 FloatSize diff = localBoundsForCancelButton.size() - FloatSize(cancelButtonSizeBasedOnFontSize);
1690 if (!diff.width() && !diff.height())
1691 return localBoundsForCancelButton;
1692 // Vertically centered and right aligned.
1693 FloatRect adjustedLocalBoundsForCancelButton = localBoundsForCancelButton;
1694 adjustedLocalBoundsForCancelButton.move(diff.width(), floorToDevicePixel(diff.height() / 2, box.document().deviceScaleFactor()));
1695 adjustedLocalBoundsForCancelButton.setSize(cancelButtonSizeBasedOnFontSize);
1696 return adjustedLocalBoundsForCancelButton;
1701 Element* input = box.element()->shadowHost();
1703 input = box.element();
1705 if (!is<RenderBox>(input->renderer()))
1708 const RenderBox& inputBox = downcast<RenderBox>(*input->renderer());
1709 LocalCurrentGraphicsContext localContext(paintInfo.context());
1710 setSearchCellState(inputBox, r);
1712 NSSearchFieldCell* search = this->search();
1714 if (!input->isDisabledFormControl() && (is<HTMLTextFormControlElement>(*input) && !downcast<HTMLTextFormControlElement>(*input).isReadOnly()))
1715 updatePressedState([search cancelButtonCell], box);
1716 else if ([[search cancelButtonCell] isHighlighted])
1717 [[search cancelButtonCell] setHighlighted:NO];
1719 GraphicsContextStateSaver stateSaver(paintInfo.context());
1721 float zoomLevel = box.style().effectiveZoom();
1723 FloatRect localBounds = adjustedCancelButtonRect([search cancelButtonRectForBounds:NSRect(snappedIntRect(inputBox.contentBoxRect()))]);
1724 FloatPoint paintingPos = convertToPaintingPosition(inputBox, box, localBounds.location(), r.location());
1726 FloatRect unzoomedRect(paintingPos, localBounds.size());
1727 if (zoomLevel != 1.0f) {
1728 unzoomedRect.setWidth(unzoomedRect.width() / zoomLevel);
1729 unzoomedRect.setHeight(unzoomedRect.height() / zoomLevel);
1730 paintInfo.context().translate(unzoomedRect.x(), unzoomedRect.y());
1731 paintInfo.context().scale(FloatSize(zoomLevel, zoomLevel));
1732 paintInfo.context().translate(-unzoomedRect.x(), -unzoomedRect.y());
1734 [[search cancelButtonCell] drawWithFrame:unzoomedRect inView:documentViewFor(box)];
1735 [[search cancelButtonCell] setControlView:nil];
1739 const IntSize* RenderThemeMac::cancelButtonSizes() const
1741 static const IntSize sizes[3] = { IntSize(22, 22), IntSize(19, 19), IntSize(15, 15) };
1745 void RenderThemeMac::adjustSearchFieldCancelButtonStyle(StyleResolver&, RenderStyle& style, Element*) const
1747 IntSize size = sizeForSystemFont(style, cancelButtonSizes());
1748 style.setWidth(Length(size.width(), Fixed));
1749 style.setHeight(Length(size.height(), Fixed));
1750 style.setBoxShadow(nullptr);
1753 const int resultsArrowWidth = 5;
1754 const IntSize* RenderThemeMac::resultsButtonSizes() const
1756 static const IntSize sizes[3] = { IntSize(19, 22), IntSize(17, 19), IntSize(17, 15) };
1760 const int emptyResultsOffset = 9;
1761 void RenderThemeMac::adjustSearchFieldDecorationPartStyle(StyleResolver&, RenderStyle& style, Element*) const
1763 IntSize size = sizeForSystemFont(style, resultsButtonSizes());
1764 style.setWidth(Length(size.width() - emptyResultsOffset, Fixed));
1765 style.setHeight(Length(size.height(), Fixed));
1766 style.setBoxShadow(nullptr);
1769 bool RenderThemeMac::paintSearchFieldDecorationPart(const RenderObject&, const PaintInfo&, const IntRect&)
1774 void RenderThemeMac::adjustSearchFieldResultsDecorationPartStyle(StyleResolver&, RenderStyle& style, Element*) const
1776 IntSize size = sizeForSystemFont(style, resultsButtonSizes());
1777 style.setWidth(Length(size.width(), Fixed));
1778 style.setHeight(Length(size.height(), Fixed));
1779 style.setBoxShadow(nullptr);
1782 bool RenderThemeMac::paintSearchFieldResultsDecorationPart(const RenderBox& box, const PaintInfo& paintInfo, const IntRect& r)
1786 Element* input = box.element()->shadowHost();
1788 input = box.element();
1789 if (!is<RenderBox>(input->renderer()))
1792 const RenderBox& inputBox = downcast<RenderBox>(*input->renderer());
1793 LocalCurrentGraphicsContext localContext(paintInfo.context());
1794 setSearchCellState(inputBox, r);
1796 NSSearchFieldCell* search = this->search();
1798 if ([search searchMenuTemplate] != nil)
1799 [search setSearchMenuTemplate:nil];
1801 FloatRect localBounds = [search searchButtonRectForBounds:NSRect(snappedIntRect(inputBox.borderBoxRect()))];
1802 FloatPoint paintingPos = convertToPaintingPosition(inputBox, box, localBounds.location(), r.location());
1803 localBounds.setLocation(paintingPos);
1805 [[search searchButtonCell] drawWithFrame:localBounds inView:documentViewFor(box)];
1806 [[search searchButtonCell] setControlView:nil];
1810 void RenderThemeMac::adjustSearchFieldResultsButtonStyle(StyleResolver&, RenderStyle& style, Element*) const
1812 IntSize size = sizeForSystemFont(style, resultsButtonSizes());
1813 style.setWidth(Length(size.width() + resultsArrowWidth, Fixed));
1814 style.setHeight(Length(size.height(), Fixed));
1815 style.setBoxShadow(nullptr);
1818 bool RenderThemeMac::paintSearchFieldResultsButton(const RenderBox& box, const PaintInfo& paintInfo, const IntRect& r)
1820 auto adjustedResultButtonRect = [this, &box] (const FloatRect& localBounds) -> FloatRect
1822 IntSize buttonSize = sizeForSystemFont(box.style(), resultsButtonSizes());
1823 buttonSize.expand(resultsArrowWidth, 0);
1824 FloatSize diff = localBounds.size() - FloatSize(buttonSize);
1827 // Vertically centered and left aligned.
1828 FloatRect adjustedLocalBounds = localBounds;
1829 adjustedLocalBounds.move(0, floorToDevicePixel(diff.height() / 2, box.document().deviceScaleFactor()));
1830 adjustedLocalBounds.setSize(buttonSize);
1831 return adjustedLocalBounds;
1834 Element* input = box.element()->shadowHost();
1836 input = box.element();
1837 if (!is<RenderBox>(input->renderer()))
1840 const RenderBox& inputBox = downcast<RenderBox>(*input->renderer());
1841 LocalCurrentGraphicsContext localContext(paintInfo.context());
1842 setSearchCellState(inputBox, r);
1844 NSSearchFieldCell* search = this->search();
1846 if (![search searchMenuTemplate])
1847 [search setSearchMenuTemplate:searchMenuTemplate()];
1849 GraphicsContextStateSaver stateSaver(paintInfo.context());
1850 float zoomLevel = box.style().effectiveZoom();
1852 FloatRect localBounds = adjustedResultButtonRect([search searchButtonRectForBounds:NSRect(snappedIntRect(inputBox.contentBoxRect()))]);
1853 FloatPoint paintingPos = convertToPaintingPosition(inputBox, box, localBounds.location(), r.location());
1855 FloatRect unzoomedRect(paintingPos, localBounds.size());
1856 if (zoomLevel != 1.0f) {
1857 unzoomedRect.setWidth(unzoomedRect.width() / zoomLevel);
1858 unzoomedRect.setHeight(unzoomedRect.height() / zoomLevel);
1859 paintInfo.context().translate(unzoomedRect.x(), unzoomedRect.y());
1860 paintInfo.context().scale(FloatSize(zoomLevel, zoomLevel));
1861 paintInfo.context().translate(-unzoomedRect.x(), -unzoomedRect.y());
1864 [[search searchButtonCell] drawWithFrame:unzoomedRect inView:documentViewFor(box)];
1865 [[search searchButtonCell] setControlView:nil];
1870 bool RenderThemeMac::paintSnapshottedPluginOverlay(const RenderObject& renderer, const PaintInfo& paintInfo, const IntRect&)
1872 if (paintInfo.phase != PaintPhaseBlockBackground)
1875 if (!is<RenderBlock>(renderer))
1878 const RenderBlock& renderBlock = downcast<RenderBlock>(renderer);
1880 LayoutUnit contentWidth = renderBlock.contentWidth();
1881 LayoutUnit contentHeight = renderBlock.contentHeight();
1882 if (!contentWidth || !contentHeight)
1885 GraphicsContext& context = paintInfo.context();
1887 LayoutSize contentSize(contentWidth, contentHeight);
1888 LayoutPoint contentLocation = renderBlock.location();
1889 contentLocation.move(renderBlock.borderLeft() + renderBlock.paddingLeft(), renderBlock.borderTop() + renderBlock.paddingTop());
1891 LayoutRect rect(contentLocation, contentSize);
1892 IntRect alignedRect = snappedIntRect(rect);
1893 if (alignedRect.width() <= 0 || alignedRect.height() <= 0)
1896 // We need to get the snapshot image from the plugin element, which should be available
1897 // from our node. Assuming this node is the plugin overlay element, we should get to the
1898 // plugin itself by asking for the shadow root parent, and then its parent.
1900 if (!is<HTMLElement>(*renderBlock.element()))
1903 HTMLElement& plugInOverlay = downcast<HTMLElement>(*renderBlock.element());
1904 Element* parent = plugInOverlay.parentOrShadowHostElement();
1905 while (parent && !is<HTMLPlugInElement>(*parent))
1906 parent = parent->parentOrShadowHostElement();
1911 HTMLPlugInElement& plugInElement = downcast<HTMLPlugInElement>(*parent);
1912 if (!is<HTMLPlugInImageElement>(plugInElement))
1915 HTMLPlugInImageElement& plugInImageElement = downcast<HTMLPlugInImageElement>(plugInElement);
1917 Image* snapshot = plugInImageElement.snapshotImage();
1921 RenderSnapshottedPlugIn& plugInRenderer = downcast<RenderSnapshottedPlugIn>(*plugInImageElement.renderer());
1922 FloatPoint snapshotAbsPos = plugInRenderer.localToAbsolute();
1923 snapshotAbsPos.move(plugInRenderer.borderLeft() + plugInRenderer.paddingLeft(), plugInRenderer.borderTop() + plugInRenderer.paddingTop());
1925 // We could draw the snapshot with that coordinates, but we need to make sure there
1926 // isn't a composited layer between us and the plugInRenderer.
1927 for (auto* renderBox = &downcast<RenderBox>(renderer); renderBox != &plugInRenderer; renderBox = renderBox->parentBox()) {
1928 if (renderBox->isComposited()) {
1929 snapshotAbsPos = -renderBox->location();
1934 LayoutSize pluginSize(plugInRenderer.contentWidth(), plugInRenderer.contentHeight());
1935 LayoutRect pluginRect(snapshotAbsPos, pluginSize);
1936 IntRect alignedPluginRect = snappedIntRect(pluginRect);
1938 if (alignedPluginRect.width() <= 0 || alignedPluginRect.height() <= 0)
1941 context.drawImage(*snapshot, alignedPluginRect, CompositeSourceOver);
1945 #if ENABLE(DATALIST_ELEMENT)
1946 IntSize RenderThemeMac::sliderTickSize() const
1948 return IntSize(1, 3);
1951 int RenderThemeMac::sliderTickOffsetFromTrackCenter() const
1957 const int sliderThumbWidth = 15;
1958 const int sliderThumbHeight = 15;
1960 void RenderThemeMac::adjustSliderThumbSize(RenderStyle& style, Element*) const
1962 float zoomLevel = style.effectiveZoom();
1963 if (style.appearance() == SliderThumbHorizontalPart || style.appearance() == SliderThumbVerticalPart) {
1964 style.setWidth(Length(static_cast<int>(sliderThumbWidth * zoomLevel), Fixed));
1965 style.setHeight(Length(static_cast<int>(sliderThumbHeight * zoomLevel), Fixed));
1969 bool RenderThemeMac::shouldHaveCapsLockIndicator(HTMLInputElement& element) const
1971 return element.isPasswordField();
1974 NSPopUpButtonCell* RenderThemeMac::popupButton() const
1976 if (!m_popupButton) {
1977 m_popupButton = adoptNS([[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:NO]);
1978 [m_popupButton.get() setUsesItemFromMenu:NO];
1979 [m_popupButton.get() setFocusRingType:NSFocusRingTypeExterior];
1980 // We don't want the app's UI layout direction to affect the appearance of popup buttons in
1981 // web content, which has its own layout direction.
1982 // FIXME: Make this depend on the directionality of the select element, once the rest of the
1983 // rendering code can account for the popup arrows appearing on the other side.
1984 [m_popupButton setUserInterfaceLayoutDirection:NSUserInterfaceLayoutDirectionLeftToRight];
1987 return m_popupButton.get();
1990 NSSearchFieldCell* RenderThemeMac::search() const
1993 m_search = adoptNS([[NSSearchFieldCell alloc] initTextCell:@""]);
1994 [m_search.get() setBezelStyle:NSTextFieldRoundedBezel];
1995 [m_search.get() setBezeled:YES];
1996 [m_search.get() setEditable:YES];
1997 [m_search.get() setFocusRingType:NSFocusRingTypeExterior];
1998 [m_search.get() setCenteredLook:NO];
2001 return m_search.get();
2004 NSMenu* RenderThemeMac::searchMenuTemplate() const
2006 if (!m_searchMenuTemplate)
2007 m_searchMenuTemplate = adoptNS([[NSMenu alloc] initWithTitle:@""]);
2009 return m_searchMenuTemplate.get();
2012 NSSliderCell* RenderThemeMac::sliderThumbHorizontal() const
2014 if (!m_sliderThumbHorizontal) {
2015 m_sliderThumbHorizontal = adoptNS([[NSSliderCell alloc] init]);
2016 #pragma clang diagnostic push
2017 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
2018 [m_sliderThumbHorizontal.get() setSliderType:NSLinearSlider];
2019 [m_sliderThumbHorizontal.get() setControlSize:NSSmallControlSize];
2020 [m_sliderThumbHorizontal.get() setFocusRingType:NSFocusRingTypeExterior];
2021 #pragma clang diagnostic pop
2024 return m_sliderThumbHorizontal.get();
2027 NSSliderCell* RenderThemeMac::sliderThumbVertical() const
2029 if (!m_sliderThumbVertical) {
2030 m_sliderThumbVertical = adoptNS([[NSSliderCell alloc] init]);
2031 #pragma clang diagnostic push
2032 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
2033 [m_sliderThumbVertical.get() setSliderType:NSLinearSlider];
2034 [m_sliderThumbVertical.get() setControlSize:NSSmallControlSize];
2035 #pragma clang diagnostic pop
2036 [m_sliderThumbVertical.get() setFocusRingType:NSFocusRingTypeExterior];
2039 return m_sliderThumbVertical.get();
2042 NSTextFieldCell* RenderThemeMac::textField() const
2045 m_textField = adoptNS([[WebCoreTextFieldCell alloc] initTextCell:@""]);
2046 [m_textField.get() setBezeled:YES];
2047 [m_textField.get() setEditable:YES];
2048 [m_textField.get() setFocusRingType:NSFocusRingTypeExterior];
2049 // Post-Lion, WebCore can be in charge of paintinng the background thanks to
2050 // the workaround in place for <rdar://problem/11385461>, which is implemented
2051 // above as _coreUIDrawOptionsWithFrame.
2052 [m_textField.get() setDrawsBackground:NO];
2055 return m_textField.get();
2058 String RenderThemeMac::fileListNameForWidth(const FileList* fileList, const FontCascade& font, int width, bool multipleFilesAllowed) const
2063 String strToTruncate;
2064 if (fileList->isEmpty())
2065 strToTruncate = fileListDefaultLabel(multipleFilesAllowed);
2066 else if (fileList->length() == 1)
2067 strToTruncate = [[NSFileManager defaultManager] displayNameAtPath:(fileList->item(0)->path())];
2069 return StringTruncator::rightTruncate(multipleFileUploadText(fileList->length()), width, font);
2071 return StringTruncator::centerTruncate(strToTruncate, width, font);
2074 #if ENABLE(SERVICE_CONTROLS)
2075 NSServicesRolloverButtonCell* RenderThemeMac::servicesRolloverButtonCell() const
2077 #if HAVE(APPKIT_SERVICE_CONTROLS_SUPPORT)
2078 if (!m_servicesRolloverButton) {
2079 m_servicesRolloverButton = [NSServicesRolloverButtonCell serviceRolloverButtonCellForStyle:NSSharingServicePickerStyleRollover];
2080 [m_servicesRolloverButton setBezelStyle:NSRoundedDisclosureBezelStyle];
2081 [m_servicesRolloverButton setButtonType:NSPushOnPushOffButton];
2082 [m_servicesRolloverButton setImagePosition:NSImageOnly];
2083 [m_servicesRolloverButton setState:NO];
2086 return m_servicesRolloverButton.get();
2092 bool RenderThemeMac::paintImageControlsButton(const RenderObject& renderer, const PaintInfo& paintInfo, const IntRect& rect)
2094 if (paintInfo.phase != PaintPhaseBlockBackground)
2097 #if HAVE(APPKIT_SERVICE_CONTROLS_SUPPORT)
2098 NSServicesRolloverButtonCell *cell = servicesRolloverButtonCell();
2100 LocalCurrentGraphicsContext localContext(paintInfo.context());
2101 GraphicsContextStateSaver stateSaver(paintInfo.context());
2103 paintInfo.context().translate(rect.x(), rect.y());
2105 IntRect innerFrame(IntPoint(), rect.size());
2106 [cell drawWithFrame:innerFrame inView:documentViewFor(renderer)];
2107 [cell setControlView:nil];
2109 UNUSED_PARAM(renderer);
2116 IntSize RenderThemeMac::imageControlsButtonSize(const RenderObject&) const
2118 #if HAVE(APPKIT_SERVICE_CONTROLS_SUPPORT)
2119 return IntSize(servicesRolloverButtonCell().cellSize);
2125 IntSize RenderThemeMac::imageControlsButtonPositionOffset() const
2127 #if HAVE(APPKIT_SERVICE_CONTROLS_SUPPORT)
2128 // FIXME: Currently the offsets will always be the same no matter what image rect you try with.
2129 // This may not always be true in the future.
2130 static const int dummyDimension = 100;
2131 IntRect dummyImageRect(0, 0, dummyDimension, dummyDimension);
2132 NSRect bounds = [servicesRolloverButtonCell() rectForBounds:dummyImageRect preferredEdge:NSMinYEdge];
2134 return IntSize(dummyDimension - bounds.origin.x, bounds.origin.y);
2141 #if ENABLE(ATTACHMENT_ELEMENT)
2142 const CGFloat attachmentIconSize = 48;
2143 const CGFloat attachmentIconBackgroundPadding = 6;
2144 const CGFloat attachmentIconBackgroundSize = attachmentIconSize + attachmentIconBackgroundPadding;
2145 const CGFloat attachmentIconSelectionBorderThickness = 1;
2146 const CGFloat attachmentIconBackgroundRadius = 3;
2147 const CGFloat attachmentIconToTitleMargin = 2;
2149 static Color attachmentIconBackgroundColor() { return Color(0, 0, 0, 30); }
2150 static Color attachmentIconBorderColor() { return Color(255, 255, 255, 125); }
2152 const CGFloat attachmentTitleFontSize = 12;
2153 const CGFloat attachmentTitleBackgroundRadius = 3;
2154 const CGFloat attachmentTitleBackgroundPadding = 3;
2155 const CGFloat attachmentTitleMaximumWidth = 100 - (attachmentTitleBackgroundPadding * 2);
2156 const CFIndex attachmentTitleMaximumLineCount = 2;
2158 static Color attachmentTitleInactiveBackgroundColor() { return Color(204, 204, 204, 255); }
2159 static Color attachmentTitleInactiveTextColor() { return Color(100, 100, 100, 255); }
2161 const CGFloat attachmentSubtitleFontSize = 10;
2162 static Color attachmentSubtitleTextColor() { return Color(82, 145, 214, 255); }
2164 const CGFloat attachmentProgressBarWidth = 30;
2165 const CGFloat attachmentProgressBarHeight = 5;
2166 const CGFloat attachmentProgressBarOffset = -9;
2167 const CGFloat attachmentProgressBarBorderWidth = 1;
2168 static Color attachmentProgressBarBackgroundColor() { return Color(0, 0, 0, 89); }
2169 static Color attachmentProgressBarFillColor() { return Color(Color::white); }
2170 static Color attachmentProgressBarBorderColor() { return Color(0, 0, 0, 128); }
2172 const CGFloat attachmentMargin = 3;
2174 struct AttachmentLayout {
2175 explicit AttachmentLayout(const RenderAttachment&);
2178 FloatRect backgroundRect;
2180 RetainPtr<CTLineRef> line;
2183 Vector<LabelLine> lines;
2186 FloatRect iconBackgroundRect;
2187 FloatRect attachmentRect;
2191 RetainPtr<CTLineRef> subtitleLine;
2192 FloatRect subtitleTextRect;
2195 void layOutTitle(const RenderAttachment&);
2196 void layOutSubtitle(const RenderAttachment&);
2198 void addTitleLine(CTLineRef, CGFloat& yOffset, Vector<CGPoint> origins, CFIndex lineIndex, const RenderAttachment&);
2201 static NSColor *titleTextColorForAttachment(const RenderAttachment& attachment)
2203 if (attachment.selectionState() != RenderObject::SelectionNone) {
2204 if (attachment.frame().selection().isFocusedAndActive())
2205 return [NSColor alternateSelectedControlTextColor];
2206 return (NSColor *)cachedCGColor(attachmentTitleInactiveTextColor());
2209 return [NSColor blackColor];
2212 void AttachmentLayout::addTitleLine(CTLineRef line, CGFloat& yOffset, Vector<CGPoint> origins, CFIndex lineIndex, const RenderAttachment& attachment)
2214 CGRect lineBounds = CTLineGetBoundsWithOptions(line, 0);
2215 CGFloat trailingWhitespaceWidth = CTLineGetTrailingWhitespaceWidth(line);
2216 CGFloat lineWidthIgnoringTrailingWhitespace = lineBounds.size.width - trailingWhitespaceWidth;
2217 CGFloat lineHeight = CGCeiling(lineBounds.size.height);
2219 // Center the line relative to the icon.
2220 CGFloat xOffset = (attachmentIconBackgroundSize / 2) - (lineWidthIgnoringTrailingWhitespace / 2);
2223 yOffset += origins[lineIndex - 1].y - origins[lineIndex].y;
2225 LabelLine labelLine;
2226 labelLine.origin = FloatPoint(xOffset, yOffset + lineHeight - origins.last().y);
2227 labelLine.line = line;
2228 labelLine.backgroundRect = FloatRect(xOffset, yOffset, lineWidthIgnoringTrailingWhitespace, lineHeight);
2229 labelLine.backgroundRect.inflateX(attachmentTitleBackgroundPadding);
2230 labelLine.backgroundRect = encloseRectToDevicePixels(labelLine.backgroundRect, attachment.document().deviceScaleFactor());
2232 // If the text rects are close in size, the curved enclosing background won't
2233 // look right, so make them the same exact size.
2234 if (!lines.isEmpty()) {
2235 float previousBackgroundRectWidth = lines.last().backgroundRect.width();
2236 if (fabs(labelLine.backgroundRect.width() - previousBackgroundRectWidth) < attachmentTitleBackgroundRadius * 4) {
2237 float newBackgroundRectWidth = std::max(previousBackgroundRectWidth, labelLine.backgroundRect.width());
2238 labelLine.backgroundRect.inflateX((newBackgroundRectWidth - labelLine.backgroundRect.width()) / 2);
2239 lines.last().backgroundRect.inflateX((newBackgroundRectWidth - previousBackgroundRectWidth) / 2);
2243 lines.append(labelLine);
2246 void AttachmentLayout::layOutTitle(const RenderAttachment& attachment)
2248 CFStringRef language = 0; // By not specifying a language we use the system language.
2249 RetainPtr<CTFontRef> font = adoptCF(CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, attachmentTitleFontSize, language));
2250 baseline = CGRound(attachmentIconBackgroundSize + attachmentIconToTitleMargin + CTFontGetAscent(font.get()));
2252 String title = attachment.attachmentElement().attachmentTitle();
2253 if (title.isEmpty())
2256 NSDictionary *textAttributes = @{
2257 (id)kCTFontAttributeName: (id)font.get(),
2258 (id)kCTForegroundColorAttributeName: titleTextColorForAttachment(attachment)
2260 RetainPtr<NSAttributedString> attributedTitle = adoptNS([[NSAttributedString alloc] initWithString:title attributes:textAttributes]);
2261 RetainPtr<CTFramesetterRef> titleFramesetter = adoptCF(CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedTitle.get()));
2264 CGSize titleTextSize = CTFramesetterSuggestFrameSizeWithConstraints(titleFramesetter.get(), CFRangeMake(0, 0), nullptr, CGSizeMake(attachmentTitleMaximumWidth, CGFLOAT_MAX), &fitRange);
2266 RetainPtr<CGPathRef> titlePath = adoptCF(CGPathCreateWithRect(CGRectMake(0, 0, titleTextSize.width, titleTextSize.height), nullptr));
2267 RetainPtr<CTFrameRef> titleFrame = adoptCF(CTFramesetterCreateFrame(titleFramesetter.get(), fitRange, titlePath.get(), nullptr));
2269 CFArrayRef ctLines = CTFrameGetLines(titleFrame.get());
2270 CFIndex lineCount = CFArrayGetCount(ctLines);
2274 Vector<CGPoint> origins(lineCount);
2275 CTFrameGetLineOrigins(titleFrame.get(), CFRangeMake(0, 0), origins.data());
2277 // Lay out and record the first (attachmentTitleMaximumLineCount - 1) lines.
2278 CFIndex lineIndex = 0;
2279 CGFloat yOffset = attachmentIconBackgroundSize + attachmentIconToTitleMargin;
2280 for (; lineIndex < std::min(attachmentTitleMaximumLineCount - 1, lineCount); ++lineIndex) {
2281 CTLineRef line = (CTLineRef)CFArrayGetValueAtIndex(ctLines, lineIndex);
2282 addTitleLine(line, yOffset, origins, lineIndex, attachment);
2285 if (lineIndex == lineCount)
2288 // We had text that didn't fit in the first (attachmentTitleMaximumLineCount - 1) lines.
2289 // Combine it into one last line, and center-truncate it.
2290 CTLineRef firstRemainingLine = (CTLineRef)CFArrayGetValueAtIndex(ctLines, lineIndex);
2291 CFIndex remainingRangeStart = CTLineGetStringRange(firstRemainingLine).location;
2292 NSRange remainingRange = NSMakeRange(remainingRangeStart, [attributedTitle length] - remainingRangeStart);
2293 NSAttributedString *remainingString = [attributedTitle attributedSubstringFromRange:remainingRange];
2294 RetainPtr<CTLineRef> remainingLine = adoptCF(CTLineCreateWithAttributedString((CFAttributedStringRef)remainingString));
2295 RetainPtr<NSAttributedString> ellipsisString = adoptNS([[NSAttributedString alloc] initWithString:@"\u2026" attributes:textAttributes]);
2296 RetainPtr<CTLineRef> ellipsisLine = adoptCF(CTLineCreateWithAttributedString((CFAttributedStringRef)ellipsisString.get()));
2297 RetainPtr<CTLineRef> truncatedLine = adoptCF(CTLineCreateTruncatedLine(remainingLine.get(), attachmentTitleMaximumWidth, kCTLineTruncationMiddle, ellipsisLine.get()));
2300 truncatedLine = remainingLine;
2302 addTitleLine(truncatedLine.get(), yOffset, origins, lineIndex, attachment);
2305 void AttachmentLayout::layOutSubtitle(const RenderAttachment& attachment)
2307 String subtitleText = attachment.attachmentElement().fastGetAttribute(subtitleAttr);
2309 if (subtitleText.isEmpty())
2312 CFStringRef language = 0; // By not specifying a language we use the system language.
2313 RetainPtr<CTFontRef> font = adoptCF(CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, attachmentSubtitleFontSize, language));
2314 NSDictionary *textAttributes = @{
2315 (id)kCTFontAttributeName: (id)font.get(),
2316 (id)kCTForegroundColorAttributeName: (NSColor *)cachedCGColor(attachmentSubtitleTextColor())
2318 RetainPtr<NSAttributedString> attributedSubtitleText = adoptNS([[NSAttributedString alloc] initWithString:subtitleText attributes:textAttributes]);
2319 subtitleLine = adoptCF(CTLineCreateWithAttributedString((CFAttributedStringRef)attributedSubtitleText.get()));
2321 CGRect lineBounds = CTLineGetBoundsWithOptions(subtitleLine.get(), 0);
2323 // Center the line relative to the icon.
2324 CGFloat xOffset = (attachmentIconBackgroundSize / 2) - (lineBounds.size.width / 2);
2325 CGFloat yOffset = 0;
2327 if (!lines.isEmpty())
2328 yOffset = lines.last().backgroundRect.maxY();
2330 yOffset = attachmentIconBackgroundSize + attachmentIconToTitleMargin;
2332 LabelLine labelLine;
2333 subtitleTextRect = FloatRect(xOffset, yOffset, lineBounds.size.width, lineBounds.size.height);
2336 AttachmentLayout::AttachmentLayout(const RenderAttachment& attachment)
2338 layOutTitle(attachment);
2339 layOutSubtitle(attachment);
2341 iconBackgroundRect = FloatRect(0, 0, attachmentIconBackgroundSize, attachmentIconBackgroundSize);
2343 iconRect = iconBackgroundRect;
2344 iconRect.setSize(FloatSize(attachmentIconSize, attachmentIconSize));
2345 iconRect.move(attachmentIconBackgroundPadding / 2, attachmentIconBackgroundPadding / 2);
2347 attachmentRect = iconBackgroundRect;
2348 for (const auto& line : lines)
2349 attachmentRect.unite(line.backgroundRect);
2350 attachmentRect.unite(subtitleTextRect);
2351 attachmentRect.inflate(attachmentMargin);
2352 attachmentRect = encloseRectToDevicePixels(attachmentRect, attachment.document().deviceScaleFactor());
2355 LayoutSize RenderThemeMac::attachmentIntrinsicSize(const RenderAttachment& attachment) const
2357 AttachmentLayout layout(attachment);
2358 return LayoutSize(layout.attachmentRect.size());
2361 int RenderThemeMac::attachmentBaseline(const RenderAttachment& attachment) const
2363 AttachmentLayout layout(attachment);
2364 return layout.baseline;
2367 static void paintAttachmentIconBackground(const RenderAttachment&, GraphicsContext& context, AttachmentLayout& layout)
2369 // FIXME: Finder has a discontinuous behavior here when you have a background color other than white,
2370 // where it switches into 'bordered mode' and the border pops in on top of the background.
2371 bool paintBorder = true;
2373 FloatRect backgroundRect = layout.iconBackgroundRect;
2375 backgroundRect.inflate(-attachmentIconSelectionBorderThickness);
2377 context.fillRoundedRect(FloatRoundedRect(backgroundRect, FloatRoundedRect::Radii(attachmentIconBackgroundRadius)), attachmentIconBackgroundColor());
2380 FloatRect borderRect = layout.iconBackgroundRect;
2381 borderRect.inflate(-attachmentIconSelectionBorderThickness / 2);
2383 FloatSize iconBackgroundRadiusSize(attachmentIconBackgroundRadius, attachmentIconBackgroundRadius);
2385 borderPath.addRoundedRect(borderRect, iconBackgroundRadiusSize);
2386 context.setStrokeColor(attachmentIconBorderColor());
2387 context.setStrokeThickness(attachmentIconSelectionBorderThickness);
2388 context.strokePath(borderPath);
2392 static RefPtr<Icon> iconForAttachment(const RenderAttachment& attachment)
2394 String MIMEType = attachment.attachmentElement().attachmentType();
2395 if (!MIMEType.isEmpty()) {
2396 if (equalIgnoringASCIICase(MIMEType, "multipart/x-folder") || equalIgnoringASCIICase(MIMEType, "application/vnd.apple.folder")) {
2397 if (auto icon = Icon::createIconForUTI("public.directory"))
2399 } else if (auto icon = Icon::createIconForMIMEType(MIMEType))
2403 if (File* file = attachment.attachmentElement().file()) {
2404 if (auto icon = Icon::createIconForFiles({ file->path() }))
2408 NSString *fileExtension = [static_cast<NSString *>(attachment.attachmentElement().attachmentTitle()) pathExtension];
2409 if (fileExtension.length) {
2410 if (auto icon = Icon::createIconForFileExtension(fileExtension))
2414 return Icon::createIconForUTI("public.data");
2417 static void paintAttachmentIcon(const RenderAttachment& attachment, GraphicsContext& context, AttachmentLayout& layout)
2419 auto icon = iconForAttachment(attachment);
2422 icon->paint(context, layout.iconRect);
2425 static void paintAttachmentTitleBackground(const RenderAttachment& attachment, GraphicsContext& context, AttachmentLayout& layout)
2427 if (layout.lines.isEmpty())
2430 Vector<FloatRect> backgroundRects;
2432 for (size_t i = 0; i < layout.lines.size(); ++i)
2433 backgroundRects.append(layout.lines[i].backgroundRect);
2435 Color backgroundColor;
2436 if (attachment.frame().selection().isFocusedAndActive())
2437 backgroundColor = convertNSColorToColor([NSColor alternateSelectedControlColor]);
2439 backgroundColor = attachmentTitleInactiveBackgroundColor();
2441 context.setFillColor(backgroundColor);
2443 Path backgroundPath = PathUtilities::pathWithShrinkWrappedRects(backgroundRects, attachmentTitleBackgroundRadius);
2444 context.fillPath(backgroundPath);
2447 static void paintAttachmentTitle(const RenderAttachment&, GraphicsContext& context, AttachmentLayout& layout)
2449 for (const auto& line : layout.lines) {
2450 GraphicsContextStateSaver saver(context);
2452 context.translate(toFloatSize(line.origin));
2453 context.scale(FloatSize(1, -1));
2455 CGContextSetTextMatrix(context.platformContext(), CGAffineTransformIdentity);
2456 CTLineDraw(line.line.get(), context.platformContext());
2460 static void paintAttachmentSubtitle(const RenderAttachment&, GraphicsContext& context, AttachmentLayout& layout)
2462 GraphicsContextStateSaver saver(context);
2464 context.translate(toFloatSize(layout.subtitleTextRect.minXMaxYCorner()));
2465 context.scale(FloatSize(1, -1));
2467 CGContextSetTextMatrix(context.platformContext(), CGAffineTransformIdentity);
2468 CTLineDraw(layout.subtitleLine.get(), context.platformContext());
2471 static void paintAttachmentProgress(const RenderAttachment& attachment, GraphicsContext& context, AttachmentLayout& layout)
2473 String progressString = attachment.attachmentElement().fastGetAttribute(progressAttr);
2474 if (progressString.isEmpty())
2477 float progress = progressString.toFloat(&validProgress);
2481 GraphicsContextStateSaver saver(context);
2483 FloatRect progressBounds((attachmentIconBackgroundSize - attachmentProgressBarWidth) / 2, layout.iconBackgroundRect.maxY() + attachmentProgressBarOffset - attachmentProgressBarHeight, attachmentProgressBarWidth, attachmentProgressBarHeight);
2485 FloatRect borderRect = progressBounds;
2486 borderRect.inflate(-0.5);
2487 FloatRect backgroundRect = borderRect;
2488 backgroundRect.inflate(-attachmentProgressBarBorderWidth / 2);
2490 FloatRoundedRect backgroundRoundedRect(backgroundRect, FloatRoundedRect::Radii(backgroundRect.height() / 2));
2491 context.fillRoundedRect(backgroundRoundedRect, attachmentProgressBarBackgroundColor());
2494 GraphicsContextStateSaver clipSaver(context);
2495 context.clipRoundedRect(backgroundRoundedRect);
2497 FloatRect progressRect = progressBounds;
2498 progressRect.setWidth(progressRect.width() * progress);
2499 progressRect = encloseRectToDevicePixels(progressRect, attachment.document().deviceScaleFactor());
2501 context.fillRect(progressRect, attachmentProgressBarFillColor());
2505 float borderRadius = borderRect.height() / 2;
2506 borderPath.addRoundedRect(borderRect, FloatSize(borderRadius, borderRadius));
2507 context.setStrokeColor(attachmentProgressBarBorderColor());
2508 context.setStrokeThickness(attachmentProgressBarBorderWidth);
2509 context.strokePath(borderPath);
2512 bool RenderThemeMac::paintAttachment(const RenderObject& renderer, const PaintInfo& paintInfo, const IntRect& paintRect)
2514 if (!is<RenderAttachment>(renderer))
2517 const RenderAttachment& attachment = downcast<RenderAttachment>(renderer);
2519 AttachmentLayout layout(attachment);
2521 GraphicsContext& context = paintInfo.context();
2522 LocalCurrentGraphicsContext localContext(context);
2523 GraphicsContextStateSaver saver(context);
2525 context.translate(toFloatSize(paintRect.location()));
2526 context.translate(FloatSize((layout.attachmentRect.width() - attachmentIconBackgroundSize) / 2, 0));
2528 bool useSelectedStyle = attachment.selectionState() != RenderObject::SelectionNone;
2530 if (useSelectedStyle)
2531 paintAttachmentIconBackground(attachment, context, layout);
2532 paintAttachmentIcon(attachment, context, layout);
2533 if (useSelectedStyle)
2534 paintAttachmentTitleBackground(attachment, context, layout);
2535 paintAttachmentTitle(attachment, context, layout);
2536 paintAttachmentSubtitle(attachment, context, layout);
2537 paintAttachmentProgress(attachment, context, layout);
2542 #endif // ENABLE(ATTACHMENT_ELEMENT)
2544 } // namespace WebCore
2546 #endif // !PLATFORM(IOS)