Skip to content

Commit

Permalink
Using autload
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanKert committed Apr 6, 2019
1 parent 0e8d802 commit 7b7d4a7
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 128 deletions.
6 changes: 3 additions & 3 deletions src/BuildVision.UI/Common/Logging/TraceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private static void TraceAction(string message, EventLogEntryType type)
switch (type)
{
case EventLogEntryType.Error:
ActivityLog.LogError(Resources.ProductName, message);
//ActivityLog.LogError(Resources.ProductName, message);
#if DEBUG
System.Diagnostics.Trace.TraceError(message);
MessageBox.Show(message, Resources.ProductName + " error", MessageBoxButton.OK, MessageBoxImage.Error);
Expand All @@ -77,15 +77,15 @@ private static void TraceAction(string message, EventLogEntryType type)

case EventLogEntryType.Warning:
case EventLogEntryType.FailureAudit:
ActivityLog.LogWarning(Resources.ProductName, message);
//ActivityLog.LogWarning(Resources.ProductName, message);
#if DEBUG
System.Diagnostics.Trace.TraceWarning(message);
#endif
break;

case EventLogEntryType.Information:
case EventLogEntryType.SuccessAudit:
ActivityLog.LogInformation(Resources.ProductName, message);
//ActivityLog.LogInformation(Resources.ProductName, message);
#if DEBUG
System.Diagnostics.Trace.TraceInformation(message);
#endif
Expand Down
1 change: 1 addition & 0 deletions src/BuildVision/BuildVision.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
<Reference Include="System.Xaml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Commands\ShowToolWindowCommand.cs" />
<Compile Include="Constants\EnvDTECodeModelLanguageConstants2.cs" />
<Compile Include="Extensions\IServiceProviderExtensions.cs" />
<Compile Include="Services\BuildInformationProvider.cs" />
Expand Down
71 changes: 6 additions & 65 deletions src/BuildVision/BuildVision.vsct
Original file line number Diff line number Diff line change
@@ -1,71 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<CommandTable xmlns="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<!-- This is the file that defines the actual layout and type of the commands.
It is divided in different sections (e.g. command definition, command
placement, ...), with each defining a specific set of properties.
See the comment before each section for more details about how to
use it. -->

<!-- The VSCT compiler (the tool that translates this file into the binary
format that VisualStudio will consume) has the ability to run a preprocessor
on the vsct file; this preprocessor is (usually) the C++ preprocessor, so
it is possible to define includes and macros with the same syntax used
in C++ files. Using this ability of the compiler here, we include some files
defining some of the constants that we will use inside the file. -->

<!--This is the file that defines the IDs for all the commands exposed by VisualStudio. -->
<Extern href="stdidcmd.h" />

<!--This header contains the command ids for the menus provided by the shell. -->

<Extern href="vsshlids.h" />

<!--Definition of some VSCT specific constants. In this sample we use it for the IDs inside the guidOfficeIcon group. -->
<!--<Extern href="msobtnid.h" xmlns="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable" />-->

<!--The Commands section is where we the commands, menus and menu groups are defined.
This section uses a Guid to identify the package that provides the command defined inside it. -->

<Commands package="GuidBuildVisionPackage">
<!-- Inside this section we have different sub-sections: one for the menus, another
for the menu groups, one for the buttons (the actual commands), one for the combos
and the last one for the bitmaps used. Each element is identified by a command id that
is a unique pair of guid and numeric identifier; the guid part of the identifier is usually
called "command set" and is used to group different command inside a logically related
group; your package should define its own command set in order to avoid collisions
with command ids defined by other packages. -->


<!-- In this section you can define new menu groups. A menu group is a container for
other menus or buttons (commands); from a visual point of view you can see the
group as the part of a menu contained between two lines. The parent of a group
must be a menu. -->
<Groups>
<Group guid="GuidBuildVisionCmdSet" id="BuildVisionMenuGroup" priority="0x0600">
<Parent guid="guidSHLMainMenu" id="IDM_VS_MENU_TOOLS" />
</Group>
</Groups>

<!--Buttons section. -->
<!--This section defines the elements the user can interact with, like a menu command or a button
or combo box in a toolbar. -->
<Buttons>
<!--To define a menu group you have to specify its ID, the parent menu and its display priority.
The command is visible and enabled by default. If you need to change the visibility, status, etc, you can use
the CommandFlag node.
You can add more than one CommandFlag node e.g.:
<CommandFlag>DefaultInvisible</CommandFlag>
<CommandFlag>DynamicVisibility</CommandFlag>
If you do not want an image next to your command, remove the Icon node or set it to <Icon guid="guidOfficeIcon" id="msotcidNoIcon" /> -->

<!--<Button guid="guidBuildVisionCmdSet" id="cmdidBuildVisionCommand" priority="0x0100" type="Button">
<Parent guid="guidBuildVisionCmdSet" id="MyMenuGroup" />
<Icon guid="guidImages" id="bmpPic1" />
<Strings>
<CommandName>cmdidBuildVisionCommand</CommandName>
<ButtonText>BuildVision</ButtonText>
</Strings>
</Button>-->


<Button guid="GuidBuildVisionCmdSet" id="CmdIdBuildVisionToolWindow" priority="0x0100" type="Button">
<Parent guid="guidSHLMainMenu" id="IDG_VS_WNDO_OTRWNDWS1" />
Expand All @@ -76,25 +23,17 @@
</Strings>
</Button>


</Buttons>

<!--The bitmaps section is used to define the bitmaps that are used for the commands.-->

<Bitmaps>
<!-- The bitmap id is defined in a way that is a little bit different from the others:
the declaration starts with a guid for the bitmap strip, then there is the resource id of the
bitmap strip containing the bitmaps and then there are the numeric ids of the elements used
inside a button definition. An important aspect of this declaration is that the element id
must be the actual index (1-based) of the bitmap inside the bitmap strip. -->
<Bitmap guid="GuidImages" href="Resources\Images_32bit.bmp" usedList="bmpPic1, bmpPic2, bmpPicSearch, bmpPicX, bmpPicArrows" />
</Bitmaps>

</Commands>

<Symbols>
<!-- This is the package guid. -->
<GuidSymbol name="GuidBuildVisionPackage" value="{837c3c3b-8382-4839-9c9a-807b758a929f}" />

<!-- This is the guid used to group the menu commands together -->
<GuidSymbol name="GuidBuildVisionCmdSet" value="{caca30de-6571-483c-b8a1-b6a067964af1}">
<IDSymbol name="BuildVisionMenuGroup" value="0x1020" />
<IDSymbol name="CmdIdBuildVisionToolWindow" value="0x0101" />
Expand All @@ -107,5 +46,7 @@
<IDSymbol name="bmpPicX" value="4" />
<IDSymbol name="bmpPicArrows" value="5" />
</GuidSymbol>

</Symbols>

</CommandTable>
45 changes: 45 additions & 0 deletions src/BuildVision/Commands/ShowToolWindowCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.ComponentModel.Design;
using BuildVision.Core;
using BuildVision.Tool;
using BuildVision.UI;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Task = System.Threading.Tasks.Task;

namespace BuildVision.Commands
{
internal sealed class ShowToolWindowCommand
{
public static async Task InitializeAsync(AsyncPackage package)
{
var commandService = (IMenuCommandService)await package.GetServiceAsync(typeof(IMenuCommandService));
var toolwndCommandId = new CommandID(PackageGuids.GuidBuildVisionCmdSet, (int)PackageIds.CmdIdBuildVisionToolWindow);
var cmd = new MenuCommand((s, e) => Execute(package), toolwndCommandId);
commandService.AddCommand(cmd);
}

private static void Execute(AsyncPackage package)
{
package.JoinableTaskFactory.RunAsync(async () =>
{
var window = ShowToolWindow(package);
});
}

private static ToolWindowPane ShowToolWindow(AsyncPackage package)
{
// Get the instance number 0 of this tool window. This window is single instance so this instance
// is actually the only one.
// The last flag is set to true so that if the tool window does not exists it will be created.
var window = package.FindToolWindow(typeof(BuildVisionPane), 0, true);
if (window == null || window.Frame == null)
throw new InvalidOperationException(Resources.CanNotCreateWindow);

var windowFrame = (IVsWindowFrame)window.Frame;
ErrorHandler.ThrowOnFailure(windowFrame.Show());
return window;
}
}
}
76 changes: 16 additions & 60 deletions src/BuildVision/Core/BuildVisionPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using BuildVision.Commands;
using BuildVision.Common.Diagnostics;
using BuildVision.Exports.Providers;
using BuildVision.Exports.Services;
using BuildVision.Helpers;
using BuildVision.Services;
using BuildVision.Tool;
using BuildVision.Tool.Building;
using BuildVision.UI;
Expand All @@ -28,6 +27,7 @@
namespace BuildVision.Core
{
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
[ProvideAutoLoad(UIContextGuids80.SolutionExists)] // This will lead to a warning on startup
[ProvideMenuResource("Menus.ctmenu", 1)]
[ProvideToolWindow(typeof(BuildVisionPane))]
[Guid(PackageGuids.GuidBuildVisionPackageString)]
Expand All @@ -39,7 +39,7 @@ namespace BuildVision.Core
[ProvideOptionPage(typeof(GridSettingsDialogPage), "BuildVision", "Projects Grid", 0, 0, true)]
[ProvideOptionPage(typeof(BuildMessagesSettingsDialogPage), "BuildVision", "Build Messages", 0, 0, true)]
[ProvideOptionPage(typeof(ProjectItemSettingsDialogPage), "BuildVision", "Project Item", 0, 0, true)]
public sealed partial class BuildVisionPackage : AsyncPackage
public sealed class BuildVisionPackage : AsyncPackage
{
private DTE _dte;
private DTE2 _dte2;
Expand Down Expand Up @@ -75,16 +75,14 @@ private void Current_DispatcherUnhandledException(object sender, DispatcherUnhan
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
await base.InitializeAsync(cancellationToken, progress);

await JoinableTaskFactory.SwitchToMainThreadAsync(DisposalToken);
await ShowToolWindowCommand.InitializeAsync(this);

_dte = await GetServiceAsync(typeof(DTE)) as DTE;
Assumes.Present(_dte);
_dte2 = await GetServiceAsync(typeof(DTE)) as DTE2;
if (await GetServiceAsync(typeof(IMenuCommandService)) is OleMenuCommandService mcs)
{
var toolwndCommandId = new CommandID(PackageGuids.GuidBuildVisionCmdSet, (int) PackageIds.CmdIdBuildVisionToolWindow);
var menuToolWin = new OleMenuCommand(ShowToolWindowAsync, toolwndCommandId);
mcs.AddCommand(menuToolWin);
}
Assumes.Present(_dte2);
_solutionBuildManager = await GetServiceAsync(typeof(SVsSolutionBuildManager)) as IVsSolutionBuildManager2;
Assumes.Present(_solutionBuildManager);
_solutionBuildManager4 = await GetServiceAsync(typeof(SVsSolutionBuildManager)) as IVsSolutionBuildManager5;
Expand All @@ -108,6 +106,11 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
{
SolutionEvents_Opened();
}

var toolWindow = GetWindowPane(typeof(BuildVisionPane));
var windowStateService = await GetServiceAsync(typeof(IWindowStateService)) as IWindowStateService;
Assumes.Present(windowStateService);
windowStateService.Initialize(toolWindow);
}

private void SolutionEvents_Opened()
Expand All @@ -117,7 +120,7 @@ private void SolutionEvents_Opened()
_solutionProvider.ReloadSolution();
_buildInformationProvider.ResetCurrentProjects();
_buildInformationProvider.ResetBuildInformationModel();

_solutionBuildEvents = new SolutionBuildEvents(_solutionProvider, _buildInformationProvider);
_solutionBuildManager.AdviseUpdateSolutionEvents(_solutionBuildEvents, out _updateSolutionEvents4Cookie);
_solutionBuildManager4.AdviseUpdateSolutionEvents4(_solutionBuildEvents, out _updateSolutionEvents4Cookie);
Expand Down Expand Up @@ -169,56 +172,9 @@ private void CommandEvents_AfterExecute(string guid, int id, object customIn, ob
}
}

private async void ShowToolWindowAsync(object sender, EventArgs e)
private ToolWindowPane GetWindowPane(Type windowType)
{
try
{
var window = ShowToolWindow(Guid.Parse(PackageGuids.GuidBuildVisionToolWindowString));

// Get the instance number 0 of this tool window. This window is single instance so this instance
// is actually the only one.
// The last flag is set to true so that if the tool window does not exists it will be created.
// var window = FindToolWindow(typeof(BuildVisionPane), 0, true);
if (window == null || window.Frame == null)
throw new InvalidOperationException(Resources.CanNotCreateWindow);

await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
var windowFrame = (IVsWindowFrame)window.Frame;
ErrorHandler.ThrowOnFailure(windowFrame.Show());

var windowStateService = await GetServiceAsync(typeof(IWindowStateService)) as IWindowStateService;
Assumes.Present(windowStateService);
windowStateService.Initialize(window);
}
catch (Exception ex)
{
ex.TraceUnknownException();
}
}

ToolWindowPane ShowToolWindow(Guid windowGuid)
{
ThreadHelper.ThrowIfNotOnUIThread();


var shell = GetGlobalService(typeof(SVsUIShell)) as IVsUIShell;
IVsWindowFrame frame;
if (ErrorHandler.Failed(shell.FindToolWindow((uint)__VSCREATETOOLWIN.CTW_fForceCreate,
ref windowGuid, out frame)))
{
return null;
}
if (ErrorHandler.Failed(frame.Show()))
{
return null;
}

object docView = null;
if (ErrorHandler.Failed(frame.GetProperty((int)__VSFPROPID.VSFPROPID_DocView, out docView)))
{
return null;
}
return docView as ToolWindowPane;
return FindToolWindow(windowType, 0, false) ?? FindToolWindow(windowType, 0, true);
}
}
}

0 comments on commit 7b7d4a7

Please sign in to comment.