Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 8c6bd29

Browse files
committed
Show add comment glyph for the current mouse line
1 parent 46011fb commit 8c6bd29

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed

src/GitHub.InlineReviews/GitHub.InlineReviews.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
<Compile Include="InlineReviewsPackage.cs" />
8383
<Compile Include="Models\InlineCommentThreadModel.cs" />
8484
<Compile Include="Models\PullRequestSessionFile.cs" />
85+
<Compile Include="Tags\MouseEnterAndLeaveEventRouter.cs" />
8586
<Compile Include="Peek\InlineCommentPeekableItem.cs" />
8687
<Compile Include="Peek\InlineCommentPeekableItemSource.cs" />
8788
<Compile Include="Peek\InlineCommentPeekableItemSourceProvider.cs" />

src/GitHub.InlineReviews/InlineCommentMarginProvider.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,14 @@ IWpfTextViewMargin CreateMargin<TGlyphTag>(IGlyphFactory<TGlyphTag> glyphFactory
5858
IWpfTextViewHost wpfTextViewHost, IWpfTextViewMargin parent, IEditorFormatMap editorFormatMap) where TGlyphTag : ITag
5959
{
6060
var tagAggregator = tagAggregatorFactory.CreateTagAggregator<TGlyphTag>(wpfTextViewHost.TextView);
61-
return new GlyphMargin<TGlyphTag>(wpfTextViewHost, glyphFactory, gridFactory, tagAggregator, editorFormatMap,
61+
var margin = new GlyphMargin<TGlyphTag>(wpfTextViewHost, glyphFactory, gridFactory, tagAggregator, editorFormatMap,
6262
IsMarginVisible, MarginPropertiesName, MarginName, true, 17.0);
63+
64+
var router = new MouseEnterAndLeaveEventRouter<AddInlineCommentGlyph>();
65+
wpfTextViewHost.TextView.VisualElement.MouseMove += (t, e) => router.MouseMove(margin.VisualElement, e);
66+
wpfTextViewHost.TextView.VisualElement.MouseLeave += (t, e) => router.MouseLeave(margin.VisualElement, e);
67+
68+
return margin;
6369
}
6470

6571
bool IsMarginVisible(ITextBuffer buffer)
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using System;
2+
using System.Windows;
3+
using System.Windows.Controls;
4+
using System.Windows.Input;
5+
6+
namespace GitHub.InlineReviews.Tags
7+
{
8+
class MouseEnterAndLeaveEventRouter<T> where T : FrameworkElement
9+
{
10+
T previousMouseOverElement;
11+
12+
public void MouseMove(object target, MouseEventArgs e)
13+
{
14+
T mouseOverElement = null;
15+
Action<T> visitAction = element =>
16+
{
17+
mouseOverElement = element;
18+
};
19+
20+
var visitor = new Visitor(e, visitAction);
21+
visitor.Visit(target);
22+
23+
if (mouseOverElement != previousMouseOverElement)
24+
{
25+
MouseLeave(previousMouseOverElement, e);
26+
MouseEnter(mouseOverElement, e);
27+
}
28+
}
29+
30+
public void MouseLeave(object target, MouseEventArgs e)
31+
{
32+
MouseLeave(previousMouseOverElement, e);
33+
}
34+
35+
void MouseEnter(T element, MouseEventArgs e)
36+
{
37+
element?.RaiseEvent(new MouseEventArgs(e.MouseDevice, e.Timestamp)
38+
{
39+
RoutedEvent = Mouse.MouseEnterEvent,
40+
});
41+
42+
previousMouseOverElement = element;
43+
}
44+
45+
void MouseLeave(T element, MouseEventArgs e)
46+
{
47+
element?.RaiseEvent(new MouseEventArgs(e.MouseDevice, e.Timestamp)
48+
{
49+
RoutedEvent = Mouse.MouseLeaveEvent,
50+
});
51+
52+
previousMouseOverElement = null;
53+
}
54+
55+
class Visitor
56+
{
57+
MouseEventArgs mouseEventArgs;
58+
Action<T> action;
59+
60+
internal Visitor(MouseEventArgs mouseEventArgs, Action<T> action)
61+
{
62+
this.mouseEventArgs = mouseEventArgs;
63+
this.action = action;
64+
}
65+
66+
internal void Visit(object obj)
67+
{
68+
if (obj is Panel)
69+
{
70+
Visit((Panel)obj);
71+
return;
72+
}
73+
74+
if (obj is T)
75+
{
76+
Visit((T)obj);
77+
return;
78+
}
79+
}
80+
81+
internal void Visit(Panel panel)
82+
{
83+
foreach (var child in panel.Children)
84+
{
85+
Visit(child);
86+
}
87+
}
88+
89+
internal void Visit(T element)
90+
{
91+
var point = mouseEventArgs.GetPosition(element);
92+
if (point.Y >= 0 && point.Y < element.ActualHeight)
93+
{
94+
action(element);
95+
}
96+
}
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)