Skip to content

Commit

Permalink
widget lifecycle fixes + make apis protected
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Kosyakov <anton.kosyakov@typefox.io>
  • Loading branch information
akosyakov committed May 10, 2019
1 parent d3b92fc commit fe6ea99
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
16 changes: 8 additions & 8 deletions packages/console/src/browser/console-content-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import { Message } from '@phosphor/messaging';
import { interfaces, Container, injectable } from 'inversify';
import { MenuPath, MessageType } from '@theia/core';
import { TreeProps } from '@theia/core/lib/browser/tree';
import { TreeSourceNode } from '@theia/core/lib/browser/source-tree';
import { Message } from '@theia/core/lib/browser';
import { SourceTreeWidget, TreeElementNode } from '@theia/core/lib/browser/source-tree';
import { ConsoleItem } from './console-session';

Expand All @@ -27,14 +27,14 @@ export class ConsoleContentWidget extends SourceTreeWidget {

static CONTEXT_MENU: MenuPath = ['console-context-menu'];

private _shouldScrollToEnd: boolean = true;
protected _shouldScrollToEnd = true;

set shouldScrollToEnd(value: boolean) {
this._shouldScrollToEnd = value;
protected set shouldScrollToEnd(shouldScrollToEnd: boolean) {
this._shouldScrollToEnd = shouldScrollToEnd;
this.shouldScrollToRow = this._shouldScrollToEnd;
}

get shouldScrollToEnd() {
protected get shouldScrollToEnd() {
return this._shouldScrollToEnd;
}

Expand All @@ -50,9 +50,9 @@ export class ConsoleContentWidget extends SourceTreeWidget {

protected onAfterAttach(msg: Message): void {
super.onAfterAttach(msg);
this.toDispose.push(this.onScrollUp(() => this.shouldScrollToEnd = false));
this.toDispose.push(this.onScrollYReachEnd(() => this.shouldScrollToEnd = true));
this.toDispose.push(this.model.onChanged(() => this.revealLastOutputIfNeeded()));
this.toDisposeOnDetach.push(this.onScrollUp(() => this.shouldScrollToEnd = false));
this.toDisposeOnDetach.push(this.onScrollYReachEnd(() => this.shouldScrollToEnd = true));
this.toDisposeOnDetach.push(this.model.onChanged(() => this.revealLastOutputIfNeeded()));
}

protected revealLastOutputIfNeeded(): void {
Expand Down
13 changes: 10 additions & 3 deletions packages/core/src/browser/tree/tree-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class TreeWidget extends ReactWidget implements StatefulWidget {
@inject(SelectionService)
protected readonly selectionService: SelectionService;

protected shouldScrollToRow: boolean = true;
protected shouldScrollToRow = true;

constructor(
@inject(TreeProps) readonly props: TreeProps,
Expand Down Expand Up @@ -243,11 +243,18 @@ export class TreeWidget extends ReactWidget implements StatefulWidget {

protected scrollToRow: number | undefined;
protected updateScrollToRow(updateOptions?: TreeWidget.ForceUpdateOptions): void {
this.scrollToRow = this.getScrollToRow();
this.forceUpdate(updateOptions);
}

protected getScrollToRow(): number | undefined {
if (!this.shouldScrollToRow) {
return undefined;
}
const selected = this.model.selectedNodes;
const node: TreeNode | undefined = selected.find(SelectableTreeNode.hasFocus) || selected[0];
const row = node && this.rows.get(node.id);
this.scrollToRow = !this.shouldScrollToRow ? undefined : (row && row.index);
this.forceUpdate(updateOptions);
return row && row.index;
}

protected readonly updateDecorations = debounce(() => this.doUpdateDecorations(), 150);
Expand Down
18 changes: 12 additions & 6 deletions packages/core/src/browser/widgets/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

// tslint:disable:no-any

import { injectable, decorate, unmanaged } from 'inversify';
import { Widget } from '@phosphor/widgets';
import { Message } from '@phosphor/messaging';
Expand All @@ -37,15 +39,19 @@ export const FOCUS_CLASS = 'theia-mod-focus';
@injectable()
export class BaseWidget extends Widget {

protected readonly toDispose = new DisposableCollection();
protected readonly toDisposeOnDetach = new DisposableCollection();
protected scrollBar?: PerfectScrollbar;
protected scrollOptions?: PerfectScrollbar.Options;
protected readonly onScrollYReachEndEmitter = new Emitter<void>();
readonly onScrollYReachEnd: Event<void> = this.onScrollYReachEndEmitter.event;
protected readonly onScrollUpEmitter = new Emitter<void>();
readonly onScrollUp: Event<void> = this.onScrollUpEmitter.event;

protected readonly toDispose = new DisposableCollection(
this.onScrollYReachEndEmitter,
this.onScrollUpEmitter
);
protected readonly toDisposeOnDetach = new DisposableCollection();
protected scrollBar?: PerfectScrollbar;
protected scrollOptions?: PerfectScrollbar.Options;

dispose(): void {
if (this.isDisposed) {
return;
Expand Down Expand Up @@ -85,8 +91,8 @@ export class BaseWidget extends Widget {
const container = await this.getScrollContainer();
container.style.overflow = 'hidden';
this.scrollBar = new PerfectScrollbar(container, this.scrollOptions);
container.addEventListener('ps-y-reach-end', () => { this.onScrollYReachEndEmitter.fire(undefined); });
container.addEventListener('ps-scroll-up', () => { this.onScrollUpEmitter.fire(undefined); });
this.toDisposeOnDetach.push(addEventListener(container, <any>'ps-y-reach-end', () => { this.onScrollYReachEndEmitter.fire(undefined); }));
this.toDisposeOnDetach.push(addEventListener(container, <any>'ps-scroll-up', () => { this.onScrollUpEmitter.fire(undefined); }));
this.toDisposeOnDetach.push(Disposable.create(() => {
if (this.scrollBar) {
this.scrollBar.destroy();
Expand Down

0 comments on commit fe6ea99

Please sign in to comment.