enhance: disable some MenuItems if repository that linked by node has been auto-removed from Preference (#90).

This commit is contained in:
leo 2024-04-28 13:51:44 +08:00
parent 9b5e8429b9
commit 32e685622b
3 changed files with 81 additions and 34 deletions

View file

@ -1,6 +1,7 @@
using System;
using Avalonia.Collections;
using Avalonia.Controls;
using CommunityToolkit.Mvvm.ComponentModel;
@ -79,7 +80,7 @@ namespace SourceGit.ViewModels
SearchFilter = string.Empty;
}
public void AddFolder()
public void AddRootNode()
{
if (PopupHost.CanCreatePopup())
PopupHost.ShowPopup(new CreateGroup(null));
@ -90,6 +91,72 @@ namespace SourceGit.ViewModels
Preference.MoveNode(from, to);
}
public ContextMenu CreateContextMenu(RepositoryNode node)
{
var menu = new ContextMenu();
var hasRepo = Preference.FindRepository(node.Id) != null;
var edit = new MenuItem();
edit.Header = App.Text("Welcome.Edit");
edit.Icon = App.CreateMenuIcon("Icons.Edit");
edit.IsEnabled = !node.IsRepository || hasRepo;
edit.Click += (_, e) =>
{
node.Edit();
e.Handled = true;
};
menu.Items.Add(edit);
if (node.IsRepository)
{
var explore = new MenuItem();
explore.Header = App.Text("Repository.Explore");
explore.Icon = App.CreateMenuIcon("Icons.Folder.Open");
explore.IsEnabled = hasRepo;
explore.Click += (_, e) =>
{
node.OpenInFileManager();
e.Handled = true;
};
menu.Items.Add(explore);
var terminal = new MenuItem();
terminal.Header = App.Text("Repository.Terminal");
terminal.Icon = App.CreateMenuIcon("Icons.Terminal");
terminal.IsEnabled = hasRepo;
terminal.Click += (_, e) =>
{
node.OpenTerminal();
e.Handled = true;
};
menu.Items.Add(terminal);
}
else
{
var addSubFolder = new MenuItem();
addSubFolder.Header = App.Text("Welcome.AddSubFolder");
addSubFolder.Icon = App.CreateMenuIcon("Icons.Folder.Add");
addSubFolder.Click += (_, e) =>
{
node.AddSubFolder();
e.Handled = true;
};
menu.Items.Add(addSubFolder);
}
var delete = new MenuItem();
delete.Header = App.Text("Welcome.Delete");
delete.Icon = App.CreateMenuIcon("Icons.Clear");
delete.Click += (_, e) =>
{
node.Delete();
e.Handled = true;
};
menu.Items.Add(delete);
return menu;
}
private void Referesh()
{
if (string.IsNullOrWhiteSpace(_searchFilter))

View file

@ -57,7 +57,7 @@
LostFocus="OnTreeViewLostFocus">
<TreeView.ContextMenu>
<ContextMenu>
<MenuItem Header="{DynamicResource Text.Welcome.AddRootFolder}" Command="{Binding AddFolder}">
<MenuItem Header="{DynamicResource Text.Welcome.AddRootFolder}" Command="{Binding AddRootNode}">
<MenuItem.Icon>
<Path Width="12" Height="12" Data="{DynamicResource Icons.Folder.Add}"/>
</MenuItem.Icon>
@ -70,37 +70,6 @@
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
<Setter Property="IsVisible" Value="{Binding IsVisible}"/>
<Setter Property="CornerRadius" Value="4"/>
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="{DynamicResource Text.Welcome.Edit}" Command="{Binding Edit}">
<MenuItem.Icon>
<Path Width="12" Height="12" Data="{DynamicResource Icons.Edit}"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="{DynamicResource Text.Welcome.AddSubFolder}" Command="{Binding AddSubFolder}" IsVisible="{Binding !IsRepository}">
<MenuItem.Icon>
<Path Width="12" Height="12" Data="{DynamicResource Icons.Folder.Add}"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="{DynamicResource Text.Repository.Explore}" Command="{Binding OpenInFileManager}" IsVisible="{Binding IsRepository}">
<MenuItem.Icon>
<Path Width="12" Height="12" Data="{DynamicResource Icons.Folder.Open}"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="{DynamicResource Text.Repository.Terminal}" Command="{Binding OpenTerminal}" IsVisible="{Binding IsRepository}">
<MenuItem.Icon>
<Path Width="12" Height="12" Data="{DynamicResource Icons.Terminal}"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="{DynamicResource Text.Welcome.Delete}" Command="{Binding Delete}">
<MenuItem.Icon>
<Path Width="12" Height="12" Data="{DynamicResource Icons.Clear}"/>
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</TreeView.Styles>
@ -109,11 +78,12 @@
<Grid Height="30"
ColumnDefinitions="Auto,Auto,*"
Background="Transparent"
Loaded="SetupTreeNodeDragAndDrop"
ContextRequested="OnTreeNodeContextRequested"
PointerPressed="OnPointerPressedTreeNode"
PointerMoved="OnPointerMovedOverTreeNode"
PointerReleased="OnPointerReleasedOnTreeNode"
DoubleTapped="OnDoubleTappedTreeNode"
Loaded="SetupTreeNodeDragAndDrop"
ClipToBounds="True">
<Path Grid.Column="0" Width="12" Height="12" Margin="0,0,8,0"
Fill="{Binding Bookmark, Converter={x:Static c:BookmarkConverters.ToBrush}}"

View file

@ -37,6 +37,16 @@ namespace SourceGit.Views
}
}
private void OnTreeNodeContextRequested(object sender, ContextRequestedEventArgs e)
{
if (sender is Grid grid && DataContext is ViewModels.Welcome vm)
{
var menu = vm.CreateContextMenu(grid.DataContext as ViewModels.RepositoryNode);
menu?.Open(grid);
e.Handled = true;
}
}
private void OnPointerPressedTreeNode(object sender, PointerPressedEventArgs e)
{
if (e.GetCurrentPoint(sender as Visual).Properties.IsLeftButtonPressed)