UNPKG

5.55 kBTypeScriptView Raw
1import { Widget } from '@phosphor/widgets';
2import { FrontendApplication } from '../frontend-application';
3import { WidgetManager, WidgetConstructionOptions } from '../widget-manager';
4import { StorageService } from '../storage-service';
5import { ILogger } from '../../common/logger';
6import { CommandContribution, CommandRegistry, Command } from '../../common/command';
7import { ThemeService } from '../theming';
8import { ContributionProvider } from '../../common/contribution-provider';
9import { ApplicationShell, ApplicationShellLayoutVersion } from './application-shell';
10import { WindowService } from '../window/window-service';
11import { MaybePromise } from '../../common';
12/**
13 * A contract for widgets that want to store and restore their inner state, between sessions.
14 */
15export interface StatefulWidget {
16 /**
17 * Called on unload to store the inner state. Returns 'undefined' if the widget cannot be stored.
18 */
19 storeState(): object | undefined;
20 /**
21 * Called when the widget got created by the storage service
22 */
23 restoreState(oldState: object): void;
24}
25export declare namespace StatefulWidget {
26 function is(arg: unknown): arg is StatefulWidget;
27}
28export interface WidgetDescription {
29 constructionOptions: WidgetConstructionOptions;
30 innerWidgetState?: string | object;
31}
32export interface ApplicationShellLayoutMigrationContext {
33 /**
34 * A resolved version of a current layout.
35 */
36 layoutVersion: number;
37 /**
38 * A layout to be inflated.
39 */
40 layout: ApplicationShell.LayoutData;
41 /**
42 * A parent widget is to be inflated. `undefined` if the application shell
43 */
44 parent?: Widget;
45}
46export interface ApplicationShellLayoutMigrationError extends Error {
47 code: 'ApplicationShellLayoutMigrationError';
48}
49export declare namespace ApplicationShellLayoutMigrationError {
50 function create(message?: string): ApplicationShellLayoutMigrationError;
51 function is(error: Error | undefined): error is ApplicationShellLayoutMigrationError;
52}
53export declare const ApplicationShellLayoutMigration: unique symbol;
54export interface ApplicationShellLayoutMigration {
55 /**
56 * A target migration version.
57 */
58 readonly layoutVersion: ApplicationShellLayoutVersion;
59 /**
60 * A migration can transform layout before it will be inflated.
61 *
62 * @throws `ApplicationShellLayoutMigrationError` if a layout cannot be migrated,
63 * in this case the default layout will be initialized.
64 */
65 onWillInflateLayout?(context: ApplicationShellLayoutMigrationContext): MaybePromise<void>;
66 /**
67 * A migration can transform the given description before it will be inflated.
68 *
69 * @returns a migrated widget description, or `undefined`
70 * @throws `ApplicationShellLayoutMigrationError` if a widget description cannot be migrated,
71 * in this case the default layout will be initialized.
72 */
73 onWillInflateWidget?(desc: WidgetDescription, context: ApplicationShellLayoutMigrationContext): MaybePromise<WidgetDescription | undefined>;
74}
75export declare const RESET_LAYOUT: Command;
76export declare class ShellLayoutRestorer implements CommandContribution {
77 protected widgetManager: WidgetManager;
78 protected logger: ILogger;
79 protected storageService: StorageService;
80 protected storageKey: string;
81 protected shouldStoreLayout: boolean;
82 protected readonly migrations: ContributionProvider<ApplicationShellLayoutMigration>;
83 protected readonly windowService: WindowService;
84 protected readonly themeService: ThemeService;
85 constructor(widgetManager: WidgetManager, logger: ILogger, storageService: StorageService);
86 registerCommands(commands: CommandRegistry): void;
87 protected resetLayout(): Promise<void>;
88 storeLayout(app: FrontendApplication): void;
89 restoreLayout(app: FrontendApplication): Promise<boolean>;
90 protected isWidgetProperty(propertyName: string): boolean;
91 protected isWidgetsProperty(propertyName: string): boolean;
92 /**
93 * Turns the layout data to a string representation.
94 */
95 protected deflate(data: object): string;
96 private convertToDescription;
97 /**
98 * Creates the layout data from its string representation.
99 */
100 protected inflate(layoutData: string): Promise<ApplicationShell.LayoutData>;
101 protected fireWillInflateLayout(context: ShellLayoutRestorer.InflateContext): Promise<void>;
102 protected parse<T>(layoutData: string, parseContext: ShellLayoutRestorer.ParseContext): T;
103 protected fireWillInflateWidget(desc: WidgetDescription, context: ShellLayoutRestorer.InflateContext): Promise<WidgetDescription>;
104 protected convertToWidget(desc: WidgetDescription, context: ShellLayoutRestorer.InflateContext): Promise<Widget | undefined>;
105}
106export declare namespace ShellLayoutRestorer {
107 class ParseContext {
108 protected readonly toInflate: Inflate[];
109 protected readonly toFilter: Widgets[];
110 /**
111 * Returns an array, which will be filtered from undefined elements
112 * after resolving promises, that create widgets.
113 */
114 filteredArray(): Widgets;
115 push(toInflate: Inflate): void;
116 inflate(context: InflateContext): Promise<void>;
117 }
118 type Widgets = (Widget | undefined)[];
119 type Inflate = (context: InflateContext) => Promise<void>;
120 interface InflateContext extends ApplicationShellLayoutMigrationContext {
121 readonly migrations: ApplicationShellLayoutMigration[];
122 }
123}
124//# sourceMappingURL=shell-layout-restorer.d.ts.map
\No newline at end of file