feature: use [$workspace] $repo_name ($repo_path) as main window's title (#818)

This commit is contained in:
leo 2024-12-16 15:47:33 +08:00
parent 5425fa64fe
commit f418b72c64
No known key found for this signature in database
2 changed files with 46 additions and 4 deletions

View file

@ -11,6 +11,12 @@ namespace SourceGit.ViewModels
{ {
public class Launcher : ObservableObject public class Launcher : ObservableObject
{ {
public string Title
{
get => _title;
private set => SetProperty(ref _title, value);
}
public AvaloniaList<LauncherPage> Pages public AvaloniaList<LauncherPage> Pages
{ {
get; get;
@ -31,9 +37,10 @@ namespace SourceGit.ViewModels
if (SetProperty(ref _activePage, value)) if (SetProperty(ref _activePage, value))
{ {
PopupHost.Active = value; PopupHost.Active = value;
UpdateTitle();
if (!_ignoreIndexChange && value is { Data: Repository repo }) if (!_ignoreIndexChange && value is { Data: Repository repo })
ActiveWorkspace.ActiveIdx = ActiveWorkspace.Repositories.IndexOf(repo.FullPath); _activeWorkspace.ActiveIdx = _activeWorkspace.Repositories.IndexOf(repo.FullPath);
} }
} }
} }
@ -105,6 +112,9 @@ namespace SourceGit.ViewModels
} }
_ignoreIndexChange = false; _ignoreIndexChange = false;
if (string.IsNullOrEmpty(_title))
UpdateTitle();
} }
public void Quit(double width, double height) public void Quit(double width, double height)
@ -185,6 +195,7 @@ namespace SourceGit.ViewModels
last.Node = new RepositoryNode() { Id = Guid.NewGuid().ToString() }; last.Node = new RepositoryNode() { Id = Guid.NewGuid().ToString() };
last.Data = Welcome.Instance; last.Data = Welcome.Instance;
last.Popup = null; last.Popup = null;
UpdateTitle();
GC.Collect(); GC.Collect();
} }
@ -193,7 +204,6 @@ namespace SourceGit.ViewModels
App.Quit(0); App.Quit(0);
} }
_ignoreIndexChange = false;
return; return;
} }
@ -308,7 +318,10 @@ namespace SourceGit.ViewModels
page.Data = repo; page.Data = repo;
} }
if (page != _activePage)
ActivePage = page; ActivePage = page;
else
UpdateTitle();
ActiveWorkspace.Repositories.Clear(); ActiveWorkspace.Repositories.Clear();
foreach (var p in Pages) foreach (var p in Pages)
@ -530,8 +543,37 @@ namespace SourceGit.ViewModels
page.Data = null; page.Data = null;
} }
private void UpdateTitle()
{
if (_activeWorkspace == null)
return;
var workspace = _activeWorkspace.Name;
if (_activePage is { Data: Repository repo })
{
var node = _activePage.Node;
var name = node.Name;
var path = node.Id;
if (!OperatingSystem.IsWindows())
{
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var prefixLen = home.EndsWith('/') ? home.Length - 1 : home.Length;
if (path.StartsWith(home, StringComparison.Ordinal))
path = "~" + path.Substring(prefixLen);
}
Title = $"[{workspace}] {name} ({path})";
}
else
{
Title = $"[{workspace}] Repositories";
}
}
private Workspace _activeWorkspace = null; private Workspace _activeWorkspace = null;
private LauncherPage _activePage = null; private LauncherPage _activePage = null;
private bool _ignoreIndexChange = false; private bool _ignoreIndexChange = false;
private string _title = string.Empty;
} }
} }

View file

@ -10,7 +10,7 @@
x:DataType="vm:Launcher" x:DataType="vm:Launcher"
x:Name="ThisControl" x:Name="ThisControl"
Icon="/App.ico" Icon="/App.ico"
Title="SourceGit" Title="{Binding Title}"
MinWidth="1024" MinHeight="600" MinWidth="1024" MinHeight="600"
WindowStartupLocation="CenterScreen"> WindowStartupLocation="CenterScreen">
<Grid> <Grid>