Skip to content

Commit af2b644

Browse files
committed
code_review: PR #1416
- Split `DoubleTapped` into two methods: `CheckoutBranchByDecorator` and `CheckoutBranchByCommit` - Move `DoubleTappedEvent` from whole ListBox to the row tapped actually - Do nothing if the decorator double-clicked is HEAD - Code-style Signed-off-by: leo <[email protected]>
1 parent 88fd8f3 commit af2b644

File tree

6 files changed

+97
-88
lines changed

6 files changed

+97
-88
lines changed

src/Models/Watcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public Watcher(IRepository repo, string fullpath, string gitDir)
5555

5656
_watchers.Add(wc);
5757
_watchers.Add(git);
58-
}
58+
}
5959

6060
_timer = new Timer(Tick, null, 100, 100);
6161
}

src/ViewModels/Histories.cs

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -209,77 +209,85 @@ public void Select(IList commits)
209209
}
210210
}
211211

212-
public void DoubleTapped(Models.Commit commit, Models.Decorator decorator = null)
212+
public bool CheckoutBranchByDecorator(Models.Decorator decorator)
213213
{
214-
if (decorator != null)
214+
if (decorator == null)
215+
return false;
216+
217+
if (decorator.Type == Models.DecoratorType.CurrentBranchHead ||
218+
decorator.Type == Models.DecoratorType.CurrentCommitHead)
219+
return true;
220+
221+
if (decorator.Type == Models.DecoratorType.LocalBranchHead)
222+
{
223+
var b = _repo.Branches.Find(x => x.Name == decorator.Name);
224+
if (b == null)
225+
return false;
226+
227+
_repo.CheckoutBranch(b);
228+
return true;
229+
}
230+
231+
if (decorator.Type == Models.DecoratorType.RemoteBranchHead)
215232
{
216-
if (decorator.Type == Models.DecoratorType.LocalBranchHead)
233+
var rb = _repo.Branches.Find(x => x.FriendlyName == decorator.Name);
234+
if (rb == null)
235+
return false;
236+
237+
var lb = _repo.Branches.Find(x => x.IsLocal && x.Upstream == rb.FullName);
238+
if (lb == null || lb.TrackStatus.Ahead.Count > 0)
217239
{
218-
var b = _repo.Branches.Find(x => x.FriendlyName == decorator.Name);
219-
if (b != null)
220-
{
221-
_repo.CheckoutBranch(b);
222-
return;
223-
}
240+
if (_repo.CanCreatePopup())
241+
_repo.ShowPopup(new CreateBranch(_repo, rb));
224242
}
225-
else if (decorator.Type == Models.DecoratorType.RemoteBranchHead)
243+
else if (lb.TrackStatus.Behind.Count > 0)
226244
{
227-
var remoteBranch = _repo.Branches.Find(x => x.FriendlyName == decorator.Name);
228-
if (remoteBranch != null)
229-
{
230-
var localBranch = _repo.Branches.Find(x => x.IsLocal && x.Upstream == remoteBranch.FullName);
231-
if (localBranch != null)
232-
{
233-
if (localBranch.IsCurrent)
234-
return;
235-
if (localBranch.TrackStatus.Ahead.Count > 0)
236-
{
237-
if (_repo.CanCreatePopup())
238-
_repo.ShowPopup(new CreateBranch(_repo, remoteBranch));
239-
}
240-
else if (localBranch.TrackStatus.Behind.Count > 0)
241-
{
242-
if (_repo.CanCreatePopup())
243-
_repo.ShowPopup(new CheckoutAndFastForward(_repo, localBranch, remoteBranch));
244-
}
245-
else
246-
_repo.CheckoutBranch(localBranch);
247-
return;
248-
}
249-
}
245+
if (_repo.CanCreatePopup())
246+
_repo.ShowPopup(new CheckoutAndFastForward(_repo, lb, rb));
247+
}
248+
else if (!lb.IsCurrent)
249+
{
250+
_repo.CheckoutBranch(lb);
250251
}
252+
253+
return true;
251254
}
252255

253-
if (commit == null || commit.IsCurrentHead)
256+
return false;
257+
}
258+
259+
public void CheckoutBranchByCommit(Models.Commit commit)
260+
{
261+
if (commit.IsCurrentHead)
254262
return;
255263

256264
var firstRemoteBranch = null as Models.Branch;
257265
foreach (var d in commit.Decorators)
258266
{
259267
if (d.Type == Models.DecoratorType.LocalBranchHead)
260268
{
261-
var b = _repo.Branches.Find(x => x.FriendlyName == d.Name);
262-
if (b != null)
263-
{
264-
_repo.CheckoutBranch(b);
265-
return;
266-
}
269+
var b = _repo.Branches.Find(x => x.Name == d.Name);
270+
if (b == null)
271+
continue;
272+
273+
_repo.CheckoutBranch(b);
274+
return;
267275
}
268276
else if (d.Type == Models.DecoratorType.RemoteBranchHead)
269277
{
270-
var remoteBranch = _repo.Branches.Find(x => x.FriendlyName == d.Name);
271-
if (remoteBranch != null)
278+
var rb = _repo.Branches.Find(x => x.FriendlyName == d.Name);
279+
if (rb == null)
280+
continue;
281+
282+
var lb = _repo.Branches.Find(x => x.IsLocal && x.Upstream == rb.FullName);
283+
if (lb is { TrackStatus.Ahead.Count: 0 })
272284
{
273-
var localBranch = _repo.Branches.Find(x => x.IsLocal && x.Upstream == remoteBranch.FullName);
274-
if (localBranch is { TrackStatus.Ahead.Count: 0 })
275-
{
276-
if (_repo.CanCreatePopup())
277-
_repo.ShowPopup(new CheckoutAndFastForward(_repo, localBranch, remoteBranch));
278-
return;
279-
}
285+
if (_repo.CanCreatePopup())
286+
_repo.ShowPopup(new CheckoutAndFastForward(_repo, lb, rb));
287+
return;
280288
}
281289

282-
firstRemoteBranch ??= remoteBranch;
290+
firstRemoteBranch ??= rb;
283291
}
284292
}
285293

src/ViewModels/WorkingCopy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ public ContextMenu CreateContextMenuForUnstagedChanges(string selectedSingleFold
810810
};
811811
addToIgnore.Items.Add(byExtensionInSameFolder);
812812
}
813-
}
813+
}
814814

