Changeset 273912 in webkit


Ignore:
Timestamp:
Mar 4, 2021 12:12:09 PM (17 months ago)
Author:
commit-queue@webkit.org
Message:

Web Inspector: Persist CSS Grid overlay colors
https://bugs.webkit.org/show_bug.cgi?id=222319
<rdar://problem/74647242>

Patch by Razvan Caliman <Razvan Caliman> on 2021-03-04
Reviewed by Devin Rousso.

Save and restore CSS Grid overlay colors edited by a user.

  • UserInterface/Controllers/OverlayManager.js:

(WI.OverlayManager):
(WI.OverlayManager.prototype.showGridOverlay):
(WI.OverlayManager.prototype.getGridColorForNode):
(WI.OverlayManager.prototype.setGridColorForNode):
(WI.OverlayManager.prototype._handleMainResourceDidChange):

  • UserInterface/Views/CSSGridSection.js:

(WI.CSSGridSection.prototype.layout):

Location:
trunk/Source/WebInspectorUI
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebInspectorUI/ChangeLog

    r273900 r273912  
     12021-03-04  Razvan Caliman  <rcaliman@apple.com>
     2
     3        Web Inspector: Persist CSS Grid overlay colors
     4        https://bugs.webkit.org/show_bug.cgi?id=222319
     5        <rdar://problem/74647242>
     6
     7        Reviewed by Devin Rousso.
     8
     9        Save and restore CSS Grid overlay colors edited by a user.
     10
     11        * UserInterface/Controllers/OverlayManager.js:
     12        (WI.OverlayManager):
     13        (WI.OverlayManager.prototype.showGridOverlay):
     14        (WI.OverlayManager.prototype.getGridColorForNode):
     15        (WI.OverlayManager.prototype.setGridColorForNode):
     16        (WI.OverlayManager.prototype._handleMainResourceDidChange):
     17        * UserInterface/Views/CSSGridSection.js:
     18        (WI.CSSGridSection.prototype.layout):
     19
    1202021-03-04  Razvan Caliman  <rcaliman@apple.com>
    221
  • trunk/Source/WebInspectorUI/UserInterface/Controllers/OverlayManager.js

    r273164 r273912  
    3131
    3232        this._gridOverlayForNodeMap = new Map;
    33 
    34         // Can't reuse `this._gridOverlayForNodeMap` because nodes are removed from it when overlay isn't visible.
    35         this._colorForNodeMap = new WeakMap;
    36         this._nextDOMNodeColorIndex = 0;
     33        this._nextDefaultGridColorIndex = 0;
     34        this._gridColorForNodeMap = new WeakMap;
     35        this._gridColorSettingForNodeMap = new WeakMap;
    3736
    3837        WI.settings.gridOverlayShowExtendedGridLines.addEventListener(WI.Setting.Event.Changed, this._handleGridSettingChanged, this);
     
    6160        console.assert(domNode.layoutContextType === WI.DOMNode.LayoutContextType.Grid, domNode.layoutContextType);
    6261
    63         color ||= this.colorForNode(domNode);
     62        color ||= this.getGridColorForNode(domNode);
    6463        let target = WI.assumingMainTarget();
    6564        let commandArguments = {
     
    7574
    7675        let overlay = {domNode, ...commandArguments};
     76
     77        // The method to show the overlay will be called repeatedly while updating the grid overlay color. Avoid adding duplicate event listeners
     78        if (!this._gridOverlayForNodeMap.has(domNode))
     79            domNode.addEventListener(WI.DOMNode.Event.LayoutContextTypeChanged, this._handleLayoutContextTypeChanged, this);
     80
    7781        this._gridOverlayForNodeMap.set(domNode, overlay);
    78         this._colorForNodeMap.set(domNode, color);
    79 
    80         domNode.addEventListener(WI.DOMNode.Event.LayoutContextTypeChanged, this._handleLayoutContextTypeChanged, this);
     82
    8183        this.dispatchEventToListeners(WI.OverlayManager.Event.GridOverlayShown, overlay);
    8284    }
     
    114116    }
    115117
    116     colorForNode(domNode)
    117     {
    118         let color = this._colorForNodeMap.get(domNode);
     118    getGridColorForNode(domNode)
     119    {
     120        let color = this._gridColorForNodeMap.get(domNode);
    119121        if (color)
    120122            return color;
    121123
    122         const hslColors = [
     124        const defaultGridHSLColors = [
    123125            [329, 91, 70],
    124126            [207, 96, 69],
     
    128130        ];
    129131
    130         color = new WI.Color(WI.Color.Format.HSL, hslColors[this._nextDOMNodeColorIndex]);
    131         this._colorForNodeMap.set(domNode, color);
    132         this._nextDOMNodeColorIndex = (this._nextDOMNodeColorIndex + 1) % hslColors.length;
     132        let colorSetting = this._gridColorSettingForNodeMap.get(domNode);
     133        if (!colorSetting) {
     134            let defaultColor = defaultGridHSLColors[this._nextDefaultGridColorIndex];
     135            this._nextDefaultGridColorIndex = (this._nextDefaultGridColorIndex + 1) % defaultGridHSLColors.length;
     136
     137            let url = domNode.ownerDocument.documentURL || WI.networkManager.mainFrame.url;
     138            colorSetting = new WI.Setting(`grid-overlay-color-${url.hash}-${domNode.path().hash}`, defaultColor);
     139            this._gridColorSettingForNodeMap.set(domNode, colorSetting);
     140        }
     141
     142        color = new WI.Color(WI.Color.Format.HSL, colorSetting.value);
     143        this._gridColorForNodeMap.set(domNode, color);
    133144
    134145        return color;
     146    }
     147
     148    setGridColorForNode(domNode, color)
     149    {
     150        console.assert(domNode instanceof WI.DOMNode, domNode);
     151        console.assert(color instanceof WI.Color, color);
     152
     153        let colorSetting = this._gridColorSettingForNodeMap.get(domNode);
     154        console.assert(colorSetting, "There should already be a setting created form a previous call to getGridColorForNode()");
     155        colorSetting.value = color.hsl;
     156
     157        this._gridColorForNodeMap.set(domNode, color);
    135158    }
    136159
     
    164187        // 3. Click on the badge of the same element.
    165188        //
    166         // We should see the same 1st default overlay color. If we don't reset _nextDOMNodeColorIndex,
     189        // We should see the same 1st default overlay color. If we don't reset _nextDefaultGridColorIndex,
    167190        // the 2nd default color would be used instead.
    168191        //
    169192        // `domNode.id` is different for the same DOM element after page reload.
    170         this._nextDOMNodeColorIndex = 0;
     193        this._nextDefaultGridColorIndex = 0;
    171194    }
    172195};
  • trunk/Source/WebInspectorUI/UserInterface/Views/CSSGridSection.js

    r273847 r273912  
    113113            });
    114114
    115             let gridColor = WI.overlayManager.colorForNode(domNode);
     115            let gridColor = WI.overlayManager.getGridColorForNode(domNode);
    116116            let swatch = new WI.InlineSwatch(WI.InlineSwatch.Type.Color, gridColor);
    117117            itemContainerElement.append(swatch.element);
     
    120120                if (checkboxElement.checked)
    121121                    WI.overlayManager.showGridOverlay(domNode, {color: event.data.value});
     122            }, swatch);
     123
     124            swatch.addEventListener(WI.InlineSwatch.Event.Deactivated, (event) => {
     125                if (event.target.value === gridColor)
     126                    return;
     127
     128                gridColor = event.target.value;
     129                WI.overlayManager.setGridColorForNode(domNode, gridColor);
    122130            }, swatch);
    123131        }
Note: See TracChangeset for help on using the changeset viewer.