Skip to content

Commit

Permalink
Refactor MyToolWindow and MyToolWindowData
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Willis committed Aug 29, 2024
1 parent 5a72615 commit 6eefab7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace SettingsSample;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Extensibility;
using Microsoft.VisualStudio.Extensibility.Commands;
using Microsoft.VisualStudio.Extensibility.ToolWindows;
using Microsoft.VisualStudio.RpcContracts.RemoteUI;

Expand All @@ -16,7 +15,7 @@ namespace SettingsSample;
[VisualStudioContribution]
public class MyToolWindow : ToolWindow
{
private object? dataContext;
private MyToolWindowData? dataContext;

/// <summary>
/// Initializes a new instance of the <see cref="MyToolWindow" /> class.
Expand All @@ -37,8 +36,7 @@ public MyToolWindow()
public override Task InitializeAsync(CancellationToken cancellationToken)
{
this.dataContext = new MyToolWindowData(this.Extensibility);

return Task.CompletedTask;
return this.dataContext.InitializeAsync(cancellationToken);
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@ internal class MyToolWindowControl : RemoteUserControl
/// <param name="dataContext">
/// Data context of the remote control which can be referenced from xaml through data binding.
/// </param>
/// <param name="synchronizationContext">
/// Optional synchronizationContext that the extender can provide to ensure that <see cref="IAsyncCommand"/>
/// are executed and properties are read and updated from the extension main thread.
/// </param>
public MyToolWindowControl(object? dataContext, SynchronizationContext? synchronizationContext = null)
: base(dataContext, synchronizationContext)
public MyToolWindowControl(object? dataContext)
: base(dataContext)
{
}
}
20 changes: 14 additions & 6 deletions New_Extensibility_Model/Samples/SettingsSample/MyToolWindowData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace SettingsSample;
using Microsoft.VisualStudio.Extensibility;
using Microsoft.VisualStudio.Extensibility.Settings;
using Microsoft.VisualStudio.Extensibility.UI;
using Microsoft.VisualStudio.Threading;

/// <summary>
/// A sample data context object to use with tool window UI content.
Expand Down Expand Up @@ -43,8 +42,6 @@ public MyToolWindowData(VisualStudioExtensibility extensibility)
this.extensibility = Requires.NotNull(extensibility, nameof(extensibility));

this.UpdateCommand = new AsyncCommand(this.UpdateAsync);

this.InitializeSettingsAsync(extensibility).Forget();
}

/// <summary>
Expand Down Expand Up @@ -77,13 +74,23 @@ public string SampleText
set => this.SetProperty(ref this.sampleText, value);
}

/// <summary>
/// Initializes the current instance of <see cref="MyToolWindowData"/>.
/// </summary>
/// <param name="cancellationToken">Cancellation token to monitor.</param>
/// <returns>Task indicating completion of initialization.</returns>
public Task InitializeAsync(CancellationToken cancellationToken)
{
return this.InitializeSettingsAsync(cancellationToken);
}

#pragma warning disable VSEXTPREVIEW_SETTINGS // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.

private async Task InitializeSettingsAsync(VisualStudioExtensibility extensibility)
private async Task InitializeSettingsAsync(CancellationToken cancellationToken)
{
await extensibility.Settings().SubscribeAsync(
await this.extensibility.Settings().SubscribeAsync(
[SettingDefinitions.SettingsSampleCategory],
CancellationToken.None,
cancellationToken,
values =>
{
if (values.TryGetValue(SettingDefinitions.AutoUpdateSetting.FullId, out ISettingValue? autoUpdateValue))
Expand Down Expand Up @@ -127,5 +134,6 @@ private void UpdateSampleTextFromSettings(SettingValues values)
};
}
}

#pragma warning restore VSEXTPREVIEW_SETTINGS // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
}

0 comments on commit 6eefab7

Please sign in to comment.