Skip to content

Commit

Permalink
- Fixed #135 (NaviBar got broken after splitting window)
Browse files Browse the repository at this point in the history
  • Loading branch information
wmjordan committed Mar 31, 2021
1 parent 6ce7f55 commit 24d3478
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 29 deletions.
33 changes: 22 additions & 11 deletions Codist/NaviBar/CSharpBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,20 @@ public sealed class CSharpBar : NaviBar
static string __ProjectWideSearchExpression;

readonly IAdornmentLayer _SyntaxNodeRangeAdornment;
readonly SemanticContext _SemanticContext;
SemanticContext _SemanticContext;

readonly RootItem _RootItem;
CancellationTokenSource _cancellationSource = new CancellationTokenSource();
NodeItem _MouseHoverItem;
SymbolList _SymbolList;
ThemedImageButton _ActiveItem;

public CSharpBar(IWpfTextView textView) : base(textView) {
public CSharpBar(IWpfTextView view) : base(view) {
_SyntaxNodeRangeAdornment = View.GetAdornmentLayer(SyntaxNodeRange);
_SemanticContext = SemanticContext.GetOrCreateSingetonInstance(textView);
Name = nameof(CSharpBar);
BindView(view);
Items.Add(_RootItem = new RootItem(this));
View.Closed += ViewClosed;
View.TextBuffer.Changed += TextBuffer_Changed;
View.Selection.SelectionChanged += Update;
ListContainer.ChildRemoved += ListContainer_MenuRemoved;
Config.Updated += Config_Updated;
Update(this, EventArgs.Empty);
}

Expand Down Expand Up @@ -88,6 +84,24 @@ protected override void OnMouseMove(MouseEventArgs e) {
}
}

internal protected override void BindView(IWpfTextView view) {
UnbindViewEvents();
View = view;
View.Closed += ViewClosed;
View.TextBuffer.Changed += TextBuffer_Changed;
View.Selection.SelectionChanged += Update;
Config.Updated += Config_Updated;
SyncHelper.CancelAndDispose(ref _cancellationSource, true);
_SemanticContext = SemanticContext.GetOrCreateSingetonInstance(view);
}

protected override void UnbindViewEvents() {
View.Selection.SelectionChanged -= Update;
View.TextBuffer.Changed -= TextBuffer_Changed;
Config.Updated -= Config_Updated;
View.Closed -= ViewClosed;
}

