Skip to content

Commit

Permalink
getting-started: implement startup editor preference (#12813)
Browse files Browse the repository at this point in the history
* implement workbench: startup editor preference

This commit introduces 5 new startup editor preferences:
- `none`: no editor will be shown on startup
- `welcomePage`: the default welcome page will be shown on startup
- `readme`: it will open the folder's readme in preview mode
- `newUntitledFile`: opens an untitled file on startup
- `welcomePageInEmptyWorkbench`: only opens the welcome page when opening
  empty workbench

Signed-Off-By: Vlad Arama <[email protected]>
  • Loading branch information
vladarama authored Aug 31, 2023
1 parent 97fe17e commit 704996d
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

- [deps] Bumped supported Node.js version from 16.x to >=18, you may need to update your environments.

<a name="breaking_changes_1.41.0">[Breaking Changes:](#breaking_changes_1.41.0)</a>

- [preferences] removed the `welcome.alwaysShowWelcomePage` preference in favor of `workbench.startupEditor`: [#12813](https://github.com/eclipse-theia/theia/pull/12813)

## v1.40.0 - 07/27/2023

- [application-package] bumped the default supported VS Code API from `1.78.0` to `1.79.0` [#12764](https://github.com/eclipse-theia/theia/pull/12764) - Contributed on behalf of STMicroelectronics.
Expand Down
3 changes: 3 additions & 0 deletions packages/getting-started/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"description": "Theia - GettingStarted Extension",
"dependencies": {
"@theia/core": "1.40.0",
"@theia/editor": "1.40.0",
"@theia/filesystem": "1.40.0",
"@theia/keymaps": "1.40.0",
"@theia/preview": "1.40.0",
"@theia/workspace": "1.40.0"
},
"publishConfig": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
// *****************************************************************************

import { injectable, inject } from '@theia/core/shared/inversify';
import { CommandRegistry, MenuModelRegistry } from '@theia/core/lib/common';
import { CommonMenus, AbstractViewContribution, FrontendApplicationContribution, FrontendApplication, NavigatableWidget, PreferenceService } from '@theia/core/lib/browser';
import { ArrayUtils, CommandRegistry, MenuModelRegistry } from '@theia/core/lib/common';
import { CommonCommands, CommonMenus, AbstractViewContribution, FrontendApplicationContribution, FrontendApplication, PreferenceService } from '@theia/core/lib/browser';
import { EditorManager } from '@theia/editor/lib/browser/editor-manager';
import { GettingStartedWidget } from './getting-started-widget';
import { FileService } from '@theia/filesystem/lib/browser/file-service';
import { FrontendApplicationStateService } from '@theia/core/lib/browser/frontend-application-state';
import { PreviewContribution } from '@theia/preview/lib/browser/preview-contribution';
import { WorkspaceService } from '@theia/workspace/lib/browser';

/**
Expand All @@ -32,15 +35,27 @@ export const GettingStartedCommand = {
@injectable()
export class GettingStartedContribution extends AbstractViewContribution<GettingStartedWidget> implements FrontendApplicationContribution {

@inject(CommandRegistry)
protected readonly commandRegistry: CommandRegistry;

@inject(EditorManager)
protected readonly editorManager: EditorManager;

@inject(FileService)
protected readonly fileService: FileService;

@inject(PreferenceService)
protected readonly preferenceService: PreferenceService;

@inject(PreviewContribution)
protected readonly previewContribution: PreviewContribution;

@inject(FrontendApplicationStateService)
protected readonly stateService: FrontendApplicationStateService;

@inject(WorkspaceService)
protected readonly workspaceService: WorkspaceService;

@inject(PreferenceService)
protected readonly preferenceService: PreferenceService;

constructor() {
super({
widgetId: GettingStartedWidget.ID,
Expand All @@ -52,19 +67,49 @@ export class GettingStartedContribution extends AbstractViewContribution<Getting
}

async onStart(app: FrontendApplication): Promise<void> {
this.stateService.reachedState('ready').then(() => {
const editors = this.shell.widgets.filter((widget): widget is NavigatableWidget => NavigatableWidget.is(widget));
if (editors.length === 0) {
this.preferenceService.ready.then(() => {
const showWelcomePage: boolean = this.preferenceService.get('welcome.alwaysShowWelcomePage', true);
if (showWelcomePage) {
this.stateService.reachedState('ready').then(async () => {
if (this.editorManager.all.length === 0) {
await this.preferenceService.ready;
const startupEditor = this.preferenceService.get('workbench.startupEditor');
switch (startupEditor) {
case 'welcomePage':
this.openView({ reveal: true, activate: true });
}
});
break;
case 'welcomePageInEmptyWorkbench':
if (!this.workspaceService.opened) {
this.openView({ reveal: true, activate: true });
}
break;
case 'newUntitledFile':
this.commandRegistry.executeCommand(CommonCommands.NEW_UNTITLED_TEXT_FILE.id);
break;
case 'readme':
await this.openReadme();
break;
}
}
});
}

protected async openReadme(): Promise<void> {
const roots = await this.workspaceService.roots;
const readmes = await Promise.all(roots.map(async folder => {
const folderStat = await this.fileService.resolve(folder.resource);
const fileArr = folderStat?.children?.sort((a, b) => a.name.localeCompare(b.name)) || [];
const filePath = fileArr.find(file => file.name.toLowerCase() === 'readme.md') || fileArr.find(file => file.name.toLowerCase().startsWith('readme'));
return filePath?.resource;
}));
const validReadmes = ArrayUtils.coalesce(readmes);
if (validReadmes.length) {
for (const readme of validReadmes) {
await this.previewContribution.open(readme);
}
} else {
// If no readme is found, show the welcome page.
this.openView({ reveal: true, activate: true });
}
}

override registerCommands(registry: CommandRegistry): void {
registry.registerCommand(GettingStartedCommand, {
execute: () => this.openView({ reveal: true, activate: true }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,32 @@ import {
PreferenceSchema,
PreferenceContribution
} from '@theia/core/lib/browser/preferences';
import { FrontendApplicationConfigProvider } from '@theia/core/lib/browser/frontend-application-config-provider';
import { nls } from '@theia/core/lib/common/nls';

export const GettingStartedPreferenceSchema: PreferenceSchema = {
'type': 'object',
properties: {
'welcome.alwaysShowWelcomePage': {
type: 'boolean',
description: nls.localizeByDefault('Show welcome page on startup'),
default: true
}
'workbench.startupEditor': {
type: 'string',
enum: ['none', 'welcomePage', 'readme', 'newUntitledFile', 'welcomePageInEmptyWorkbench'],
enumDescriptions: [
nls.localizeByDefault('Start without an editor.'),
nls.localize('theia/getting-started/startup-editor/welcomePage', 'Open the Welcome page, with content to aid in getting started with {0} and extensions.',
FrontendApplicationConfigProvider.get().applicationName),
nls.localizeByDefault(`Open the README when opening a folder that contains one, fallback to \'welcomePage\' otherwise.
Note: This is only observed as a global configuration, it will be ignored if set in a workspace or folder configuration.`),
nls.localizeByDefault('Open a new untitled text file (only applies when opening an empty window).'),
nls.localizeByDefault('Open the Welcome page when opening an empty workbench.'),
],
default: 'welcomePage',
description: nls.localizeByDefault('Controls which editor is shown at startup, if none are restored from the previous session.')
},
}
};

export interface GettingStartedConfiguration {
'welcome.alwaysShowWelcomePage': boolean;
'workbench.startupEditor': string;
}

export const GettingStartedPreferenceContribution = Symbol('GettingStartedPreferenceContribution');
Expand Down
18 changes: 9 additions & 9 deletions packages/getting-started/src/browser/getting-started-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -496,32 +496,32 @@ export interface PreferencesProps {
}

function WelcomePreferences(props: PreferencesProps): JSX.Element {
const [alwaysShowWelcomePage, setAlwaysShowWelcomePage] = React.useState<boolean>(
props.preferenceService.get('welcome.alwaysShowWelcomePage', true)
const [startupEditor, setStartupEditor] = React.useState<string>(
props.preferenceService.get('workbench.startupEditor', 'welcomePage')
);
React.useEffect(() => {
const prefListener = props.preferenceService.onPreferenceChanged(change => {
if (change.preferenceName === 'welcome.alwaysShowWelcomePage') {
if (change.preferenceName === 'workbench.startupEditor') {
const prefValue = change.newValue;
setAlwaysShowWelcomePage(prefValue);
setStartupEditor(prefValue);
}
});
return () => prefListener.dispose();
}, [props.preferenceService]);
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const newChecked = e.target.checked;
props.preferenceService.updateValue('welcome.alwaysShowWelcomePage', newChecked);
const newValue = e.target.checked ? 'welcomePage' : 'none';
props.preferenceService.updateValue('workbench.startupEditor', newValue);
};
return (
<div className='gs-preference'>
<input
type="checkbox"
className="theia-input"
id="alwaysShowWelcomePage"
id="startupEditor"
onChange={handleChange}
checked={alwaysShowWelcomePage}
checked={startupEditor === 'welcomePage' || startupEditor === 'welcomePageInEmptyWorkbench'}
/>
<label htmlFor="alwaysShowWelcomePage">
<label htmlFor="startupEditor">
{nls.localizeByDefault('Show welcome page on startup')}
</label>
</div>
Expand Down
9 changes: 9 additions & 0 deletions packages/getting-started/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,18 @@
{
"path": "../core"
},
{
"path": "../editor"
},
{
"path": "../filesystem"
},
{
"path": "../keymaps"
},
{
"path": "../preview"
},
{
"path": "../workspace"
}
Expand Down

0 comments on commit 704996d

Please sign in to comment.