Implement <attachment> element appearance on Mac
authortimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 27 Feb 2015 02:07:22 +0000 (02:07 +0000)
committertimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 27 Feb 2015 02:07:22 +0000 (02:07 +0000)
https://bugs.webkit.org/show_bug.cgi?id=142023

Reviewed by Dean Jackson.

* css/CSSParser.cpp:
(WebCore::isValidKeywordPropertyAndValue):
* css/CSSPrimitiveValueMappings.h:
(WebCore::CSSPrimitiveValue::CSSPrimitiveValue):
* css/CSSValueKeywords.in:
* css/html.css:
(attachment):
* platform/ThemeTypes.h:
Add a new -webkit-appearance value, attachment.

* WebCore.xcodeproj/project.pbxproj:
* html/HTMLAttachmentElement.cpp:
(WebCore::HTMLAttachmentElement::setFile):
Let the renderer know when the file changes (so it can relayout and repaint).

(WebCore::HTMLAttachmentElement::filePath):
(WebCore::HTMLAttachmentElement::fileName):
* html/HTMLAttachmentElement.h:
Handy acccessors for the full path and the name of the file.

* platform/spi/mac/IconServicesSPI.h: Added.
* platform/spi/mac/LaunchServicesSPI.h: Added.
Add some IconServices and LaunchServices SPI headers.

* rendering/RenderAttachment.cpp:
(WebCore::RenderAttachment::RenderAttachment):
Don't have a default intrinsic size; we'll adjust in layout().

(WebCore::RenderAttachment::isSelected):
(WebCore::RenderAttachment::isFocused):
(WebCore::RenderAttachment::isSelectedOrFocused):
Factor out from paintReplaced().

(WebCore::RenderAttachment::layout):
Use RenderTheme to compute the intrinsic size of this attachment.

(WebCore::RenderAttachment::paintReplaced):
Use RenderTheme to paint the attachment.

(WebCore::RenderAttachment::representedFileChanged):
When the represented file changes, we need to recompute the intrinsic size and repaint.

* rendering/RenderTheme.cpp:
(WebCore::RenderTheme::adjustStyle):
(WebCore::RenderTheme::paint):
(WebCore::RenderTheme::adjustAttachmentStyle):
(WebCore::RenderTheme::paintAttachment):
Plumb through the attachment appearance.

* rendering/RenderAttachment.h:
* rendering/RenderTheme.h:
(WebCore::RenderTheme::paintAttachment):
(WebCore::RenderTheme::attachmentIntrinsicSize):
* rendering/RenderThemeMac.h:
* rendering/RenderThemeMac.mm:
(WebCore::attachmentIconBackgroundColor):
(WebCore::attachmentIconBorderColor):
(WebCore::AttachmentLayout):
AttachmentLayout lays out the innards of the RenderThemeMac attachment
presentation: an icon with an optional bordered background, with a label
underneath it with an optional background.

(WebCore::RenderThemeMac::attachmentIntrinsicSize):
(WebCore::paintAttachmentIconBackground):
(WebCore::paintAttachmentIcon):
(WebCore::RenderThemeMac::paintAttachmentLabelBackground):
(WebCore::RenderThemeMac::paintAttachmentLabel):
(WebCore::RenderThemeMac::paintAttachment):
Paint the attachment element. Paint the optional backgrounds (and swap
out the text color) if the element is selected/focused.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@180720 268f45cc-cd09-0410-ab3c-d52691b4dbfc