void HighlightNodeRanges(SyntaxNode node, SnapshotSpan span) {
_SyntaxNodeRangeAdornment.AddAdornment(span, null, new GeometryAdornment(ThemeHelper.MenuHoverBackgroundColor, View.TextViewLines.GetMarkerGeometry(span), 3));
var p = View.Caret.Position.BufferPosition;
Expand Down Expand Up @@ -241,11 +255,8 @@ async Task<ImmutableArray<SyntaxNode>> UpdateModelAndGetContainingNodesAsync(Can
}

void ViewClosed(object sender, EventArgs e) {
View.Selection.SelectionChanged -= Update;
View.TextBuffer.Changed -= TextBuffer_Changed;
Config.Updated -= Config_Updated;
UnbindViewEvents();
SyncHelper.CancelAndDispose(ref _cancellationSource, false);
View.Closed -= ViewClosed;
}

void Config_Updated(object sender, ConfigUpdatedEventArgs e) {
Expand Down
18 changes: 14 additions & 4 deletions Codist/NaviBar/MarkdownBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ public MarkdownBar(IWpfTextView view, ITextSearchService2 textSearch) : base(vie
_TextSearch = textSearch;
_Tags = view.Properties.GetProperty<TaggerResult>(typeof(TaggerResult));
Name = nameof(MarkdownBar);
view.Selection.SelectionChanged += Update;
view.TextBuffer.PostChanged += Update;
view.Closed += View_Closed;
BindView(view);
_ActiveTitleLabel = new ThemedToolBarText(DefaultActiveTitle);
_ActiveItem = new ThemedImageButton(IconIds.Headings, _ActiveTitleLabel);
_ActiveItem.Click += ShowTitleList;
Expand All @@ -57,12 +55,24 @@ public MarkdownBar(IWpfTextView view, ITextSearchService2 textSearch) : base(vie
//AddItem(KnownImageIds.StrikeThrough, ToggleStrikeThrough);
}

void View_Closed(object sender, EventArgs e) {
protected internal override void BindView(IWpfTextView view) {
UnbindViewEvents();
View = view;
View.Selection.SelectionChanged += Update;
View.TextBuffer.PostChanged += Update;
View.Closed += View_Closed;
}

protected override void UnbindViewEvents() {
View.Closed -= View_Closed;
View.Selection.SelectionChanged -= Update;
View.TextBuffer.PostChanged -= Update;
}

void View_Closed(object sender, EventArgs e) {
UnbindViewEvents();
}

void Update(object sender, EventArgs e) {
HideMenu();
_ActiveTitleLabel.Text = _Tags.GetPreceedingTaggedSpan(View.GetCaretPosition().Position)?.ContentText ?? DefaultActiveTitle;
Expand Down
5 changes: 4 additions & 1 deletion Codist/NaviBar/NaviBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ protected NaviBar(IWpfTextView textView) {

public abstract void ShowActiveItemMenu();
public abstract void ShowRootItemMenu(int parameter);
protected IWpfTextView View { get; }
internal protected abstract void BindView(IWpfTextView view);
protected abstract void UnbindViewEvents();

protected IWpfTextView View { get; set; }
internal ExternalAdornment ListContainer { get; }

protected override void OnPreviewMouseRightButtonUp(MouseButtonEventArgs e) {
Expand Down
29 changes: 17 additions & 12 deletions Codist/NaviBar/NaviBarFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,29 +89,34 @@ public Overrider(IWpfTextView view, ITextSearchService2 textSearch) {
void AddNaviBar(object sender, RoutedEventArgs e) {
_View.VisualElement.Loaded -= AddNaviBar;

var view = sender as FrameworkElement;
var naviBar = view
var view = sender as IWpfTextView ?? _View;
NaviBar naviBar;
if ((naviBar = view.VisualElement?.GetParent<Grid>().GetFirstVisualChild<NaviBar>()) != null) {
naviBar.BindView(view);
return;
}
var naviBarHolder = view.VisualElement
?.GetParent<Border>(b => b.Name == "PART_ContentPanel")
?.GetFirstVisualChild<Border>(b => b.Name == "DropDownBarMargin");
if (naviBar == null) {
var naviBarHolder = view.GetParent<Panel>(b => b.GetType().Name == "WpfMultiViewHost");
if (naviBarHolder != null) {
if (naviBarHolder == null) {
var viewHost = view.VisualElement.GetParent<Panel>(b => b.GetType().Name == "WpfMultiViewHost");
if (viewHost != null) {
var b = new MarkdownBar(_View, _TextSearch);
DockPanel.SetDock(b, Dock.Top);
if (naviBarHolder.Children.Count == 1) {
naviBarHolder.Children.Insert(0, b);
if (viewHost.Children.Count == 1) {
viewHost.Children.Insert(0, b);
}
else {
var c = naviBarHolder.Children[0] as ContentControl;
var c = viewHost.Children[0] as ContentControl;
if (c != null && c.Content == null) {
c.Content = b;
}
}
}
return;
}
var dropDown1 = naviBar.GetFirstVisualChild<ComboBox>(c => c.Name == "DropDown1");
var dropDown2 = naviBar.GetFirstVisualChild<ComboBox>(c => c.Name == "DropDown2");
var dropDown1 = naviBarHolder.GetFirstVisualChild<ComboBox>(c => c.Name == "DropDown1");
var dropDown2 = naviBarHolder.GetFirstVisualChild<ComboBox>(c => c.Name == "DropDown2");
if (dropDown1 == null || dropDown2 == null) {
return;
}
Expand All @@ -127,7 +132,7 @@ void AddNaviBar(object sender, RoutedEventArgs e) {
container.Children.Add(bar);
dropDown1.Visibility = Visibility.Hidden;
dropDown2.Visibility = Visibility.Hidden;
naviBar.Unloaded += ResurrectNaviBar_OnUnloaded;
naviBarHolder.Unloaded += ResurrectNaviBar_OnUnloaded;
}

// Fixes https://github.com/wmjordan/Codist/issues/131
Expand All @@ -142,7 +147,7 @@ async void ResurrectNaviBar_OnUnloaded(object sender, RoutedEventArgs e) {
await Task.Delay(1000).ConfigureAwait(false);
await Microsoft.VisualStudio.Shell.ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(default);
if (_View.VisualElement.IsVisible && _View.Properties.ContainsProperty(nameof(NaviBar)) == false) {
AddNaviBar(_View.VisualElement, e);
AddNaviBar(_View, e);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Codist/SmartBars/SmartBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ async Task CreateToolBarAsync(CancellationToken cancellationToken) {
while ((Mouse.LeftButton == MouseButtonState.Pressed || Keyboard.Modifiers == ModifierKeys.Shift)
&& cancellationToken.IsCancellationRequested == false) {
// postpone the even handler until the mouse button is released
await Task.Delay(100);
await Task.Delay(100, cancellationToken);
}
if (View.Selection.IsEmpty || Interlocked.Exchange(ref _SelectionStatus, Working) != Selecting) {
goto EXIT;
Expand Down

0 comments on commit 24d3478

Please sign in to comment.