Create a toggleable comamnd palette

This commit is contained in:
Mike Griese 2019-07-23 14:59:35 -05:00
parent 77fc58b7ea
commit 846778b288
16 changed files with 199 additions and 48 deletions

View file

@ -13,7 +13,9 @@ using namespace winrt::Windows::Data::Json;
namespace winrt::TerminalApp::implementation
{
DEFINE_GETSET_PROPERTY(Action, winrt::hstring, Name);
// DEFINE_GETSET_PROPERTY(Action, winrt::hstring, Name);
DEFINE_GETSET_PROPERTY(Action, winrt::hstring, IconPath);
DEFINE_GETSET_PROPERTY(Action, winrt::TerminalApp::ShortcutAction, Command);
DEFINE_EVENT(Action, PropertyChanged, _propertyChanged, Windows::UI::Xaml::Data::PropertyChangedEventHandler);
}

View file

@ -12,9 +12,12 @@ namespace winrt::TerminalApp::implementation
{
Action() = default;
DECLARE_GETSET_PROPERTY(winrt::hstring, Name);
// DECLARE_GETSET_PROPERTY(winrt::hstring, Name);
DECLARE_GETSET_PROPERTY(winrt::hstring, IconPath);
DECLARE_GETSET_PROPERTY(winrt::TerminalApp::ShortcutAction, Command);
DECLARE_EVENT(PropertyChanged, _propertyChanged, Windows::UI::Xaml::Data::PropertyChangedEventHandler);
DEFINE_OBSERVABLE_GETSET_PROPERTY(winrt::hstring, Name, _propertyChanged);
};
}

View file

