Skip to content

Commit

Permalink
small refactoring of button contribution
Browse files Browse the repository at this point in the history
Signed-off-by: Jonah Iden <[email protected]>
  • Loading branch information
jonah-iden committed Jan 31, 2025
1 parent 2a3e2ef commit 0d8373b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
8 changes: 6 additions & 2 deletions packages/filesystem/src/browser/file-dialog/file-dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export const FILENAME_TEXTFIELD_CLASS = 'theia-FileNameTextField';
export const CONTROL_PANEL_CLASS = 'theia-ControlPanel';
export const TOOLBAR_ITEM_TRANSFORM_TIMEOUT = 100;

export interface AdditionalButtonDefinition<T> {
label: string;
onClick: (resolve: (v: T | undefined) => void, reject: (v: unknown) => void) => void;
}
export class FileDialogProps extends DialogProps {

/**
Expand Down Expand Up @@ -86,7 +90,7 @@ export class FileDialogProps extends DialogProps {
/**
* Additional buttons to show beside the close and accept buttons.
*/
additionalButtons?: [string, <T>(resolve: (v: T | undefined) => void, reject: (v: T | unknown) => void) => void][];
additionalButtons?: AdditionalButtonDefinition<unknown>[];

}

Expand Down Expand Up @@ -190,7 +194,7 @@ export abstract class FileDialog<T> extends AbstractDialog<T> {
this.hiddenFilesToggleRenderer = this.hiddenFilesToggleFactory(this.widget.model.tree);
this.contentNode.appendChild(this.hiddenFilesToggleRenderer.host);

this.props.additionalButtons?.forEach(([label, onClick]) => {
this.props.additionalButtons?.forEach(({ label, onClick }) => {
const button = this.appendButton(label, false);
button.onclick = () => {
if (this.resolve && this.reject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { MaybeArray, URI, nls } from '@theia/core';
import { inject, injectable } from '@theia/core/shared/inversify';
import { OpenFileDialogProps, SaveFileDialogProps } from '@theia/filesystem/lib/browser/file-dialog';
import { AdditionalButtonDefinition, OpenFileDialogProps, SaveFileDialogProps } from '@theia/filesystem/lib/browser/file-dialog';
import { FileStat } from '@theia/filesystem/lib/common/files';
import { DefaultFileDialogService } from '@theia/filesystem/lib/browser/file-dialog/file-dialog-service';
import { ElectronFileDialogService } from '@theia/filesystem/lib/electron-browser/file-dialog/electron-file-dialog-service';
Expand Down Expand Up @@ -48,14 +48,15 @@ export class RemoteElectronFileDialogService extends ElectronFileDialogService {
}

protected addLocalFilesButton(props: OpenFileDialogProps): void {
const localFilesButton: [string, (res: typeof Promise.resolve) => void] = ['Show Local Files', async resolve => {
const localFile = await super.showOpenDialog({ ...props, title: nls.localizeByDefault('Show Local'), fileScheme: LOCAL_FILE_SCHEME });
if (localFile) {
resolve({ uri: localFile });
} else {
resolve(undefined);
const localFilesButton: AdditionalButtonDefinition<{ uri: URI }> = {
label: nls.localizeByDefault('Show Local'),
onClick: async resolve => {
const localFile = await super.showOpenDialog({ ...props, fileScheme: LOCAL_FILE_SCHEME });
if (localFile) {
resolve({ uri: localFile });
}
}
}];
};
if (props.additionalButtons) {
props.additionalButtons.push(localFilesButton);
} else {
Expand Down

0 comments on commit 0d8373b

Please sign in to comment.