Skip to content

Commit

Permalink
LOTS OF PLUMBING, but they go all the way from json to the ControlCore
Browse files Browse the repository at this point in the history
  • Loading branch information
zadjii-msft committed Aug 17, 2022
1 parent 8cf5697 commit a3d3389
Show file tree
Hide file tree
Showing 23 changed files with 485 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/cascadia/TerminalControl/ControlCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ namespace winrt::Microsoft::Terminal::Control::implementation
auto pfnPlayMidiNote = std::bind(&ControlCore::_terminalPlayMidiNote, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
_terminal->SetPlayMidiNoteCallback(pfnPlayMidiNote);

auto pfnTrigger = std::bind(&ControlCore::_terminalTrigger, this, std::placeholders::_1, std::placeholders::_2);
_terminal->SetTriggerCallback(pfnTrigger);

// MSFT 33353327: Initialize the renderer in the ctor instead of Initialize().
// We need the renderer to be ready to accept new engines before the SwapChainPanel is ready to go.
// If we wait, a screen reader may try to get the AutomationPeer (aka the UIA Engine), and we won't be able to attach
Expand Down Expand Up @@ -2137,4 +2140,14 @@ namespace winrt::Microsoft::Terminal::Control::implementation
}
}
}

void ControlCore::_terminalTrigger(size_t index, std::wstring_view line)
{
if (index > _settings->Triggers().Size())
return;

auto pattern = _settings->Triggers().GetAt(base::saturated_cast<uint32_t>(index));

_TriggerHitHandlers(*this, *winrt::make_self<implementation::TriggerHitArgs>(index, line));
}
}
3 changes: 3 additions & 0 deletions src/cascadia/TerminalControl/ControlCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
TYPED_EVENT(ShowWindowChanged, IInspectable, Control::ShowWindowArgs);
TYPED_EVENT(UpdateSelectionMarkers, IInspectable, Control::UpdateSelectionMarkersEventArgs);
TYPED_EVENT(OpenHyperlink, IInspectable, Control::OpenHyperlinkEventArgs);
TYPED_EVENT(TriggerHit, IInspectable, Control::TriggerHitArgs);
// clang-format on

private:
Expand Down Expand Up @@ -291,6 +292,8 @@ namespace winrt::Microsoft::Terminal::Control::implementation
void _terminalPlayMidiNote(const int noteNumber,
const int velocity,
const std::chrono::microseconds duration);

void _terminalTrigger(size_t index, std::wstring_view line);
#pragma endregion

std::unique_ptr<MidiAudio> _midiAudio;
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalControl/ControlCore.idl
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,7 @@ namespace Microsoft.Terminal.Control
event Windows.Foundation.TypedEventHandler<Object, ShowWindowArgs> ShowWindowChanged;
event Windows.Foundation.TypedEventHandler<Object, UpdateSelectionMarkersEventArgs> UpdateSelectionMarkers;
event Windows.Foundation.TypedEventHandler<Object, OpenHyperlinkEventArgs> OpenHyperlink;

event Windows.Foundation.TypedEventHandler<Object, TriggerHitArgs> TriggerHit;
};
}
1 change: 1 addition & 0 deletions src/cascadia/TerminalControl/EventArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
#include "FoundResultsArgs.g.cpp"
#include "ShowWindowArgs.g.cpp"
#include "UpdateSelectionMarkersEventArgs.g.cpp"
#include "TriggerHitArgs.g.cpp"
16 changes: 16 additions & 0 deletions src/cascadia/TerminalControl/EventArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "FoundResultsArgs.g.h"
#include "ShowWindowArgs.g.h"
#include "UpdateSelectionMarkersEventArgs.g.h"
#include "TriggerHitArgs.g.h"

namespace winrt::Microsoft::Terminal::Control::implementation
{
Expand Down Expand Up @@ -169,4 +170,19 @@ namespace winrt::Microsoft::Terminal::Control::implementation

WINRT_PROPERTY(bool, ClearMarkers, false);
};