@ -5,7 +5,8 @@ import "../ShortcutActionDispatch.idl";
namespace TerminalApp
{
[default_interface] runtimeclass Action {
[default_interface] runtimeclass Action : Windows.UI.Xaml.Data.INotifyPropertyChanged
{
Action();
String Name;

View file

@ -0,0 +1,37 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "pch.h"
#include "ActionList.h"
#include "ActionList.g.cpp"
using namespace winrt;
using namespace winrt::TerminalApp;
using namespace Windows::UI::Xaml;
namespace winrt::TerminalApp::implementation
{
ActionList::ActionList()
{
InitializeComponent();
_filteredActions = winrt::single_threaded_observable_vector<winrt::TerminalApp::Action>();
Action action1{};
action1.Name(L"Foo");
action1.Command(winrt::TerminalApp::ShortcutAction::NewTab);
Action action2{};
action2.Name(L"Bar");
action2.Command(winrt::TerminalApp::ShortcutAction::CloseTab);
_filteredActions.Append(action1);
_filteredActions.Append(action2);
}
Windows::Foundation::Collections::IObservableVector<Action> ActionList::FilteredActions()
{
return _filteredActions;
}
}

View file

@ -0,0 +1,30 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#pragma once
#include "winrt/Microsoft.UI.Xaml.Controls.h"
#include "ActionList.g.h"
namespace winrt::TerminalApp::implementation
{
struct ActionList : ActionListT<ActionList>
{
ActionList();
// void OnNewTabButtonClick(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::Controls::SplitButtonClickEventArgs const& args);
Windows::Foundation::Collections::IObservableVector<TerminalApp::Action> FilteredActions();
private:
Windows::Foundation::Collections::IObservableVector<TerminalApp::Action> _filteredActions{ nullptr };
};
}
namespace winrt::TerminalApp::factory_implementation
{
struct ActionList : ActionListT<ActionList, implementation::ActionList>
{
};
}

View file

@ -0,0 +1,14 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import "../Action.idl";
namespace TerminalApp
{
[default_interface] runtimeclass ActionList : Windows.UI.Xaml.Controls.Grid
{
ActionList();
Windows.Foundation.Collections.IObservableVector<Action> FilteredActions { get; };
}
}

View file

@ -0,0 +1,39 @@
<!-- Copyright (c) Microsoft Corporation. All rights reserved. Licensed under
the MIT License. See LICENSE in the project root for license information. -->
<Grid
x:Class="TerminalApp.ActionList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TerminalApp"
xmlns:mux="using:Microsoft.UI.Xaml.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Background="{ThemeResource SystemChromeMediumLowColor}"
mc:Ignorable="d">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBox
Grid.Row="0"
x:Name="_SearchBox"
Text="">
</TextBox>
<ListView
Grid.Row="1"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ItemsSource="{x:Bind FilteredActions}">
<ItemsControl.ItemTemplate>
<DataTemplate x:DataType="local:Action">
<TextBlock Text="{x:Bind Name, Mode=OneWay}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListView>
</Grid>

View file

@ -82,6 +82,7 @@ namespace winrt::TerminalApp::implementation
_tabRow = terminalPage->TabRow();
_tabView = _tabRow.TabView();
_newTabButton = _tabRow.NewTabButton();
_commandPalette = terminalPage->_ActionList();
if (_settings->GlobalSettings().GetShowTabsInTitlebar())
{
@ -500,6 +501,7 @@ namespace winrt::TerminalApp::implementation
_actionDispatch.MoveFocus([this](const auto direction) { _MoveFocus(direction); });
_actionDispatch.CopyText([this](const auto trimWhitespace) { _CopyText(trimWhitespace); });
_actionDispatch.PasteText([this]() { _PasteText(); });
_actionDispatch.ToggleCommandPalette([this]() { _ToggleCommandPalette(); });
}
// Method Description:
@ -512,28 +514,6 @@ namespace winrt::TerminalApp::implementation
void App::_HookupKeyBindings(TerminalApp::AppKeyBindings bindings) noexcept
{
bindings.SetDispatch(_actionDispatch);
// // Hook up the KeyBinding object's events to our handlers.
// // They should all be hooked up here, regardless of whether or not
// // there's an actual keychord for them.
// bindings.NewTab([this]() { _OpenNewTab(std::nullopt); });
// bindings.DuplicateTab([this]() { _DuplicateTabViewItem(); });
// bindings.CloseTab([this]() { _CloseFocusedTab(); });
// bindings.ClosePane([this]() { _CloseFocusedPane(); });
// bindings.NewTabWithProfile([this](const auto index) { _OpenNewTab({ index }); });
// bindings.ScrollUp([this]() { _Scroll(-1); });
// bindings.ScrollDown([this]() { _Scroll(1); });
// bindings.NextTab([this]() { _SelectNextTab(true); });
// bindings.PrevTab([this]() { _SelectNextTab(false); });
// bindings.SplitVertical([this]() { _SplitVertical(std::nullopt); });
// bindings.SplitHorizontal([this]() { _SplitHorizontal(std::nullopt); });
// bindings.ScrollUpPage([this]() { _ScrollPage(-1); });
// bindings.ScrollDownPage([this]() { _ScrollPage(1); });
// bindings.SwitchToTab([this](const auto index) { _SelectTab({ index }); });
// bindings.OpenSettings([this]() { _OpenSettings(); });
// bindings.ResizePane([this](const auto direction) { _ResizePane(direction); });
// bindings.MoveFocus([this](const auto direction) { _MoveFocus(direction); });
// bindings.CopyText([this](const auto trimWhitespace) { _CopyText(trimWhitespace); });
// bindings.PasteText([this]() { _PasteText(); });
}
// Method Description:
@ -1129,6 +1109,12 @@ namespace winrt::TerminalApp::implementation
}
}
void App::_ToggleCommandPalette()
{
const bool isVisible = _commandPalette.Visibility() == Visibility::Visible;
_commandPalette.Visibility(isVisible ? Visibility::Collapsed : Visibility::Visible);
}
// Method Description:
// - Responds to the TabView control's Selection Changed event (to move a
// new terminal control into focus.)

View file

@ -55,6 +55,7 @@ namespace winrt::TerminalApp::implementation
TerminalApp::TabRowControl _tabRow{ nullptr };
Windows::UI::Xaml::Controls::Grid _tabContent{ nullptr };
Windows::UI::Xaml::Controls::SplitButton _newTabButton{ nullptr };
TerminalApp::ActionList _commandPalette{ nullptr };
std::vector<std::shared_ptr<Tab>> _tabs;
@ -124,6 +125,7 @@ namespace winrt::TerminalApp::implementation
void _ScrollPage(int delta);
void _ResizePane(const Direction& direction);
void _MoveFocus(const Direction& direction);
void _ToggleCommandPalette();
void _OnLoaded(const IInspectable& sender, const Windows::UI::Xaml::RoutedEventArgs& eventArgs);
void _OnTabSelectionChanged(const IInspectable& sender, const Windows::UI::Xaml::Controls::SelectionChangedEventArgs& eventArgs);

View file

@ -155,6 +155,9 @@ namespace winrt::TerminalApp::implementation
case ShortcutAction::MoveFocusDown:
_MoveFocusHandlers(Direction::Down);
return true;
case ShortcutAction::ToggleCommandPalette:
_ToggleCommandPaletteHandlers();
return true;
default:
return false;
}
@ -186,5 +189,7 @@ namespace winrt::TerminalApp::implementation
DEFINE_EVENT(ShortcutActionDispatch, OpenSettings, _OpenSettingsHandlers, TerminalApp::OpenSettingsEventArgs);
DEFINE_EVENT(ShortcutActionDispatch, ResizePane, _ResizePaneHandlers, TerminalApp::ResizePaneEventArgs);
DEFINE_EVENT(ShortcutActionDispatch, MoveFocus, _MoveFocusHandlers, TerminalApp::MoveFocusEventArgs);
DEFINE_EVENT(ShortcutActionDispatch, ToggleCommandPalette, _ToggleCommandPaletteHandlers, TerminalApp::ToggleCommandPaletteEventArgs);
// Adding an event here? make sure to add to ShortcutActionSerializationKeys too!
// clang-format on
}

