UNPKG

6.9 kBTypeScriptView Raw
1import { Widget } from '@phosphor/widgets';
2import { ILogger, Emitter, Event, ContributionProvider, MaybePromise, WaitUntilEvent } from '../common';
3export declare const WidgetFactory: unique symbol;
4/**
5 * A {@link WidgetFactory} is used to create new widgets. Factory-specific information (options) can be passed as serializable JSON data.
6 * The common {@link WidgetManager} collects `WidgetFactory` contributions and delegates to the corresponding factory when
7 * a widget should be created or restored. To identify widgets the `WidgetManager` uses a description composed of the factory id and the options.
8 * The `WidgetFactory` does support both, synchronous and asynchronous widget creation.
9 *
10 * ### Example usage
11 *
12 * ```typescript
13 * export class MyWidget extends BaseWidget {
14 * }
15 *
16 * @injectable()
17 * export class MyWidgetFactory implements WidgetFactory {
18 * id = 'myWidgetFactory';
19 *
20 * createWidget(): MaybePromise<Widget> {
21 * return new MyWidget();
22 * }
23 * }
24 * ```
25 */
26export interface WidgetFactory {
27 /**
28 * The factory id.
29 */
30 readonly id: string;
31 /**
32 * Creates a widget using the given options.
33 * @param options factory specific information as serializable JSON data.
34 *
35 * @returns the newly created widget or a promise of the widget
36 */
37 createWidget(options?: any): MaybePromise<Widget>;
38}
39/**
40 * Representation of the `WidgetConstructionOptions`.
41 * Defines a serializable description to create widgets.
42 */
43export interface WidgetConstructionOptions {
44 /**
45 * The id of the widget factory to use.
46 */
47 factoryId: string;
48 /**
49 * The widget factory specific information.
50 */
51 options?: any;
52}
53/**
54 * Representation of a `willCreateWidgetEvent`.
55 */
56export interface WillCreateWidgetEvent extends WaitUntilEvent {
57 /**
58 * The widget which will be created.
59 */
60 readonly widget: Widget;
61 /**
62 * The widget factory id.
63 */
64 readonly factoryId: string;
65}
66/**
67 * Representation of a `didCreateWidgetEvent`.
68 */
69export interface DidCreateWidgetEvent {
70 /**
71 * The widget which was created.
72 */
73 readonly widget: Widget;
74 /**
75 * The widget factory id.
76 */
77 readonly factoryId: string;
78}
79/**
80 * The {@link WidgetManager} is the common component responsible for creating and managing widgets. Additional widget factories
81 * can be registered by using the {@link WidgetFactory} contribution point. To identify a widget, created by a factory, the factory id and
82 * the creation options are used. This key is commonly referred to as `description` of the widget.
83 */
84export declare class WidgetManager {
85 protected _cachedFactories: Map<string, WidgetFactory>;
86 protected readonly widgets: Map<string, Widget>;
87 protected readonly pendingWidgetPromises: Map<string, MaybePromise<Widget>>;
88 protected readonly factoryProvider: ContributionProvider<WidgetFactory>;
89 protected readonly logger: ILogger;
90 protected readonly onWillCreateWidgetEmitter: Emitter<WillCreateWidgetEvent>;
91 /**
92 * An event can be used to participate in the widget creation.
93 * Listeners may not dispose the given widget.
94 */
95 readonly onWillCreateWidget: Event<WillCreateWidgetEvent>;
96 protected readonly onDidCreateWidgetEmitter: Emitter<DidCreateWidgetEvent>;
97 readonly onDidCreateWidget: Event<DidCreateWidgetEvent>;
98 /**
99 * Get the list of widgets created by the given widget factory.
100 * @param factoryId the widget factory id.
101 *
102 * @returns the list of widgets created by the factory with the given id.
103 */
104 getWidgets(factoryId: string): Widget[];
105 /**
106 * Try to get the existing widget for the given description.
107 * @param factoryId The widget factory id.
108 * @param options The widget factory specific information.
109 *
110 * @returns the widget if available, else `undefined`.
111 *
112 * The widget is 'available' if it has been created with the same {@link factoryId} and {@link options} by the {@link WidgetManager}.
113 * If the widget's creation is asynchronous, it is only available when the associated `Promise` is resolved.
114 */
115 tryGetWidget<T extends Widget>(factoryId: string, options?: any): T | undefined;
116 /**
117 * Try to get the existing widget for the given description.
118 * @param factoryId The widget factory id.
119 * @param options The widget factory specific information.
120 *
121 * @returns A promise that resolves to the widget, if any exists. The promise may be pending, so be cautious when assuming that it will not reject.
122 */
123 tryGetPendingWidget<T extends Widget>(factoryId: string, options?: any): MaybePromise<T> | undefined;
124 /**
125 * Get the widget for the given description.
126 * @param factoryId The widget factory id.
127 * @param options The widget factory specific information.
128 *
129 * @returns a promise resolving to the widget if available, else `undefined`.
130 */
131 getWidget<T extends Widget>(factoryId: string, options?: any): Promise<T | undefined>;
132 /**
133 * Finds a widget that matches the given test predicate.
134 * @param factoryId The widget factory id.
135 * @param predicate The test predicate.
136 *
137 * @returns a promise resolving to the widget if available, else `undefined`.
138 */
139 findWidget<T extends Widget>(factoryId: string, predicate: (options?: any) => boolean): Promise<T | undefined>;
140 protected testPredicate(key: string, factoryId: string, predicate: (options?: any) => boolean): boolean;
141 protected doGetWidget<T extends Widget>(key: string): MaybePromise<T> | undefined;
142 /**
143 * Creates a new widget or returns the existing widget for the given description.
144 * @param factoryId the widget factory id.
145 * @param options the widget factory specific information.
146 *
147 * @returns a promise resolving to the widget.
148 */
149 getOrCreateWidget<T extends Widget>(factoryId: string, options?: any): Promise<T>;
150 /**
151 * Get the widget construction options.
152 * @param widget the widget.
153 *
154 * @returns the widget construction options if the widget was created through the manager, else `undefined`.
155 */
156 getDescription(widget: Widget): WidgetConstructionOptions | undefined;
157 /**
158 * Convert the widget construction options to string.
159 * @param options the widget construction options.
160 *
161 * @returns the widget construction options represented as a string.
162 */
163 protected toKey(options: WidgetConstructionOptions): string;
164 /**
165 * Convert the key into the widget construction options object.
166 * @param key the key.
167 *
168 * @returns the widget construction options object.
169 */
170 protected fromKey(key: string): WidgetConstructionOptions;
171 protected get factories(): Map<string, WidgetFactory>;
172}
173//# sourceMappingURL=widget-manager.d.ts.map
\No newline at end of file