@@ -20,8 +20,28 @@ import { ILogger, Emitter, Event, ContributionProvider, MaybePromise, WaitUntilE
20
20
21
21
/* eslint-disable @typescript-eslint/no-explicit-any */
22
22
export const WidgetFactory = Symbol ( 'WidgetFactory' ) ;
23
+
23
24
/**
24
- * `OpenHandler` should be implemented to provide a new opener.
25
+ * A {@link WidgetFactory} is used to create new widgets. Factory-specific information (options) can be passed as serializable JSON data.
26
+ * The common {@link WidgetManager} collects `WidgetFactory` contributions and delegates to the corresponding factory when
27
+ * a widget should be created or restored. To identify widgets the `WidgetManager` uses a description composed of the factory id and the options.
28
+ * The `WidgetFactory` does support both, synchronous and asynchronous widget creation.
29
+ *
30
+ * ### Example usage
31
+ *
32
+ * ```typescript
33
+ * export class MyWidget extends BaseWidget {
34
+ * }
35
+ *
36
+ * @injectable ()
37
+ * export class MyWidgetFactory implements WidgetFactory {
38
+ * id = 'myWidgetFactory';
39
+ *
40
+ * createWidget(): MaybePromise<Widget> {
41
+ * return new MyWidget();
42
+ * }
43
+ * }
44
+ * ```
25
45
*/
26
46
export interface WidgetFactory {
27
47
@@ -31,8 +51,10 @@ export interface WidgetFactory {
31
51
readonly id : string ;
32
52
33
53
/**
34
- * Creates a widget and attaches it to the application shell.
35
- * @param options serializable JSON data.
54
+ * Creates a widget using the given options.
55
+ * @param options factory specific information as serializable JSON data.
56
+ *
57
+ * @returns the newly created widget or a promise of the widget
36
58
*/
37
59
createWidget ( options ?: any ) : MaybePromise < Widget > ;
38
60
}
@@ -82,7 +104,9 @@ export interface DidCreateWidgetEvent {
82
104
}
83
105
84
106
/**
85
- * Creates and manages widgets.
107
+ * The {@link WidgetManager} is the common component responsible for creating and managing widgets. Additional widget factories
108
+ * can be registered by using the {@link WidgetFactory} contribution point. To identify a widget, created by a factory, the factory id and
109
+ * the creation options are used. This key is commonly referred to as `description` of the widget.
86
110
*/
87
111
@injectable ( )
88
112
export class WidgetManager {
@@ -106,13 +130,14 @@ export class WidgetManager {
106
130
readonly onWillCreateWidget : Event < WillCreateWidgetEvent > = this . onWillCreateWidgetEmitter . event ;
107
131
108
132
protected readonly onDidCreateWidgetEmitter = new Emitter < DidCreateWidgetEvent > ( ) ;
133
+
109
134
readonly onDidCreateWidget : Event < DidCreateWidgetEvent > = this . onDidCreateWidgetEmitter . event ;
110
135
111
136
/**
112
- * Get the list of widgets created for the given factory id .
137
+ * Get the list of widgets created by the given widget factory .
113
138
* @param factoryId the widget factory id.
114
139
*
115
- * @returns the list of widgets created for the given factory id.
140
+ * @returns the list of widgets created by the factory with the given id.
116
141
*/
117
142
getWidgets ( factoryId : string ) : Widget [ ] {
118
143
const result : Widget [ ] = [ ] ;
@@ -125,7 +150,9 @@ export class WidgetManager {
125
150
}
126
151
127
152
/**
128
- * Try and get the widget.
153
+ * Try to get the existing widget for the given description.
154
+ * @param factoryId The widget factory id.
155
+ * @param options The widget factory specific information.
129
156
*
130
157
* @returns the widget if available, else `undefined`.
131
158
*/
@@ -140,8 +167,10 @@ export class WidgetManager {
140
167
141
168
/**
142
169
* Get the widget for the given description.
170
+ * @param factoryId The widget factory id.
171
+ * @param options The widget factory specific information.
143
172
*
144
- * @returns a promise resolving to the widget if available, else `undefined.
173
+ * @returns a promise resolving to the widget if available, else `undefined` .
145
174
*/
146
175
async getWidget < T extends Widget > ( factoryId : string , options ?: any ) : Promise < T | undefined > {
147
176
const key = this . toKey ( { factoryId, options } ) ;
@@ -159,7 +188,11 @@ export class WidgetManager {
159
188
}
160
189
161
190
/**
162
- * Creates or returns the widget for the given description.
191
+ * Creates a new widget or returns the existing widget for the given description.
192
+ * @param factoryId the widget factory id.
193
+ * @param options the widget factory specific information.
194
+ *
195
+ * @returns a promise resolving to the widget.
163
196
*/
164
197
async getOrCreateWidget < T extends Widget > ( factoryId : string , options ?: any ) : Promise < T > {
165
198
const key = this . toKey ( { factoryId, options } ) ;
0 commit comments