View file

@ -15,29 +15,30 @@ namespace winrt::TerminalApp::implementation
bool DoAction(ShortcutAction action);
// clang-format off
DECLARE_EVENT(CopyText, _CopyTextHandlers, TerminalApp::CopyTextEventArgs);
DECLARE_EVENT(PasteText, _PasteTextHandlers, TerminalApp::PasteTextEventArgs);
DECLARE_EVENT(NewTab, _NewTabHandlers, TerminalApp::NewTabEventArgs);
DECLARE_EVENT(DuplicateTab, _DuplicateTabHandlers, TerminalApp::DuplicateTabEventArgs);
DECLARE_EVENT(NewTabWithProfile, _NewTabWithProfileHandlers, TerminalApp::NewTabWithProfileEventArgs);
DECLARE_EVENT(NewWindow, _NewWindowHandlers, TerminalApp::NewWindowEventArgs);
DECLARE_EVENT(CloseWindow, _CloseWindowHandlers, TerminalApp::CloseWindowEventArgs);
DECLARE_EVENT(CloseTab, _CloseTabHandlers, TerminalApp::CloseTabEventArgs);
DECLARE_EVENT(ClosePane, _ClosePaneHandlers, TerminalApp::ClosePaneEventArgs);
DECLARE_EVENT(SwitchToTab, _SwitchToTabHandlers, TerminalApp::SwitchToTabEventArgs);
DECLARE_EVENT(NextTab, _NextTabHandlers, TerminalApp::NextTabEventArgs);
DECLARE_EVENT(PrevTab, _PrevTabHandlers, TerminalApp::PrevTabEventArgs);
DECLARE_EVENT(SplitVertical, _SplitVerticalHandlers, TerminalApp::SplitVerticalEventArgs);
DECLARE_EVENT(SplitHorizontal, _SplitHorizontalHandlers, TerminalApp::SplitHorizontalEventArgs);
DECLARE_EVENT(IncreaseFontSize, _IncreaseFontSizeHandlers, TerminalApp::IncreaseFontSizeEventArgs);
DECLARE_EVENT(DecreaseFontSize, _DecreaseFontSizeHandlers, TerminalApp::DecreaseFontSizeEventArgs);
DECLARE_EVENT(ScrollUp, _ScrollUpHandlers, TerminalApp::ScrollUpEventArgs);
DECLARE_EVENT(ScrollDown, _ScrollDownHandlers, TerminalApp::ScrollDownEventArgs);
DECLARE_EVENT(ScrollUpPage, _ScrollUpPageHandlers, TerminalApp::ScrollUpPageEventArgs);
DECLARE_EVENT(ScrollDownPage, _ScrollDownPageHandlers, TerminalApp::ScrollDownPageEventArgs);
DECLARE_EVENT(OpenSettings, _OpenSettingsHandlers, TerminalApp::OpenSettingsEventArgs);
DECLARE_EVENT(ResizePane, _ResizePaneHandlers, TerminalApp::ResizePaneEventArgs);
DECLARE_EVENT(MoveFocus, _MoveFocusHandlers, TerminalApp::MoveFocusEventArgs);
DECLARE_EVENT(CopyText, _CopyTextHandlers, TerminalApp::CopyTextEventArgs);
DECLARE_EVENT(PasteText, _PasteTextHandlers, TerminalApp::PasteTextEventArgs);
DECLARE_EVENT(NewTab, _NewTabHandlers, TerminalApp::NewTabEventArgs);
DECLARE_EVENT(DuplicateTab, _DuplicateTabHandlers, TerminalApp::DuplicateTabEventArgs);
DECLARE_EVENT(NewTabWithProfile, _NewTabWithProfileHandlers, TerminalApp::NewTabWithProfileEventArgs);
DECLARE_EVENT(NewWindow, _NewWindowHandlers, TerminalApp::NewWindowEventArgs);
DECLARE_EVENT(CloseWindow, _CloseWindowHandlers, TerminalApp::CloseWindowEventArgs);
DECLARE_EVENT(CloseTab, _CloseTabHandlers, TerminalApp::CloseTabEventArgs);
DECLARE_EVENT(ClosePane, _ClosePaneHandlers, TerminalApp::ClosePaneEventArgs);
DECLARE_EVENT(SwitchToTab, _SwitchToTabHandlers, TerminalApp::SwitchToTabEventArgs);
DECLARE_EVENT(NextTab, _NextTabHandlers, TerminalApp::NextTabEventArgs);
DECLARE_EVENT(PrevTab, _PrevTabHandlers, TerminalApp::PrevTabEventArgs);
DECLARE_EVENT(SplitVertical, _SplitVerticalHandlers, TerminalApp::SplitVerticalEventArgs);
DECLARE_EVENT(SplitHorizontal, _SplitHorizontalHandlers, TerminalApp::SplitHorizontalEventArgs);
DECLARE_EVENT(IncreaseFontSize, _IncreaseFontSizeHandlers, TerminalApp::IncreaseFontSizeEventArgs);
DECLARE_EVENT(DecreaseFontSize, _DecreaseFontSizeHandlers, TerminalApp::DecreaseFontSizeEventArgs);
DECLARE_EVENT(ScrollUp, _ScrollUpHandlers, TerminalApp::ScrollUpEventArgs);
DECLARE_EVENT(ScrollDown, _ScrollDownHandlers, TerminalApp::ScrollDownEventArgs);
DECLARE_EVENT(ScrollUpPage, _ScrollUpPageHandlers, TerminalApp::ScrollUpPageEventArgs);
DECLARE_EVENT(ScrollDownPage, _ScrollDownPageHandlers, TerminalApp::ScrollDownPageEventArgs);
DECLARE_EVENT(OpenSettings, _OpenSettingsHandlers, TerminalApp::OpenSettingsEventArgs);
DECLARE_EVENT(ResizePane, _ResizePaneHandlers, TerminalApp::ResizePaneEventArgs);
DECLARE_EVENT(MoveFocus, _MoveFocusHandlers, TerminalApp::MoveFocusEventArgs);
DECLARE_EVENT(ToggleCommandPalette, _ToggleCommandPaletteHandlers, TerminalApp::ToggleCommandPaletteEventArgs);
// clang-format on
};
}

