mirror of
https://github.com/sourcegit-scm/sourcegit
synced 2025-05-19 19:34:58 +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
|
@ -386,7 +386,7 @@
|
|||
<x:String x:Key="Text.Hotkeys.Global.GotoPrevTab" xml:space="preserve">Go to previous page</x:String>
|
||||
<x:String x:Key="Text.Hotkeys.Global.NewTab" xml:space="preserve">Create new page</x:String>
|
||||
<x:String x:Key="Text.Hotkeys.Global.OpenPreferences" xml:space="preserve">Open Preferences dialog</x:String>
|
||||
<x:String x:Key="Text.Hotkeys.Global.SwitchWorkspace" xml:space="preserve">Switch to corresponding workspace</x:String>
|
||||
<x:String x:Key="Text.Hotkeys.Global.SwitchWorkspace" xml:space="preserve">Switch active workspace</x:String>
|
||||
<x:String x:Key="Text.Hotkeys.Repo" xml:space="preserve">REPOSITORY</x:String>
|
||||
<x:String x:Key="Text.Hotkeys.Repo.Commit" xml:space="preserve">Commit staged changes</x:String>
|
||||
<x:String x:Key="Text.Hotkeys.Repo.CommitAndPush" xml:space="preserve">Commit and push staged changes</x:String>
|
||||
|
@ -635,6 +635,7 @@
|
|||
<x:String x:Key="Text.Repository.UseRelativeTimeInHistories" xml:space="preserve">Use relative time in histories</x:String>
|
||||
<x:String x:Key="Text.Repository.ViewLogs" xml:space="preserve">View Logs</x:String>
|
||||
<x:String x:Key="Text.Repository.Visit" xml:space="preserve">Visit '{0}' in Browser</x:String>
|
||||
<x:String x:Key="Text.Repository.WorkspaceSwitcher" xml:space="preserve">Switch Workspace</x:String>
|
||||
<x:String x:Key="Text.Repository.Worktrees" xml:space="preserve">WORKTREES</x:String>
|
||||
<x:String x:Key="Text.Repository.Worktrees.Add" xml:space="preserve">Add Worktree</x:String>
|
||||
<x:String x:Key="Text.Repository.Worktrees.Prune" xml:space="preserve">Prune</x:String>
|
||||
|
|
|
@ -639,6 +639,7 @@
|
|||
<x:String x:Key="Text.Repository.UseRelativeTimeInHistories" xml:space="preserve">在提交列表中使用相对时间</x:String>
|
||||
<x:String x:Key="Text.Repository.ViewLogs" xml:space="preserve">查看命令日志</x:String>
|
||||
<x:String x:Key="Text.Repository.Visit" xml:space="preserve">访问远程仓库 '{0}'</x:String>
|
||||
<x:String x:Key="Text.Repository.WorkspaceSwitcher" xml:space="preserve">切换工作区</x:String>
|
||||
<x:String x:Key="Text.Repository.Worktrees" xml:space="preserve">工作树列表</x:String>
|
||||
<x:String x:Key="Text.Repository.Worktrees.Add" xml:space="preserve">新增工作树</x:String>
|
||||
<x:String x:Key="Text.Repository.Worktrees.Prune" xml:space="preserve">清理</x:String>
|
||||
|
|
|
@ -639,6 +639,7 @@
|
|||
<x:String x:Key="Text.Repository.UseRelativeTimeInHistories" xml:space="preserve">在提交列表中使用相對時間</x:String>
|
||||
<x:String x:Key="Text.Repository.ViewLogs" xml:space="preserve">檢視 Git 指令記錄</x:String>
|
||||
<x:String x:Key="Text.Repository.Visit" xml:space="preserve">檢視遠端存放庫 '{0}'</x:String>
|
||||
<x:String x:Key="Text.Repository.WorkspaceSwitcher" xml:space="preserve">切換工作區</x:String>
|
||||
<x:String x:Key="Text.Repository.Worktrees" xml:space="preserve">工作區列表</x:String>
|
||||
<x:String x:Key="Text.Repository.Worktrees.Add" xml:space="preserve">新增工作區</x:String>
|
||||
<x:String x:Key="Text.Repository.Worktrees.Prune" xml:space="preserve">清理</x:String>
|
||||
|
|
|
@ -23,6 +23,12 @@ namespace SourceGit.ViewModels
|
|||
private set;
|
||||
}
|
||||
|
||||
public WorkspaceSwitcher WorkspaceSwitcher
|
||||
{
|
||||
get => _workspaceSwitcher;
|
||||
set => SetProperty(ref _workspaceSwitcher, value);
|
||||
}
|
||||
|
||||
public Workspace ActiveWorkspace
|
||||
{
|
||||
get => _activeWorkspace;
|
||||
|
@ -130,8 +136,21 @@ namespace SourceGit.ViewModels
|
|||
_ignoreIndexChange = false;
|
||||
}
|
||||
|
||||
public void OpenWorkspaceSwitcher()
|
||||
{
|
||||
WorkspaceSwitcher = new WorkspaceSwitcher(this);
|
||||
}
|
||||
|
||||
public void CancelWorkspaceSwitcher()
|
||||
{
|
||||
WorkspaceSwitcher = null;
|
||||
}
|
||||
|
||||
public void SwitchWorkspace(Workspace to)
|
||||
{
|
||||
if (to.IsActive)
|
||||
return;
|
||||
|
||||
foreach (var one in Pages)
|
||||
{
|
||||
if (!one.CanCreatePopup() || one.Data is Repository { IsAutoFetching: true })
|
||||
|
@ -599,5 +618,6 @@ namespace SourceGit.ViewModels
|
|||
private LauncherPage _activePage = null;
|
||||
private bool _ignoreIndexChange = false;
|
||||
private string _title = string.Empty;
|
||||
private WorkspaceSwitcher _workspaceSwitcher = null;
|
||||
}
|
||||
}
|
||||
|
|
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;
|
||||
}
|
||||
}
|
|
@ -70,7 +70,7 @@
|
|||
<TextBlock Grid.Row="7" Grid.Column="0" Classes="primary bold" Text="{OnPlatform Ctrl+Q, macOS=⌘+Q}"/>
|
||||
<TextBlock Grid.Row="7" Grid.Column="1" Margin="16,0,0,0" Text="{DynamicResource Text.Quit}" />
|
||||
|
||||
<TextBlock Grid.Row="8" Grid.Column="0" Classes="primary bold" Text="{OnPlatform Alt+[1..9], macOS=⌥+[1..9]}"/>
|
||||
<TextBlock Grid.Row="8" Grid.Column="0" Classes="primary bold" Text="{OnPlatform Ctrl+P, macOS=⌘+P}"/>
|
||||
<TextBlock Grid.Row="8" Grid.Column="1" Margin="16,0,0,0" Text="{DynamicResource Text.Hotkeys.Global.SwitchWorkspace}" />
|
||||
</Grid>
|
||||
|
||||
|
|
|
@ -72,7 +72,7 @@
|
|||
<Path Width="12" Height="12" Data="{StaticResource Icons.Menu}"/>
|
||||
</Button>
|
||||
|
||||
<!-- Workspace Switcher -->
|
||||
<!-- Workspace Dropdown Menu -->
|
||||
<Button Grid.Column="1" Classes="icon_button" VerticalAlignment="Bottom" Margin="0,0,0,1" Click="OnOpenWorkspaceMenu">
|
||||
<ToolTip.Tip>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
|
@ -85,10 +85,10 @@
|
|||
Fill="{Binding ActiveWorkspace.Brush}"/>
|
||||
</Button>
|
||||
|
||||
<!-- Pages Tabs-->
|
||||
<!-- Pages Tabs -->
|
||||
<v:LauncherTabBar Grid.Column="2" Height="30" Margin="0,0,16,0" VerticalAlignment="Bottom"/>
|
||||
|
||||
<!-- Caption Buttons (Windows/Linux)-->
|
||||
<!-- Caption Buttons (Windows/Linux) -->
|
||||
<Border Grid.Column="3" Margin="16,0,0,0" IsVisible="{Binding #ThisControl.HasRightCaptionButton}">
|
||||
<v:CaptionButtons Height="30" VerticalAlignment="Top"/>
|
||||
</Border>
|
||||
|
@ -102,5 +102,22 @@
|
|||
</DataTemplate>
|
||||
</ContentControl.DataTemplates>
|
||||
</ContentControl>
|
||||
|
||||
<!-- Workspace Switcher -->
|
||||
<Border Grid.Row="1"
|
||||
Background="Transparent"
|
||||
IsVisible="{Binding WorkspaceSwitcher, Converter={x:Static ObjectConverters.IsNotNull}}">
|
||||
<Border HorizontalAlignment="Center" VerticalAlignment="Center" Effect="drop-shadow(0 0 12 #A0000000)">
|
||||
<Border Background="{DynamicResource Brush.Popup}" CornerRadius="8">
|
||||
<ContentControl Margin="16" Content="{Binding WorkspaceSwitcher}">
|
||||
<ContentControl.DataTemplates>
|
||||
<DataTemplate DataType="vm:WorkspaceSwitcher">
|
||||
<v:WorkspaceSwitcher/>
|
||||
</DataTemplate>
|
||||
</ContentControl.DataTemplates>
|
||||
</ContentControl>
|
||||
</Border>
|
||||
</Border>
|
||||
</Border>
|
||||
</Grid>
|
||||
</v:ChromelessWindow>
|
||||
|
|
|
@ -157,6 +157,12 @@ namespace SourceGit.Views
|
|||
|
||||
if (e.KeyModifiers.HasFlag(OperatingSystem.IsMacOS() ? KeyModifiers.Meta : KeyModifiers.Control))
|
||||
{
|
||||
if (e.Key == Key.P)
|
||||
{
|
||||
vm.OpenWorkspaceSwitcher();
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
if (e.Key == Key.W)
|
||||
{
|
||||
vm.CloseTab(null);
|
||||
|
@ -248,17 +254,10 @@ namespace SourceGit.Views
|
|||
}
|
||||
}
|
||||
}
|
||||
else if (e.KeyModifiers.HasFlag(KeyModifiers.Alt))
|
||||
{
|
||||
if (SwitchWorkspace(e.Key))
|
||||
{
|
||||
e.Handled = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (e.Key == Key.Escape)
|
||||
{
|
||||
vm.ActivePage.CancelPopup();
|
||||
vm.CancelWorkspaceSwitcher();
|
||||
e.Handled = true;
|
||||
return;
|
||||
}
|
||||
|
@ -314,44 +313,6 @@ namespace SourceGit.Views
|
|||
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
private bool SwitchWorkspace(Key eKey)
|
||||
{
|
||||
var exec = (ViewModels.Launcher l, int idx) =>
|
||||
{
|
||||
var pref = ViewModels.Preferences.Instance;
|
||||
if (idx < pref.Workspaces.Count)
|
||||
l.SwitchWorkspace(pref.Workspaces[idx]);
|
||||
return true; // Alt+1..9 (or Option+1..9) always mark handled
|
||||
};
|
||||
|
||||
if (DataContext is ViewModels.Launcher launcher)
|
||||
{
|
||||
switch (eKey)
|
||||
{
|
||||
case Key.D1 or Key.NumPad1:
|
||||
return exec(launcher, 0);
|
||||
case Key.D2 or Key.NumPad2:
|
||||
return exec(launcher, 1);
|
||||
case Key.D3 or Key.NumPad3:
|
||||
return exec(launcher, 2);
|
||||
case Key.D4 or Key.NumPad4:
|
||||
return exec(launcher, 3);
|
||||
case Key.D5 or Key.NumPad5:
|
||||
return exec(launcher, 4);
|
||||
case Key.D6 or Key.NumPad6:
|
||||
return exec(launcher, 5);
|
||||
case Key.D7 or Key.NumPad7:
|
||||
return exec(launcher, 6);
|
||||
case Key.D8 or Key.NumPad8:
|
||||
return exec(launcher, 7);
|
||||
case Key.D9 or Key.NumPad9:
|
||||
return exec(launcher, 8);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private KeyModifiers _unhandledModifiers = KeyModifiers.None;
|
||||
private WindowState _lastWindowState = WindowState.Normal;
|
||||
|
|
111
src/Views/WorkspaceSwitcher.axaml
Normal file
111
src/Views/WorkspaceSwitcher.axaml
Normal file
|
@ -0,0 +1,111 @@
|
|||
<UserControl xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:vm="using:SourceGit.ViewModels"
|
||||
xmlns:v="using:SourceGit.Views"
|
||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||
x:Class="SourceGit.Views.WorkspaceSwitcher"
|
||||
x:DataType="vm:WorkspaceSwitcher">
|
||||
<Grid RowDefinitions="28,Auto,Auto">
|
||||
<TextBlock Grid.Row="0"
|
||||
Text="{DynamicResource Text.Repository.WorkspaceSwitcher}"
|
||||
FontWeight="Bold"
|
||||
HorizontalAlignment="Center" VerticalAlignment="Top"/>
|
||||
|
||||
<TextBox Grid.Row="1"
|
||||
Height="24"
|
||||
Margin="4,0"
|
||||
BorderThickness="1"
|
||||
CornerRadius="12"
|
||||
Text="{Binding SearchFilter, Mode=TwoWay}"
|
||||
KeyDown="OnSearchBoxKeyDown"
|
||||
BorderBrush="{DynamicResource Brush.Border2}"
|
||||
VerticalContentAlignment="Center"
|
||||
v:AutoFocusBehaviour.IsEnabled="True">
|
||||
<TextBox.InnerLeftContent>
|
||||
<Path Width="14" Height="14"
|
||||
Margin="6,0,0,0"
|
||||
Fill="{DynamicResource Brush.FG2}"
|
||||
Data="{StaticResource Icons.Search}"/>
|
||||
</TextBox.InnerLeftContent>
|
||||
|
||||
<TextBox.InnerRightContent>
|
||||
<Button Classes="icon_button"
|
||||
Width="16"
|
||||
Margin="0,0,6,0"
|
||||
Command="{Binding ClearFilter}"
|
||||
IsVisible="{Binding SearchFilter, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"
|
||||
HorizontalAlignment="Right">
|
||||
<Path Width="14" Height="14"
|
||||
Margin="0,1,0,0"
|
||||
Fill="{DynamicResource Brush.FG1}"
|
||||
Data="{StaticResource Icons.Clear}"/>
|
||||
</Button>
|
||||
</TextBox.InnerRightContent>
|
||||
</TextBox>
|
||||
|
||||
<ListBox Grid.Row="2"
|
||||
x:Name="WorkspaceListBox"
|
||||
Width="300"
|
||||
MaxHeight="400"
|
||||
Margin="0,8,0,0"
|
||||
BorderThickness="0"
|
||||
SelectionMode="Single"
|
||||
Background="Transparent"
|
||||
Focusable="True"
|
||||
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
|
||||
ScrollViewer.VerticalScrollBarVisibility="Auto"
|
||||
ItemsSource="{Binding VisibleWorkspaces, Mode=OneWay}"
|
||||
SelectedItem="{Binding SelectedWorkspace, Mode=TwoWay}">
|
||||
<ListBox.Styles>
|
||||
<Style Selector="ListBoxItem">
|
||||
<Setter Property="Padding" Value="8,0"/>
|
||||
<Setter Property="MinHeight" Value="26"/>
|
||||
<Setter Property="CornerRadius" Value="4"/>
|
||||
</Style>
|
||||
|
||||
<Style Selector="ListBox">
|
||||
<Setter Property="FocusAdorner">
|
||||
<FocusAdornerTemplate>
|
||||
<Grid/>
|
||||
</FocusAdornerTemplate>
|
||||
</Setter>
|
||||
</Style>
|
||||
</ListBox.Styles>
|
||||
|
||||
<ListBox.ItemsPanel>
|
||||
<ItemsPanelTemplate>
|
||||
<StackPanel Orientation="Vertical"/>
|
||||
</ItemsPanelTemplate>
|
||||
</ListBox.ItemsPanel>
|
||||
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate DataType="vm:Workspace">
|
||||
<Grid ColumnDefinitions="Auto,*" VerticalAlignment="Center" Background="Transparent" DoubleTapped="OnItemDoubleTapped">
|
||||
<Path Grid.Column="0"
|
||||
Width="12" Height="12"
|
||||
Fill="{Binding Brush}"
|
||||
Data="{StaticResource Icons.Workspace}"
|
||||
IsVisible="{Binding !IsActive}"
|
||||
IsHitTestVisible="False"/>
|
||||
<Path Grid.Column="0"
|
||||
Width="12" Height="12"
|
||||
Fill="{Binding Brush}"
|
||||
Data="{StaticResource Icons.Check}"
|
||||
IsVisible="{Binding IsActive}"
|
||||
IsHitTestVisible="False"/>
|
||||
<TextBlock Grid.Column="1"
|
||||
Margin="8,0,0,0"
|
||||
Classes="primary"
|
||||
VerticalAlignment="Center"
|
||||
Text="{Binding Name}"
|
||||
IsHitTestVisible="False"
|
||||
TextTrimming="CharacterEllipsis"/>
|
||||
</Grid>
|
||||
</DataTemplate>
|
||||
</ListBox.ItemTemplate>
|
||||
</ListBox>
|
||||
</Grid>
|
||||
</UserControl>
|
||||
|
49
src/Views/WorkspaceSwitcher.axaml.cs
Normal file
49
src/Views/WorkspaceSwitcher.axaml.cs
Normal file
|
@ -0,0 +1,49 @@
|
|||
using Avalonia.Controls;
|
||||
using Avalonia.Input;
|
||||
|
||||
namespace SourceGit.Views
|
||||
{
|
||||
public partial class WorkspaceSwitcher : UserControl
|
||||
{
|
||||
public WorkspaceSwitcher()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
protected override void OnKeyDown(KeyEventArgs e)
|
||||
{
|
||||
base.OnKeyDown(e);
|
||||
|
||||
if (e.Key == Key.Enter && DataContext is ViewModels.WorkspaceSwitcher switcher)
|
||||
{
|
||||
switcher.Switch();
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnItemDoubleTapped(object sender, TappedEventArgs e)
|
||||
{
|
||||
if (DataContext is ViewModels.WorkspaceSwitcher switcher)
|
||||
{
|
||||
switcher.Switch();
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnSearchBoxKeyDown(object sender, KeyEventArgs e)
|
||||
{
|
||||
if (e.Key == Key.Down && WorkspaceListBox.ItemCount > 0)
|
||||
{
|
||||
WorkspaceListBox.Focus(NavigationMethod.Directional);
|
||||
|
||||
if (WorkspaceListBox.SelectedIndex < 0)
|
||||
WorkspaceListBox.SelectedIndex = 0;
|
||||
else if (WorkspaceListBox.SelectedIndex < WorkspaceListBox.ItemCount)
|
||||
WorkspaceListBox.SelectedIndex++;
|
||||
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue