From 8b4ca15be705594b5822d2eb781ae05accfb4042 Mon Sep 17 00:00:00 2001 From: Charles Willis <5862883+trippwill@users.noreply.github.com> Date: Mon, 9 Sep 2024 18:40:48 +0100 Subject: [PATCH] Add sample for Settings API (#412) * Add sample for Settings API --------- Co-authored-by: Charles Willis --- .../.vsextension/string-resources.json | 14 ++ .../Samples/SettingsSample/MyToolWindow.cs | 47 ++++++ .../SettingsSample/MyToolWindowCommand.cs | 29 ++++ .../SettingsSample/MyToolWindowControl.cs | 24 +++ .../SettingsSample/MyToolWindowControl.xaml | 31 ++++ .../SettingsSample/MyToolWindowData.cs | 139 ++++++++++++++++++ .../Samples/SettingsSample/README.md | 83 +++++++++++ .../SettingsSample/SettingDefinitions.cs | 43 ++++++ .../SettingsSample/SettingsSample.csproj | 18 +++ .../SettingsSample/SettingsSampleExtension.cs | 31 ++++ 10 files changed, 459 insertions(+) create mode 100644 New_Extensibility_Model/Samples/SettingsSample/.vsextension/string-resources.json create mode 100644 New_Extensibility_Model/Samples/SettingsSample/MyToolWindow.cs create mode 100644 New_Extensibility_Model/Samples/SettingsSample/MyToolWindowCommand.cs create mode 100644 New_Extensibility_Model/Samples/SettingsSample/MyToolWindowControl.cs create mode 100644 New_Extensibility_Model/Samples/SettingsSample/MyToolWindowControl.xaml create mode 100644 New_Extensibility_Model/Samples/SettingsSample/MyToolWindowData.cs create mode 100644 New_Extensibility_Model/Samples/SettingsSample/README.md create mode 100644 New_Extensibility_Model/Samples/SettingsSample/SettingDefinitions.cs create mode 100644 New_Extensibility_Model/Samples/SettingsSample/SettingsSample.csproj create mode 100644 New_Extensibility_Model/Samples/SettingsSample/SettingsSampleExtension.cs diff --git a/New_Extensibility_Model/Samples/SettingsSample/.vsextension/string-resources.json b/New_Extensibility_Model/Samples/SettingsSample/.vsextension/string-resources.json new file mode 100644 index 0000000..46593c6 --- /dev/null +++ b/New_Extensibility_Model/Samples/SettingsSample/.vsextension/string-resources.json @@ -0,0 +1,14 @@ +{ + "SettingsSample.MyToolWindowCommand.DisplayName": "Sample Text Tool Window", + "SettingsSample.Settings.Category.DisplayName": "Settings Sample", + "SettingsSample.Settings.Category.Description": "Settings for the LoremIpsum sample generator.", + "SettingsSample.Settings.AutoUpdate.DisplayName": "Auto Update", + "SettingsSample.Settings.AutoUpdate.Description": "Whether to update the sample text when a setting changes.", + "SettingsSample.Settings.TextLength.DisplayName": "Text Length", + "SettingsSample.Settings.TextLength.Description": "Number of characters to include in the generated text.", + "SettingsSample.Settings.QuoteStyle.DisplayName": "Quote Style", + "SettingsSample.Settings.QuoteStyle.Description": "Style of quotes to enclose the generated text.", + "SettingsSample.Settings.QuoteStyle.None": "None", + "SettingsSample.Settings.QuoteStyle.Single": "Single", + "SettingsSample.Settings.QuoteStyle.Double": "Double" +} \ No newline at end of file diff --git a/New_Extensibility_Model/Samples/SettingsSample/MyToolWindow.cs b/New_Extensibility_Model/Samples/SettingsSample/MyToolWindow.cs new file mode 100644 index 0000000..725958f --- /dev/null +++ b/New_Extensibility_Model/Samples/SettingsSample/MyToolWindow.cs @@ -0,0 +1,47 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace SettingsSample; + +using System.Threading; +using System.Threading.Tasks; +using Microsoft.VisualStudio.Extensibility; +using Microsoft.VisualStudio.Extensibility.ToolWindows; +using Microsoft.VisualStudio.RpcContracts.RemoteUI; + +/// +/// A sample tool window. +/// +[VisualStudioContribution] +public class MyToolWindow : ToolWindow +{ + private MyToolWindowData? dataContext; + + /// + /// Initializes a new instance of the class. + /// + public MyToolWindow() + { + this.Title = "Settings Sample Tool Window"; + } + + /// + public override ToolWindowConfiguration ToolWindowConfiguration => new() + { + Placement = ToolWindowPlacement.DocumentWell, + AllowAutoCreation = false, + }; + + /// + public override Task InitializeAsync(CancellationToken cancellationToken) + { + this.dataContext = new MyToolWindowData(this.Extensibility); + return this.dataContext.InitializeAsync(cancellationToken); + } + + /// + public override Task GetContentAsync(CancellationToken cancellationToken) + { + return Task.FromResult(new MyToolWindowControl(this.dataContext)); + } +} diff --git a/New_Extensibility_Model/Samples/SettingsSample/MyToolWindowCommand.cs b/New_Extensibility_Model/Samples/SettingsSample/MyToolWindowCommand.cs new file mode 100644 index 0000000..2ca4cf5 --- /dev/null +++ b/New_Extensibility_Model/Samples/SettingsSample/MyToolWindowCommand.cs @@ -0,0 +1,29 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace SettingsSample; + +using System.Threading; +using System.Threading.Tasks; +using Microsoft.VisualStudio.Extensibility; +using Microsoft.VisualStudio.Extensibility.Commands; + +/// +/// A sample command for showing a tool window. +/// +[VisualStudioContribution] +public class MyToolWindowCommand : Command +{ + /// + public override CommandConfiguration CommandConfiguration => new("%SettingsSample.MyToolWindowCommand.DisplayName%") + { + Placements = [CommandPlacement.KnownPlacements.ToolsMenu], + Icon = new(ImageMoniker.KnownValues.ToolWindow, IconSettings.IconAndText), + }; + + /// + public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken) + { + await this.Extensibility.Shell().ShowToolWindowAsync(activate: true, cancellationToken); + } +} diff --git a/New_Extensibility_Model/Samples/SettingsSample/MyToolWindowControl.cs b/New_Extensibility_Model/Samples/SettingsSample/MyToolWindowControl.cs new file mode 100644 index 0000000..1e95b6b --- /dev/null +++ b/New_Extensibility_Model/Samples/SettingsSample/MyToolWindowControl.cs @@ -0,0 +1,24 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace SettingsSample; + +using System.Threading; +using Microsoft.VisualStudio.Extensibility.UI; + +/// +/// A sample remote user control to use as tool window UI content. +/// +internal class MyToolWindowControl : RemoteUserControl +{ + /// + /// Initializes a new instance of the class. + /// + /// + /// Data context of the remote control which can be referenced from xaml through data binding. + /// + public MyToolWindowControl(object? dataContext) + : base(dataContext) + { + } +} diff --git a/New_Extensibility_Model/Samples/SettingsSample/MyToolWindowControl.xaml b/New_Extensibility_Model/Samples/SettingsSample/MyToolWindowControl.xaml new file mode 100644 index 0000000..fc320a6 --- /dev/null +++ b/New_Extensibility_Model/Samples/SettingsSample/MyToolWindowControl.xaml @@ -0,0 +1,31 @@ + + + + + + +