Bug 190119 - [Cocoa] Add SPI to expose typing attributes at the current selection on WKWebView
Summary: [Cocoa] Add SPI to expose typing attributes at the current selection on WKWeb...
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: HTML Editing (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Wenson Hsieh
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2018-09-30 14:58 PDT by Wenson Hsieh
Modified: 2022-08-02 13:39 PDT (History)
9 users (show)

See Also:


Attachments
Patch (39.70 KB, patch)
2018-10-03 08:39 PDT, Wenson Hsieh
achristensen: review-
Details | Formatted Diff | Diff
Patch (45.69 KB, patch)
2018-10-03 14:39 PDT, Wenson Hsieh
thorton: review+
Details | Formatted Diff | Diff
Patch for EWS (45.52 KB, patch)
2018-10-04 16:59 PDT, Wenson Hsieh
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Wenson Hsieh 2018-09-30 14:58:38 PDT
<rdar://problem/44767062>
Comment 1 Wenson Hsieh 2018-10-03 08:39:45 PDT
Created attachment 351524 [details]
Patch
Comment 2 Alex Christensen 2018-10-03 11:07:40 PDT
Comment on attachment 351524 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=351524&action=review

> Source/WebKit/UIProcess/API/Cocoa/WKUIDelegatePrivate.h:124
> +- (void)_webView:(WKWebView *)webView fontAttributesDidChange:(NSDictionary<NSString *, id> *)fontAttributes WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA));

Why not use a KVO observable property on the WKWebView?  This sounds exactly like what KVO is for.

> Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm:1264
> +    if ([uiDelegate respondsToSelector:@selector(_webView:fontAttributesDidChange:)]) {

We cache the results of all the respondsToSelector booleans in UIDelegate.h for performance.  Reading 1 bit is much faster than respondsToSelector.
Comment 3 Wenson Hsieh 2018-10-03 14:34:53 PDT
(In reply to Alex Christensen from comment #2)
> Comment on attachment 351524 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=351524&action=review
> 
> > Source/WebKit/UIProcess/API/Cocoa/WKUIDelegatePrivate.h:124
> > +- (void)_webView:(WKWebView *)webView fontAttributesDidChange:(NSDictionary<NSString *, id> *)fontAttributes WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA));
> 
> Why not use a KVO observable property on the WKWebView?  This sounds exactly
> like what KVO is for.

Yes, this would be an elegant interface, but I'd need to know whether or not to compute and propagate font attributes when computing editor state, so I'd need to override -addObserver: and -removeObserver: to know whether we need font attributes in the UI process. Given this, I don't think adding a new delegate method is worse than this KVO-able property.

> 
> > Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm:1264
> > +    if ([uiDelegate respondsToSelector:@selector(_webView:fontAttributesDidChange:)]) {
> 
> We cache the results of all the respondsToSelector booleans in UIDelegate.h
> for performance.  Reading 1 bit is much faster than respondsToSelector.

Good call! Done.
Comment 4 Wenson Hsieh 2018-10-03 14:39:51 PDT
Created attachment 351547 [details]
Patch
Comment 5 Tim Horton 2018-10-04 15:07:22 PDT
Comment on attachment 351547 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=351547&action=review

> Source/WebKit/ChangeLog:9
> +        Add support new WebKit2 SPI to notify the UI delegate about font attributes when the editor state changes (e.g.

"add support new"?

> Source/WebKit/Shared/EditorState.cpp:141
> +    encoder << !!fontAttributes;

Don't we have the ability to directly encode optionals now?

> Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm:1255
> +    // FIXME: We should either rename -_webView:editorStateDidChange: to clarify that it's only intended for use when testing,
> +    // or remove it entirely and use -_webView:fontAttributesDidChange: instead once text alignment is supported in the set of

Are there any clients other than our testing code? If not, maybe just rename it right away?

> Source/WebKit/UIProcess/Cocoa/UIDelegate.mm:156
> +    m_delegateMethods.webViewFontAttributesDidChange = [delegate respondsToSelector:@selector(_webView:fontAttributesDidChange:)];

I think "webView:didChangeFontAttributes:" is more traditional style, but I see you were copying an existing method.

> Tools/TestWebKitAPI/Tests/WebKitCocoa/FontAttributes.mm:94
> +enum class Nullability : uint8_t { Null, NonNull };

Is this about nullability (can it be null) or nullity (is it null)
Comment 6 Wenson Hsieh 2018-10-04 16:39:44 PDT
Comment on attachment 351547 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=351547&action=review

>> Source/WebKit/ChangeLog:9
>> +        Add support new WebKit2 SPI to notify the UI delegate about font attributes when the editor state changes (e.g.
> 
> "add support new"?

I accidentally a word :P

>> Source/WebKit/Shared/EditorState.cpp:141
>> +    encoder << !!fontAttributes;
> 
> Don't we have the ability to directly encode optionals now?

Sounds good — I changed it to this on the encoding side:

    encoder << fontAttributes;

...and this on the decoding side:

    std::optional<std::optional<FontAttributes>> optionalFontAttributes;
    decoder >> optionalFontAttributes;
    if (!optionalFontAttributes)
        return false;

    result.fontAttributes = optionalFontAttributes.value();

>> Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm:1255
>> +    // or remove it entirely and use -_webView:fontAttributesDidChange: instead once text alignment is supported in the set of
> 
> Are there any clients other than our testing code? If not, maybe just rename it right away?

There aren't any clients apart from our testing code (at least, from what I can tell using internal tooling). I'm going to clean this up in a followup patch, when I decide what I want to do with these. One idea I have is refactoring them to actually test user-facing state (e.g. calling -canPerformAction:withSender:, -textStylingAtPosition:inDirection:, etc.), but I could also repurpose those tests to be about font attributes. Or I could simply rename the delegate hook.

>> Source/WebKit/UIProcess/Cocoa/UIDelegate.mm:156
>> +    m_delegateMethods.webViewFontAttributesDidChange = [delegate respondsToSelector:@selector(_webView:fontAttributesDidChange:)];
> 
> I think "webView:didChangeFontAttributes:" is more traditional style, but I see you were copying an existing method.

Sounds good — renamed to `_webView:didChangeFontAttributes:`

>> Tools/TestWebKitAPI/Tests/WebKitCocoa/FontAttributes.mm:94
>> +enum class Nullability : uint8_t { Null, NonNull };
> 
> Is this about nullability (can it be null) or nullity (is it null)

It is indeed about Nullity — renamed!
Comment 7 Wenson Hsieh 2018-10-04 16:59:41 PDT
Created attachment 351644 [details]
Patch for EWS
Comment 8 WebKit Commit Bot 2018-10-04 17:47:14 PDT
Comment on attachment 351644 [details]
Patch for EWS

Clearing flags on attachment: 351644

Committed r236865: <https://trac.webkit.org/changeset/236865>