struct TriggerHitArgs : public TriggerHitArgsT<TriggerHitArgs>
{
public:
TriggerHitArgs() = default;
TriggerHitArgs(size_t index, std::wstring_view line)
{
_Index = ::base::saturated_cast<uint32_t>(index);
_Matches = winrt::single_threaded_vector<winrt::hstring>();
_Matches.Append(winrt::hstring{ line });
};

WINRT_PROPERTY(uint32_t, Index);
WINRT_PROPERTY(winrt::Windows::Foundation::Collections::IVector<winrt::hstring>, Matches);
};
}
6 changes: 6 additions & 0 deletions src/cascadia/TerminalControl/EventArgs.idl
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,10 @@ namespace Microsoft.Terminal.Control
{
Boolean ClearMarkers { get; };
}

runtimeclass TriggerHitArgs
{
UInt32 Index { get; };
IVector<String> Matches { get; };
}
}
3 changes: 2 additions & 1 deletion src/cascadia/TerminalControl/TermControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ namespace winrt::Microsoft::Terminal::Control::implementation
PROJECTED_FORWARDED_TYPED_EVENT(SetTaskbarProgress, IInspectable, IInspectable, _core, TaskbarProgressChanged);
PROJECTED_FORWARDED_TYPED_EVENT(ConnectionStateChanged, IInspectable, IInspectable, _core, ConnectionStateChanged);
PROJECTED_FORWARDED_TYPED_EVENT(ShowWindowChanged, IInspectable, Control::ShowWindowArgs, _core, ShowWindowChanged);
PROJECTED_FORWARDED_TYPED_EVENT(TriggerHit, IInspectable, Control::TriggerHitArgs, _core, TriggerHit);

PROJECTED_FORWARDED_TYPED_EVENT(PasteFromClipboard, IInspectable, Control::PasteFromClipboardEventArgs, _interactivity, PasteFromClipboard);
PROJECTED_FORWARDED_TYPED_EVENT(PasteFromClipboard, IInspectable, Control::PasteFromClipboardEventArgs, _interactivity, PasteFromClipboard);

TYPED_EVENT(OpenHyperlink, IInspectable, Control::OpenHyperlinkEventArgs);
TYPED_EVENT(RaiseNotice, IInspectable, Control::NoticeEventArgs);
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalControl/TermControl.idl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ namespace Microsoft.Terminal.Control

event Windows.Foundation.TypedEventHandler<Object, ShowWindowArgs> ShowWindowChanged;

event Windows.Foundation.TypedEventHandler<Object, TriggerHitArgs> TriggerHit;

Boolean CopySelectionToClipboard(Boolean singleLine, Windows.Foundation.IReference<CopyFormat> formats);
void PasteTextFromClipboard();
void SelectAll();
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalCore/ICoreSettings.idl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace Microsoft.Terminal.Core

Boolean AutoMarkPrompts;

IVector<String> Triggers;
};

}
34 changes: 34 additions & 0 deletions src/cascadia/TerminalCore/Terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ void Terminal::UpdateSettings(ICoreSettings settings)
_startingTabColor = settings.StartingTabColor().Value();
}

_triggers.clear();
for (const auto& trigger : settings.Triggers())
{
_triggers.emplace_back(std::wregex{ trigger.c_str() });
}

// TODO:MSFT:21327402 - if HistorySize has changed, resize the buffer so we
// have a smaller scrollback. We should do this carefully - if the new buffer
// size is smaller than where the mutable viewport currently is, we'll want
Expand Down Expand Up @@ -1377,6 +1383,11 @@ void Terminal::SetPlayMidiNoteCallback(std::function<void(const int, const int,
_pfnPlayMidiNote.swap(pfn);
}

void Terminal::SetTriggerCallback(std::function<void(const size_t, std::wstring_view)> pfn) noexcept
{
_pfnTriggerCallback.swap(pfn);
}

// Method Description:
// - Sets the cursor to be currently on. On/Off is tracked independently of
// cursor visibility (hidden/visible). On/off is controlled by the cursor
Expand Down Expand Up @@ -1638,3 +1649,26 @@ til::color Terminal::GetColorForMark(const Microsoft::Console::VirtualTerminal::
}
}
}

