-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathLauncher.cs
603 lines (502 loc) · 18 KB
/
Launcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
using System;
using System.IO;
using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Input;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
{
public class Launcher : ObservableObject
{
public string Title
{
get => _title;
private set => SetProperty(ref _title, value);
}
public AvaloniaList<LauncherPage> Pages
{
get;
private set;
}
public Workspace ActiveWorkspace
{
get => _activeWorkspace;
private set => SetProperty(ref _activeWorkspace, value);
}
public LauncherPage ActivePage
{
get => _activePage;
set
{
if (SetProperty(ref _activePage, value))
{
UpdateTitle();
if (!_ignoreIndexChange && value is { Data: Repository repo })
_activeWorkspace.ActiveIdx = _activeWorkspace.Repositories.IndexOf(repo.FullPath);
}
}
}
public Launcher(string startupRepo)
{
_ignoreIndexChange = true;
Pages = new AvaloniaList<LauncherPage>();
AddNewTab();
var pref = Preferences.Instance;
if (string.IsNullOrEmpty(startupRepo))
{
ActiveWorkspace = pref.GetActiveWorkspace();
var repos = ActiveWorkspace.Repositories.ToArray();
foreach (var repo in repos)
{
var node = pref.FindNode(repo);
if (node == null)
{
node = new RepositoryNode()
{
Id = repo,
Name = Path.GetFileName(repo),
Bookmark = 0,
IsRepository = true,
};
}
OpenRepositoryInTab(node, null);
}
var activeIdx = ActiveWorkspace.ActiveIdx;
if (activeIdx >= 0 && activeIdx < Pages.Count)
{
ActivePage = Pages[activeIdx];
}
else
{
ActivePage = Pages[0];
ActiveWorkspace.ActiveIdx = 0;
}
}
else
{
ActiveWorkspace = new Workspace() { Name = "Unnamed" };
foreach (var w in pref.Workspaces)
w.IsActive = false;
var test = new Commands.QueryRepositoryRootPath(startupRepo).ReadToEnd();
if (!test.IsSuccess || string.IsNullOrEmpty(test.StdOut))
{
Pages[0].Notifications.Add(new Models.Notification
{
IsError = true,
Message = $"Given path: '{startupRepo}' is NOT a valid repository!"
});
}
else
{
var node = pref.FindOrAddNodeByRepositoryPath(test.StdOut.Trim(), null, false);
Welcome.Instance.Refresh();
OpenRepositoryInTab(node, null);
}
}
_ignoreIndexChange = false;
if (string.IsNullOrEmpty(_title))
UpdateTitle();
}
public void Quit(double width, double height)
{
var pref = Preferences.Instance;
pref.Layout.LauncherWidth = width;
pref.Layout.LauncherHeight = height;
pref.Save();
_ignoreIndexChange = true;
foreach (var one in Pages)
CloseRepositoryInTab(one, false);
_ignoreIndexChange = false;
}
public void AddNewTab()
{
var page = new LauncherPage();
Pages.Add(page);
ActivePage = page;
}
public void MoveTab(LauncherPage from, LauncherPage to)
{
_ignoreIndexChange = true;
var fromIdx = Pages.IndexOf(from);
var toIdx = Pages.IndexOf(to);
Pages.Move(fromIdx, toIdx);
ActivePage = from;
ActiveWorkspace.Repositories.Clear();
foreach (var p in Pages)
{
if (p.Data is Repository r)
ActiveWorkspace.Repositories.Add(r.FullPath);
}
ActiveWorkspace.ActiveIdx = ActiveWorkspace.Repositories.IndexOf(from.Node.Id);
_ignoreIndexChange = false;
}
public void GotoNextTab()
{
if (Pages.Count == 1)
return;
var activeIdx = Pages.IndexOf(_activePage);
var nextIdx = (activeIdx + 1) % Pages.Count;
ActivePage = Pages[nextIdx];
}
public void GotoPrevTab()
{
if (Pages.Count == 1)
return;
var activeIdx = Pages.IndexOf(_activePage);
var prevIdx = activeIdx == 0 ? Pages.Count - 1 : activeIdx - 1;
ActivePage = Pages[prevIdx];
}
public void CloseTab(LauncherPage page)
{
if (Pages.Count == 1)
{
var last = Pages[0];
if (last.Data is Repository repo)
{
ActiveWorkspace.Repositories.Clear();
ActiveWorkspace.ActiveIdx = 0;
repo.Close();
Welcome.Instance.ClearSearchFilter();
last.Node = new RepositoryNode() { Id = Guid.NewGuid().ToString() };
last.Data = Welcome.Instance;
last.Popup = null;
UpdateTitle();
GC.Collect();
}
else
{
App.Quit(0);
}
return;
}
if (page == null)
page = _activePage;
var removeIdx = Pages.IndexOf(page);
var activeIdx = Pages.IndexOf(_activePage);
if (removeIdx == activeIdx)
{
ActivePage = Pages[removeIdx > 0 ? removeIdx - 1 : removeIdx + 1];
CloseRepositoryInTab(page);
Pages.RemoveAt(removeIdx);
}
else
{
CloseRepositoryInTab(page);
Pages.RemoveAt(removeIdx);
}
GC.Collect();
}
public void CloseOtherTabs()
{
if (Pages.Count == 1)
return;
_ignoreIndexChange = true;
var id = ActivePage.Node.Id;
foreach (var one in Pages)
{
if (one.Node.Id != id)
CloseRepositoryInTab(one);
}
Pages = new AvaloniaList<LauncherPage> { ActivePage };
ActiveWorkspace.ActiveIdx = 0;
OnPropertyChanged(nameof(Pages));
_ignoreIndexChange = false;
GC.Collect();
}
public void CloseRightTabs()
{
_ignoreIndexChange = true;
var endIdx = Pages.IndexOf(ActivePage);
for (var i = Pages.Count - 1; i > endIdx; i--)
{
CloseRepositoryInTab(Pages[i]);
Pages.Remove(Pages[i]);
}
_ignoreIndexChange = false;
GC.Collect();
}
public void OpenRepositoryInTab(RepositoryNode node, LauncherPage page)
{
foreach (var one in Pages)
{
if (one.Node.Id == node.Id)
{
ActivePage = one;
return;
}
}
if (!Path.Exists(node.Id))
{
App.RaiseException(node.Id, "Repository does NOT exists any more. Please remove it.");
return;
}
var isBare = new Commands.IsBareRepository(node.Id).Result();
var gitDir = isBare ? node.Id : GetRepositoryGitDir(node.Id);
if (string.IsNullOrEmpty(gitDir))
{
App.RaiseException(node.Id, "Given path is not a valid git repository!");
return;
}
var repo = new Repository(isBare, node.Id, gitDir);
repo.Open();
if (page == null)
{
if (ActivePage == null || ActivePage.Node.IsRepository)
{
page = new LauncherPage(node, repo);
Pages.Add(page);
}
else
{
page = ActivePage;
page.Node = node;
page.Data = repo;
}
}
else
{
page.Node = node;
page.Data = repo;
}
if (page != _activePage)
ActivePage = page;
else
UpdateTitle();
ActiveWorkspace.Repositories.Clear();
foreach (var p in Pages)
{
if (p.Data is Repository r)
ActiveWorkspace.Repositories.Add(r.FullPath);
}
if (!_ignoreIndexChange)
ActiveWorkspace.ActiveIdx = ActiveWorkspace.Repositories.IndexOf(node.Id);
}
public void DispatchNotification(string pageId, string message, bool isError)
{
var notification = new Models.Notification()
{
IsError = isError,
Message = message,
};
foreach (var page in Pages)
{
var id = page.Node.Id.Replace("\\", "/");
if (id == pageId)
{
page.Notifications.Add(notification);
return;
}
}
if (_activePage != null)
_activePage.Notifications.Add(notification);
}
public ContextMenu CreateContextForWorkspace()
{
var pref = Preferences.Instance;
var menu = new ContextMenu();
for (var i = 0; i < pref.Workspaces.Count; i++)
{
var workspace = pref.Workspaces[i];
var icon = App.CreateMenuIcon(workspace.IsActive ? "Icons.Check" : "Icons.Workspace");
icon.Fill = workspace.Brush;
var item = new MenuItem();
item.Header = workspace.Name;
item.Icon = icon;
item.Click += (_, e) =>
{
if (!workspace.IsActive)
SwitchWorkspace(workspace);
e.Handled = true;
};
menu.Items.Add(item);
}
menu.Items.Add(new MenuItem() { Header = "-" });
var configure = new MenuItem();
configure.Header = App.Text("Workspace.Configure");
configure.Click += (_, e) =>
{
App.ShowWindow(new ConfigureWorkspace(), true);
e.Handled = true;
};
menu.Items.Add(configure);
return menu;
}
public ContextMenu CreateContextForPageTab(LauncherPage page)
{
if (page == null)
return null;
var menu = new ContextMenu();
var close = new MenuItem();
close.Header = App.Text("PageTabBar.Tab.Close");
close.InputGesture = KeyGesture.Parse(OperatingSystem.IsMacOS() ? "⌘+W" : "Ctrl+W");
close.Click += (_, e) =>
{
CloseTab(page);
e.Handled = true;
};
menu.Items.Add(close);
var closeOthers = new MenuItem();
closeOthers.Header = App.Text("PageTabBar.Tab.CloseOther");
closeOthers.Click += (_, e) =>
{
CloseOtherTabs();
e.Handled = true;
};
menu.Items.Add(closeOthers);
var closeRight = new MenuItem();
closeRight.Header = App.Text("PageTabBar.Tab.CloseRight");
closeRight.Click += (_, e) =>
{
CloseRightTabs();
e.Handled = true;
};
menu.Items.Add(closeRight);
if (page.Node.IsRepository)
{
var bookmark = new MenuItem();
bookmark.Header = App.Text("PageTabBar.Tab.Bookmark");
bookmark.Icon = App.CreateMenuIcon("Icons.Bookmark");
for (int i = 0; i < Models.Bookmarks.Supported.Count; i++)
{
var icon = App.CreateMenuIcon("Icons.Bookmark");
if (i != 0)
icon.Fill = Models.Bookmarks.Brushes[i];
var dupIdx = i;
var setter = new MenuItem();
setter.Header = icon;
setter.Click += (_, e) =>
{
page.Node.Bookmark = dupIdx;
e.Handled = true;
};
bookmark.Items.Add(setter);
}
menu.Items.Add(new MenuItem() { Header = "-" });
menu.Items.Add(bookmark);
var copyPath = new MenuItem();
copyPath.Header = App.Text("PageTabBar.Tab.CopyPath");
copyPath.Icon = App.CreateMenuIcon("Icons.Copy");
copyPath.Click += (_, e) =>
{
page.CopyPath();
e.Handled = true;
};
menu.Items.Add(new MenuItem() { Header = "-" });
menu.Items.Add(copyPath);
}
return menu;
}
private string GetRepositoryGitDir(string repo)
{
var fullpath = Path.Combine(repo, ".git");
if (Directory.Exists(fullpath))
{
if (Directory.Exists(Path.Combine(fullpath, "refs")) &&
Directory.Exists(Path.Combine(fullpath, "objects")) &&
File.Exists(Path.Combine(fullpath, "HEAD")))
return fullpath;
return null;
}
if (File.Exists(fullpath))
{
var redirect = File.ReadAllText(fullpath).Trim();
if (redirect.StartsWith("gitdir: ", StringComparison.Ordinal))
redirect = redirect.Substring(8);
if (!Path.IsPathRooted(redirect))
redirect = Path.GetFullPath(Path.Combine(repo, redirect));
if (Directory.Exists(redirect))
return redirect;
return null;
}
return new Commands.QueryGitDir(repo).Result();
}
private void SwitchWorkspace(Workspace to)
{
foreach (var one in Pages)
{
if (!one.CanCreatePopup() || one.Data is Repository { IsAutoFetching: true })
{
App.RaiseException(null, "You have unfinished task(s) in opened pages. Please wait!!!");
return;
}
}
_ignoreIndexChange = true;
var pref = Preferences.Instance;
foreach (var w in pref.Workspaces)
w.IsActive = false;
ActiveWorkspace = to;
to.IsActive = true;
foreach (var one in Pages)
CloseRepositoryInTab(one, false);
Pages.Clear();
AddNewTab();
var repos = to.Repositories.ToArray();
foreach (var repo in repos)
{
var node = pref.FindNode(repo);
if (node == null)
{
node = new RepositoryNode()
{
Id = repo,
Name = Path.GetFileName(repo),
Bookmark = 0,
IsRepository = true,
};
}
OpenRepositoryInTab(node, null);
}
var activeIdx = to.ActiveIdx;
if (activeIdx >= 0 && activeIdx < Pages.Count)
{
ActivePage = Pages[activeIdx];
}
else
{
ActivePage = Pages[0];
to.ActiveIdx = 0;
}
_ignoreIndexChange = false;
Preferences.Instance.Save();
GC.Collect();
}
private void CloseRepositoryInTab(LauncherPage page, bool removeFromWorkspace = true)
{
if (page.Data is Repository repo)
{
if (removeFromWorkspace)
ActiveWorkspace.Repositories.Remove(repo.FullPath);
repo.Close();
}
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 LauncherPage _activePage = null;
private bool _ignoreIndexChange = false;
private string _title = string.Empty;
}
}