815815
menu.Items.Add(addToIgnore);
816816
hasExtra = true;
@@ -1414,7 +1414,7 @@ public ContextMenu CreateContextMenuForStagedChanges(string selectedSingleFolder
14141414

14151415
menu.Items.Add(explore);
14161416
menu.Items.Add(new MenuItem() { Header = "-" });
1417-
}
1417+
}
14181418

14191419
var unstage = new MenuItem();
14201420
unstage.Header = App.Text("FileCM.UnstageMulti", _selectedStaged.Count);

src/Views/CommitRefsPresenter.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class RenderItem
1717
public IBrush Brush { get; set; } = null;
1818
public bool IsHead { get; set; } = false;
1919
public double Width { get; set; } = 0.0;
20+
public Models.Decorator Decorator { get; set; } = null;
2021
}
2122

2223
public static readonly StyledProperty<FontFamily> FontFamilyProperty =
@@ -93,6 +94,19 @@ static CommitRefsPresenter()
9394
ShowTagsProperty);
9495
}
9596

97+
public Models.Decorator DecoratorAt(Point point)
98+
{
99+
var x = 0.0;
100+
foreach (var item in _items)
101+
{
102+
x += item.Width;
103+
if (point.X < x)
104+
return item.Decorator;
105+
}
106+
107+
return null;
108+
}
109+
96110
public override void Render(DrawingContext context)
97111
{
98112
if (_items.Count == 0)
@@ -156,22 +170,6 @@ protected override void OnDataContextChanged(EventArgs e)
156170
InvalidateMeasure();
157171
}
158172