void Terminal::_runTriggers()
{
if (!_pfnTriggerCallback || _triggers.size() == 0)
{
return;
}

const auto cursorPos = _activeBuffer().GetCursor().GetPosition();
// const auto bufferRow = cursorPos.Y;
const auto& row = _activeBuffer().GetRowByOffset(cursorPos.Y);
const auto text = row.GetText();

for (auto i = 0u; i < _triggers.size(); i++)
{
const std::wregex& trigger = _triggers[i];
if (std::regex_search(text, trigger))
{
// That regex matched. Bubble it up to the handler.
_pfnTriggerCallback(i, text);
}
}
}
5 changes: 5 additions & 0 deletions src/cascadia/TerminalCore/Terminal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ class Microsoft::Terminal::Core::Terminal final :
void TaskbarProgressChangedCallback(std::function<void()> pfn) noexcept;
void SetShowWindowCallback(std::function<void(bool)> pfn) noexcept;
void SetPlayMidiNoteCallback(std::function<void(const int, const int, const std::chrono::microseconds)> pfn) noexcept;
void SetTriggerCallback(std::function<void(const size_t, std::wstring_view)> pfn) noexcept;

void SetCursorOn(const bool isOn);
bool IsCursorBlinkingAllowed() const noexcept;
Expand Down Expand Up @@ -314,6 +315,7 @@ class Microsoft::Terminal::Core::Terminal final :
std::function<void()> _pfnTaskbarProgressChanged;
std::function<void(bool)> _pfnShowWindowChanged;
std::function<void(const int, const int, const std::chrono::microseconds)> _pfnPlayMidiNote;
std::function<void(const size_t, std::wstring_view)> _pfnTriggerCallback;

RenderSettings _renderSettings;
std::unique_ptr<::Microsoft::Console::VirtualTerminal::StateMachine> _stateMachine;
Expand Down Expand Up @@ -401,6 +403,7 @@ class Microsoft::Terminal::Core::Terminal final :
std::optional<KeyEventCodes> _lastKeyEventCodes;

std::vector<Microsoft::Console::VirtualTerminal::DispatchTypes::ScrollMark> _scrollMarks;
std::vector<std::wregex> _triggers;

static WORD _ScanCodeFromVirtualKey(const WORD vkey) noexcept;
static WORD _VirtualKeyFromScanCode(const WORD scanCode) noexcept;
Expand Down Expand Up @@ -428,6 +431,8 @@ class Microsoft::Terminal::Core::Terminal final :
TextBuffer& _activeBuffer() const noexcept;
void _updateUrlDetection();

void _runTriggers();

#pragma region TextSelection
// These methods are defined in TerminalSelection.cpp
std::vector<til::inclusive_rect> _GetSelectionRects() const noexcept;
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalCore/TerminalApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ void Terminal::LineFeed(const bool withReturn)
// row we just came from
_activeBuffer().GetRowByOffset(cursorPos.Y).SetWrapForced(false);

_runTriggers();

