-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bump mac editor and implement CocoaTextBufferVisibilityTracker #65643
Merged
davidwengier
merged 1 commit into
release/vsmac17.4
from
dev/therzok/cocoa-text-buffer-visibility-tracker-17.4
Nov 29, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
225 changes: 225 additions & 0 deletions
225
src/EditorFeatures/Core.Cocoa/Workspaces/CocoaTextBufferVisibilityTracker.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Collections.ObjectModel; | ||
using System.ComponentModel.Composition; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis.Editor; | ||
using Microsoft.CodeAnalysis.Editor.Shared.Extensions; | ||
using Microsoft.CodeAnalysis.Editor.Shared.Utilities; | ||
using Microsoft.CodeAnalysis.Host.Mef; | ||
using Microsoft.VisualStudio.Text; | ||
using Microsoft.VisualStudio.Text.Editor; | ||
using Roslyn.Utilities; | ||
|
||
namespace Microsoft.CodeAnalysis.Workspaces | ||
{ | ||
[Export(typeof(ITextBufferVisibilityTracker))] | ||
internal sealed class CocoaTextBufferVisibilityTracker : ITextBufferVisibilityTracker | ||
{ | ||
private readonly ITextBufferAssociatedViewService _associatedViewService; | ||
private readonly IThreadingContext _threadingContext; | ||
|
||
private readonly Dictionary<ITextBuffer, VisibleTrackerData> _subjectBufferToCallbacks = new(); | ||
|
||
[ImportingConstructor] | ||
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] | ||
public CocoaTextBufferVisibilityTracker( | ||
ITextBufferAssociatedViewService associatedViewService, | ||
IThreadingContext threadingContext) | ||
{ | ||
_associatedViewService = associatedViewService; | ||
_threadingContext = threadingContext; | ||
|
||
associatedViewService.SubjectBuffersConnected += AssociatedViewService_SubjectBuffersConnected; | ||
associatedViewService.SubjectBuffersDisconnected += AssociatedViewService_SubjectBuffersDisconnected; | ||
} | ||
|
||
private void AssociatedViewService_SubjectBuffersConnected(object sender, SubjectBuffersConnectedEventArgs e) | ||
{ | ||
_threadingContext.ThrowIfNotOnUIThread(); | ||
UpdateAllAssociatedViews(e.SubjectBuffers); | ||
} | ||
|
||
private void AssociatedViewService_SubjectBuffersDisconnected(object sender, SubjectBuffersConnectedEventArgs e) | ||
{ | ||
_threadingContext.ThrowIfNotOnUIThread(); | ||
UpdateAllAssociatedViews(e.SubjectBuffers); | ||
} | ||
|
||
private void UpdateAllAssociatedViews(ReadOnlyCollection<ITextBuffer> subjectBuffers) | ||
{ | ||
// Whenever views get attached/detached from buffers, make sure we're hooked up to the appropriate events | ||
// for them. | ||
foreach (var buffer in subjectBuffers) | ||
{ | ||
if (_subjectBufferToCallbacks.TryGetValue(buffer, out var data)) | ||
data.UpdateAssociatedViews(); | ||
} | ||
} | ||
|
||
public bool IsVisible(ITextBuffer subjectBuffer) | ||
{ | ||
_threadingContext.ThrowIfNotOnUIThread(); | ||
|
||
var views = _associatedViewService.GetAssociatedTextViews(subjectBuffer).ToImmutableArrayOrEmpty(); | ||
|
||
// If we don't have any views at all, then assume the buffer is visible. | ||
if (views.Length == 0) | ||
return true; | ||
|
||
// if any of the views were *not* cocoa text views, assume the buffer is visible. We don't know how to | ||
// determine the visibility of this buffer. While unlikely to happen, this is possible with VS's | ||
// extensibility model, which allows for a plugin to host an ITextBuffer in their own impl of an ITextView. | ||
// For those cases, just assume these buffers are visible. | ||
if (views.Any(static v => v is not ICocoaTextView)) | ||
return true; | ||
|
||
return views.OfType<ICocoaTextView>().Any(v => v.IsVisible); | ||
} | ||
|
||
public void RegisterForVisibilityChanges(ITextBuffer subjectBuffer, Action callback) | ||
{ | ||
_threadingContext.ThrowIfNotOnUIThread(); | ||
if (!_subjectBufferToCallbacks.TryGetValue(subjectBuffer, out var data)) | ||
{ | ||
data = new VisibleTrackerData(this, subjectBuffer); | ||
_subjectBufferToCallbacks.Add(subjectBuffer, data); | ||
} | ||
|
||
data.AddCallback(callback); | ||
} | ||
|
||
public void UnregisterForVisibilityChanges(ITextBuffer subjectBuffer, Action callback) | ||
{ | ||
_threadingContext.ThrowIfNotOnUIThread(); | ||
|
||
// Both of these methods must succeed. Otherwise we're somehow unregistering something we don't know about. | ||
Contract.ThrowIfFalse(_subjectBufferToCallbacks.TryGetValue(subjectBuffer, out var data)); | ||
Contract.ThrowIfFalse(data.Callbacks.Contains(callback)); | ||
data.RemoveCallback(callback); | ||
|
||
// If we have nothing that wants to listen to information about this buffer anymore, then disconnect it | ||
// from all events and remove our map. | ||
if (data.Callbacks.Count == 0) | ||
{ | ||
data.Dispose(); | ||
_subjectBufferToCallbacks.Remove(subjectBuffer); | ||
} | ||
} | ||
|
||
public TestAccessor GetTestAccessor() | ||
=> new TestAccessor(this); | ||
|
||
public readonly struct TestAccessor | ||
{ | ||
private readonly CocoaTextBufferVisibilityTracker _visibilityTracker; | ||
|
||
public TestAccessor(CocoaTextBufferVisibilityTracker visibilityTracker) | ||
{ | ||
_visibilityTracker = visibilityTracker; | ||
} | ||
|
||
public void TriggerCallbacks(ITextBuffer subjectBuffer) | ||
{ | ||
var data = _visibilityTracker._subjectBufferToCallbacks[subjectBuffer]; | ||
data.TriggerCallbacks(); | ||
} | ||
} | ||
|
||
private sealed class VisibleTrackerData : IDisposable | ||
{ | ||
public readonly HashSet<ITextView> TextViews = new(); | ||
|
||
private readonly CocoaTextBufferVisibilityTracker _tracker; | ||
private readonly ITextBuffer _subjectBuffer; | ||
|
||
/// <summary> | ||
/// The callbacks that want to be notified when our <see cref="TextViews"/> change visibility. Stored as an | ||
/// <see cref="ImmutableHashSet{T}"/> so we can enumerate it safely without it changing underneath us. | ||
/// </summary> | ||
public ImmutableHashSet<Action> Callbacks { get; private set; } = ImmutableHashSet<Action>.Empty; | ||
|
||
public VisibleTrackerData( | ||
CocoaTextBufferVisibilityTracker tracker, | ||
ITextBuffer subjectBuffer) | ||
{ | ||
_tracker = tracker; | ||
_subjectBuffer = subjectBuffer; | ||
UpdateAssociatedViews(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_tracker._threadingContext.ThrowIfNotOnUIThread(); | ||
|
||
// Shouldn't be disposing of this if we still have clients that want to hear about visibility changes. | ||
Contract.ThrowIfTrue(Callbacks.Count > 0); | ||
|
||
// Clear out all our textviews. This will disconnect us from any events we have registered with them. | ||
UpdateTextViews(Array.Empty<ITextView>()); | ||
|
||
Contract.ThrowIfTrue(TextViews.Count > 0); | ||
} | ||
|
||
public void AddCallback(Action callback) | ||
{ | ||
_tracker._threadingContext.ThrowIfNotOnUIThread(); | ||
this.Callbacks = this.Callbacks.Add(callback); | ||
} | ||
|
||
public void RemoveCallback(Action callback) | ||
{ | ||
_tracker._threadingContext.ThrowIfNotOnUIThread(); | ||
this.Callbacks = this.Callbacks.Remove(callback); | ||
} | ||
|
||
public void UpdateAssociatedViews() | ||
{ | ||
_tracker._threadingContext.ThrowIfNotOnUIThread(); | ||
|
||
// Update us to whatever the currently associated text views are for this buffer. | ||
UpdateTextViews(_tracker._associatedViewService.GetAssociatedTextViews(_subjectBuffer)); | ||
} | ||
|
||
private void UpdateTextViews(IEnumerable<ITextView> associatedTextViews) | ||
{ | ||
var removedViews = TextViews.Except(associatedTextViews); | ||
var addedViews = associatedTextViews.Except(TextViews); | ||
|
||
// Disconnect from hearing about visibility changes for any views we're no longer associated with. | ||
foreach (var removedView in removedViews) | ||
{ | ||
if (removedView is ICocoaTextView removedCocoaView) | ||
removedCocoaView.IsVisibleChanged -= VisualElement_IsVisibleChanged; | ||
} | ||
|
||
// Connect to hearing about visbility changes for any views we are associated with. | ||
foreach (var addedView in addedViews) | ||
{ | ||
if (addedView is ICocoaTextView addedCocoaView) | ||
addedCocoaView.IsVisibleChanged += VisualElement_IsVisibleChanged; | ||
} | ||
|
||
TextViews.Clear(); | ||
TextViews.AddRange(associatedTextViews); | ||
} | ||
|
||
private void VisualElement_IsVisibleChanged(object sender, EventArgs e) | ||
{ | ||
TriggerCallbacks(); | ||
} | ||
|
||
internal void TriggerCallbacks() | ||
{ | ||
_tracker._threadingContext.ThrowIfNotOnUIThread(); | ||
foreach (var callback in Callbacks) | ||
callback(); | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i haven't looked closely. but the presumption is that this is a straight port, just tweaked fort he cocoa visibility checks. if so, this is ok with me. if anything else was changed, lmk. thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, that is the intent! Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's just the commit I initially made I think. And that was the changeset, I think the diff itself is just removing
.VisualElement
and changing the event handler type.