refactor: rewrite workspace switcher (#1267)

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2025-05-17 20:14:09 +08:00
parent bd553405c2
commit 4c1ba717a7
No known key found for this signature in database
10 changed files with 287 additions and 51 deletions

View file

@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
{
public class WorkspaceSwitcher : ObservableObject
{
public List<Workspace> VisibleWorkspaces
{
get => _visibleWorkspaces;
private set => SetProperty(ref _visibleWorkspaces, value);
}
public string SearchFilter
{
get => _searchFilter;
set
{
if (SetProperty(ref _searchFilter, value))
UpdateVisibleWorkspaces();
}
}
public Workspace SelectedWorkspace
{
get => _selectedWorkspace;
set => SetProperty(ref _selectedWorkspace, value);
}
public WorkspaceSwitcher(Launcher launcher)
{
_launcher = launcher;
UpdateVisibleWorkspaces();
}
public void ClearFilter()
{
SearchFilter = string.Empty;
}
public void Switch()
{
if (_selectedWorkspace is { })
_launcher.SwitchWorkspace(_selectedWorkspace);
_launcher.CancelWorkspaceSwitcher();
}
private void UpdateVisibleWorkspaces()
{
var visible = new List<Workspace>();
if (string.IsNullOrEmpty(_searchFilter))
{
visible.AddRange(Preferences.Instance.Workspaces);
}
else
{
foreach (var workspace in Preferences.Instance.Workspaces)
{
if (workspace.Name.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase))
visible.Add(workspace);
}
}
VisibleWorkspaces = visible;
SelectedWorkspace = visible.Count == 0 ? null : visible[0];
}
private Launcher _launcher = null;
private List<Workspace> _visibleWorkspaces = null;
private string _searchFilter = string.Empty;
private Workspace _selectedWorkspace = null;
}
}