focus to button

main
Stepan Pilipenko 3 weeks ago
parent bad21de73d
commit c7f4012004

@ -79,15 +79,15 @@ public partial class MainPage : ContentPage, INotifyPropertyChanged
private void InitializeCollectionViews()
{
LeftPanel.ItemTemplate = PanelCollectionView.CreateItemTemplate(isLeftPanel: true, page: this);
RightPanel.ItemTemplate = PanelCollectionView.CreateItemTemplate(isLeftPanel: true, page: this);
RightPanel.ItemTemplate = PanelCollectionView.CreateItemTemplate(isLeftPanel: false, page: this);
}
protected override void OnHandlerChanged()
{
base.OnHandlerChanged();
this.Focus();
}
//protected override void OnHandlerChanged()
//{
// base.OnHandlerChanged();
// this.Focus();
//}
public void HandleItemClick(FileSystemItem item, bool isLeftPanel)
{
@ -150,9 +150,9 @@ public partial class MainPage : ContentPage, INotifyPropertyChanged
}
// Переключаем фокус с указанием направления
private void MoveFocus(bool moveForward = true)
private void MoveFocus(bool moveForward, bool isLeft)
{
var allFocusable = GetFocusableElements();
var allFocusable = GetFocusableElements(isLeft);
var current = allFocusable.FirstOrDefault(x => x.IsFocused);
var next = moveForward ?
GetNextFocusable(current, allFocusable) :
@ -185,14 +185,31 @@ public partial class MainPage : ContentPage, INotifyPropertyChanged
return allElements[prevIndex];
}
private List<View> GetFocusableElements()
private List<View> GetFocusableElements(bool isLeft)
{
return this.GetVisualTreeDescendants()
.OfType<View>()
.Where(x => x.IsEnabled && x.IsVisible)
List<View>? elements = null;
if (isLeft)
{
elements = LeftPanel.GetVisualTreeDescendants().OfType<View>()
.Where(x => x.IsEnabled && x.IsVisible && x is Button)
.OrderBy(GetVisualTreeOrder) // Сортируем по порядку в визуальном дереве
.ToList();
}
else
{
elements = RightPanel.GetVisualTreeDescendants().OfType<View>()
.Where(x => x.IsEnabled && x.IsVisible && x is Button)
.OrderBy(GetVisualTreeOrder) // Сортируем по порядку в визуальном дереве
.ToList();
}
//foreach (var el in elements)
//{
// System.Diagnostics.Debug.WriteLine($"!!! {el.ToString()}");
//}
return elements;
}
private int GetVisualTreeOrder(View view)
{
@ -214,19 +231,19 @@ public partial class MainPage : ContentPage, INotifyPropertyChanged
_leftSelectedIndex = LeftItems.IndexOf(selectedItem);
_isLeftPanelActive = true;
System.Diagnostics.Debug.WriteLine($"L !!! {_leftSelectedIndex} {_lastLeftSelectedIndex}");
// System.Diagnostics.Debug.WriteLine($"L !!! {_leftSelectedIndex} {_lastLeftSelectedIndex}");
if (_lastLeftSelectedIndex < LeftItems.Count && _lastLeftSelectedIndex >= 0)
{
// Автоматически переключаем фокус при достижении границ
if (_leftSelectedIndex > _lastLeftSelectedIndex)
{
// Достигли конца списка - переходим к следующему элементу
MoveFocus(moveForward: true);
MoveFocus(moveForward: true, isLeft: true);
}
else if (_leftSelectedIndex < _lastLeftSelectedIndex)
{
// Достигли начала списка - переходим к предыдущему элементу
MoveFocus(moveForward: false);
MoveFocus(moveForward: false, isLeft: true);
}
}
@ -241,19 +258,19 @@ public partial class MainPage : ContentPage, INotifyPropertyChanged
_rightSelectedIndex = RightItems.IndexOf(selectedItem);
_isLeftPanelActive = false;
System.Diagnostics.Debug.WriteLine($"R !!! {_rightSelectedIndex} {_lastRightSelectedIndex}");
// System.Diagnostics.Debug.WriteLine($"R !!! {_rightSelectedIndex} {_lastRightSelectedIndex}");
if (_lastRightSelectedIndex < RightItems.Count && _lastRightSelectedIndex >= 0)
{
// Автоматически переключаем фокус при достижении границ
if (_rightSelectedIndex > _lastRightSelectedIndex)
{
// Достигли конца списка - переходим к следующему элементу
MoveFocus(moveForward: true);
MoveFocus(moveForward: true, isLeft: false);
}
else if (_rightSelectedIndex < _lastRightSelectedIndex)
{
// Достигли начала списка - переходим к предыдущему элементу
MoveFocus(moveForward: false);
MoveFocus(moveForward: false, isLeft: false);
}
}

Loading…
Cancel
Save