UNPKG

@theia/core

Version:

Theia is a cloud & desktop IDE framework implemented in TypeScript.

211 lines • 9.05 kB
import { __decorate, __param } from "tslib"; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; // ***************************************************************************** // Copyright (C) 2017-2018 TypeFox and others. // // This program and the accompanying materials are made available under the // terms of the Eclipse Public License v. 2.0 which is available at // http://www.eclipse.org/legal/epl-2.0. // // This Source Code may also be made available under the following Secondary // Licenses when the conditions for such availability set forth in the Eclipse // Public License v. 2.0 are satisfied: GNU General Public License, version 2 // with the GNU Classpath Exception which is available at // https://www.gnu.org/software/classpath/license.html. // // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 // ***************************************************************************** import * as React from 'react'; import { injectable, inject } from 'inversify'; import debounce from '../../../shared/lodash.debounce/index.cjs'; import { CancellationTokenSource, CommandService, nls } from '../../common/index.js'; import { ReactWidget } from '../widgets/react-widget.js'; import { FrontendApplicationStateService } from '../frontend-application-state.js'; import { LabelParser, LabelIcon } from '../label-parser.js'; import { StatusBar, StatusBarAlignment } from './status-bar-types.js'; import { StatusBarViewModel } from './status-bar-view-model.js'; import { HoverService } from '../hover-service.js'; import { codicon } from '../widgets/index.js'; import { PreferenceService } from '../../common/preferences/index.js'; import { MarkdownString } from '../../common/markdown-rendering/index.js'; export { StatusBar, StatusBarAlignment }; let StatusBarImpl = class StatusBarImpl extends ReactWidget { constructor(commands, entryService, applicationStateService, preferences, viewModel, hoverService) { super(); this.commands = commands; this.entryService = entryService; this.applicationStateService = applicationStateService; this.preferences = preferences; this.viewModel = viewModel; this.hoverService = hoverService; this.preferencesReady = false; this.debouncedUpdate = debounce(() => this.update(), 50); delete this.scrollOptions; this.id = 'theia-statusBar'; this.addClass('noselect'); // Hide the status bar until the `workbench.statusBar.visible` preference returns with a `true` value. this.hide(); this.preferences.ready.then(() => { this.preferencesReady = true; this.updateVisibility(); }); this.toDispose.push(this.preferences.onPreferenceChanged(preference => { if (preference.preferenceName === 'workbench.statusBar.visible') { this.updateVisibility(); } })); this.toDispose.push(this.viewModel.onDidChange(() => this.debouncedUpdate())); } updateVisibility() { if (!this.preferencesReady) { return; // Don't change visibility until preferences have loaded } const prefHides = !this.preferences.get('workbench.statusBar.visible', true); this.setHidden(prefHides); } get ready() { return this.applicationStateService.reachedAnyState('initialized_layout', 'ready'); } async setElement(id, entry) { await this.ready; this.viewModel.set(id, entry); } async removeElement(id) { await this.ready; this.viewModel.remove(id); } async setBackgroundColor(color) { await this.ready; this.internalSetBackgroundColor(color); this.update(); } internalSetBackgroundColor(color) { this.backgroundColor = color; this.node.style.backgroundColor = this.backgroundColor || ''; } async setColor(color) { await this.ready; this.internalSetColor(color); this.update(); } internalSetColor(color) { this.color = color; } render() { const leftEntries = Array.from(this.viewModel.getLeft(), entry => this.renderElement(entry)); const rightEntries = Array.from(this.viewModel.getRight(), entry => this.renderElement(entry)); return _jsxs(React.Fragment, { children: [_jsx("div", { className: 'area left', children: leftEntries }), _jsx("div", { className: 'area right', children: rightEntries })] }); } triggerCommand(entry) { return () => { if (entry.command) { const args = entry.arguments || []; this.commands.executeCommand(entry.command, ...args); } }; } /** * Request a hover to be displayed for a status bar entry. * @param e The mouse event that triggered the hover request * @param entry The status bar entry to display hover for * @param skipDelay When true, requests the hover immediately without delay (for click-triggered hovers) */ requestHover(e, entry, skipDelay = false) { const target = e.currentTarget; if (typeof entry.tooltip === 'function') { const cancellationSource = new CancellationTokenSource(); this.doRequestHover(target, nls.localizeByDefault('Loading...'), skipDelay, () => cancellationSource.dispose()); Promise.resolve(entry.tooltip(cancellationSource.token)) .catch(() => undefined) .then(res => res && !cancellationSource.token.isCancellationRequested && this.doRequestHover(target, res, skipDelay)); } else { this.doRequestHover(target, entry.tooltip, skipDelay); } } doRequestHover(target, content, skipDelay = false, onHide) { this.hoverService.requestHover({ content, target, position: 'top', interactive: content instanceof HTMLElement || MarkdownString.is(content), onHide, skipHoverDelay: skipDelay }); } createAttributes(viewEntry) { const attrs = {}; const entry = viewEntry.entry; attrs.id = 'status-bar-' + viewEntry.id; attrs.className = 'element'; if (entry.command || entry.onclick || entry.tooltip) { attrs.className += ' hasCommand'; } if (entry.command) { attrs.onClick = this.triggerCommand(entry); } else if (entry.onclick) { attrs.onClick = e => entry.onclick?.(e.nativeEvent); } else { attrs.onClick = e => this.requestHover(e, entry, true); } if (viewEntry.compact && viewEntry.alignment !== undefined) { attrs.className += viewEntry.alignment === StatusBarAlignment.RIGHT ? ' compact-right' : ' compact-left'; } if (entry.tooltip) { attrs.onMouseEnter = e => this.requestHover(e, entry); } if (entry.className) { attrs.className += ' ' + entry.className; } if (entry.accessibilityInformation) { attrs['aria-label'] = entry.accessibilityInformation.label; attrs.role = entry.accessibilityInformation.role; } else { attrs['aria-label'] = [entry.text, entry.tooltip].join(', '); } if (entry.backgroundColor) { attrs.className += ' has-background'; } attrs.style = { color: entry.color || this.color, backgroundColor: entry.backgroundColor }; return attrs; } renderElement(entry) { const childStrings = this.entryService.parse(entry.entry.text); const children = []; childStrings.forEach((val, key) => { if (LabelIcon.is(val)) { const animation = val.animation ? ` fa-${val.animation}` : ''; if (val.name.startsWith('codicon-')) { children.push(_jsx("span", { className: `codicon ${val.name}${animation}` }, key)); } else if (val.name.startsWith('fa-')) { children.push(_jsx("span", { className: `fa ${val.name}${animation}` }, key)); } else { children.push(_jsx("span", { className: `${codicon(val.name)}${animation}` }, key)); } } else { children.push(_jsx("span", { children: val }, key)); } }); return _jsx("div", { ...this.createAttributes(entry), children: children }, entry.id); } }; StatusBarImpl = __decorate([ injectable(), __param(0, inject(CommandService)), __param(1, inject(LabelParser)), __param(2, inject(FrontendApplicationStateService)), __param(3, inject(PreferenceService)), __param(4, inject(StatusBarViewModel)), __param(5, inject(HoverService)) ], StatusBarImpl); export { StatusBarImpl }; //# sourceMappingURL=status-bar.js.map