[macOS] Implement a way for the UI process to request typing attributes at the curren...
authorwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 2 Oct 2018 10:39:40 +0000 (10:39 +0000)
committerwenson_hsieh@apple.com <wenson_hsieh@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 2 Oct 2018 10:39:40 +0000 (10:39 +0000)
commitbea681c6f5415bada1cf2142c871383ba9c0e706
tree2179b1a5b7da5e19b04751bf3c364dfa1cb58728
parent4114dd5ccd76db639e61033ca7bf87036a740dc2
[macOS] Implement a way for the UI process to request typing attributes at the current selection
https://bugs.webkit.org/show_bug.cgi?id=189983
<rdar://problem/44648705>

Reviewed by Ryosuke Niwa.

Source/WebKit:

Implements `-[WKWebView typingAttributesWithCompletionHandler:]`, which asynchronously retrieves an NSDictionary
containing the typing attributes at the start of the current selection. This is a new asynchronous text input
client hook on macOS that is needed in order to support NSInspectorBar on WKWebView.

At a high level, this patch builds on top of refactoring done in r236445 to make it possible to send typing
attributes from the web process to the UI process, and then adds plumbing in the WebKit2 client layer to surface
typing attributes to WKWebView on macOS.

Tests:  FontManagerTests.TypingAttributesAfterSubscriptAndSuperscript
        FontManagerTests.ChangeTypingAttributesWithInspectorBar
        FontManagerTests.ChangeAttributesWithFontEffectsBox

* Platform/spi/mac/AppKitSPI.h:
* Shared/WebCoreArgumentCoders.cpp:
(IPC::ArgumentCoder<FontAttributes>::encode):
(IPC::ArgumentCoder<FontAttributes>::decode):

Implement IPC coding support for FontAttributes.

* Shared/WebCoreArgumentCoders.h:
* Shared/mac/ArgumentCodersMac.h:
* Shared/mac/ArgumentCodersMac.mm:
(IPC::encode):
(IPC::decode):

Add the ability to encode and decode UIFont. This allows Cocoa platforms to send `FontAttributes.font` over IPC
with a single call to IPC::encode/IPC::decode.

* UIProcess/API/Cocoa/WKWebView.mm:
(-[WKWebView typingAttributesWithCompletionHandler:]):

Add plumbing to WebViewImpl.

* UIProcess/Cocoa/WebViewImpl.h:
* UIProcess/Cocoa/WebViewImpl.mm:
(WebKit::WebViewImpl::selectionDidChange):

Update the NSInspectorBar on selection change, but only if it is present and visible, and WKWebView is currently
first responder.

(WebKit::WebViewImpl::typingAttributesWithCompletionHandler):

Call into WebPageProxy to retrieve FontAttributes from the web process, and invoke the callback with an
NSDictionary constructed from the retrieved FontAttributes.

* UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::requestFontAttributesAtSelectionStart):
(WebKit::WebPageProxy::fontAttributesCallback):

Add plumbing to retrieve FontAttributes from the web process at the current selection. Additionally, cache the
font attributes to avoid doing extra work during subsequent requests.

(WebKit::WebPageProxy::resetStateAfterProcessExited):
* UIProcess/WebPageProxy.h:
(WebKit::WebPageProxy::cachedFontAttributesAtSelectionStart const):
* UIProcess/WebPageProxy.messages.in:
* UIProcess/ios/WebPageProxyIOS.mm:
(WebKit::WebPageProxy::editorStateChanged):
* UIProcess/mac/WebPageProxyMac.mm:
(WebKit::WebPageProxy::editorStateChanged):

Invalidate cached font attributes.

* WebKit.xcodeproj/project.pbxproj:
* WebProcess/WebPage/WebPage.cpp:
(WebKit::WebPage::requestFontAttributesAtSelectionStart):

Use the currently focused frame's Editor to compute FontAttributes at the current selection, and send the
computed FontAttributes back to the UI process.

* WebProcess/WebPage/WebPage.h:
* WebProcess/WebPage/WebPage.messages.in:

Tools:

