Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
Add TerminalRenderer API
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya Biryukov committed Jun 12, 2018
1 parent 6cb23f2 commit fccd3c0
Show file tree
Hide file tree
Showing 21 changed files with 635 additions and 182 deletions.
56 changes: 56 additions & 0 deletions Microsoft.VisualStudio.Terminal.Embeddable/ITerminalRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.VisualStudio.Terminal
{
public delegate void TerminalDataRecievedEventHandler(object sender, string data);

/// <summary>
/// Terminal renderer.
/// The terminal starts in auto-fit mode where terminal size always matches the window size when window resizes.
/// After a first call to <c>ResizeAsyc()</c>, the terminal switches to fixed size mode where the terminal size stays
/// fixed and doesn't change when the window size changes. In this mode the terminal size can only be changed
/// with <c>ResizeAsync()</c>. To show the terminal inside the window, a dotted border will be displayed
/// on the bottom and right sides of it.
/// If the terminal window gets smaller than the terminal, some parts of the terminal may get clipped from view.
/// </summary>
[ComImport, Guid("150B7535-03F9-41C0-9515-17ECB8199FFE")]
public interface ITerminalRenderer : ITerminal
{
/// <summary>
/// Gets current cols of the terminal window.
/// </summary>
int Cols { get; }

/// <summary>
/// Gets current rows of the terminal window.
/// </summary>
int Rows { get; }

/// <summary>
/// Changes the size of the terminal.
/// After that the terminal size may not match the terminal window size.
/// <c>Rows</c> and <c>Cols</c> always match the terminal window, and may differ from the terminal size
/// after <c>ResizeAsync</c> is called.
/// Calling<c>ResizeAsync</c> doesn't cause <c>TerminalResized</c> event.
/// </summary>
Task ResizeAsync(int cols, int rows, CancellationToken cancellationToken);

/// <summary>
/// An event that is fired when the terminal window is resized and either Rows or Cols have changed.
/// </summary>
event EventHandler TerminalResized;

/// <summary>
/// An event that is fired when user input is recieved.
/// </summary>
event TerminalDataRecievedEventHandler TerminalDataRecieved;

/// <summary>
/// Sends data to the terminal making it render the data.
/// </summary>
Task PtyDataAsync(string data, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;

namespace Microsoft.VisualStudio.Terminal
{
[ComImport, Guid("250301DA-0BEA-4C4E-B199-C363C7C164B2")]
public interface ITerminalRendererService
{
/// <summary>
/// Create a new terminal renderer instance with the given name that fits the window.
/// </summary>
/// <param name="name">The name that will be displayed as the tool window title.</param>
/// <returns>An instance of ITerminalRenderer</returns>
Task<object> CreateTerminalRendererAsync(string name);

/// <summary>
/// Create a new terminal renderer instance with the given name and dimensions.
/// The terminal size won't change when user resizes the window.
/// </summary>
/// <param name="name">The name that will be displayed as the tool window title.</param>
/// <returns>An instance of ITerminalRenderer</returns>
Task<object> CreateTerminalRendererAsync(string name, int cols, int rows);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Permissions;

namespace Microsoft.VisualStudio.Terminal
{
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[ComVisible(true)]
public class RendererScriptingObject : TerminalScriptingObjectBase
{
internal RendererScriptingObject(TermWindowPackage package) : base(package)
{
}

internal EventHandler<string> TermDataRecieved { get; set; }

internal EventHandler TermInit { get; set; }

internal EventHandler<ResizeEventArgs> TermResized { get; set; }


public override void ClosePty()
{
// There is already Closed event that is fired by the TerminalWindow
}

public override void InitPty(int cols, int rows, string directory) => TermInit?.Invoke(this, EventArgs.Empty);

public override void ResizePty(int cols, int rows) => TermResized?.Invoke(this, new ResizeEventArgs(cols, rows));

public override void TermData(string data) => TermDataRecieved?.Invoke(this, data);

public override string GetSolutionDir() => null;
}

[DebuggerStepThrough]
public class ResizeEventArgs : EventArgs
{
public ResizeEventArgs(int cols, int rows)
{
Cols = cols;
Rows = rows;
}

public int Cols { get; }
public int Rows { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Windows;

namespace Microsoft.VisualStudio.Terminal
{
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[ComVisible(true)]
public class TerminalScriptingObject : ITerminalScriptingObject
public class TerminalScriptingObject : TerminalScriptingObjectBase
{
private readonly TermWindowPackage package;
private readonly JsonRpc ptyService;
private readonly SolutionUtils solutionUtils;
private readonly string workingDirectory;
Expand All @@ -29,9 +27,8 @@ internal TerminalScriptingObject(
bool useSolutionDir,
string shellPath,
IEnumerable<string> args,
IDictionary<string, string> env)
IDictionary<string, string> env) : base(package)
{
this.package = package;
this.ptyService = ptyService;
this.solutionUtils = solutionUtils;
this.workingDirectory = workingDirectory;
Expand All @@ -41,22 +38,7 @@ internal TerminalScriptingObject(
this.env = env;
}

public string GetTheme()
{
return TerminalThemer.GetTheme();
}

public string GetFontFamily()
{
return this.package.OptionFontFamily;
}

public int GetFontSize()
{
return this.package.OptionFontSize;
}

public string GetSolutionDir()
public override string GetSolutionDir()
{
if (!this.useSolutionDir)
{
Expand All @@ -71,32 +53,9 @@ public string GetSolutionDir()
return solutionDir;
}

public void ClosePty()
{
this.ptyService.InvokeAsync("closeTerm");
}

public void CopyStringToClipboard(string stringToCopy)
{
Clipboard.SetText(stringToCopy ?? "");
}

public string GetClipboard()
{
return Clipboard.GetText();
}

public string GetLinkRegex()
{
return TerminalRegex.LocalLinkRegex.ToString();
}

public void HandleLocalLink(string uri)
{
TerminalRegex.HandleLocalLink(uri);
}
public override void ClosePty() => this.ptyService.InvokeAsync("closeTerm");

public void InitPty(int cols, int rows, string directory)
public override void InitPty(int cols, int rows, string directory)
{
string configuredShellPath;
if (this.package.OptionTerminal == DefaultTerminal.Other)
Expand All @@ -111,19 +70,10 @@ public void InitPty(int cols, int rows, string directory)
this.ptyService.InvokeAsync("initTerm", this.shellPath ?? configuredShellPath, cols, rows, directory, ((object)this.args) ?? this.package.OptionStartupArgument, env).FileAndForget("WhackWhackTerminal/InitPty");
}

public void ResizePty(int cols, int rows)
{
public override void ResizePty(int cols, int rows) =>
this.ptyService.InvokeAsync("resizeTerm", cols, rows).FileAndForget("WhackWhackTerminal/ResizePty");
}

public void TermData(string data)
{
public override void TermData(string data) =>
this.ptyService.InvokeAsync("termData", data).FileAndForget("WhackWhackTerminal/TermData");
}

public bool ValidateLocalLink(string link)
{
return TerminalRegex.ValidateLocalLink(link);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Windows;

namespace Microsoft.VisualStudio.Terminal
{
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[ComVisible(true)]
public abstract class TerminalScriptingObjectBase : ITerminalScriptingObject
{
protected readonly TermWindowPackage package;

internal TerminalScriptingObjectBase(TermWindowPackage package)
{
this.package = package;
}

public string GetTheme() => TerminalThemer.GetTheme();

public string GetFontFamily() => this.package.OptionFontFamily;

public int GetFontSize() => this.package.OptionFontSize;

public abstract string GetSolutionDir();
public abstract void ClosePty();

public void CopyStringToClipboard(string stringToCopy) => Clipboard.SetText(stringToCopy ?? "");

public string GetClipboard() => Clipboard.GetText();

public string GetLinkRegex() => TerminalRegex.LocalLinkRegex.ToString();

public void HandleLocalLink(string uri) => TerminalRegex.HandleLocalLink(uri);

public abstract void InitPty(int cols, int rows, string directory);

public abstract void ResizePty(int cols, int rows);

public abstract void TermData(string data);

public bool ValidateLocalLink(string link) => TerminalRegex.ValidateLocalLink(link);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,12 @@ public string BrightWhite
get;
set;
}

[DataMember(Name = "border")]
public string Border
{
get;
set;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public static string GetTheme()
theme.Background = ColorTranslator.ToHtml(VSColorTheme.GetThemedColor(EnvironmentColors.ToolboxBackgroundColorKey));
theme.Foreground = ColorTranslator.ToHtml(VSColorTheme.GetThemedColor(EnvironmentColors.ToolboxContentTextColorKey));
theme.Cursor = ColorTranslator.ToHtml(VSColorTheme.GetThemedColor(EnvironmentColors.ToolboxContentTextColorKey));
theme.Border = ColorTranslator.ToHtml(VSColorTheme.GetThemedColor(EnvironmentColors.ToolboxDisabledContentTextColorKey));
return JsonConvert.SerializeObject(theme);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\Microsoft.TypeScript.MSBuild.2.8.3\build\Microsoft.TypeScript.MSBuild.props" Condition="Exists('..\packages\Microsoft.TypeScript.MSBuild.2.8.3\build\Microsoft.TypeScript.MSBuild.props')" />
<Import Project="..\packages\Microsoft.VSSDK.BuildTools.15.5.100\build\Microsoft.VSSDK.BuildTools.props" Condition="Exists('..\packages\Microsoft.VSSDK.BuildTools.15.5.100\build\Microsoft.VSSDK.BuildTools.props')" />
<Import Project="..\packages\Microsoft.TypeScript.MSBuild.2.7.1\build\Microsoft.TypeScript.MSBuild.props" Condition="Exists('..\packages\Microsoft.TypeScript.MSBuild.2.7.1\build\Microsoft.TypeScript.MSBuild.props')" />
<PropertyGroup>
<MinimumVisualStudioVersion>15.0</MinimumVisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
Expand Down Expand Up @@ -86,13 +86,20 @@
<Compile Include="BrowserBridge\BetterBrowser.cs" />
<Compile Include="BrowserBridge\ITerminalScriptingObject.cs" />
<Compile Include="BrowserBridge\TerminalRegex.cs" />
<Compile Include="BrowserBridge\RendererScriptingObject.cs" />
<Compile Include="BrowserBridge\TerminalScriptingObjectBase.cs" />
<Compile Include="BrowserBridge\TerminalScriptingObject.cs" />
<Compile Include="BrowserBridge\TerminalEvent.cs" />
<Compile Include="BrowserBridge\TerminalThemer.cs" />
<Compile Include="RendererToolWindow.cs" />
<Compile Include="ServiceToolWindow.cs" />
<Compile Include="RendererToolWindowControl.xaml.cs">
<DependentUpon>RendererToolWindowControl.xaml</DependentUpon>
</Compile>
<Compile Include="ServiceToolWindowControl.xaml.cs">
<DependentUpon>ServiceToolWindowControl.xaml</DependentUpon>
</Compile>
<Compile Include="VsService\TerminalWindow.cs" />
<Compile Include="VsService\EmbeddedTerminal.cs" />
<Compile Include="VsService\EmbeddedTerminalService.cs" />
<Compile Include="BrowserBridge\TerminalTheme.cs" />
Expand All @@ -111,6 +118,7 @@
</Compile>
<Compile Include="TermWindowPackage.cs" />
<Compile Include="ToolWindowContext.cs" />
<Compile Include="VsService\TerminalRenderer.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="BrandWhackWhackTerminal_256x.png">
Expand Down Expand Up @@ -153,6 +161,10 @@
</VSCTCompile>
</ItemGroup>
<ItemGroup>
<Page Include="RendererToolWindowControl.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="ServiceToolWindowControl.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -349,6 +361,7 @@
<ItemGroup>
<TypeScriptCompile Include="WebView\ts\external.d.ts" />
<TypeScriptCompile Include="WebView\ts\main.ts" />
<TypeScriptCompile Include="WebView\ts\TerminalFit.d.ts" />
<TypeScriptCompile Include="WebView\ts\TerminalLinkMatcher.ts" />
<TypeScriptCompile Include="WebView\ts\TermView.ts" />
<TypeScriptCompile Include="WebView\ts\VsEventManager.ts" />
Expand All @@ -370,12 +383,11 @@
</PropertyGroup>
<Error Condition="!Exists('..\packages\Microsoft.VSSDK.BuildTools.15.5.100\build\Microsoft.VSSDK.BuildTools.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.VSSDK.BuildTools.15.5.100\build\Microsoft.VSSDK.BuildTools.props'))" />
<Error Condition="!Exists('..\packages\Microsoft.VSSDK.BuildTools.15.5.100\build\Microsoft.VSSDK.BuildTools.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.VSSDK.BuildTools.15.5.100\build\Microsoft.VSSDK.BuildTools.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.TypeScript.MSBuild.2.7.1\build\Microsoft.TypeScript.MSBuild.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.TypeScript.MSBuild.2.7.1\build\Microsoft.TypeScript.MSBuild.props'))" />
<Error Condition="!Exists('..\packages\Microsoft.TypeScript.MSBuild.2.7.1\build\Microsoft.TypeScript.MSBuild.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.TypeScript.MSBuild.2.7.1\build\Microsoft.TypeScript.MSBuild.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.VisualStudio.SDK.EmbedInteropTypes.15.0.16\build\Microsoft.VisualStudio.SDK.EmbedInteropTypes.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.VisualStudio.SDK.EmbedInteropTypes.15.0.16\build\Microsoft.VisualStudio.SDK.EmbedInteropTypes.targets'))" />
<Error Condition="!Exists('..\packages\Nerdbank.GitVersioning.2.1.23\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Nerdbank.GitVersioning.2.1.23\build\Nerdbank.GitVersioning.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.TypeScript.MSBuild.2.8.3\build\Microsoft.TypeScript.MSBuild.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.TypeScript.MSBuild.2.8.3\build\Microsoft.TypeScript.MSBuild.props'))" />
<Error Condition="!Exists('..\packages\Microsoft.TypeScript.MSBuild.2.8.3\build\Microsoft.TypeScript.MSBuild.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.TypeScript.MSBuild.2.8.3\build\Microsoft.TypeScript.MSBuild.targets'))" />
</Target>
<Import Project="..\packages\Microsoft.TypeScript.MSBuild.2.7.1\build\Microsoft.TypeScript.MSBuild.targets" Condition="Exists('..\packages\Microsoft.TypeScript.MSBuild.2.7.1\build\Microsoft.TypeScript.MSBuild.targets')" />
<Import Project="..\packages\Microsoft.VSSDK.BuildTools.15.5.100\build\Microsoft.VSSDK.BuildTools.targets" Condition="Exists('..\packages\Microsoft.VSSDK.BuildTools.15.5.100\build\Microsoft.VSSDK.BuildTools.targets')" />
<Target Name="AddTsToContent" AfterTargets="CompileTypeScript" Condition="'$(BuildingProject)' != 'false'">
<ItemGroup>
Expand Down Expand Up @@ -405,6 +417,7 @@
</Target>
<Import Project="..\packages\Microsoft.VisualStudio.SDK.EmbedInteropTypes.15.0.16\build\Microsoft.VisualStudio.SDK.EmbedInteropTypes.targets" Condition="Exists('..\packages\Microsoft.VisualStudio.SDK.EmbedInteropTypes.15.0.16\build\Microsoft.VisualStudio.SDK.EmbedInteropTypes.targets')" />
<Import Project="..\packages\Nerdbank.GitVersioning.2.1.23\build\Nerdbank.GitVersioning.targets" Condition="Exists('..\packages\Nerdbank.GitVersioning.2.1.23\build\Nerdbank.GitVersioning.targets')" />
<Import Project="..\packages\Microsoft.TypeScript.MSBuild.2.8.3\build\Microsoft.TypeScript.MSBuild.targets" Condition="Exists('..\packages\Microsoft.TypeScript.MSBuild.2.8.3\build\Microsoft.TypeScript.MSBuild.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
Loading

0 comments on commit fccd3c0

Please sign in to comment.