Skip to content

Commit

Permalink
Merge pull request #414 from reduckted/feature/fonts-and-colors
Browse files Browse the repository at this point in the history
Fonts and Colors
  • Loading branch information
madskristensen authored Aug 19, 2024
2 parents 2f8d335 + 203c1ef commit 9bc3a3b
Show file tree
Hide file tree
Showing 62 changed files with 3,669 additions and 115 deletions.
13 changes: 13 additions & 0 deletions demo/VSSDK.TestExtension/Commands/FontsAndColorsWindowCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Community.VisualStudio.Toolkit;
using Microsoft.VisualStudio.Shell;
using Task = System.Threading.Tasks.Task;

namespace TestExtension
{
[Command(PackageIds.FontsAndColorsWindow)]
internal sealed class FontsAndColorsWindowCommand : BaseCommand<FontsAndColorsWindowCommand>
{
protected override async Task ExecuteAsync(OleMenuCmdEventArgs e) =>
await FontsAndColorsWindow.ShowAsync();
}
}
5 changes: 5 additions & 0 deletions demo/VSSDK.TestExtension/TestExtensionPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ namespace VSSDK.TestExtension
[ProvideToolWindow(typeof(ThemeWindow.Pane))]
[ProvideFileIcon(".abc", "KnownMonikers.Reference")]
[ProvideToolWindow(typeof(MultiInstanceWindow.Pane))]
[ProvideToolWindow(typeof(FontsAndColorsWindow.Pane))]
[ProvideMenuResource("Menus.ctmenu", 1)]
[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionExists_string, PackageAutoLoadFlags.BackgroundLoad)]
[ProvideService(typeof(RunnerWindowMessenger), IsAsyncQueryable = true)]
[ProvideFontsAndColors(typeof(DemoFontAndColorProvider))]
public sealed class TestExtensionPackage : ToolkitPackage
{
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
Expand All @@ -36,6 +38,9 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
// Commands
await this.RegisterCommandsAsync();

// Fonts and colors.
await this.RegisterFontAndColorProvidersAsync();

VS.Events.DocumentEvents.AfterDocumentWindowHide += DocumentEvents_AfterDocumentWindowHide;
VS.Events.DocumentEvents.Opened += DocumentEvents_Opened;
VS.Events.DocumentEvents.Closed += DocumentEvents_Closed;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Runtime.InteropServices;
using Community.VisualStudio.Toolkit;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.TextManager.Interop;

namespace TestExtension
{
[Guid("bfbcd352-3b43-4034-b951-7ca841a16c81")]
public class DemoFontAndColorCategory : BaseFontAndColorCategory<DemoFontAndColorCategory>
{
public DemoFontAndColorCategory() : base(new FontDefinition("Consolas", 12)) { }

public override string Name => "Fonts and Colors Demo";

public ColorDefinition TopLeft { get; } = new(
"Top Left",
defaultBackground: VisualStudioColor.Indexed(COLORINDEX.CI_RED),
defaultForeground: VisualStudioColor.Indexed(COLORINDEX.CI_WHITE),
options: ColorDefinition.DefaultOptions | ColorOptions.AllowBoldChange
);

public ColorDefinition TopRight { get; } = new(
"Top Right",
defaultBackground: VisualStudioColor.Automatic(),
defaultForeground: VisualStudioColor.Automatic(),
automaticBackground: VisualStudioColor.VsColor(__VSSYSCOLOREX.VSCOLOR_ENVIRONMENT_BACKGROUND),
automaticForeground: VisualStudioColor.VsColor(__VSSYSCOLOREX.VSCOLOR_PANEL_TEXT),
options: ColorDefinition.DefaultOptions | ColorOptions.AllowBoldChange
);

public ColorDefinition BottomLeft { get; } = new(
"Bottom Left",
defaultBackground: VisualStudioColor.SysColor(13),
defaultForeground: VisualStudioColor.SysColor(14),
options: ColorOptions.AllowBackgroundChange | ColorOptions.AllowForegroundChange
);

public ColorDefinition BottomRight { get; } = new(
"Bottom Right",
defaultBackground: VisualStudioColor.Indexed(COLORINDEX.CI_DARKGREEN),
defaultForeground: VisualStudioColor.Indexed(COLORINDEX.CI_WHITE)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;
using System.Runtime.InteropServices;
using Community.VisualStudio.Toolkit;

namespace TestExtension
{
[Guid("d2bc5f5f-6c24-4f6c-b6ef-aea775be5fa4")]
public class DemoFontAndColorProvider : BaseFontAndColorProvider { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Community.VisualStudio.Toolkit;
using Microsoft.VisualStudio.Imaging;

namespace TestExtension
{
public class FontsAndColorsWindow : BaseToolWindow<FontsAndColorsWindow>
{
public override string GetTitle(int toolWindowId) => "Fonts and Colors Window";

public override Type PaneType => typeof(Pane);

public override async Task<FrameworkElement> CreateAsync(int toolWindowId, CancellationToken cancellationToken)
{
return new FontsAndColorsWindowControl
{
DataContext = new FontsAndColorsWindowViewModel(
await VS.FontsAndColors.GetConfiguredFontAndColorsAsync<DemoFontAndColorCategory>(),
Package.JoinableTaskFactory
)
};
}

[Guid("b7141d35-7b95-4ad0-a37d-58220c1aa5e3")]
internal class Pane : ToolkitToolWindowPane
{
public Pane()
{
BitmapImageMoniker = KnownMonikers.ColorDialog;
}

protected override void Dispose(bool disposing)
{
((Content as FontsAndColorsWindowControl).DataContext as IDisposable)?.Dispose();
base.Dispose(disposing);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<UserControl
x:Class="TestExtension.FontsAndColorsWindowControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:TestExtension"
xmlns:toolkit="clr-namespace:Community.VisualStudio.Toolkit;assembly=Community.VisualStudio.Toolkit"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
d:DataContext="{d:DesignInstance Type=local:FontsAndColorsWindowViewModel, IsDesignTimeCreatable=False}"
toolkit:Themes.UseVsTheme="True"
>

<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>

<Button Grid.Row="0" Command="{Binding EditFontsAndColorsCommand}">
Edit Fonts and Colors...
</Button>

<Grid
Grid.Row="1"
Margin="0,10"
TextElement.FontFamily="{Binding Font.Family}"
TextElement.FontSize="{Binding Font.Size}"
>

<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
</Grid.Resources>

<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>

<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="10"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<Border
Grid.Column="0"
Grid.Row="0"
Background="{Binding TopLeft.BackgroundBrush}"
TextElement.Foreground="{Binding TopLeft.ForegroundBrush}"
TextElement.FontWeight="{Binding TopLeft.FontWeight}"
>
<TextBlock>
Top Left
<LineBreak/>
Default: Red / White
</TextBlock>
</Border>

<Border
Grid.Column="2"
Grid.Row="0"
Background="{Binding TopRight.BackgroundBrush}"
TextElement.Foreground="{Binding TopRight.ForegroundBrush}"
TextElement.FontWeight="{Binding TopRight.FontWeight}"
>
<TextBlock>
Top Right
<LineBreak/>
Default: Auto / Auto
</TextBlock>
</Border>

<Border
Grid.Column="0"
Grid.Row="2"
Background="{Binding BottomLeft.BackgroundBrush}"
TextElement.Foreground="{Binding BottomLeft.ForegroundBrush}"
TextElement.FontWeight="{Binding BottomLeft.FontWeight}"
>
<TextBlock>
Bottom Left
<LineBreak/>
Default: Yellow / Black
</TextBlock>
</Border>

<Border
Grid.Column="2"
Grid.Row="2"
Background="{Binding BottomRight.BackgroundBrush}"
TextElement.Foreground="{Binding BottomRight.ForegroundBrush}"
TextElement.FontWeight="{Binding BottomRight.FontWeight}"
>
<TextBlock>
Bottom Right
<LineBreak/>
Default: Green / White
</TextBlock>
</Border>
</Grid>

<ListBox Grid.Row="2" ItemsSource="{Binding Events}"/>
</Grid>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Windows.Controls;
using System.Windows.Documents;
using Community.VisualStudio.Toolkit;

namespace TestExtension
{
public partial class FontsAndColorsWindowControl : UserControl
{
public FontsAndColorsWindowControl()
{
InitializeComponent();
}

private void ApplyColor(Border border, ConfiguredColor color)
{
// Bind the border's properties to the configured
// color properties. This could also be done in XAML.
border.DataContext = color;
border.SetBinding(BackgroundProperty, nameof(ConfiguredColor.BackgroundBrush));
border.SetBinding(TextElement.ForegroundProperty, nameof(ConfiguredColor.ForegroundBrush));
border.SetBinding(TextElement.FontWeightProperty, nameof(ConfiguredColor.FontWeight));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Community.VisualStudio.Toolkit;
using Microsoft.VisualStudio.PlatformUI;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Threading;

namespace TestExtension
{
public class FontsAndColorsWindowViewModel : IDisposable
{
private readonly ConfiguredFontAndColorSet<DemoFontAndColorCategory> _fontAndColors;
private readonly ObservableCollection<string> _events;

public FontsAndColorsWindowViewModel(ConfiguredFontAndColorSet<DemoFontAndColorCategory> fontAndColors, JoinableTaskFactory joinableTaskFactory)
{
// Remember the font and color set so that
// we can dispose of it when we are disposed.
_fontAndColors = fontAndColors;

EditFontsAndColorsCommand = new DelegateCommand(
() => VS.Commands.ExecuteAsync(
KnownCommands.Tools_Options,
"{57F6B7D2-1436-11D1-883C-0000F87579D2}"
).FireAndForget(),
() => true,
joinableTaskFactory
);

_events = new ObservableCollection<string>();
Events = new ReadOnlyObservableCollection<string>(_events);

fontAndColors.FontChanged += OnFontChanged;
fontAndColors.ColorChanged += OnColorChanged;

TopLeft = fontAndColors.GetColor(fontAndColors.Category.TopLeft);
TopRight = fontAndColors.GetColor(fontAndColors.Category.TopRight);
BottomLeft = fontAndColors.GetColor(fontAndColors.Category.BottomLeft);
BottomRight = fontAndColors.GetColor(fontAndColors.Category.BottomRight);
}

private void OnFontChanged(object sender, EventArgs e)
{
_events.Insert(0, $"{DateTime.Now}: Font Changed");
}

private void OnColorChanged(object sender, ConfiguredColorChangedEventArgs e)
{
_events.Insert(0, $"{DateTime.Now}: Color Changed - {e.Definition.Name}");
}

public ICommand EditFontsAndColorsCommand { get; }

public ReadOnlyObservableCollection<string> Events { get; }

public ConfiguredFont Font => _fontAndColors.Font;

public ConfiguredColor TopLeft { get; }

public ConfiguredColor TopRight { get; }

public ConfiguredColor BottomLeft { get; }

public ConfiguredColor BottomRight { get; }

public void Dispose()
{
_fontAndColors.FontChanged -= OnFontChanged;
_fontAndColors.ColorChanged -= OnColorChanged;

// Dispose of the font and color set so that it stops
// listening for changes to the font and colors.
_fontAndColors.Dispose();
}
}
}
1 change: 1 addition & 0 deletions demo/VSSDK.TestExtension/VSCommandTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ internal sealed partial class PackageIds
public const int LoadSelectedProject = 0x0112;
public const int UnloadSelectedProject = 0x0113;
public const int SendMessageToRunnerWindow = 0x0114;
public const int FontsAndColorsWindow = 0x0115;
public const int EditProjectFile = 0x2001;
public const int RunnerWindowToolbar = 0x0BB8;
}
Expand Down
10 changes: 10 additions & 0 deletions demo/VSSDK.TestExtension/VSCommandTable.vsct
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@
</Strings>
</Button>

<Button guid="TestExtension" id="FontsAndColorsWindow" priority="0x0108" type="Button">
<Parent guid="VSMainMenu" id="View.DevWindowsGroup.OtherWindows.Group1"/>
<Icon guid="ImageCatalogGuid" id="ColorDialog" />
<CommandFlag>IconIsMoniker</CommandFlag>
<Strings>
<ButtonText>Fonts and Colors Window</ButtonText>
</Strings>
</Button>

<Button guid="TestExtension" id="VsixManifestSolutionExplorerFilter" priority="0x0400" type="Button">
<Parent guid="guidSHLMainMenu" id="IDG_VS_TOOLBAR_PROJWIN_FILTERS" />
<Icon guid="ImageCatalogGuid" id="VisualStudioSettingsFile" />
Expand Down Expand Up @@ -237,6 +246,7 @@
<IDSymbol name="LoadSelectedProject" value="0x0112" />
<IDSymbol name="UnloadSelectedProject" value="0x0113" />
<IDSymbol name="SendMessageToRunnerWindow" value="0x0114" />
<IDSymbol name="FontsAndColorsWindow" value="0x0115" />

<IDSymbol name="EditProjectFile" value="0x2001" />

Expand Down
Loading

0 comments on commit 9bc3a3b

Please sign in to comment.