@@ -15,6 +15,7 @@ const lazy = {};
15
15
ChromeUtils . defineESModuleGetters ( lazy , {
16
16
AMBrowserExtensionsImport : "resource://gre/modules/AddonManager.sys.mjs" ,
17
17
AbuseReporter : "resource://gre/modules/AbuseReporter.sys.mjs" ,
18
+ ExtensionCommon : "resource://gre/modules/ExtensionCommon.sys.mjs" ,
18
19
ExtensionParent : "resource://gre/modules/ExtensionParent.sys.mjs" ,
19
20
ExtensionPermissions : "resource://gre/modules/ExtensionPermissions.sys.mjs" ,
20
21
OriginControls : "resource://gre/modules/ExtensionPermissions.sys.mjs" ,
@@ -737,19 +738,104 @@ customElements.define(
737
738
}
738
739
) ;
739
740
741
+ class BrowserActionWidgetObserver {
742
+ #connected = false ;
743
+
744
+
745
+
746
+
747
+
748
+
749
+ constructor ( addonId , onButtonAreaChanged ) {
750
+ this . addonId = addonId ;
751
+
752
+
753
+ this . widgetId = `${ lazy . ExtensionCommon . makeWidgetId ( addonId ) } -browser-action` ;
754
+ this . onButtonAreaChanged = onButtonAreaChanged ;
755
+ }
756
+
757
+ startObserving ( ) {
758
+ if ( this . #connected) {
759
+ return ;
760
+ }
761
+ this . #connected = true ;
762
+ CustomizableUI . addListener ( this ) ;
763
+ window . addEventListener ( "unload" , this ) ;
764
+ }
765
+
766
+ stopObserving ( ) {
767
+ if ( ! this . #connected) {
768
+ return ;
769
+ }
770
+ this . #connected = false ;
771
+ CustomizableUI . removeListener ( this ) ;
772
+ window . removeEventListener ( "unload" , this ) ;
773
+ }
774
+
775
+ hasBrowserActionUI ( ) {
776
+ const policy = WebExtensionPolicy . getByID ( this . addonId ) ;
777
+ if ( ! policy ?. canAccessWindow ( window ) ) {
778
+
779
+
780
+ return false ;
781
+ }
782
+ if ( ! gUnifiedExtensions . browserActionFor ( policy ) ) {
783
+
784
+ return false ;
785
+ }
786
+ return true ;
787
+ }
788
+
789
+ onWidgetCreated ( aWidgetId ) {
790
+
791
+
792
+
793
+ if ( aWidgetId === this . widgetId ) {
794
+ this . onButtonAreaChanged ( ) ;
795
+ }
796
+ }
797
+
798
+ onWidgetAdded ( aWidgetId ) {
799
+ if ( aWidgetId === this . widgetId ) {
800
+ this . onButtonAreaChanged ( ) ;
801
+ }
802
+ }
803
+
804
+ onWidgetMoved ( aWidgetId ) {
805
+ if ( aWidgetId === this . widgetId ) {
806
+ this . onButtonAreaChanged ( ) ;
807
+ }
808
+ }
809
+
810
+ handleEvent ( event ) {
811
+ if ( event . type === "unload" ) {
812
+ this . stopObserving ( ) ;
813
+ }
814
+ }
815
+ }
816
+
740
817
customElements . define (
741
818
"addon-installed-notification" ,
742
819
class MozAddonInstalledNotification extends customElements . get (
743
820
"popupnotification"
744
821
) {
822
+ #shouldIgnoreCheckboxStateChangeEvent = false ;
823
+ #browserActionWidgetObserver;
745
824
connectedCallback ( ) {
746
825
this . descriptionEl = this . querySelector ( "#addon-install-description" ) ;
826
+ this . pinExtensionEl = this . querySelector (
827
+ "#addon-pin-toolbarbutton-checkbox"
828
+ ) ;
747
829
748
830
this . addEventListener ( "click" , this ) ;
831
+ this . pinExtensionEl . addEventListener ( "CheckboxStateChange" , this ) ;
832
+ this . #browserActionWidgetObserver?. startObserving ( ) ;
749
833
}
750
834
751
835
disconnectedCallback ( ) {
752
836
this . removeEventListener ( "click" , this ) ;
837
+ this . pinExtensionEl . removeEventListener ( "CheckboxStateChange" , this ) ;
838
+ this . #browserActionWidgetObserver?. stopObserving ( ) ;
753
839
}
754
840
755
841
get #settingsLinkId( ) {
@@ -763,13 +849,19 @@ customElements.define(
763
849
case "click" : {
764
850
if ( target . id === this . #settingsLinkId) {
765
851
const { addonId } = this . notification . options . customElementOptions ;
766
-
767
852
BrowserAddonUI . openAddonsMgr (
768
853
"addons://detail/" + encodeURIComponent ( addonId )
769
854
) ;
770
855
}
771
856
break ;
772
857
}
858
+ case "CheckboxStateChange" :
859
+
860
+
861
+ if ( ! this . #shouldIgnoreCheckboxStateChangeEvent) {
862
+ this . #handlePinnedCheckboxStateChange( ) ;
863
+ }
864
+ break ;
773
865
}
774
866
}
775
867
@@ -786,7 +878,16 @@ customElements.define(
786
878
) ;
787
879
}
788
880
881
+ this . #browserActionWidgetObserver?. stopObserving ( ) ;
882
+ this . #browserActionWidgetObserver = new BrowserActionWidgetObserver (
883
+ this . notification . options . customElementOptions . addonId ,
884
+ ( ) => this . #renderPinToolbarButtonCheckbox( )
885
+ ) ;
886
+
789
887
this . render ( ) ;
888
+ if ( this . isConnected ) {
889
+ this . #browserActionWidgetObserver. startObserving ( ) ;
890
+ }
790
891
}
791
892
792
893
render ( ) {
@@ -808,6 +909,7 @@ customElements.define(
808
909
}
809
910
810
911
this . ownerDocument . l10n . setAttributes ( this . descriptionEl , fluentId ) ;
912
+ this . #renderPinToolbarButtonCheckbox( ) ;
811
913
}
812
914
813
915
get #dataCollectionPermissionsEnabled( ) {
@@ -816,6 +918,50 @@ customElements.define(
816
918
false
817
919
) ;
818
920
}
921
+
922
+ #renderPinToolbarButtonCheckbox( ) {
923
+
924
+
925
+
926
+ this . pinExtensionEl . hidden = true ;
927
+
928
+ if ( ! this . #browserActionWidgetObserver. hasBrowserActionUI ( ) ) {
929
+ return ;
930
+ }
931
+ const widgetId = this . #browserActionWidgetObserver. widgetId ;
932
+
933
+
934
+
935
+
936
+
937
+
938
+
939
+ const area = CustomizableUI . getPlacementOfWidget ( widgetId ) ?. area ;
940
+ let shouldPinToToolbar = area !== CustomizableUI . AREA_ADDONS ;
941
+ if ( shouldPinToToolbar && area !== CustomizableUI . AREA_NAVBAR ) {
942
+
943
+ return ;
944
+ }
945
+ this . #shouldIgnoreCheckboxStateChangeEvent = true ;
946
+ this . pinExtensionEl . checked = shouldPinToToolbar ;
947
+ this . #shouldIgnoreCheckboxStateChangeEvent = false ;
948
+ this . pinExtensionEl . hidden = false ;
949
+ }
950
+
951
+ #handlePinnedCheckboxStateChange( ) {
952
+ if ( ! this . #browserActionWidgetObserver. hasBrowserActionUI ( ) ) {
953
+
954
+
955
+ const { addonId } = this . notification . options . customElementOptions ;
956
+ throw new Error ( `No browser action widget found for ${ addonId } !` ) ;
957
+ }
958
+ const widgetId = this . #browserActionWidgetObserver. widgetId ;
959
+ const shouldPinToToolbar = this . pinExtensionEl . checked ;
960
+ if ( shouldPinToToolbar ) {
961
+ gUnifiedExtensions . _maybeMoveWidgetNodeBack ( widgetId ) ;
962
+ }
963
+ gUnifiedExtensions . pinToToolbar ( widgetId , shouldPinToToolbar ) ;
964
+ }
819
965
} ,
820
966
{ extends : "popupnotification" }
821
967
) ;
0 commit comments