public void RestorePrevious(FocusRecord? hint) { FocusRecord record; Control control; do { if (_previous.Count == 0) { control = null; break; } record = _previous[_previous.Count - 1]; _previous.RemoveAt(_previous.Count - 1); control = record.Control; } while ((control == null || control.IsDisposed) || (hint != null && hint.Value.ID < record.ID)); Focus(control, false); }
protected virtual void Focus(Control control, bool rememberPrevious) { if (control == _focused) return; // clear buffers _unfocused.Clear(); _newFocused.Clear(); if (control != null && !control.LikesHavingFocus) { foreach (var item in control.Children.OrderBy(_byFocusPriority)) { if (item.LikesHavingFocus) { control = item; break; } } } // find all old controls being unfocused for (var c = _focused; c != null; c = c.Parent) _unfocused.Add(c); // find all new controls being focused for (var c = control; c != null; c = c.Parent) _newFocused.Add(c); var newRoot = _newFocused.Count > 0 ? _newFocused[_newFocused.Count - 1] : null; // remove all the common controls // walk both lists backwards (from the shared root) until the trees diverge for (int i = _newFocused.Count - 1; i >= 0; i--) { if (_unfocused.Count > 0 && _newFocused[i] == _unfocused[_unfocused.Count - 1]) { _newFocused.RemoveAt(i); _unfocused.RemoveAt(_unfocused.Count - 1); } else { break; } } // walk through the old controls, from leaf towards root, unfocusing them for (int i = 0; i < _unfocused.Count; i++) { var u = _unfocused[i]; _focused = u; RemoveFocus(u); u.FocusedCount--; // quit if FocusChanged focused another control if (_focused != u) return; } // walk through the new controls, from root to leaf, focusing them for (int i = _newFocused.Count - 1; i >= 0; i--) { var f = _newFocused[i]; _focused = f; AddFocus(_newFocused[i]); _newFocused[i].FocusedCount++; // quit if FocusChanged focused another control if (_focused != f) return; } // push old control onto stack if (rememberPrevious && _focused != null) { unchecked { _idCounter++; } var record = new FocusRecord() { ID = _idCounter, Control = _focused }; _previous.Add(record); } // remember new control //focused = control; _root = newRoot; CompactPreviousList(); }
protected virtual void Focus(Control control, bool rememberPrevious) { if (control == focused) { return; } // clear buffers unfocused.Clear(); newFocused.Clear(); if (control != null && !control.LikesHavingFocus) { foreach (var item in control.Children.OrderBy(byFocusPriority)) { if (item.LikesHavingFocus) { control = item; break; } } } // find all old controls being unfocused for (var c = focused; c != null; c = c.Parent) { unfocused.Add(c); } // find all new controls being focused for (var c = control; c != null; c = c.Parent) { newFocused.Add(c); } var newRoot = newFocused.Count > 0 ? newFocused[newFocused.Count - 1] : null; // remove all the common controls // walk both lists backwards (from the shared root) until the trees diverge for (int i = newFocused.Count - 1; i >= 0; i--) { if (unfocused.Count > 0 && newFocused[i] == unfocused[unfocused.Count - 1]) { newFocused.RemoveAt(i); unfocused.RemoveAt(unfocused.Count - 1); } else { break; } } var currentlyFocused = focused; // walk through the old controls, from leaf towards root, unfocusing them for (int i = 0; i < unfocused.Count; i++) { var u = unfocused[i]; focused = u; RemoveFocus(u); u.FocusedCount--; // quit if FocusChanged focused another control if (focused != u) { return; } } // walk through the new controls, from root to leaf, focusing them for (int i = newFocused.Count - 1; i >= 0; i--) { var f = newFocused[i]; focused = f; AddFocus(newFocused[i]); newFocused[i].FocusedCount++; // quit if FocusChanged focused another control if (focused != f) { return; } } // push old control onto stack if (rememberPrevious && focused != null) { unchecked { idCounter++; } var record = new FocusRecord() { ID = idCounter, Control = focused }; previous.Add(record); } // remember new control //focused = control; root = newRoot; CompactPreviousList(); }