159-
public Models.Decorator DecoratorAt(Point point)
160-
{
161-
if (DataContext is not Models.Commit commit)
162-
return null;
163-
164-
var x = 0.0;
165-
for (var i = 0; i < _items.Count; i++)
166-
{
167-
x += _items[i].Width + 4;
168-
if (point.X < x)
169-
return commit.Decorators[i];
170-
}
171-
172-
return null;
173-
}
174-
175173
protected override Size MeasureOverride(Size availableSize)
176174
{
177175
_items.Clear();
@@ -214,7 +212,8 @@ protected override Size MeasureOverride(Size availableSize)
214212
{
215213
Label = label,
216214
Brush = normalBG,
217-
IsHead = isHead
215+
IsHead = isHead,
216+
Decorator = decorator,
218217
};
219218

220219
StreamGeometry geo;

src/Views/Histories.axaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
LayoutUpdated="OnCommitListLayoutUpdated"
7171
SelectionChanged="OnCommitListSelectionChanged"
7272
ContextRequested="OnCommitListContextRequested"
73-
DoubleTapped="OnCommitListDoubleTapped"
7473
KeyDown="OnCommitListKeyDown">
7574
<ListBox.Styles>
7675
<Style Selector="ListBoxItem">
@@ -117,7 +116,7 @@
117116

118117
<ListBox.ItemTemplate>
119118
<DataTemplate DataType="m:Commit">
120-
<Grid Height="26">
119+
<Grid Height="26" Background="Transparent" DoubleTapped="OnCommitListItemDoubleTapped">
121120
<Grid.ColumnDefinitions>
122121
<ColumnDefinition Width="*"/>
123122
<ColumnDefinition SharedSizeGroup="AuthorName" Width="Auto"/>

src/Views/Histories.axaml.cs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -169,22 +169,6 @@ private void OnCommitListContextRequested(object sender, ContextRequestedEventAr
169169
e.Handled = true;
170170
}
171171

172-
private void OnCommitListDoubleTapped(object sender, TappedEventArgs e)
173-
{
174-
if (DataContext is ViewModels.Histories histories && sender is ListBox { SelectedItems.Count: 1 })
175-
{
176-
Models.Decorator decorator = null;
177-
if (e.Source is CommitRefsPresenter crp)
178-
decorator = crp.DecoratorAt(e.GetPosition(crp));
179-
180-
var source = e.Source as Control;
181-
var item = source.FindAncestorOfType<ListBoxItem>();
182-
if (item is { DataContext: Models.Commit commit })
183-
histories.DoubleTapped(commit, decorator);
184-
}
185-
e.Handled = true;
186-
}
187-
188172
private void OnCommitListKeyDown(object sender, KeyEventArgs e)
189173
{
190174
if (!e.KeyModifiers.HasFlag(OperatingSystem.IsMacOS() ? KeyModifiers.Meta : KeyModifiers.Control))
@@ -228,6 +212,25 @@ private void OnCommitListKeyDown(object sender, KeyEventArgs e)
228212
}
229213
}
230214

215+
private void OnCommitListItemDoubleTapped(object sender, TappedEventArgs e)
216+
{
217+
e.Handled = true;
218+
219+
if (DataContext is ViewModels.Histories histories &&
220+
CommitListContainer.SelectedItems is { Count: 1 } &&
221+
sender is Grid { DataContext: Models.Commit commit })
222+
{
223+
if (e.Source is CommitRefsPresenter crp)
224+
{
225+
var decorator = crp.DecoratorAt(e.GetPosition(crp));
226+
if (histories.CheckoutBranchByDecorator(decorator))
227+
return;
228+
}
229+
230+
histories.CheckoutBranchByCommit(commit);
231+
}
232+
}
233+
231234
private double _lastScrollY = 0;
232235
private double _lastAuthorNameColumnWidth = 0;
233236
}

0 commit comments

Comments
 (0)