Skip to content

Commit 32e6856

Browse files
committed
enhance: disable some MenuItems if repository that linked by node has been auto-removed from Preference (#90).
1 parent 9b5e842 commit 32e6856

File tree

3 files changed

+81
-34
lines changed

3 files changed

+81
-34
lines changed

src/ViewModels/Welcome.cs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22

33
using Avalonia.Collections;
4+
using Avalonia.Controls;
45

56
using CommunityToolkit.Mvvm.ComponentModel;
67

@@ -79,7 +80,7 @@ public void ClearSearchFilter()
7980
SearchFilter = string.Empty;
8081
}
8182

82-
public void AddFolder()
83+
public void AddRootNode()
8384
{
8485
if (PopupHost.CanCreatePopup())
8586
PopupHost.ShowPopup(new CreateGroup(null));
@@ -90,6 +91,72 @@ public void MoveNode(RepositoryNode from, RepositoryNode to)
9091
Preference.MoveNode(from, to);
9192
}
9293

94+
public ContextMenu CreateContextMenu(RepositoryNode node)
95+
{
96+
var menu = new ContextMenu();
97+
var hasRepo = Preference.FindRepository(node.Id) != null;
98+
99+
var edit = new MenuItem();
100+
edit.Header = App.Text("Welcome.Edit");
101+
edit.Icon = App.CreateMenuIcon("Icons.Edit");
102+
edit.IsEnabled = !node.IsRepository || hasRepo;
103+
edit.Click += (_, e) =>
104+
{
105+
node.Edit();
106+
e.Handled = true;
107+
};
108+
menu.Items.Add(edit);
109+
110+
if (node.IsRepository)
111+
{
112+
var explore = new MenuItem();
113+
explore.Header = App.Text("Repository.Explore");
114+
explore.Icon = App.CreateMenuIcon("Icons.Folder.Open");
115+
explore.IsEnabled = hasRepo;
116+
explore.Click += (_, e) =>
117+
{
118+
node.OpenInFileManager();
119+
e.Handled = true;
120+
};
121+
menu.Items.Add(explore);
122+
123+
var terminal = new MenuItem();
124+
terminal.Header = App.Text("Repository.Terminal");
125+
terminal.Icon = App.CreateMenuIcon("Icons.Terminal");
126+
terminal.IsEnabled = hasRepo;
127+
terminal.Click += (_, e) =>
128+
{
129+
node.OpenTerminal();
130+
e.Handled = true;
131+
};
132+
menu.Items.Add(terminal);
133+
}
134+
else
135+
{
136+
var addSubFolder = new MenuItem();
137+
addSubFolder.Header = App.Text("Welcome.AddSubFolder");
138+
addSubFolder.Icon = App.CreateMenuIcon("Icons.Folder.Add");
139+
addSubFolder.Click += (_, e) =>
140+
{
141+
node.AddSubFolder();
142+
e.Handled = true;
143+
};
144+
menu.Items.Add(addSubFolder);
145+
}
146+
147+
var delete = new MenuItem();
148+
delete.Header = App.Text("Welcome.Delete");
149+
delete.Icon = App.CreateMenuIcon("Icons.Clear");
150+
delete.Click += (_, e) =>
151+
{
152+
node.Delete();
153+
e.Handled = true;
154+
};
155+
menu.Items.Add(delete);
156+
157+
return menu;
158+
}
159+
93160
private void Referesh()
94161
{
95162
if (string.IsNullOrWhiteSpace(_searchFilter))

src/Views/Welcome.axaml

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
LostFocus="OnTreeViewLostFocus">
5858
<TreeView.ContextMenu>
5959
<ContextMenu>
60-
<MenuItem Header="{DynamicResource Text.Welcome.AddRootFolder}" Command="{Binding AddFolder}">
60+
<MenuItem Header="{DynamicResource Text.Welcome.AddRootFolder}" Command="{Binding AddRootNode}">
6161
<MenuItem.Icon>
6262
<Path Width="12" Height="12" Data="{DynamicResource Icons.Folder.Add}"/>
6363
</MenuItem.Icon>
@@ -70,37 +70,6 @@
7070
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
7171
<Setter Property="IsVisible" Value="{Binding IsVisible}"/>
7272
<Setter Property="CornerRadius" Value="4"/>
73-
<Setter Property="ContextMenu">
74-
<Setter.Value>
75-
<ContextMenu>
76-
<MenuItem Header="{DynamicResource Text.Welcome.Edit}" Command="{Binding Edit}">
77-
<MenuItem.Icon>
78-
<Path Width="12" Height="12" Data="{DynamicResource Icons.Edit}"/>
79-
</MenuItem.Icon>
80-
</MenuItem>
81-
<MenuItem Header="{DynamicResource Text.Welcome.AddSubFolder}" Command="{Binding AddSubFolder}" IsVisible="{Binding !IsRepository}">
82-
<MenuItem.Icon>
83-
<Path Width="12" Height="12" Data="{DynamicResource Icons.Folder.Add}"/>
84-
</MenuItem.Icon>
85-
</MenuItem>
86-
<MenuItem Header="{DynamicResource Text.Repository.Explore}" Command="{Binding OpenInFileManager}" IsVisible="{Binding IsRepository}">
87-
<MenuItem.Icon>
88-
<Path Width="12" Height="12" Data="{DynamicResource Icons.Folder.Open}"/>
89-
</MenuItem.Icon>
90-
</MenuItem>
91-
<MenuItem Header="{DynamicResource Text.Repository.Terminal}" Command="{Binding OpenTerminal}" IsVisible="{Binding IsRepository}">
92-
<MenuItem.Icon>
93-
<Path Width="12" Height="12" Data="{DynamicResource Icons.Terminal}"/>
94-
</MenuItem.Icon>
95-
</MenuItem>
96-
<MenuItem Header="{DynamicResource Text.Welcome.Delete}" Command="{Binding Delete}">
97-
<MenuItem.Icon>
98-
<Path Width="12" Height="12" Data="{DynamicResource Icons.Clear}"/>
99-
</MenuItem.Icon>
100-
</MenuItem>
101-
</ContextMenu>
102-
</Setter.Value>
103-
</Setter>
10473
</Style>
10574
</TreeView.Styles>
10675

@@ -109,11 +78,12 @@
10978
<Grid Height="30"
11079
ColumnDefinitions="Auto,Auto,*"
11180
Background="Transparent"
81+
Loaded="SetupTreeNodeDragAndDrop"
82+
ContextRequested="OnTreeNodeContextRequested"
11283
PointerPressed="OnPointerPressedTreeNode"
11384
PointerMoved="OnPointerMovedOverTreeNode"
11485
PointerReleased="OnPointerReleasedOnTreeNode"
11586
DoubleTapped="OnDoubleTappedTreeNode"
116-
Loaded="SetupTreeNodeDragAndDrop"
11787
ClipToBounds="True">
11888
<Path Grid.Column="0" Width="12" Height="12" Margin="0,0,8,0"
11989
Fill="{Binding Bookmark, Converter={x:Static c:BookmarkConverters.ToBrush}}"

src/Views/Welcome.axaml.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ private void SetupTreeNodeDragAndDrop(object sender, RoutedEventArgs e)
3737
}
3838
}
3939

40+
private void OnTreeNodeContextRequested(object sender, ContextRequestedEventArgs e)
41+
{
42+
if (sender is Grid grid && DataContext is ViewModels.Welcome vm)
43+
{
44+
var menu = vm.CreateContextMenu(grid.DataContext as ViewModels.RepositoryNode);
45+
menu?.Open(grid);
46+
e.Handled = true;
47+
}
48+
}
49+
4050
private void OnPointerPressedTreeNode(object sender, PointerPressedEventArgs e)
4151
{
4252
if (e.GetCurrentPoint(sender as Visual).Properties.IsLeftButtonPressed)

0 commit comments

Comments
 (0)