UNPKG

5.33 kBTypeScriptView Raw
1import { IDisposable } from '@phosphor/disposable';
2import { ISignal } from '@phosphor/signaling';
3import { Widget } from './widget';
4/**
5 * A class which tracks focus among a set of widgets.
6 *
7 * This class is useful when code needs to keep track of the most
8 * recently focused widget(s) among a set of related widgets.
9 */
10export declare class FocusTracker<T extends Widget> implements IDisposable {
11 /**
12 * Construct a new focus tracker.
13 */
14 constructor();
15 /**
16 * Dispose of the resources held by the tracker.
17 */
18 dispose(): void;
19 /**
20 * A signal emitted when the current widget has changed.
21 */
22 readonly currentChanged: ISignal<this, FocusTracker.IChangedArgs<T>>;
23 /**
24 * A signal emitted when the active widget has changed.
25 */
26 readonly activeChanged: ISignal<this, FocusTracker.IChangedArgs<T>>;
27 /**
28 * A flag indicating whether the tracker is disposed.
29 */
30 readonly isDisposed: boolean;
31 /**
32 * The current widget in the tracker.
33 *
34 * #### Notes
35 * The current widget is the widget among the tracked widgets which
36 * has the *descendant node* which has most recently been focused.
37 *
38 * The current widget will not be updated if the node loses focus. It
39 * will only be updated when a different tracked widget gains focus.
40 *
41 * If the current widget is removed from the tracker, the previous
42 * current widget will be restored.
43 *
44 * This behavior is intended to follow a user's conceptual model of
45 * a semantically "current" widget, where the "last thing of type X"
46 * to be interacted with is the "current instance of X", regardless
47 * of whether that instance still has focus.
48 */
49 readonly currentWidget: T | null;
50 /**
51 * The active widget in the tracker.
52 *
53 * #### Notes
54 * The active widget is the widget among the tracked widgets which
55 * has the *descendant node* which is currently focused.
56 */
57 readonly activeWidget: T | null;
58 /**
59 * A read only array of the widgets being tracked.
60 */
61 readonly widgets: ReadonlyArray<T>;
62 /**
63 * Get the focus number for a particular widget in the tracker.
64 *
65 * @param widget - The widget of interest.
66 *
67 * @returns The focus number for the given widget, or `-1` if the
68 * widget has not had focus since being added to the tracker, or
69 * is not contained by the tracker.
70 *
71 * #### Notes
72 * The focus number indicates the relative order in which the widgets
73 * have gained focus. A widget with a larger number has gained focus
74 * more recently than a widget with a smaller number.
75 *
76 * The `currentWidget` will always have the largest focus number.
77 *
78 * All widgets start with a focus number of `-1`, which indicates that
79 * the widget has not been focused since being added to the tracker.
80 */
81 focusNumber(widget: T): number;
82 /**
83 * Test whether the focus tracker contains a given widget.
84 *
85 * @param widget - The widget of interest.
86 *
87 * @returns `true` if the widget is tracked, `false` otherwise.
88 */
89 has(widget: T): boolean;
90 /**
91 * Add a widget to the focus tracker.
92 *
93 * @param widget - The widget of interest.
94 *
95 * #### Notes
96 * A widget will be automatically removed from the tracker if it
97 * is disposed after being added.
98 *
99 * If the widget is already tracked, this is a no-op.
100 */
101 add(widget: T): void;
102 /**
103 * Remove a widget from the focus tracker.
104 *
105 * #### Notes
106 * If the widget is the `currentWidget`, the previous current widget
107 * will become the new `currentWidget`.
108 *
109 * A widget will be automatically removed from the tracker if it
110 * is disposed after being added.
111 *
112 * If the widget is not tracked, this is a no-op.
113 */
114 remove(widget: T): void;
115 /**
116 * Handle the DOM events for the focus tracker.
117 *
118 * @param event - The DOM event sent to the panel.
119 *
120 * #### Notes
121 * This method implements the DOM `EventListener` interface and is
122 * called in response to events on the tracked nodes. It should
123 * not be called directly by user code.
124 */
125 handleEvent(event: Event): void;
126 /**
127 * Set the current and active widgets for the tracker.
128 */
129 private _setWidgets;
130 /**
131 * Handle the `'focus'` event for a tracked widget.
132 */
133 private _evtFocus;
134 /**
135 * Handle the `'blur'` event for a tracked widget.
136 */
137 private _evtBlur;
138 /**
139 * Handle the `disposed` signal for a tracked widget.
140 */
141 private _onWidgetDisposed;
142 private _counter;
143 private _widgets;
144 private _activeWidget;
145 private _currentWidget;
146 private _numbers;
147 private _nodes;
148 private _activeChanged;
149 private _currentChanged;
150}
151/**
152 * The namespace for the `FocusTracker` class statics.
153 */
154export declare namespace FocusTracker {
155 /**
156 * An arguments object for the changed signals.
157 */
158 interface IChangedArgs<T extends Widget> {
159 /**
160 * The old value for the widget.
161 */
162 oldValue: T | null;
163 /**
164 * The new value for the widget.
165 */
166 newValue: T | null;
167 }
168}