提交 4da71de4 编写于 作者: B Benjamin Pasero

better way of finding out active list (for #11517)

上级 4617cb7f
......@@ -286,7 +286,7 @@ export class List<T> implements IDisposable {
this.disposables = [this.focus, this.selection, this.view, this._onDispose];
const tracker = DOM.trackFocus(this.view.domNode);
this.disposables.push(tracker.addFocusListener(() => setTimeout(() => this._onDOMFocus.fire())));
this.disposables.push(tracker.addFocusListener(() => this._onDOMFocus.fire()));
this.disposables.push(tracker.addBlurListener(() => this._onDOMBlur.fire()));
if (typeof options.keyboardSupport !== 'boolean' || options.keyboardSupport) {
......@@ -469,6 +469,10 @@ export class List<T> implements IDisposable {
return this.view.domNode === document.activeElement;
}
getHTMLElement(): HTMLElement {
return this.view.domNode;
}
private toListEvent({ indexes }: ITraitChangeEvent) {
return { indexes, elements: indexes.map(i => this.view.element(i)) };
}
......
......@@ -93,7 +93,7 @@ export class Tree extends Events.EventEmitter implements _.ITree {
this.addEmitter2(this.model);
this.addEmitter2(this.view);
this.toDispose.push(this.model.addListener2('highlight', () => setTimeout(() => this._onHighlightChange.fire())));
this.toDispose.push(this.model.addListener2('highlight', () => this._onHighlightChange.fire()));
}
get onDOMFocus(): Event<void> {
......
......@@ -1523,7 +1523,7 @@ export class TreeView extends HeightMap {
DOM.addClass(this.domNode, 'focused');
}
setTimeout(() => this._onDOMFocus.fire());
this._onDOMFocus.fire();
}
private onBlur(): void {
......
......@@ -9,6 +9,7 @@ import { List } from 'vs/base/browser/ui/list/listWidget';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { dispose, IDisposable } from 'vs/base/common/lifecycle';
import { IContextKeyService, IContextKey, RawContextKey } from 'vs/platform/contextkey/common/contextkey';
import { RunOnceScheduler } from 'vs/base/common/async';
export const IListService = createDecorator<IListService>('listService');
......@@ -29,53 +30,75 @@ export class ListService implements IListService {
public _serviceBrand: any;
private focusedTreeOrList: ITree | List<any>;
private lists: (ITree | List<any>)[];
private listFocusContext: IContextKey<boolean>;
private focusChangeScheduler: RunOnceScheduler;
constructor(
@IContextKeyService contextKeyService: IContextKeyService
) {
this.listFocusContext = ListFocusContext.bindTo(contextKeyService);
this.lists = [];
this.focusChangeScheduler = new RunOnceScheduler(() => this.onFocusChange(), 50 /* delay until the focus/blur dust settles */);
}
public register(tree: ITree): IDisposable;
public register(list: List<any>): IDisposable;
public register(widget: ITree | List<any>): IDisposable {
if (this.lists.indexOf(widget) >= 0) {
throw new Error('Cannot register the same widget multiple times');
}
// Keep in our lists list
this.lists.push(widget);
// Check for currently being focused
if (widget.isDOMFocused()) {
this.onListDOMFocus(widget);
this.setFocusedList(widget);
}
const toDispose = [
widget.onDOMFocus(() => this.onListDOMFocus(widget)),
widget.onDOMBlur(() => this.onListDOMBlur(widget))
widget.onDOMFocus(() => this.focusChangeScheduler.schedule()),
widget.onDOMBlur(() => this.focusChangeScheduler.schedule())
];
// Special treatment for tree highlight mode
if (!(widget instanceof List)) {
toDispose.push(widget.onHighlightChange(() => {
if (this.focusedTreeOrList === widget && widget.getHighlight()) {
this.onListDOMBlur(widget);
} else if (widget.isDOMFocused()) {
this.onListDOMFocus(widget);
}
const tree = widget;
toDispose.push(tree.onHighlightChange(() => {
this.focusChangeScheduler.schedule();
}));
}
// Remove list once disposed
toDispose.push({
dispose: () => { this.lists.splice(this.lists.indexOf(widget), 1); }
});
return {
dispose: () => dispose(toDispose)
};
}
private onListDOMFocus(list: ITree | List<any>): void {
this.focusedTreeOrList = list;
this.listFocusContext.set(true);
private onFocusChange(): void {
let focusedList: ITree | List<any>;
for (let i = 0; i < this.lists.length; i++) {
const list = this.lists[i];
if (document.activeElement === list.getHTMLElement()) {
focusedList = list;
break;
}
}
this.setFocusedList(focusedList);
}
private onListDOMBlur(list: ITree | List<any>): void {
this.focusedTreeOrList = void 0;
this.listFocusContext.set(false);
private setFocusedList(focusedList: ITree | List<any>): void {
this.focusedTreeOrList = focusedList;
this.listFocusContext.set(!!focusedList);
}
public getFocused(): ITree | List<any> {
......
......@@ -370,8 +370,10 @@ export class ExplorerView extends CollapsibleViewletView {
// Update explorer focus context
this.toDispose.push(this.explorerViewer.onDOMFocus(() => {
this.filesExplorerFocussedContext.set(true);
this.explorerFocussedContext.set(true);
setTimeout(() => {
this.filesExplorerFocussedContext.set(true);
this.explorerFocussedContext.set(true);
}, 0 /* helps when focus jumps from one tree to another */);
}));
this.toDispose.push(this.explorerViewer.onDOMBlur(() => {
......
......@@ -126,8 +126,10 @@ export class OpenEditorsView extends AdaptiveCollapsibleViewletView {
// Update open editors focus context
this.toDispose.push(this.tree.onDOMFocus(() => {
this.openEditorsFocussedContext.set(true);
this.explorerFocussedContext.set(true);
setTimeout(() => {
this.openEditorsFocussedContext.set(true);
this.explorerFocussedContext.set(true);
}, 0 /* helps when focus jumps from one tree to another */);
}));
this.toDispose.push(this.tree.onDOMBlur(() => {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册