Skip to content

Commit 9bc3a3b

Browse files
Merge pull request #414 from reduckted/feature/fonts-and-colors
Fonts and Colors
2 parents 2f8d335 + 203c1ef commit 9bc3a3b

File tree

62 files changed

+3669
-115
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+3669
-115
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Community.VisualStudio.Toolkit;
2+
using Microsoft.VisualStudio.Shell;
3+
using Task = System.Threading.Tasks.Task;
4+
5+
namespace TestExtension
6+
{
7+
[Command(PackageIds.FontsAndColorsWindow)]
8+
internal sealed class FontsAndColorsWindowCommand : BaseCommand<FontsAndColorsWindowCommand>
9+
{
10+
protected override async Task ExecuteAsync(OleMenuCmdEventArgs e) =>
11+
await FontsAndColorsWindow.ShowAsync();
12+
}
13+
}

demo/VSSDK.TestExtension/TestExtensionPackage.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ namespace VSSDK.TestExtension
1919
[ProvideToolWindow(typeof(ThemeWindow.Pane))]
2020
[ProvideFileIcon(".abc", "KnownMonikers.Reference")]
2121
[ProvideToolWindow(typeof(MultiInstanceWindow.Pane))]
22+
[ProvideToolWindow(typeof(FontsAndColorsWindow.Pane))]
2223
[ProvideMenuResource("Menus.ctmenu", 1)]
2324
[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionExists_string, PackageAutoLoadFlags.BackgroundLoad)]
2425
[ProvideService(typeof(RunnerWindowMessenger), IsAsyncQueryable = true)]
26+
[ProvideFontsAndColors(typeof(DemoFontAndColorProvider))]
2527
public sealed class TestExtensionPackage : ToolkitPackage
2628
{
2729
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
@@ -36,6 +38,9 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
3638
// Commands
3739
await this.RegisterCommandsAsync();
3840

41+
// Fonts and colors.
42+
await this.RegisterFontAndColorProvidersAsync();
43+
3944
VS.Events.DocumentEvents.AfterDocumentWindowHide += DocumentEvents_AfterDocumentWindowHide;
4045
VS.Events.DocumentEvents.Opened += DocumentEvents_Opened;
4146
VS.Events.DocumentEvents.Closed += DocumentEvents_Closed;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Runtime.InteropServices;
2+
using Community.VisualStudio.Toolkit;
3+
using Microsoft.VisualStudio.Shell.Interop;
4+
using Microsoft.VisualStudio.TextManager.Interop;
5+
6+
namespace TestExtension
7+
{
8+
[Guid("bfbcd352-3b43-4034-b951-7ca841a16c81")]
9+
public class DemoFontAndColorCategory : BaseFontAndColorCategory<DemoFontAndColorCategory>
10+
{
11+
public DemoFontAndColorCategory() : base(new FontDefinition("Consolas", 12)) { }
12+
13+
public override string Name => "Fonts and Colors Demo";
14+
15+
public ColorDefinition TopLeft { get; } = new(
16+
"Top Left",
17+
defaultBackground: VisualStudioColor.Indexed(COLORINDEX.CI_RED),
18+
defaultForeground: VisualStudioColor.Indexed(COLORINDEX.CI_WHITE),
19+
options: ColorDefinition.DefaultOptions | ColorOptions.AllowBoldChange
20+
);
21+
22+
public ColorDefinition TopRight { get; } = new(
23+
"Top Right",
24+
defaultBackground: VisualStudioColor.Automatic(),
25+
defaultForeground: VisualStudioColor.Automatic(),
26+
automaticBackground: VisualStudioColor.VsColor(__VSSYSCOLOREX.VSCOLOR_ENVIRONMENT_BACKGROUND),
27+
automaticForeground: VisualStudioColor.VsColor(__VSSYSCOLOREX.VSCOLOR_PANEL_TEXT),
28+
options: ColorDefinition.DefaultOptions | ColorOptions.AllowBoldChange
29+
);
30+
31+
public ColorDefinition BottomLeft { get; } = new(
32+
"Bottom Left",
33+
defaultBackground: VisualStudioColor.SysColor(13),
34+
defaultForeground: VisualStudioColor.SysColor(14),
35+
options: ColorOptions.AllowBackgroundChange | ColorOptions.AllowForegroundChange
36+
);
37+
38+
public ColorDefinition BottomRight { get; } = new(
39+
"Bottom Right",
40+
defaultBackground: VisualStudioColor.Indexed(COLORINDEX.CI_DARKGREEN),
41+
defaultForeground: VisualStudioColor.Indexed(COLORINDEX.CI_WHITE)
42+
);
43+
}
44+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using Community.VisualStudio.Toolkit;
4+
5+
namespace TestExtension
6+
{
7+
[Guid("d2bc5f5f-6c24-4f6c-b6ef-aea775be5fa4")]
8+
public class DemoFontAndColorProvider : BaseFontAndColorProvider { }
9+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using System.Windows;
6+
using Community.VisualStudio.Toolkit;
7+
using Microsoft.VisualStudio.Imaging;
8+
9+
namespace TestExtension
10+
{
11+
public class FontsAndColorsWindow : BaseToolWindow<FontsAndColorsWindow>
12+
{
13+
public override string GetTitle(int toolWindowId) => "Fonts and Colors Window";
14+
15+
public override Type PaneType => typeof(Pane);
16+
17+
public override async Task<FrameworkElement> CreateAsync(int toolWindowId, CancellationToken cancellationToken)
18+
{
19+
return new FontsAndColorsWindowControl
20+
{
21+
DataContext = new FontsAndColorsWindowViewModel(
22+
await VS.FontsAndColors.GetConfiguredFontAndColorsAsync<DemoFontAndColorCategory>(),
23+
Package.JoinableTaskFactory
24+
)
25+
};
26+
}
27+
28+
[Guid("b7141d35-7b95-4ad0-a37d-58220c1aa5e3")]
29+
internal class Pane : ToolkitToolWindowPane
30+
{
31+
public Pane()
32+
{
33+
BitmapImageMoniker = KnownMonikers.ColorDialog;
34+
}
35+
36+
protected override void Dispose(bool disposing)
37+
{
38+
((Content as FontsAndColorsWindowControl).DataContext as IDisposable)?.Dispose();
39+
base.Dispose(disposing);
40+
}
41+
}
42+
}
43+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<UserControl
2+
x:Class="TestExtension.FontsAndColorsWindowControl"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7+
xmlns:local="clr-namespace:TestExtension"
8+
xmlns:toolkit="clr-namespace:Community.VisualStudio.Toolkit;assembly=Community.VisualStudio.Toolkit"
9+
mc:Ignorable="d"
10+
d:DesignHeight="450" d:DesignWidth="800"
11+
d:DataContext="{d:DesignInstance Type=local:FontsAndColorsWindowViewModel, IsDesignTimeCreatable=False}"
12+
toolkit:Themes.UseVsTheme="True"
13+
>
14+
15+
<Grid Margin="10">
16+
<Grid.RowDefinitions>
17+
<RowDefinition Height="Auto"/>
18+
<RowDefinition Height="*"/>
19+
<RowDefinition Height="100"/>
20+
</Grid.RowDefinitions>
21+
22+
<Button Grid.Row="0" Command="{Binding EditFontsAndColorsCommand}">
23+
Edit Fonts and Colors...
24+
</Button>
25+
26+
<Grid
27+
Grid.Row="1"
28+
Margin="0,10"
29+
TextElement.FontFamily="{Binding Font.Family}"
30+
TextElement.FontSize="{Binding Font.Size}"
31+
>
32+
33+
<Grid.Resources>
34+
<Style TargetType="TextBlock">
35+
<Setter Property="HorizontalAlignment" Value="Center"/>
36+
<Setter Property="VerticalAlignment" Value="Center"/>
37+
</Style>
38+
</Grid.Resources>
39+
40+
<Grid.ColumnDefinitions>
41+
<ColumnDefinition Width="*"/>
42+
<ColumnDefinition Width="10"/>
43+
<ColumnDefinition Width="*"/>
44+
</Grid.ColumnDefinitions>
45+
46+
<Grid.RowDefinitions>
47+
<RowDefinition Height="*"/>
48+
<RowDefinition Height="10"/>
49+
<RowDefinition Height="*"/>
50+
</Grid.RowDefinitions>
51+
52+
<Border
53+
Grid.Column="0"
54+
Grid.Row="0"
55+
Background="{Binding TopLeft.BackgroundBrush}"
56+
TextElement.Foreground="{Binding TopLeft.ForegroundBrush}"
57+
TextElement.FontWeight="{Binding TopLeft.FontWeight}"
58+
>
59+
<TextBlock>
60+
Top Left
61+
<LineBreak/>
62+
Default: Red / White
63+
</TextBlock>
64+
</Border>
65+
66+
<Border
67+
Grid.Column="2"
68+
Grid.Row="0"
69+
Background="{Binding TopRight.BackgroundBrush}"
70+
TextElement.Foreground="{Binding TopRight.ForegroundBrush}"
71+
TextElement.FontWeight="{Binding TopRight.FontWeight}"
72+
>
73+
<TextBlock>
74+
Top Right
75+
<LineBreak/>
76+
Default: Auto / Auto
77+
</TextBlock>
78+
</Border>
79+
80+
<Border
81+
Grid.Column="0"
82+
Grid.Row="2"
83+
Background="{Binding BottomLeft.BackgroundBrush}"
84+
TextElement.Foreground="{Binding BottomLeft.ForegroundBrush}"
85+
TextElement.FontWeight="{Binding BottomLeft.FontWeight}"
86+
>
87+
<TextBlock>
88+
Bottom Left
89+
<LineBreak/>
90+
Default: Yellow / Black
91+
</TextBlock>
92+
</Border>
93+
94+
<Border
95+
Grid.Column="2"
96+
Grid.Row="2"
97+
Background="{Binding BottomRight.BackgroundBrush}"
98+
TextElement.Foreground="{Binding BottomRight.ForegroundBrush}"
99+
TextElement.FontWeight="{Binding BottomRight.FontWeight}"
100+
>
101+
<TextBlock>
102+
Bottom Right
103+
<LineBreak/>
104+
Default: Green / White
105+
</TextBlock>
106+
</Border>
107+
</Grid>
108+
109+
<ListBox Grid.Row="2" ItemsSource="{Binding Events}"/>
110+
</Grid>
111+
</UserControl>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Windows.Controls;
2+
using System.Windows.Documents;
3+
using Community.VisualStudio.Toolkit;
4+
5+
namespace TestExtension
6+
{
7+
public partial class FontsAndColorsWindowControl : UserControl
8+
{
9+
public FontsAndColorsWindowControl()
10+
{
11+
InitializeComponent();
12+
}
13+
14+
private void ApplyColor(Border border, ConfiguredColor color)
15+
{
16+
// Bind the border's properties to the configured
17+
// color properties. This could also be done in XAML.
18+
border.DataContext = color;
19+
border.SetBinding(BackgroundProperty, nameof(ConfiguredColor.BackgroundBrush));
20+
border.SetBinding(TextElement.ForegroundProperty, nameof(ConfiguredColor.ForegroundBrush));
21+
border.SetBinding(TextElement.FontWeightProperty, nameof(ConfiguredColor.FontWeight));
22+
}
23+
}
24+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Collections.ObjectModel;
3+
using System.Windows.Input;
4+
using Community.VisualStudio.Toolkit;
5+
using Microsoft.VisualStudio.PlatformUI;
6+
using Microsoft.VisualStudio.Shell;
7+
using Microsoft.VisualStudio.Threading;
8+
9+
namespace TestExtension
10+
{
11+
public class FontsAndColorsWindowViewModel : IDisposable
12+
{
13+
private readonly ConfiguredFontAndColorSet<DemoFontAndColorCategory> _fontAndColors;
14+
private readonly ObservableCollection<string> _events;
15+
16+
public FontsAndColorsWindowViewModel(ConfiguredFontAndColorSet<DemoFontAndColorCategory> fontAndColors, JoinableTaskFactory joinableTaskFactory)
17+
{
18+
// Remember the font and color set so that
19+
// we can dispose of it when we are disposed.
20+
_fontAndColors = fontAndColors;
21+
22+
EditFontsAndColorsCommand = new DelegateCommand(
23+
() => VS.Commands.ExecuteAsync(
24+
KnownCommands.Tools_Options,
25+
"{57F6B7D2-1436-11D1-883C-0000F87579D2}"
26+
).FireAndForget(),
27+
() => true,
28+
joinableTaskFactory
29+
);
30+
31+
_events = new ObservableCollection<string>();
32+
Events = new ReadOnlyObservableCollection<string>(_events);
33+
34+
fontAndColors.FontChanged += OnFontChanged;
35+
fontAndColors.ColorChanged += OnColorChanged;
36+
37+
TopLeft = fontAndColors.GetColor(fontAndColors.Category.TopLeft);
38+
TopRight = fontAndColors.GetColor(fontAndColors.Category.TopRight);
39+
BottomLeft = fontAndColors.GetColor(fontAndColors.Category.BottomLeft);
40+
BottomRight = fontAndColors.GetColor(fontAndColors.Category.BottomRight);
41+
}
42+
43+
private void OnFontChanged(object sender, EventArgs e)
44+
{
45+
_events.Insert(0, $"{DateTime.Now}: Font Changed");
46+
}
47+
48+
private void OnColorChanged(object sender, ConfiguredColorChangedEventArgs e)
49+
{
50+
_events.Insert(0, $"{DateTime.Now}: Color Changed - {e.Definition.Name}");
51+
}
52+
53+
public ICommand EditFontsAndColorsCommand { get; }
54+
55+
public ReadOnlyObservableCollection<string> Events { get; }
56+
57+
public ConfiguredFont Font => _fontAndColors.Font;
58+
59+
public ConfiguredColor TopLeft { get; }
60+
61+
public ConfiguredColor TopRight { get; }
62+
63+
public ConfiguredColor BottomLeft { get; }
64+
65+
public ConfiguredColor BottomRight { get; }
66+
67+
public void Dispose()
68+
{
69+
_fontAndColors.FontChanged -= OnFontChanged;
70+
_fontAndColors.ColorChanged -= OnColorChanged;
71+
72+
// Dispose of the font and color set so that it stops
73+
// listening for changes to the font and colors.
74+
_fontAndColors.Dispose();
75+
}
76+
}
77+
}

demo/VSSDK.TestExtension/VSCommandTable.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ internal sealed partial class PackageIds
4242
public const int LoadSelectedProject = 0x0112;
4343
public const int UnloadSelectedProject = 0x0113;
4444
public const int SendMessageToRunnerWindow = 0x0114;
45+
public const int FontsAndColorsWindow = 0x0115;
4546
public const int EditProjectFile = 0x2001;
4647
public const int RunnerWindowToolbar = 0x0BB8;
4748
}

demo/VSSDK.TestExtension/VSCommandTable.vsct

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@
8686
</Strings>
8787
</Button>
8888

89+
<Button guid="TestExtension" id="FontsAndColorsWindow" priority="0x0108" type="Button">
90+
<Parent guid="VSMainMenu" id="View.DevWindowsGroup.OtherWindows.Group1"/>
91+
<Icon guid="ImageCatalogGuid" id="ColorDialog" />
92+
<CommandFlag>IconIsMoniker</CommandFlag>
93+
<Strings>
94+
<ButtonText>Fonts and Colors Window</ButtonText>
95+
</Strings>
96+
</Button>
97+
8998
<Button guid="TestExtension" id="VsixManifestSolutionExplorerFilter" priority="0x0400" type="Button">
9099
<Parent guid="guidSHLMainMenu" id="IDG_VS_TOOLBAR_PROJWIN_FILTERS" />
91100
<Icon guid="ImageCatalogGuid" id="VisualStudioSettingsFile" />
@@ -237,6 +246,7 @@
237246
<IDSymbol name="LoadSelectedProject" value="0x0112" />
238247
<IDSymbol name="UnloadSelectedProject" value="0x0113" />
239248
<IDSymbol name="SendMessageToRunnerWindow" value="0x0114" />
249+
<IDSymbol name="FontsAndColorsWindow" value="0x0115" />
240250

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

0 commit comments

Comments
 (0)