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

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

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