@@ -32,6 +32,7 @@ import { IProgressService, ProgressLocation } from 'vs/platform/progress/common/
32
32
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService' ;
33
33
import { IConfigurationRegistry , Extensions as ConfigurationExtensions } from 'vs/platform/configuration/common/configurationRegistry' ;
34
34
import { workbenchConfigurationNodeBase } from 'vs/workbench/common/configuration' ;
35
+ import { DisposableStore } from 'vs/base/common/lifecycle' ;
35
36
36
37
const targetMenus = [
37
38
MenuId . EditorContextShare ,
@@ -46,82 +47,95 @@ const targetMenus = [
46
47
class ShareWorkbenchContribution {
47
48
private static SHARE_ENABLED_SETTING = 'workbench.experimental.share.enabled' ;
48
49
50
+ private _disposables = new DisposableStore ( ) ;
51
+
49
52
constructor (
50
53
@IShareService private readonly shareService : IShareService ,
51
54
@IConfigurationService private readonly configurationService : IConfigurationService
52
55
) {
53
56
if ( this . configurationService . getValue < boolean > ( ShareWorkbenchContribution . SHARE_ENABLED_SETTING ) ) {
54
57
this . registerActions ( ) ;
55
58
}
59
+ this . configurationService . onDidChangeConfiguration ( e => {
60
+ if ( e . affectsConfiguration ( ShareWorkbenchContribution . SHARE_ENABLED_SETTING ) ) {
61
+ if ( this . configurationService . getValue < boolean > ( ShareWorkbenchContribution . SHARE_ENABLED_SETTING ) ) {
62
+ this . registerActions ( ) ;
63
+ } else {
64
+ this . _disposables . clear ( ) ;
65
+ }
66
+ }
67
+ } ) ;
56
68
}
57
69
58
70
private registerActions ( ) {
59
- registerAction2 ( class ShareAction extends Action2 {
60
- static readonly ID = 'workbench.action.share' ;
61
- static readonly LABEL = localize ( 'share' , 'Share...' ) ;
71
+ this . _disposables . add (
72
+ registerAction2 ( class ShareAction extends Action2 {
73
+ static readonly ID = 'workbench.action.share' ;
74
+ static readonly LABEL = localize ( 'share' , 'Share...' ) ;
62
75
63
- constructor ( ) {
64
- super ( {
65
- id : ShareAction . ID ,
66
- title : { value : ShareAction . LABEL , original : 'Share...' } ,
67
- f1 : true ,
68
- icon : Codicon . linkExternal ,
69
- precondition : ContextKeyExpr . and ( ShareProviderCountContext . notEqualsTo ( 0 ) , WorkspaceFolderCountContext . notEqualsTo ( 0 ) ) ,
70
- keybinding : {
71
- weight : KeybindingWeight . WorkbenchContrib ,
72
- primary : KeyMod . Alt | KeyMod . CtrlCmd | KeyCode . KeyS ,
73
- } ,
74
- menu : [
75
- { id : MenuId . CommandCenter , order : 1000 }
76
- ]
77
- } ) ;
78
- }
76
+ constructor ( ) {
77
+ super ( {
78
+ id : ShareAction . ID ,
79
+ title : { value : ShareAction . LABEL , original : 'Share...' } ,
80
+ f1 : true ,
81
+ icon : Codicon . linkExternal ,
82
+ precondition : ContextKeyExpr . and ( ShareProviderCountContext . notEqualsTo ( 0 ) , WorkspaceFolderCountContext . notEqualsTo ( 0 ) ) ,
83
+ keybinding : {
84
+ weight : KeybindingWeight . WorkbenchContrib ,
85
+ primary : KeyMod . Alt | KeyMod . CtrlCmd | KeyCode . KeyS ,
86
+ } ,
87
+ menu : [
88
+ { id : MenuId . CommandCenter , order : 1000 }
89
+ ]
90
+ } ) ;
91
+ }
79
92
80
- override async run ( accessor : ServicesAccessor , ...args : any [ ] ) : Promise < void > {
81
- const shareService = accessor . get ( IShareService ) ;
82
- const activeEditor = accessor . get ( IEditorService ) ?. activeEditor ;
83
- const resourceUri = ( activeEditor && EditorResourceAccessor . getOriginalUri ( activeEditor , { supportSideBySide : SideBySideEditor . PRIMARY } ) )
84
- ?? accessor . get ( IWorkspaceContextService ) . getWorkspace ( ) . folders [ 0 ] . uri ;
85
- const clipboardService = accessor . get ( IClipboardService ) ;
86
- const dialogService = accessor . get ( IDialogService ) ;
87
- const urlService = accessor . get ( IOpenerService ) ;
88
- const progressService = accessor . get ( IProgressService ) ;
89
- const selection = accessor . get ( ICodeEditorService ) . getActiveCodeEditor ( ) ?. getSelection ( ) ?? undefined ;
93
+ override async run ( accessor : ServicesAccessor , ...args : any [ ] ) : Promise < void > {
94
+ const shareService = accessor . get ( IShareService ) ;
95
+ const activeEditor = accessor . get ( IEditorService ) ?. activeEditor ;
96
+ const resourceUri = ( activeEditor && EditorResourceAccessor . getOriginalUri ( activeEditor , { supportSideBySide : SideBySideEditor . PRIMARY } ) )
97
+ ?? accessor . get ( IWorkspaceContextService ) . getWorkspace ( ) . folders [ 0 ] . uri ;
98
+ const clipboardService = accessor . get ( IClipboardService ) ;
99
+ const dialogService = accessor . get ( IDialogService ) ;
100
+ const urlService = accessor . get ( IOpenerService ) ;
101
+ const progressService = accessor . get ( IProgressService ) ;
102
+ const selection = accessor . get ( ICodeEditorService ) . getActiveCodeEditor ( ) ?. getSelection ( ) ?? undefined ;
90
103
91
- const result = await progressService . withProgress ( {
92
- location : ProgressLocation . Window ,
93
- detail : localize ( 'generating link' , 'Generating link...' )
94
- } , async ( ) => shareService . provideShare ( { resourceUri, selection } , new CancellationTokenSource ( ) . token ) ) ;
104
+ const result = await progressService . withProgress ( {
105
+ location : ProgressLocation . Window ,
106
+ detail : localize ( 'generating link' , 'Generating link...' )
107
+ } , async ( ) => shareService . provideShare ( { resourceUri, selection } , new CancellationTokenSource ( ) . token ) ) ;
95
108
96
- if ( result ) {
97
- const uriText = result . toString ( ) ;
98
- const isResultText = typeof result === 'string' ;
99
- await clipboardService . writeText ( uriText ) ;
109
+ if ( result ) {
110
+ const uriText = result . toString ( ) ;
111
+ const isResultText = typeof result === 'string' ;
112
+ await clipboardService . writeText ( uriText ) ;
100
113
101
- dialogService . prompt (
102
- {
103
- type : Severity . Info ,
104
- message : isResultText ? localize ( 'shareTextSuccess' , 'Copied text to clipboard!' ) : localize ( 'shareSuccess' , 'Copied link to clipboard!' ) ,
105
- custom : {
106
- icon : Codicon . check ,
107
- markdownDetails : [ {
108
- markdown : new MarkdownString ( `<div aria-label='${ uriText } '>${ uriText } </div>` , { supportHtml : true } ) ,
109
- classes : [ isResultText ? 'share-dialog-input-text' : 'share-dialog-input-link' ]
110
- } ]
111
- } ,
112
- cancelButton : localize ( 'close' , 'Close' ) ,
113
- buttons : isResultText ? [ ] : [ { label : localize ( 'open link' , 'Open Link' ) , run : ( ) => { urlService . open ( result , { openExternal : true } ) ; } } ]
114
- }
115
- ) ;
114
+ dialogService . prompt (
115
+ {
116
+ type : Severity . Info ,
117
+ message : isResultText ? localize ( 'shareTextSuccess' , 'Copied text to clipboard!' ) : localize ( 'shareSuccess' , 'Copied link to clipboard!' ) ,
118
+ custom : {
119
+ icon : Codicon . check ,
120
+ markdownDetails : [ {
121
+ markdown : new MarkdownString ( `<div aria-label='${ uriText } '>${ uriText } </div>` , { supportHtml : true } ) ,
122
+ classes : [ isResultText ? 'share-dialog-input-text' : 'share-dialog-input-link' ]
123
+ } ]
124
+ } ,
125
+ cancelButton : localize ( 'close' , 'Close' ) ,
126
+ buttons : isResultText ? [ ] : [ { label : localize ( 'open link' , 'Open Link' ) , run : ( ) => { urlService . open ( result , { openExternal : true } ) ; } } ]
127
+ }
128
+ ) ;
129
+ }
116
130
}
117
- }
118
- } ) ;
131
+ } )
132
+ ) ;
119
133
120
134
const actions = this . shareService . getShareActions ( ) ;
121
135
for ( const menuId of targetMenus ) {
122
136
for ( const action of actions ) {
123
137
// todo@joyceerhl avoid duplicates
124
- MenuRegistry . appendMenuItem ( menuId , action ) ;
138
+ this . _disposables . add ( MenuRegistry . appendMenuItem ( menuId , action ) ) ;
125
139
}
126
140
}
127
141
}
0 commit comments