21 files changed:
LayoutTests/ChangeLog
LayoutTests/fast/attachment/attachment-disabled-rendering-expected.txt
LayoutTests/fast/attachment/attachment-rendering-expected.txt
LayoutTests/platform/mac/fast/attachment/attachment-disabled-rendering-expected.png [new file with mode: 0644]
LayoutTests/platform/mac/fast/attachment/attachment-rendering-expected.png [new file with mode: 0644]
LayoutTests/platform/mac/fast/attachment/attachment-rendering-expected.txt [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/WebCore.xcodeproj/project.pbxproj
Source/WebCore/css/CSSPrimitiveValueMappings.h
Source/WebCore/css/CSSValueKeywords.in
Source/WebCore/css/html.css
Source/WebCore/html/HTMLAttachmentElement.cpp
Source/WebCore/platform/ThemeTypes.h
Source/WebCore/platform/spi/mac/IconServicesSPI.h [new file with mode: 0644]
Source/WebCore/platform/spi/mac/LaunchServicesSPI.h [new file with mode: 0644]
Source/WebCore/rendering/RenderAttachment.cpp
Source/WebCore/rendering/RenderAttachment.h
Source/WebCore/rendering/RenderTheme.cpp
Source/WebCore/rendering/RenderTheme.h
Source/WebCore/rendering/RenderThemeMac.h
Source/WebCore/rendering/RenderThemeMac.mm

index 349f7ae..4d9f474 100644 (file)
@@ -1,3 +1,16 @@
+2015-02-26  Timothy Horton  <timothy_horton@apple.com>
+
+        Implement <attachment> element appearance on Mac
+        https://bugs.webkit.org/show_bug.cgi?id=142023
+
+        Reviewed by Dean Jackson.
+
+        * fast/attachment/attachment-rendering-expected.txt:
+        * platform/mac/fast/attachment/attachment-disabled-rendering-expected.png: Added.
+        * platform/mac/fast/attachment/attachment-rendering-expected.png: Added.
+        * platform/mac/fast/attachment/attachment-rendering-expected.txt: Added.
+        Add Mac overrides for <attachment> tests (and pixel results).
+
 2015-02-26  Doug Russell  <d_russell@apple.com>
 
         AX: Expose caret browsing preference to accessibility API
index a52e125..8393156 100644 (file)
@@ -2,10 +2,10 @@ layer at (0,0) size 800x600
   RenderView at (0,0) size 800x600
 layer at (0,0) size 800x600
   RenderBlock {HTML} at (0,0) size 800x600
-    RenderBody {BODY} at (8,8) size 784x576
+    RenderBody {BODY} at (8,8) size 784x584
       RenderBlock {P} at (0,0) size 784x18
         RenderText {#text} at (0,0) size 766x18
           text run at (0,0) width 766: "This tests that attachments don't have a custom renderer when they are disabled. This test must be run in the test runner."
       RenderBlock (anonymous) at (0,34) size 784x0
-        RenderInline {ATTACHMENT} at (0,0) size 0x0
+        RenderBlock {ATTACHMENT} at (0,0) size 0x0
         RenderText {#text} at (0,0) size 0x0
index c9e991c..28a7b54 100644 (file)
@@ -6,6 +6,6 @@ layer at (0,0) size 800x600
       RenderBlock {P} at (0,0) size 784x18
         RenderText {#text} at (0,0) size 326x18
           text run at (0,0) width 326: "This tests that attachments have a custom renderer."
-      RenderBlock (anonymous) at (0,34) size 784x200
-        RenderAttachment {ATTACHMENT} at (0,0) size 200x200
+      RenderBlock (anonymous) at (0,34) size 784x0
+        RenderAttachment {ATTACHMENT} at (0,0) size 0x0
         RenderText {#text} at (0,0) size 0x0
diff --git a/LayoutTests/platform/mac/fast/attachment/attachment-disabled-rendering-expected.png b/LayoutTests/platform/mac/fast/attachment/attachment-disabled-rendering-expected.png
new file mode 100644 (file)
index 0000000..d613ff3
Binary files /dev/null and b/LayoutTests/platform/mac/fast/attachment/attachment-disabled-rendering-expected.png differ
diff --git a/LayoutTests/platform/mac/fast/attachment/attachment-rendering-expected.png b/LayoutTests/platform/mac/fast/attachment/attachment-rendering-expected.png
new file mode 100644 (file)
index 0000000..1266df7
Binary files /dev/null and b/LayoutTests/platform/mac/fast/attachment/attachment-rendering-expected.png differ
diff --git a/LayoutTests/platform/mac/fast/attachment/attachment-rendering-expected.txt b/LayoutTests/platform/mac/fast/attachment/attachment-rendering-expected.txt
new file mode 100644 (file)
index 0000000..a2727e5
--- /dev/null
@@ -0,0 +1,11 @@
+layer at (0,0) size 800x600
+  RenderView at (0,0) size 800x600
+layer at (0,0) size 800x600
+  RenderBlock {HTML} at (0,0) size 800x600
+    RenderBody {BODY} at (8,8) size 784x584
+      RenderBlock {P} at (0,0) size 784x18
+        RenderText {#text} at (0,0) size 326x18
+          text run at (0,0) width 326: "This tests that attachments have a custom renderer."
+      RenderBlock (anonymous) at (0,34) size 784x77
+        RenderAttachment {ATTACHMENT} at (0,0) size 60x77
+        RenderText {#text} at (0,0) size 0x0
index d5fb070..03ea2a0 100644 (file)
@@ -1,3 +1,81 @@
+2015-02-26  Timothy Horton  <timothy_horton@apple.com>
+
+        Implement <attachment> element appearance on Mac
+        https://bugs.webkit.org/show_bug.cgi?id=142023
+
+        Reviewed by Dean Jackson.
+
+        * css/CSSParser.cpp:
+        (WebCore::isValidKeywordPropertyAndValue):
+        * css/CSSPrimitiveValueMappings.h:
+        (WebCore::CSSPrimitiveValue::CSSPrimitiveValue):
+        * css/CSSValueKeywords.in:
+        * css/html.css:
+        (attachment):
+        * platform/ThemeTypes.h:
+        Add a new -webkit-appearance value, attachment.
+
+        * WebCore.xcodeproj/project.pbxproj:
+        * html/HTMLAttachmentElement.cpp:
+        (WebCore::HTMLAttachmentElement::setFile):
+        Let the renderer know when the file changes (so it can relayout and repaint).
+
+        (WebCore::HTMLAttachmentElement::filePath):
+        (WebCore::HTMLAttachmentElement::fileName):
+        * html/HTMLAttachmentElement.h:
+        Handy acccessors for the full path and the name of the file.
+
+        * platform/spi/mac/IconServicesSPI.h: Added.
+        * platform/spi/mac/LaunchServicesSPI.h: Added.
+        Add some IconServices and LaunchServices SPI headers.
+
+        * rendering/RenderAttachment.cpp:
+        (WebCore::RenderAttachment::RenderAttachment):
+        Don't have a default intrinsic size; we'll adjust in layout().
+
+        (WebCore::RenderAttachment::isSelected):
+        (WebCore::RenderAttachment::isFocused):
+        (WebCore::RenderAttachment::isSelectedOrFocused):
+        Factor out from paintReplaced().
+
+        (WebCore::RenderAttachment::layout):
+        Use RenderTheme to compute the intrinsic size of this attachment.
+
+        (WebCore::RenderAttachment::paintReplaced):
+        Use RenderTheme to paint the attachment.
+
+        (WebCore::RenderAttachment::representedFileChanged):
+        When the represented file changes, we need to recompute the intrinsic size and repaint.
+
+        * rendering/RenderTheme.cpp:
+        (WebCore::RenderTheme::adjustStyle):
+        (WebCore::RenderTheme::paint):
+        (WebCore::RenderTheme::adjustAttachmentStyle):
+        (WebCore::RenderTheme::paintAttachment):
+        Plumb through the attachment appearance.
+
+        * rendering/RenderAttachment.h:
+        * rendering/RenderTheme.h:
+        (WebCore::RenderTheme::paintAttachment):
+        (WebCore::RenderTheme::attachmentIntrinsicSize):
+        * rendering/RenderThemeMac.h:
+        * rendering/RenderThemeMac.mm:
+        (WebCore::attachmentIconBackgroundColor):
+        (WebCore::attachmentIconBorderColor):
+        (WebCore::AttachmentLayout):
+        AttachmentLayout lays out the innards of the RenderThemeMac attachment
+        presentation: an icon with an optional bordered background, with a label
+        underneath it with an optional background.
+
+        (WebCore::RenderThemeMac::attachmentIntrinsicSize):
+        (WebCore::paintAttachmentIconBackground):
+        (WebCore::paintAttachmentIcon):
+        (WebCore::RenderThemeMac::paintAttachmentLabelBackground):
+        (WebCore::RenderThemeMac::paintAttachmentLabel):
+        (WebCore::RenderThemeMac::paintAttachment):
+        Paint the attachment element. Paint the optional backgrounds (and swap
+        out the text color) if the element is selected/focused.
+
 2015-02-26  Doug Russell  <d_russell@apple.com>
 
         AX: Expose caret browsing preference to accessibility API
index 5b2bc53..4c908df 100644 (file)
                2D6E468417D660F500ECF8BB /* PDFDocumentImageMac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2D6E468217D660F500ECF8BB /* PDFDocumentImageMac.mm */; };
                2D70BA1318074DDF0001908A /* PlatformCALayerMac.h in Headers */ = {isa = PBXBuildFile; fileRef = 2D70BA1218074DDF0001908A /* PlatformCALayerMac.h */; settings = {ATTRIBUTES = (Private, ); }; };
                2D70BA1518074F860001908A /* PlatformCALayer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2D70BA1418074F850001908A /* PlatformCALayer.cpp */; };
+               2D74E99C1A9D3F3200DFB3D4 /* IconServicesSPI.h in Headers */ = {isa = PBXBuildFile; fileRef = 2D74E99B1A9D3F3200DFB3D4 /* IconServicesSPI.h */; };
+               2D74E99E1A9D402900DFB3D4 /* LaunchServicesSPI.h in Headers */ = {isa = PBXBuildFile; fileRef = 2D74E99D1A9D402900DFB3D4 /* LaunchServicesSPI.h */; };
                2D76BB821945632400CFD29A /* RunLoopObserver.h in Headers */ = {isa = PBXBuildFile; fileRef = 2D76BB801945632400CFD29A /* RunLoopObserver.h */; settings = {ATTRIBUTES = (Private, ); }; };
                2D76BB8419456F8100CFD29A /* RunLoopObserver.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2D76BB8319456F8100CFD29A /* RunLoopObserver.cpp */; };
                2D8287F616E4A0380086BD00 /* HitTestLocation.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2D8287F416E4A0380086BD00 /* HitTestLocation.cpp */; };
                2D6E468217D660F500ECF8BB /* PDFDocumentImageMac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = PDFDocumentImageMac.mm; sourceTree = "<group>"; };
                2D70BA1218074DDF0001908A /* PlatformCALayerMac.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PlatformCALayerMac.h; path = ca/mac/PlatformCALayerMac.h; sourceTree = "<group>"; };
                2D70BA1418074F850001908A /* PlatformCALayer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PlatformCALayer.cpp; path = ca/PlatformCALayer.cpp; sourceTree = "<group>"; };
+               2D74E99B1A9D3F3200DFB3D4 /* IconServicesSPI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IconServicesSPI.h; sourceTree = "<group>"; };
+               2D74E99D1A9D402900DFB3D4 /* LaunchServicesSPI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LaunchServicesSPI.h; sourceTree = "<group>"; };
                2D76BB801945632400CFD29A /* RunLoopObserver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RunLoopObserver.h; sourceTree = "<group>"; };
                2D76BB8319456F8100CFD29A /* RunLoopObserver.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RunLoopObserver.cpp; sourceTree = "<group>"; };
                2D8287F416E4A0380086BD00 /* HitTestLocation.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = HitTestLocation.cpp; sourceTree = "<group>"; };
                        isa = PBXGroup;
                        children = (
                                2D59F1BE1A0044C6001F3D29 /* DataDetectorsSPI.h */,
+                               2D74E99B1A9D3F3200DFB3D4 /* IconServicesSPI.h */,
+                               2D74E99D1A9D402900DFB3D4 /* LaunchServicesSPI.h */,
                                2D413F601A187A3F0041A521 /* LookupSPI.h */,
                                1C6466271A12C3F90094603C /* NSFontSPI.h */,
                                9321D58F1A3906FA008052BE /* NSImmediateActionGestureRecognizerSPI.h */,
                                2E37DFDB12DBAFB800A6B233 /* DOMURL.h in Headers */,
                                CD9DE18217AAD6A400EA386D /* DOMURLMediaSource.h in Headers */,
                                15C770A6100D41CD005BA267 /* DOMValidityState.h in Headers */,
+                               2D74E99C1A9D3F3200DFB3D4 /* IconServicesSPI.h in Headers */,
                                BC1A37C0097C715F0019F3D8 /* DOMViews.h in Headers */,
                                31C0FF490E4CEFDD007D6FE5 /* DOMWebKitAnimationEvent.h in Headers */,
                                31C0FF4B0E4CEFDD007D6FE5 /* DOMWebKitAnimationEventInternal.h in Headers */,
                                B2227A880D00BF220071B782 /* SVGPatternElement.h in Headers */,
                                B2227A8C0D00BF220071B782 /* SVGPointList.h in Headers */,
                                B2227A8F0D00BF220071B782 /* SVGPolyElement.h in Headers */,
+                               2D74E99E1A9D402900DFB3D4 /* LaunchServicesSPI.h in Headers */,
                                2D58D8551A15F65F00A5F726 /* DataDetection.h in Headers */,
                                B2227A910D00BF220071B782 /* SVGPolygonElement.h in Headers */,
                                B2227A940D00BF220071B782 /* SVGPolylineElement.h in Headers */,
index 615aefa..01b6126 100644 (file)
@@ -602,6 +602,11 @@ template<> inline CSSPrimitiveValue::CSSPrimitiveValue(ControlPart e)
     case CapsLockIndicatorPart:
         m_value.valueID = CSSValueCapsLockIndicator;
         break;
+#if ENABLE(ATTACHMENT_ELEMENT)
+    case AttachmentPart:
+        m_value.valueID = CSSValueAttachment;
+        break;
+#endif
 #if ENABLE(SERVICE_CONTROLS)
     case ImageControlsButtonPart:
         m_value.valueID = CSSValueImageControlsButton;
index cbe6260..29d2b8f 100644 (file)
@@ -745,6 +745,9 @@ rating-level-indicator
 image-controls-button
 #endif
 textarea
+#if defined(ENABLE_ATTACHMENT_ELEMENT) && ENABLE_ATTACHMENT_ELEMENT
+attachment
+#endif
 caps-lock-indicator
 
 //
index 5fbfee0..636aac4 100644 (file)
@@ -1193,6 +1193,10 @@ applet, embed, object, img {
 #endif
 
 #if defined(ENABLE_ATTACHMENT_ELEMENT) && ENABLE_ATTACHMENT_ELEMENT
+attachment {
+    -webkit-appearance: attachment;
+}
+
 attachment:focus {
     outline: none;
 }
index bd180cb..938d93b 100644 (file)
@@ -64,6 +64,12 @@ File* HTMLAttachmentElement::file()
 void HTMLAttachmentElement::setFile(File* file)
 {
     m_file = file;
+
+    auto* renderer = this->renderer();
+    if (!is<RenderAttachment>(renderer))
+        return;
+
+    downcast<RenderAttachment>(*renderer).representedFileChanged();
 }
 
 void HTMLAttachmentElement::setFocus(bool shouldBeFocused)
index 72dbbd8..4d1564c 100644 (file)
@@ -45,7 +45,11 @@ enum ControlPart {
 #if ENABLE(SERVICE_CONTROLS)
     ImageControlsButtonPart,
 #endif
-    TextAreaPart, CapsLockIndicatorPart
+    TextAreaPart,
+#if ENABLE(ATTACHMENT_ELEMENT)
+    AttachmentPart,
+#endif
+    CapsLockIndicatorPart
 };
 
 enum SelectionPart {
diff --git a/Source/WebCore/platform/spi/mac/IconServicesSPI.h b/Source/WebCore/platform/spi/mac/IconServicesSPI.h
new file mode 100644 (file)
index 0000000..4c65924
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2015 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "LaunchServicesSPI.h"
+#import "SoftLinking.h"
+#import <objc/runtime.h>
+
+SOFT_LINK_PRIVATE_FRAMEWORK(IconServices)
+
+typedef UInt32 ISScale;
+
+SOFT_LINK(IconServices, _ISCreateCGImageFromBindingWithSizeScaleAndOptions, CGImageRef, (LSBindingRef binding, CGSize size, ISScale scale, CFDictionaryRef options), (binding, size, scale, options))
diff --git a/Source/WebCore/platform/spi/mac/LaunchServicesSPI.h b/Source/WebCore/platform/spi/mac/LaunchServicesSPI.h
new file mode 100644 (file)
index 0000000..458e8ae
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2015 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#if USE(APPLE_INTERNAL_SDK)
+
+#include <LaunchServices/LaunchServicesPriv.h>
+
+#else
+
+WTF_EXTERN_C_BEGIN
+typedef struct _LSBinding* LSBindingRef;
+LSBindingRef _LSBindingCreateWithURL(CFAllocatorRef, CFURLRef);
+WTF_EXTERN_C_END
+
+#endif
index f709796..3228078 100644 (file)
 #if ENABLE(ATTACHMENT_ELEMENT)
 
 #include "FloatRect.h"
+#include "FloatRoundedRect.h"
 #include "FrameSelection.h"
 #include "HTMLAttachmentElement.h"
+#include "Page.h"
 #include "PaintInfo.h"
+#include "RenderTheme.h"
+#include "URL.h"
 
 namespace WebCore {
 
 using namespace HTMLNames;
 
 RenderAttachment::RenderAttachment(HTMLAttachmentElement& element, Ref<RenderStyle>&& style)
-    : RenderReplaced(element, WTF::move(style), LayoutSize(200, 200))
+    : RenderReplaced(element, WTF::move(style), LayoutSize())
 {
 }
 
@@ -47,17 +51,22 @@ HTMLAttachmentElement& RenderAttachment::attachmentElement() const
     return downcast<HTMLAttachmentElement>(nodeForNonAnonymous());
 }
 
-void RenderAttachment::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
+bool RenderAttachment::isSelected() const
 {
-    // FIXME: Implement
-
     RefPtr<Range> selectionRange = frame().selection().selection().firstRange();
-    bool selected = selectionRange && selectionRange->intersectsNode(&nodeForNonAnonymous(), ASSERT_NO_EXCEPTION);
-    bool focused = frame().selection().isFocusedAndActive() && document().focusedElement() == &attachmentElement();
+    return selectionRange && selectionRange->intersectsNode(&nodeForNonAnonymous(), ASSERT_NO_EXCEPTION);
+}
 
-    paintInfo.context->save();
-    paintInfo.context->fillRect(FloatRect(paintOffset.x(), paintOffset.y(), 200, 200), selected || focused ? Color::cyan : Color::lightGray, ColorSpaceSRGB);
-    paintInfo.context->restore();
+bool RenderAttachment::isFocused() const
+{
+    return frame().selection().isFocusedAndActive() && document().focusedElement() == &attachmentElement();
+}
+
+void RenderAttachment::layout()
+{
+    setIntrinsicSize(document().page()->theme().attachmentIntrinsicSize(*this));
+
+    RenderReplaced::layout();
 }
 
 void RenderAttachment::focusChanged()
@@ -65,6 +74,12 @@ void RenderAttachment::focusChanged()
     repaint();
 }
 
+void RenderAttachment::representedFileChanged()
+{
+    setNeedsLayout();
+    repaint();
+}
+
 } // namespace WebCore
 
 #endif
index 90a5d89..67a1407 100644 (file)
@@ -41,14 +41,21 @@ public:
     HTMLAttachmentElement& attachmentElement() const;
 
     void focusChanged();
+    void representedFileChanged();
+
+    bool isSelectedOrFocused() const { return isSelected() || isFocused(); }
 
 private:
     void element() const = delete;
     virtual bool isAttachment() const override { return true; }
     virtual const char* renderName() const override { return "RenderAttachment"; }
-    virtual void paintReplaced(PaintInfo&, const LayoutPoint&) override;
 
     virtual bool shouldDrawSelectionTint() const override { return false; }
+
+    virtual void layout() override;
+
+    bool isSelected() const;
+    bool isFocused() const;
 };
 
 } // namespace WebCore
index d4ed72b..991e357 100644 (file)
@@ -252,6 +252,10 @@ void RenderTheme::adjustStyle(StyleResolver& styleResolver, RenderStyle& style,
 #endif
     case CapsLockIndicatorPart:
         return adjustCapsLockIndicatorStyle(styleResolver, style, e);
+#if ENABLE(ATTACHMENT_ELEMENT)
+    case AttachmentPart:
+        return adjustAttachmentStyle(styleResolver, style, e);
+#endif
     default:
         break;
     }
@@ -390,6 +394,10 @@ bool RenderTheme::paint(const RenderObject& o, ControlStates* controlStates, con
 #endif
     case CapsLockIndicatorPart:
         return paintCapsLockIndicator(o, paintInfo, integralSnappedRect);
+#if ENABLE(ATTACHMENT_ELEMENT)
+    case AttachmentPart:
+        return paintAttachment(o, paintInfo, integralSnappedRect);
+#endif
     default:
         break;
     }
@@ -975,6 +983,17 @@ bool RenderTheme::paintCapsLockIndicator(const RenderObject&, const PaintInfo&,
     return false;
 }
 
+#if ENABLE(ATTACHMENT_ELEMENT)
+void RenderTheme::adjustAttachmentStyle(StyleResolver&, RenderStyle&, Element*) const
+{
+}
+
+bool RenderTheme::paintAttachment(const RenderObject&, const PaintInfo&, const IntRect&)
+{
+    return false;
+}
+#endif
+
 #if ENABLE(DATALIST_ELEMENT)
 LayoutUnit RenderTheme::sliderTickSnappingThreshold() const
 {
index cda2c6d..0c0d3b0 100644 (file)
@@ -43,6 +43,7 @@ class FileList;
 class HTMLInputElement;
 class Icon;
 class PopupMenu;
+class RenderAttachment;
 class RenderMenuList;
 #if ENABLE(METER_ELEMENT)
 class RenderMeter;
@@ -249,6 +250,10 @@ public:
 
     virtual bool defaultButtonHasAnimation() const { return false; }
 
+#if ENABLE(ATTACHMENT_ELEMENT)
+    virtual LayoutSize attachmentIntrinsicSize(const RenderAttachment&) const { return LayoutSize(); }
+#endif
+
 protected:
     virtual FontDescription& cachedSystemFontDescription(CSSValueID systemFontID) const;
     virtual void updateCachedSystemFontDescription(CSSValueID systemFontID, FontDescription&) const = 0;
@@ -315,6 +320,11 @@ protected:
     virtual void adjustCapsLockIndicatorStyle(StyleResolver&, RenderStyle&, Element*) const;
     virtual bool paintCapsLockIndicator(const RenderObject&, const PaintInfo&, const IntRect&);
 
+#if ENABLE(ATTACHMENT_ELEMENT)
+    virtual void adjustAttachmentStyle(StyleResolver&, RenderStyle&, Element*) const;
+    virtual bool paintAttachment(const RenderObject&, const PaintInfo&, const IntRect&);
+#endif
+
     virtual void adjustProgressBarStyle(StyleResolver&, RenderStyle&, Element*) const;
     virtual bool paintProgressBar(const RenderObject&, const PaintInfo&, const IntRect&) { return true; }
 
index cbe2b03..e70e0a5 100644 (file)
@@ -38,6 +38,7 @@ namespace WebCore {
 
 class RenderProgress;
 class RenderStyle;
+struct AttachmentLayout;
 
 class RenderThemeMac final : public RenderTheme {
 public:
@@ -167,6 +168,11 @@ protected:
 
     virtual bool paintSnapshottedPluginOverlay(const RenderObject&, const PaintInfo&, const IntRect&) override;
 
+#if ENABLE(ATTACHMENT_ELEMENT)
+    virtual LayoutSize attachmentIntrinsicSize(const RenderAttachment&) const override;
+    virtual bool paintAttachment(const RenderObject&, const PaintInfo&, const IntRect&) override;
+#endif
+
 private:
     virtual String fileListNameForWidth(const FileList*, const FontCascade&, int width, bool multipleFilesAllowed) const override;
 
@@ -226,6 +232,11 @@ private:
     NSServicesRolloverButtonCell *servicesRolloverButtonCell() const;
 #endif
 
+#if ENABLE(ATTACHMENT_ELEMENT)
+    void paintAttachmentLabelBackground(const RenderAttachment&, GraphicsContext&, AttachmentLayout&) const;
+    void paintAttachmentLabel(const RenderAttachment&, GraphicsContext&, AttachmentLayout&, bool useSelectedStyle) const;
+#endif
+
     mutable RetainPtr<NSPopUpButtonCell> m_popupButton;
     mutable RetainPtr<NSSearchFieldCell> m_search;
     mutable RetainPtr<NSMenu> m_searchMenuTemplate;
index 1acd423..039b120 100644 (file)
 #import "Frame.h"
 #import "FrameView.h"
 #import "GraphicsContextCG.h"
+#import "HTMLAttachmentElement.h"
 #import "HTMLAudioElement.h"
 #import "HTMLInputElement.h"
 #import "HTMLMediaElement.h"
 #import "HTMLNames.h"
 #import "HTMLPlugInImageElement.h"
+#import "IconServicesSPI.h"
 #import "Image.h"
 #import "ImageBuffer.h"
+#import "LaunchServicesSPI.h"
 #import "LocalCurrentGraphicsContext.h"
 #import "LocalizedStrings.h"
 #import "MediaControlElements.h"
 #import "NSSharingServicePickerSPI.h"
 #import "Page.h"
 #import "PaintInfo.h"
+#import "RenderAttachment.h"
 #import "RenderLayer.h"
 #import "RenderMedia.h"
 #import "RenderMediaControlElements.h"
@@ -2066,6 +2070,170 @@ IntSize RenderThemeMac::imageControlsButtonPositionOffset() const
 }
 #endif
 
+#if ENABLE(ATTACHMENT_ELEMENT)
+const CGFloat attachmentIconSize = 48;
+const CGFloat attachmentIconBackgroundPadding = 6;
+const CGFloat attachmentIconBackgroundSize = attachmentIconSize + attachmentIconBackgroundPadding;
+const CGFloat attachmentIconSelectionBorderThickness = 1;
+const CGFloat attachmentIconBackgroundRadius = 3;
+const CGFloat attachmentIconToLabelMargin = 2;
+
+static Color attachmentIconBackgroundColor() { return Color(0, 0, 0, 30); }
+static Color attachmentIconBorderColor() { return Color(255, 255, 255, 125); }
+
+const CGFloat attachmentLabelFontSize = 12;
+const CGFloat attachmentLabelBackgroundRadius = 3;
+const CGFloat attachmentLabelBackgroundPadding = 3;
+
+const CGFloat attachmentMargin = 3;
+
+struct AttachmentLayout {
+    AttachmentLayout(const RenderAttachment&);
+
+    FloatRect textRect;
+    FloatRect textBackgroundRect;
+    FloatRect iconRect;
+    FloatRect iconBackgroundRect;
+    FloatRect attachmentRect;
+
+    RetainPtr<CTFontRef> labelFont;
+    FontCascade labelFontCascade;
+    std::unique_ptr<TextRun> labelTextRun;
+};
+
+AttachmentLayout::AttachmentLayout(const RenderAttachment& attachment)
+{
+    // FIXME: We should have a limit on the width of the label.
+    // FIXME: We should support line-breaking (up to two lines) and always middle-truncate the second line.
+    File* file = attachment.attachmentElement().file();
+
+    labelFont = adoptCF(CTFontCreateUIFontForLanguage(kCTFontSystemFontType, attachmentLabelFontSize, nullptr));
+    labelFontCascade = FontCascade(FontPlatformData(labelFont.get(), attachmentLabelFontSize));
+
+    String filename = file ? file->name() : String();
+    labelTextRun = std::make_unique<TextRun>(filename);
+    labelTextRun->setDirection(filename.defaultWritingDirection() == U_LEFT_TO_RIGHT ? LTR : RTL);
+    float textWidth = labelFontCascade.width(*labelTextRun);
+    float textHeight = labelFontCascade.fontMetrics().height();
+    float xOffset = (attachmentIconBackgroundSize / 2) - (textWidth / 2);
+
+    textRect = FloatRect(xOffset, attachmentIconBackgroundSize + attachmentIconToLabelMargin, textWidth, textHeight);
+    textBackgroundRect = textRect;
+    textBackgroundRect.inflateX(attachmentLabelBackgroundPadding);
+    textBackgroundRect = encloseRectToDevicePixels(textBackgroundRect, attachment.document().deviceScaleFactor());
+
+    iconBackgroundRect = FloatRect(0, 0, attachmentIconBackgroundSize, attachmentIconBackgroundSize);
+
+    iconRect = iconBackgroundRect;
+    iconRect.setSize(FloatSize(attachmentIconSize, attachmentIconSize));
+    iconRect.move(attachmentIconBackgroundPadding / 2, attachmentIconBackgroundPadding / 2);
+
+    attachmentRect = iconBackgroundRect;
+    attachmentRect.unite(textBackgroundRect);
+    attachmentRect.inflate(attachmentMargin);
+    attachmentRect = encloseRectToDevicePixels(attachmentRect, attachment.document().deviceScaleFactor());
+}
+
+LayoutSize RenderThemeMac::attachmentIntrinsicSize(const RenderAttachment& attachment) const
+{
+    AttachmentLayout layout(attachment);
+    return LayoutSize(layout.attachmentRect.size());
+}
+
+static void paintAttachmentIconBackground(const RenderAttachment&, GraphicsContext& context, AttachmentLayout& layout)
+{
+    // FIXME: Finder has a discontinuous behavior here when you have a background color other than white,
+    // where it switches into 'bordered mode' and the border pops in on top of the background.
+    bool paintBorder = true;
+
+    FloatRect backgroundRect = layout.iconBackgroundRect;
+    if (paintBorder)
+        backgroundRect.inflate(-attachmentIconSelectionBorderThickness);
+
+    FloatSize iconBackgroundRadiusSize(attachmentIconBackgroundRadius, attachmentIconBackgroundRadius);
+
+    Path backgroundPath;
+    backgroundPath.addRoundedRect(backgroundRect, iconBackgroundRadiusSize);
+    context.setFillColor(attachmentIconBackgroundColor(), ColorSpaceDeviceRGB);
+    context.fillPath(backgroundPath);
+
+    if (paintBorder) {
+        FloatRect borderRect = layout.iconBackgroundRect;
+        borderRect.inflate(-attachmentIconSelectionBorderThickness / 2);
+
+        Path borderPath;
+        borderPath.addRoundedRect(borderRect, iconBackgroundRadiusSize);
+        context.setStrokeColor(attachmentIconBorderColor(), ColorSpaceDeviceRGB);
+        context.setStrokeThickness(attachmentIconSelectionBorderThickness);
+        context.strokePath(borderPath);
+    }
+}
+
+static void paintAttachmentIcon(const RenderAttachment& attachment, GraphicsContext& context, AttachmentLayout& layout)
+{
+    File* file = attachment.attachmentElement().file();
+    RetainPtr<LSBindingRef> lsBinding = adoptCF(_LSBindingCreateWithURL(kCFAllocatorDefault, (CFURLRef)[NSURL fileURLWithPath:file ? file->path() : String()]));
+    if (!lsBinding)
+        return;
+
+    // FIXME: This should take transforms and page scale into account, not just deviceScaleFactor.
+    FloatSize iconSizeInPoints(attachmentIconSize, attachmentIconSize);
+    iconSizeInPoints.scale(attachment.document().deviceScaleFactor());
+
+    RetainPtr<CGImageRef> icon = adoptCF(_ISCreateCGImageFromBindingWithSizeScaleAndOptions(lsBinding.get(), iconSizeInPoints, 1, nil));
+    if (!icon)
+        return;
+
+    context.drawNativeImage(icon.get(), iconSizeInPoints, ColorSpaceDeviceRGB, layout.iconRect, FloatRect(FloatPoint(), iconSizeInPoints));
+}
+
+void RenderThemeMac::paintAttachmentLabelBackground(const RenderAttachment&, GraphicsContext& context, AttachmentLayout& layout) const
+{
+    Path backgroundPath;
+    backgroundPath.addRoundedRect(layout.textBackgroundRect, FloatSize(attachmentLabelBackgroundRadius, attachmentLabelBackgroundRadius));
+    context.setFillColor(convertNSColorToColor([NSColor alternateSelectedControlColor]), ColorSpaceDeviceRGB);
+    context.fillPath(backgroundPath);
+}
+
+void RenderThemeMac::paintAttachmentLabel(const RenderAttachment&, GraphicsContext& context, AttachmentLayout& layout, bool useSelectedStyle) const
+{
+    FloatPoint textLocation = layout.textRect.minXMaxYCorner();
+    textLocation.move(0, -layout.labelFontCascade.fontMetrics().descent());
+
+    context.setFillColor(useSelectedStyle ? convertNSColorToColor([NSColor alternateSelectedControlTextColor]) : Color::black, ColorSpaceDeviceRGB);
+    context.drawBidiText(layout.labelFontCascade, *layout.labelTextRun, textLocation);
+}
+
+bool RenderThemeMac::paintAttachment(const RenderObject& renderer, const PaintInfo& paintInfo, const IntRect& paintRect)
+{
+    if (!is<RenderAttachment>(renderer))
+        return false;
+
+    const RenderAttachment& attachment = downcast<RenderAttachment>(renderer);
+
+    AttachmentLayout layout(attachment);
+
+    GraphicsContext& context = *paintInfo.context;
+
+    GraphicsContextStateSaver saver(context);
+
+    context.translate(toFloatSize(paintRect.location()));
+    context.translate(FloatSize((layout.attachmentRect.width() - attachmentIconBackgroundSize) / 2, 0));
+
+    bool useSelectedStyle = attachment.isSelectedOrFocused();
+
+    if (useSelectedStyle)
+        paintAttachmentIconBackground(attachment, context, layout);
+    paintAttachmentIcon(attachment, context, layout);
+    if (useSelectedStyle)
+        paintAttachmentLabelBackground(attachment, context, layout);
+    paintAttachmentLabel(attachment, context, layout, useSelectedStyle);
+
+    return true;
+}
+
+#endif // ENABLE(ATTACHMENT_ELEMENT)
+
 } // namespace WebCore
 
 #endif // !PLATFORM(IOS)