View file

@ -58,6 +58,7 @@ namespace TerminalApp
MoveFocusRight,
MoveFocusUp,
MoveFocusDown,
ToggleCommandPalette,
OpenSettings
};
@ -84,6 +85,7 @@ namespace TerminalApp
delegate void OpenSettingsEventArgs();
delegate void ResizePaneEventArgs(Direction direction);
delegate void MoveFocusEventArgs(Direction direction);
delegate void ToggleCommandPaletteEventArgs();
[default_interface] runtimeclass ShortcutActionDispatch {
ShortcutActionDispatch();
@ -113,5 +115,6 @@ namespace TerminalApp
event OpenSettingsEventArgs OpenSettings;
event ResizePaneEventArgs ResizePane;
event MoveFocusEventArgs MoveFocus;
event ToggleCommandPaletteEventArgs ToggleCommandPalette;
}
}

View file

@ -48,6 +48,7 @@ constexpr std::string_view MoveFocusLeftKey{ "moveFocusLeft" };
constexpr std::string_view MoveFocusRightKey{ "moveFocusRight" };
constexpr std::string_view MoveFocusUpKey{ "moveFocusUp" };
constexpr std::string_view MoveFocusDownKey{ "moveFocusDown" };
constexpr std::string_view ToggleCommandPaletteKey{ "toggleCommandPalette" };
// Specifically use a map here over an unordered_map. We want to be able to
// iterate over these entries in-order when we're serializing the keybindings.
@ -104,4 +105,5 @@ const std::map<std::string_view, winrt::TerminalApp::ShortcutAction, std::less<>
{ MoveFocusUpKey, winrt::TerminalApp::ShortcutAction::MoveFocusUp },
{ MoveFocusDownKey, winrt::TerminalApp::ShortcutAction::MoveFocusDown },
{ OpenSettingsKey, winrt::TerminalApp::ShortcutAction::OpenSettings },
{ ToggleCommandPaletteKey, winrt::TerminalApp::ShortcutAction::ToggleCommandPalette },
};

View file

@ -19,5 +19,6 @@ the MIT License. See LICENSE in the project root for license information. -->
<local:TabRowControl x:Name="TabRow" Grid.Row="0" />
<Grid x:Name="TabContent" Grid.Row="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
<local:ActionList Visibility="Collapsed" x:Name="_ActionList" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Top" />
</Grid>
</Page>

View file

@ -41,6 +41,9 @@
<Page Include="../TabRowControl.xaml">
<SubType>Designer</SubType>
</Page>
<Page Include="../ActionList.xaml">
<SubType>Designer</SubType>
</Page>
</ItemGroup>
<!-- ========================= Headers ======================== -->
@ -59,6 +62,9 @@
<ClInclude Include="../TabRowControl.h">
<DependentUpon>../TabRowControl.xaml</DependentUpon>
</ClInclude>
<ClInclude Include="../ActionList.h">
<DependentUpon>../ActionList.xaml</DependentUpon>
</ClInclude>
<ClInclude Include="../Tab.h" />
<ClInclude Include="../Pane.h" />
<ClInclude Include="../ColorScheme.h" />
@ -94,6 +100,9 @@
<ClCompile Include="../TabRowControl.cpp">
<DependentUpon>../TabRowControl.xaml</DependentUpon>
</ClCompile>
<ClCompile Include="../ActionList.cpp">
<DependentUpon>../ActionList.xaml</DependentUpon>
</ClCompile>
<ClCompile Include="../Tab.cpp" />
<ClCompile Include="../Pane.cpp" />
<ClCompile Include="../ColorScheme.cpp" />
@ -155,6 +164,10 @@
<DependentUpon>../TabRowControl.xaml</DependentUpon>
<SubType>Code</SubType>
</Midl>
<Midl Include="../ActionList.idl">
<DependentUpon>../ActionList.xaml</DependentUpon>
<SubType>Code</SubType>
</Midl>
</ItemGroup>
<!-- ========================= Misc Files ======================== -->

View file

@ -68,3 +68,15 @@ private: \
#define DEFINE_GETSET_PROPERTY(className, type, name) \
type className::name() { return _##name; } \
void className::name(const type& value) { _##name = value; }
#define DEFINE_OBSERVABLE_GETSET_PROPERTY(type, name, event) \
public: \
type name() { return _##name; }; \
void name(const type& value) \
{ \
_##name = value; \
event(*this, Windows::UI::Xaml::Data::PropertyChangedEventArgs{ L#name }); \
}; \
\
private: \
type _##name;