Adds new FontManagerTests that exercise -typingAttributesWithCompletionHandler:. See below for more detail.

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/TestWebKitAPI/mac/AppKitSPI.h:
* TestWebKitAPI/Tests/mac/FontManagerTests.mm:

Add a new test that exercises font attribute modification via inspector bar. Also, add a new test that checks
typing attributes when using subscript/superscript/unscript. Lastly, augment an existing NSFontPanel test to
additionally check that shadowed text and strike-through are reflected in typing attributes.

(-[FontManagerTestWKWebView inspectorBarItemIdentifiers]):
(-[TestWKWebView typingAttributes]):

Add a synchronous wrapper around `-typingAttributesWithCompletionHandler:` by spinning the runloop.

(-[TestWKWebView collapseToStart]):
(webViewForFontManagerTesting):
(TestWebKitAPI::TEST):
* TestWebKitAPI/cocoa/TestWKWebView.h:
* TestWebKitAPI/mac/TestInspectorBar.h: Copied from Tools/TestWebKitAPI/Tests/TestWebKitAPI/mac/AppKitSPI.h.
* TestWebKitAPI/mac/TestInspectorBar.mm: Added.

Introduce subclasses of `__InspectorBarItemController` and `NSInspectorBar` for testing inspector bar interaction.

(-[TestInspectorBarItemController initWithInspectorBar:]):
(-[TestInspectorBarItemController inspectorBar]):
(-[TestInspectorBarItemController updateSelectedAttributes]):
(-[TestInspectorBar initWithWebView:]):
(+[TestInspectorBar standardItemControllerClass]):
(+[TestInspectorBar standardTextItemIdentifiers]):
(-[TestInspectorBar _setStyleControlSelected:atIndex:]):
(-[TestInspectorBar chooseFontSize:]):
(-[TestInspectorBar chooseFontFamily:]):
(-[TestInspectorBar _chooseColor:inColorWell:]):
(-[TestInspectorBar chooseForegroundColor:]):
(-[TestInspectorBar chooseBackgroundColor:]):
(-[TestInspectorBar formatBold:]):
(-[TestInspectorBar formatItalic:]):
(-[TestInspectorBar formatUnderline:]):

Add helper methods to TestInspectorBar to simulate interacting with various controls (e.g. color wells and font
styling controls).

(-[TestInspectorBar itemController]):
(-[TestInspectorBar setItemController:]):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@236724 268f45cc-cd09-0410-ab3c-d52691b4dbfc
25 files changed:
Source/WebKit/ChangeLog
Source/WebKit/Platform/spi/mac/AppKitSPI.h [new file with mode: 0644]
Source/WebKit/Shared/WebCoreArgumentCoders.cpp
Source/WebKit/Shared/WebCoreArgumentCoders.h
Source/WebKit/Shared/mac/ArgumentCodersMac.h
Source/WebKit/Shared/mac/ArgumentCodersMac.mm
Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm
Source/WebKit/UIProcess/Cocoa/WebViewImpl.h
Source/WebKit/UIProcess/Cocoa/WebViewImpl.mm
Source/WebKit/UIProcess/WebPageProxy.cpp
Source/WebKit/UIProcess/WebPageProxy.h
Source/WebKit/UIProcess/WebPageProxy.messages.in
Source/WebKit/UIProcess/ios/WebPageProxyIOS.mm
Source/WebKit/UIProcess/mac/WebPageProxyMac.mm
Source/WebKit/WebKit.xcodeproj/project.pbxproj
Source/WebKit/WebProcess/WebPage/WebPage.cpp
Source/WebKit/WebProcess/WebPage/WebPage.h
Source/WebKit/WebProcess/WebPage/WebPage.messages.in
Tools/ChangeLog
Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Tools/TestWebKitAPI/Tests/TestWebKitAPI/mac/AppKitSPI.h
Tools/TestWebKitAPI/Tests/mac/FontManagerTests.mm
Tools/TestWebKitAPI/cocoa/TestWKWebView.h
Tools/TestWebKitAPI/mac/TestInspectorBar.h [new file with mode: 0644]
Tools/TestWebKitAPI/mac/TestInspectorBar.mm [new file with mode: 0644]