mirror of
https://github.com/sourcegit-scm/sourcegit
synced 2025-05-20 11:44:59 +00:00
refactor: rewrite workspace switcher (#1267)
Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
parent
bd553405c2
commit
4c1ba717a7
10 changed files with 287 additions and 51 deletions
75
src/ViewModels/WorkspaceSwitcher.cs
Normal file
75
src/ViewModels/WorkspaceSwitcher.cs
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue