mirror of
https://github.com/sourcegit-scm/sourcegit
synced 2025-05-30 08:34:59 +00:00
refactor: rewrite the welcome page since the original TreeView
has many limitations (#391)
This commit is contained in:
parent
af6d2cc725
commit
38e2e0f3f4
12 changed files with 227 additions and 243 deletions
|
@ -11,7 +11,9 @@
|
|||
<Grid RowDefinitions="*,36">
|
||||
<Grid Grid.Row="0" Margin="0,8" ColumnDefinitions="*,600,*">
|
||||
<Grid Grid.Column="1" RowDefinitions="Auto,*">
|
||||
<!-- Search Box -->
|
||||
<TextBox Grid.Row="0"
|
||||
x:Name="SearchBox"
|
||||
Height="32"
|
||||
Padding="0"
|
||||
CornerRadius="16"
|
||||
|
@ -19,7 +21,6 @@
|
|||
BorderThickness="1"
|
||||
Background="{DynamicResource Brush.Contents}"
|
||||
Watermark="{DynamicResource Text.Welcome.Search}"
|
||||
KeyDown="OnSearchBoxKeyDown"
|
||||
VerticalContentAlignment="Center"
|
||||
Text="{Binding SearchFilter, Mode=TwoWay}"
|
||||
v:AutoFocusBehaviour.IsEnabled="True">
|
||||
|
@ -34,30 +35,32 @@
|
|||
</TextBox.InnerRightContent>
|
||||
</TextBox>
|
||||
|
||||
<TreeView Grid.Row="1"
|
||||
x:Name="ReposTree"
|
||||
Margin="0,8,8,0"
|
||||
ItemsSource="{Binding RepositoryNodes}"
|
||||
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
|
||||
ScrollViewer.VerticalScrollBarVisibility="Auto"
|
||||
Loaded="SetupTreeViewDragAndDrop"
|
||||
LostFocus="OnTreeViewLostFocus"
|
||||
SelectionChanged="OnTreeViewSelectionChanged"
|
||||
KeyDown="OnTreeViewKeyDown">
|
||||
<TreeView.ContextMenu>
|
||||
<ContextMenu>
|
||||
<MenuItem Header="{DynamicResource Text.Welcome.AddRootFolder}" Command="{Binding AddRootNode}">
|
||||
<MenuItem.Icon>
|
||||
<Path Width="12" Height="12" Data="{DynamicResource Icons.Folder.Add}"/>
|
||||
</MenuItem.Icon>
|
||||
</MenuItem>
|
||||
</ContextMenu>
|
||||
</TreeView.ContextMenu>
|
||||
|
||||
<TreeView.Styles>
|
||||
<Style Selector="TreeViewItem" x:DataType="vm:RepositoryNode">
|
||||
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
|
||||
<Setter Property="IsVisible" Value="{Binding IsVisible}"/>
|
||||
<!-- Repository Tree -->
|
||||
<ListBox Grid.Row="1"
|
||||
x:Name="TreeContainer"
|
||||
Margin="0,8,8,0"
|
||||
Focusable="True"
|
||||
Background="Transparent"
|
||||
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
|
||||
ScrollViewer.VerticalScrollBarVisibility="Auto"
|
||||
ItemsSource="{Binding Rows}"
|
||||
SelectionMode="Single"
|
||||
Loaded="SetupTreeViewDragAndDrop"
|
||||
LostFocus="OnTreeViewLostFocus"
|
||||
KeyDown="OnTreeViewKeyDown">
|
||||
<ListBox.Styles>
|
||||
<Style Selector="ListBox">
|
||||
<Setter Property="FocusAdorner">
|
||||
<FocusAdornerTemplate>
|
||||
<Border Background="Transparent" BorderThickness="0"/>
|
||||
</FocusAdornerTemplate>
|
||||
</Setter>
|
||||
</Style>
|
||||
|
||||
<Style Selector="ListBoxItem" x:DataType="vm:RepositoryNode">
|
||||
<Setter Property="Margin" Value="0"/>
|
||||
<Setter Property="Padding" Value="0"/>
|
||||
<Setter Property="Height" Value="30"/>
|
||||
<Setter Property="CornerRadius" Value="4"/>
|
||||
<Setter Property="FocusAdorner">
|
||||
<FocusAdornerTemplate>
|
||||
|
@ -65,13 +68,29 @@
|
|||
</FocusAdornerTemplate>
|
||||
</Setter>
|
||||
</Style>
|
||||
</TreeView.Styles>
|
||||
</ListBox.Styles>
|
||||
|
||||
<TreeView.ItemTemplate>
|
||||
<TreeDataTemplate ItemsSource="{Binding SubNodes}">
|
||||
<Grid Height="30"
|
||||
ColumnDefinitions="18,Auto,*"
|
||||
Background="Transparent"
|
||||
<ListBox.ItemsPanel>
|
||||
<ItemsPanelTemplate>
|
||||
<VirtualizingStackPanel Orientation="Vertical"/>
|
||||
</ItemsPanelTemplate>
|
||||
</ListBox.ItemsPanel>
|
||||
|
||||
<ListBox.ContextMenu>
|
||||
<ContextMenu>
|
||||
<MenuItem Header="{DynamicResource Text.Welcome.AddRootFolder}" Command="{Binding AddRootNode}">
|
||||
<MenuItem.Icon>
|
||||
<Path Width="12" Height="12" Data="{DynamicResource Icons.Folder.Add}"/>
|
||||
</MenuItem.Icon>
|
||||
</MenuItem>
|
||||
</ContextMenu>
|
||||
</ListBox.ContextMenu>
|
||||
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate DataType="vm:RepositoryNode">
|
||||
<Grid Background="Transparent"
|
||||
ColumnDefinitions="16,18,Auto,*"
|
||||
Margin="{Binding Depth, Converter={x:Static c:IntConverters.ToTreeMargin}}"
|
||||
Loaded="SetupTreeNodeDragAndDrop"
|
||||
ContextRequested="OnTreeNodeContextRequested"
|
||||
PointerPressed="OnPointerPressedTreeNode"
|
||||
|
@ -79,14 +98,21 @@
|
|||
PointerReleased="OnPointerReleasedOnTreeNode"
|
||||
DoubleTapped="OnDoubleTappedTreeNode"
|
||||
ClipToBounds="True">
|
||||
<Path Grid.Column="0"
|
||||
<v:RepositoryTreeNodeToggleButton Grid.Column="0"
|
||||
Classes="tree_expander"
|
||||
Focusable="False"
|
||||
HorizontalAlignment="Center"
|
||||
IsChecked="{Binding IsExpanded, Mode=OneWay}"
|
||||
IsVisible="{Binding !IsRepository}"/>
|
||||
|
||||
<Path Grid.Column="1"
|
||||
Width="14" Height="14"
|
||||
Fill="{Binding Bookmark, Converter={x:Static c:BookmarkConverters.ToBrush}}"
|
||||
HorizontalAlignment="Center"
|
||||
Data="{StaticResource Icons.Bookmark}"
|
||||
IsVisible="{Binding IsRepository}"/>
|
||||
|
||||
<ToggleButton Grid.Column="0"
|
||||
<ToggleButton Grid.Column="1"
|
||||
Classes="folder"
|
||||
Focusable="False"
|
||||
Width="14" Height="14"
|
||||
|
@ -95,8 +121,11 @@
|
|||
IsChecked="{Binding IsExpanded}"
|
||||
IsVisible="{Binding !IsRepository}"/>
|
||||
|
||||
<TextBlock Grid.Column="1" Classes="primary" VerticalAlignment="Center" Text="{Binding Name}"/>
|
||||
<TextBlock Grid.Column="2"
|
||||
<TextBlock Grid.Column="2"
|
||||
Classes="primary"
|
||||
VerticalAlignment="Center"
|
||||
Text="{Binding Name}"/>
|
||||
<TextBlock Grid.Column="3"
|
||||
Classes="primary"
|
||||
Margin="8,0"
|
||||
HorizontalAlignment="Right" VerticalAlignment="Center"
|
||||
|
@ -104,12 +133,13 @@
|
|||
Text="{Binding Id}"
|
||||
IsVisible="{Binding IsRepository}"/>
|
||||
</Grid>
|
||||
</TreeDataTemplate>
|
||||
</TreeView.ItemTemplate>
|
||||
</TreeView>
|
||||
</DataTemplate>
|
||||
</ListBox.ItemTemplate>
|
||||
</ListBox>
|
||||
</Grid>
|
||||
</Grid>
|
||||
|
||||
<!-- Tips -->
|
||||
<TextBlock Grid.Row="1"
|
||||
Classes="italic"
|
||||
Margin="0,0,8,0"
|
||||
|
|
|
@ -1,13 +1,31 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.VisualTree;
|
||||
|
||||
namespace SourceGit.Views
|
||||
{
|
||||
public class RepositoryTreeNodeToggleButton : ToggleButton
|
||||
{
|
||||
protected override Type StyleKeyOverride => typeof(ToggleButton);
|
||||
|
||||
protected override void OnPointerPressed(PointerPressedEventArgs e)
|
||||
{
|
||||
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed &&
|
||||
DataContext is ViewModels.RepositoryNode { IsRepository: false } node)
|
||||
{
|
||||
ViewModels.Welcome.Instance.ToggleNodeIsExpanded(node);
|
||||
}
|
||||
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Welcome : UserControl
|
||||
{
|
||||
public Welcome()
|
||||
|
@ -15,9 +33,30 @@ namespace SourceGit.Views
|
|||
InitializeComponent();
|
||||
}
|
||||
|
||||
protected override void OnKeyDown(KeyEventArgs e)
|
||||
{
|
||||
base.OnKeyDown(e);
|
||||
|
||||
if (!e.Handled)
|
||||
{
|
||||
if (e.Key == Key.Down && ViewModels.Welcome.Instance.Rows.Count > 0)
|
||||
{
|
||||
TreeContainer.SelectedIndex = 0;
|
||||
TreeContainer.Focus(NavigationMethod.Directional);
|
||||
e.Handled = true;
|
||||
}
|
||||
else if (e.Key == Key.F &&
|
||||
((OperatingSystem.IsMacOS() && e.KeyModifiers.HasFlag(KeyModifiers.Meta)) ||
|
||||
(!OperatingSystem.IsMacOS() && e.KeyModifiers.HasFlag(KeyModifiers.Control))))
|
||||
{
|
||||
SearchBox.Focus();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SetupTreeViewDragAndDrop(object sender, RoutedEventArgs _)
|
||||
{
|
||||
if (sender is TreeView view)
|
||||
if (sender is ListBox view)
|
||||
{
|
||||
DragDrop.SetAllowDrop(view, true);
|
||||
view.AddHandler(DragDrop.DragOverEvent, DragOverTreeView);
|
||||
|
@ -35,67 +74,25 @@ namespace SourceGit.Views
|
|||
}
|
||||
}
|
||||
|
||||
private void OnSearchBoxKeyDown(object sender, KeyEventArgs e)
|
||||
private void OnTreeViewKeyDown(object sender, KeyEventArgs e)
|
||||
{
|
||||
if (e.Key == Key.Down || e.Key == Key.FnDownArrow)
|
||||
if (TreeContainer.SelectedItem is ViewModels.RepositoryNode node && e.Key == Key.Enter)
|
||||
{
|
||||
var containers = ReposTree.GetRealizedContainers();
|
||||
if (containers == null)
|
||||
return;
|
||||
|
||||
foreach (var c in containers)
|
||||
if (node.IsRepository)
|
||||
{
|
||||
if (c is TreeViewItem { IsVisible: true } item)
|
||||
{
|
||||
ReposTree.SelectedItem = item.DataContext;
|
||||
break;
|
||||
}
|
||||
var parent = this.FindAncestorOfType<Launcher>();
|
||||
if (parent is { DataContext: ViewModels.Launcher launcher })
|
||||
launcher.OpenRepositoryInTab(node, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
ViewModels.Welcome.Instance.ToggleNodeIsExpanded(node);
|
||||
}
|
||||
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTreeViewKeyDown(object sender, KeyEventArgs e)
|
||||
{
|
||||
if (ReposTree.SelectedItem is ViewModels.RepositoryNode node)
|
||||
{
|
||||
if (e.Key == Key.Space && node.IsRepository)
|
||||
{
|
||||
var parent = this.FindAncestorOfType<Launcher>();
|
||||
if (parent?.DataContext is ViewModels.Launcher launcher)
|
||||
launcher.OpenRepositoryInTab(node, null);
|
||||
|
||||
e.Handled = true;
|
||||
}
|
||||
else if (e.Key == Key.Down)
|
||||
{
|
||||
var next = ViewModels.Welcome.Instance.GetNextVisible(node);
|
||||
if (next != null)
|
||||
ReposTree.SelectedItem = next;
|
||||
|
||||
e.Handled = true;
|
||||
}
|
||||
else if (e.Key == Key.Up)
|
||||
{
|
||||
var prev = ViewModels.Welcome.Instance.GetPrevVisible(node);
|
||||
if (prev != null)
|
||||
ReposTree.SelectedItem = prev;
|
||||
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTreeViewSelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
if (ReposTree.SelectedItem is ViewModels.RepositoryNode node)
|
||||
{
|
||||
var item = FindTreeViewItemByNode(node, ReposTree);
|
||||
item?.Focus(NavigationMethod.Directional);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTreeNodeContextRequested(object sender, ContextRequestedEventArgs e)
|
||||
{
|
||||
if (sender is Grid grid)
|
||||
|
@ -256,16 +253,21 @@ namespace SourceGit.Views
|
|||
|
||||
private void OnDoubleTappedTreeNode(object sender, TappedEventArgs e)
|
||||
{
|
||||
var grid = sender as Grid;
|
||||
var to = grid?.DataContext as ViewModels.RepositoryNode;
|
||||
if (to is not { IsRepository: true })
|
||||
return;
|
||||
if (sender is Grid { DataContext: ViewModels.RepositoryNode node })
|
||||
{
|
||||
if (node.IsRepository)
|
||||
{
|
||||
var parent = this.FindAncestorOfType<Launcher>();
|
||||
if (parent is { DataContext: ViewModels.Launcher launcher })
|
||||
launcher.OpenRepositoryInTab(node, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
ViewModels.Welcome.Instance.ToggleNodeIsExpanded(node);
|
||||
}
|
||||
|
||||
var parent = this.FindAncestorOfType<Launcher>();
|
||||
if (parent?.DataContext is ViewModels.Launcher launcher)
|
||||
launcher.OpenRepositoryInTab(to, null);
|
||||
|
||||
e.Handled = true;
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void OpenOrInitRepository(string path, ViewModels.RepositoryNode parent = null)
|
||||
|
@ -287,30 +289,12 @@ namespace SourceGit.Views
|
|||
|
||||
var normalizedPath = root.Replace("\\", "/");
|
||||
var node = ViewModels.Preference.Instance.FindOrAddNodeByRepositoryPath(normalizedPath, parent, true);
|
||||
ViewModels.Welcome.Instance.Refresh();
|
||||
|
||||
var launcher = this.FindAncestorOfType<Launcher>()?.DataContext as ViewModels.Launcher;
|
||||
launcher?.OpenRepositoryInTab(node, launcher.ActivePage);
|
||||
}
|
||||
|
||||
private TreeViewItem FindTreeViewItemByNode(ViewModels.RepositoryNode node, ItemsControl container)
|
||||
{
|
||||
var items = container.GetRealizedContainers();
|
||||
|
||||
foreach (var item in items)
|
||||
{
|
||||
if (item is TreeViewItem { DataContext: ViewModels.RepositoryNode test } treeViewItem)
|
||||
{
|
||||
if (test == node)
|
||||
return treeViewItem;
|
||||
|
||||
var child = FindTreeViewItemByNode(node, treeViewItem);
|
||||
if (child != null)
|
||||
return child;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private bool _pressedTreeNode = false;
|
||||
private Point _pressedTreeNodePosition = new Point();
|
||||
private bool _startDragTreeNode = false;
|
||||
|
|
|
@ -56,6 +56,8 @@ namespace SourceGit.Views
|
|||
|
||||
var normalizedPath = root.Replace("\\", "/");
|
||||
var node = ViewModels.Preference.Instance.FindOrAddNodeByRepositoryPath(normalizedPath, parent, false);
|
||||
ViewModels.Welcome.Instance.Refresh();
|
||||
|
||||
var launcher = this.FindAncestorOfType<Launcher>()?.DataContext as ViewModels.Launcher;
|
||||
launcher?.OpenRepositoryInTab(node, launcher.ActivePage);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue