Skip to content

Commit 66676dd

Browse files
committed
refactor: all command palette should derived from ViewModels.ICommandPalette
Signed-off-by: leo <longshuang@msn.cn>
1 parent 9cfd1d4 commit 66676dd

12 files changed

+61
-57
lines changed

src/ViewModels/BlameCommandPalette.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
using System.Collections.Generic;
33
using System.Threading.Tasks;
44
using Avalonia.Threading;
5-
using CommunityToolkit.Mvvm.ComponentModel;
65

76
namespace SourceGit.ViewModels
87
{
9-
public class BlameCommandPalette : ObservableObject, IDisposable
8+
public class BlameCommandPalette : ICommandPalette
109
{
1110
public bool IsLoading
1211
{
@@ -62,7 +61,7 @@ public BlameCommandPalette(Launcher launcher, string repo)
6261
});
6362
}
6463

65-
public void Dispose()
64+
public override void Cleanup()
6665
{
6766
_launcher = null;
6867
_repo = null;

src/ViewModels/FileHistoryCommandPalette.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
using System.Collections.Generic;
33
using System.Threading.Tasks;
44
using Avalonia.Threading;
5-
using CommunityToolkit.Mvvm.ComponentModel;
65

76
namespace SourceGit.ViewModels
87
{
9-
public class FileHistoryCommandPalette : ObservableObject, IDisposable
8+
public class FileHistoryCommandPalette : ICommandPalette
109
{
1110
public bool IsLoading
1211
{
@@ -57,7 +56,7 @@ public FileHistoryCommandPalette(Launcher launcher, string repo)
5756
});
5857
}
5958

60-
public void Dispose()
59+
public override void Cleanup()
6160
{
6261
_launcher = null;
6362
_repo = null;

src/ViewModels/ICommandPalette.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using CommunityToolkit.Mvvm.ComponentModel;
3+
4+
namespace SourceGit.ViewModels
5+
{
6+
public class ICommandPalette : ObservableObject, IDisposable
7+
{
8+
public void Dispose()
9+
{
10+
Cleanup();
11+
}
12+
13+
public virtual void Cleanup()
14+
{
15+
}
16+
}
17+
}

src/ViewModels/Launcher.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public LauncherPage ActivePage
4444
}
4545
}
4646

47-
public IDisposable CommandPalette
47+
public ICommandPalette CommandPalette
4848
{
4949
get => _commandPalette;
5050
set => SetProperty(ref _commandPalette, value);
@@ -378,10 +378,11 @@ public void OpenRepositoryInTab(RepositoryNode node, LauncherPage page)
378378
ActiveWorkspace.ActiveIdx = ActiveWorkspace.Repositories.IndexOf(node.Id);
379379
}
380380

381-
public void OpenCommandPalette(IDisposable commandPalette)
381+
public void OpenCommandPalette(ICommandPalette commandPalette)
382382
{
383-
_commandPalette?.Dispose();
383+
var old = _commandPalette;
384384
CommandPalette = commandPalette;
385+
old?.Dispose();
385386
}
386387

387388
public void CancelCommandPalette()
@@ -495,6 +496,6 @@ private void UpdateTitle()
495496
private LauncherPage _activePage = null;
496497
private bool _ignoreIndexChange = false;
497498
private string _title = string.Empty;
498-
private IDisposable _commandPalette = null;
499+
private ICommandPalette _commandPalette = null;
499500
}
500501
}

src/ViewModels/LauncherPagesCommandPalette.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
using System;
22
using System.Collections.Generic;
3-
using CommunityToolkit.Mvvm.ComponentModel;
43

54
namespace SourceGit.ViewModels
65
{
7-
public class LauncherPagesCommandPalette : ObservableObject, IDisposable
6+
public class LauncherPagesCommandPalette : ICommandPalette
87
{
98
public List<LauncherPage> VisiblePages
109
{
@@ -61,7 +60,7 @@ public LauncherPagesCommandPalette(Launcher launcher)
6160
UpdateVisible();
6261
}
6362

64-
public void Dispose()
63+
public override void Cleanup()
6564
{
6665
_launcher = null;
6766
_opened.Clear();

src/ViewModels/MergeCommandPalette.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
using System;
22
using System.Collections.Generic;
3-
using CommunityToolkit.Mvvm.ComponentModel;
43

54
namespace SourceGit.ViewModels
65
{
7-
public class MergeCommandPalette : ObservableObject, IDisposable
6+
public class MergeCommandPalette : ICommandPalette
87
{
98
public List<Models.Branch> Branches
109
{
@@ -35,7 +34,7 @@ public MergeCommandPalette(Launcher launcher, Repository repo)
3534
UpdateBranches();
3635
}
3736

38-
public void Dispose()
37+
public override void Cleanup()
3938
{
4039
_launcher = null;
4140
_repo = null;

src/ViewModels/RepositoryCommandPalette.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using System;
22
using System.Collections.Generic;
3-
using CommunityToolkit.Mvvm.ComponentModel;
43

54
namespace SourceGit.ViewModels
65
{
76
public record RepositoryCommandPaletteCmd(string Name, string Label, bool AutoCloseCommandPalette, Action Action);
87

9-
public class RepositoryCommandPalette : ObservableObject, IDisposable
8+
public class RepositoryCommandPalette : ICommandPalette
109
{
1110
public List<RepositoryCommandPaletteCmd> VisibleCmds
1211
{
@@ -56,7 +55,7 @@ public RepositoryCommandPalette(Launcher launcher, Repository repo)
5655
_visibleCmds = _cmds;
5756
}
5857

59-
public void Dispose()
58+
public override void Cleanup()
6059
{
6160
_launcher = null;
6261
_repo = null;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Avalonia.Controls;
2+
using Avalonia.Controls.Templates;
3+
4+
namespace SourceGit.Views
5+
{
6+
public class CommandPaletteDataTemplates : IDataTemplate
7+
{
8+
public Control Build(object param) => App.CreateViewForViewModel(param);
9+
public bool Match(object data) => data is ViewModels.ICommandPalette;
10+
}
11+
}

src/Views/Launcher.axaml.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
using Avalonia;
44
using Avalonia.Controls;
5-
using Avalonia.Controls.Templates;
65
using Avalonia.Input;
76
using Avalonia.Interactivity;
87
using Avalonia.Media;
@@ -11,26 +10,6 @@
1110

1211
namespace SourceGit.Views
1312
{
14-
public class CommandPaletteDataTemplates : IDataTemplate
15-
{
16-
public Control Build(object param)
17-
{
18-
return App.CreateViewForViewModel(param);
19-
}
20-
21-
public bool Match(object data)
22-
{
23-
if (data is not IDisposable)
24-
return false;
25-
26-
var name = data.GetType().FullName;
27-
if (!name.EndsWith("CommandPalette", StringComparison.Ordinal) || !name.Contains(".ViewModels.", StringComparison.Ordinal))
28-
return false;
29-
30-
return true;
31-
}
32-
}
33-
3413
public partial class Launcher : ChromelessWindow
3514
{
3615
public static readonly StyledProperty<GridLength> CaptionHeightProperty =

src/Views/LauncherPage.axaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,13 @@
7070
<DataTemplate DataType="vm:Popup">
7171
<StackPanel Orientation="Vertical" Background="{DynamicResource Brush.Popup}">
7272
<!-- Popup Widget -->
73-
<ContentPresenter Margin="8,16,8,8"
74-
DataContextChanged="OnPopupDataContextChanged"
75-
IsHitTestVisible="{Binding InProgress, Converter={x:Static BoolConverters.Not}}"/>
73+
<ContentControl Margin="8,16,8,8"
74+
Content="{Binding}"
75+
IsHitTestVisible="{Binding InProgress, Converter={x:Static BoolConverters.Not}}">
76+
<ContentControl.DataTemplates>
77+
<v:PopupDataTemplates/>
78+
</ContentControl.DataTemplates>
79+
</ContentControl>
7680

7781
<!-- Options -->
7882
<StackPanel Margin="8,4,8,8"

0 commit comments

Comments
 (0)