cursorPos.Y++;
if (withReturn)
{
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalCore/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define BLOCK_TIL
#include <LibraryIncludes.h>
#include "winrt/Windows.Foundation.h"
#include "winrt/Windows.Foundation.Collections.h"

#include "winrt/Microsoft.Terminal.Core.h"
#include <til.h>
2 changes: 1 addition & 1 deletion src/cascadia/TerminalSettingsModel/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
// - input: the string to JSON escape.
// Return Value:
// - the input string escaped properly to be inserted into another json blob.
std::string _escapeForJson(const std::string& input)
static std::string _escapeForJson(const std::string& input)
{
Json::Value inJson{ input };
Json::StreamWriterBuilder builder;
Expand Down
3 changes: 2 additions & 1 deletion src/cascadia/TerminalSettingsModel/MTSMSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ Author(s):
X(bool, Elevate, "elevate", false) \
X(bool, VtPassthrough, "experimental.connection.passthroughMode", false) \
X(bool, AutoMarkPrompts, "experimental.autoMarkPrompts", false) \
X(bool, ShowMarks, "experimental.showMarksOnScrollbar", false)
X(bool, ShowMarks, "experimental.showMarksOnScrollbar", false) \
X(Windows::Foundation::Collections::IVector<winrt::Microsoft::Terminal::Settings::Model::Trigger>, Triggers, "experimental.triggers", nullptr)

// Intentionally omitted Profile settings:
// * Name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<ClInclude Include="VsDevShellGenerator.h" />
<ClInclude Include="VsSetupConfiguration.h" />
<ClInclude Include="WslDistroGenerator.h" />
<ClInclude Include="Trigger.h" />
</ItemGroup>
<!-- ========================= Cpp Files ======================== -->
<ItemGroup>
Expand Down Expand Up @@ -165,6 +166,9 @@
<ClCompile Include="VsDevShellGenerator.cpp" />
<ClCompile Include="VsSetupConfiguration.cpp" />
<ClCompile Include="WslDistroGenerator.cpp" />
<ClCompile Include="Trigger.cpp">
<DependentUpon>Profile.idl</DependentUpon>
</ClCompile>
<!-- You _NEED_ to include this file and the jsoncpp IncludePath (below) if
you want to use jsoncpp -->
<ClCompile Include="$(OpenConsoleDir)\dep\jsoncpp\jsoncpp.cpp">
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/Profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "TerminalSettingsSerializationHelpers.h"
#include "AppearanceConfig.h"
#include "FontConfig.h"
#include "Trigger.h"

#include "Profile.g.cpp"

Expand Down
20 changes: 20 additions & 0 deletions src/cascadia/TerminalSettingsModel/Profile.idl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

import "IAppearanceConfig.idl";
import "FontConfig.idl";

// For triggers, TODO! move to my own file
import "ActionArgs.idl";


#include "IInheritable.idl.h"

#define INHERITABLE_PROFILE_SETTING(Type, Name) \
Expand All @@ -11,6 +16,19 @@ import "FontConfig.idl";

namespace Microsoft.Terminal.Settings.Model
{

enum TriggerType
{
MatchRegex = 0,
};

[default_interface] runtimeclass Trigger {
Trigger();
TriggerType Type { get; };
String Match { get; };
ActionAndArgs ActionAndArgs { get; };
}

// This tag is used to identify the context in which the Profile was created
enum OriginTag
{
Expand Down Expand Up @@ -87,5 +105,7 @@ namespace Microsoft.Terminal.Settings.Model
INHERITABLE_PROFILE_SETTING(Boolean, Elevate);
INHERITABLE_PROFILE_SETTING(Boolean, AutoMarkPrompts);
INHERITABLE_PROFILE_SETTING(Boolean, ShowMarks);

INHERITABLE_PROFILE_SETTING(IVector<Trigger>, Triggers);
}
}
6 changes: 6 additions & 0 deletions src/cascadia/TerminalSettingsModel/TerminalSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,12 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
_Elevate = profile.Elevate();
_AutoMarkPrompts = Feature_ScrollbarMarks::IsEnabled() && profile.AutoMarkPrompts();
_ShowMarks = Feature_ScrollbarMarks::IsEnabled() && profile.ShowMarks();

_Triggers = winrt::single_threaded_vector<winrt::hstring>();
for (const auto& trigger : profile.Triggers())
{
_Triggers->Append(trigger.Match());
}
}

// Method Description:
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalSettingsModel/TerminalSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation

INHERITABLE_SETTING(Model::TerminalSettings, Microsoft::Terminal::Core::AdjustTextMode, AdjustIndistinguishableColors, Core::AdjustTextMode::Never);

INHERITABLE_SETTING(Model::TerminalSettings, Windows::Foundation::Collections::IVector<winrt::hstring>, Triggers, nullptr);

// ------------------------ End of Core Settings -----------------------

INHERITABLE_SETTING(Model::TerminalSettings, hstring, ProfileName);
Expand Down
Loading

0 comments on commit a3d3389

Please sign in to comment.