Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into dev/migrie/f/754-la…
Browse files Browse the repository at this point in the history
…yer-settings
  • Loading branch information
zadjii-msft committed Aug 21, 2019
2 parents 16336bb + 8096d7c commit 870fed3
Show file tree
Hide file tree
Showing 22 changed files with 314 additions and 61 deletions.
1 change: 1 addition & 0 deletions doc/cascadia/SettingsSchema.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Properties listed below affect the entire window, regardless of the profile sett
| Property | Necessity | Type | Default | Description |
| -------- | --------- | ---- | ------- | ----------- |
| `alwaysShowTabs` | _Required_ | Boolean | `true` | When set to `true`, tabs are always displayed. When set to `false` and `showTabsInTitlebar` is set to `false`, tabs only appear after typing <kbd>Ctrl</kbd> + <kbd>T</kbd>. |
| `copyOnSelect` | Optional | Boolean | `false` | When set to `true`, a selection is immediately copied to your clipboard upon creation. When set to `false`, the selection persists and awaits further action. |
| `defaultProfile` | _Required_ | String | PowerShell guid | Sets the default profile. Opens by typing <kbd>Ctrl</kbd> + <kbd>T</kbd> or by clicking the '+' icon. The guid of the desired default profile is used as the value. |
| `initialCols` | _Required_ | Integer | `120` | The number of columns displayed in the window upon first load. |
| `initialRows` | _Required_ | Integer | `30` | The number of rows displayed in the window upon first load. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<application>
<!-- Windows 10 1903 -->
<!-- See https://docs.microsoft.com/en-us/windows/apps/desktop/modernize/xaml-islands -->
<!-- "maxversiontested" is CASE SENSITIVE. Do not change this.-->
<maxversiontested Id="10.0.18362.0"/>
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
Expand Down
25 changes: 15 additions & 10 deletions src/cascadia/TerminalApp/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,19 +680,15 @@ namespace winrt::TerminalApp::implementation

// Method Description:
// - Attempt to load the settings. If we fail for any reason, returns an error.
// Arguments:
// - saveOnLoad: If true, after loading the settings, we should re-write
// them to the file, to make sure the schema is updated. See
// `CascadiaSettings::LoadAll` for details.
// Return Value:
// - S_OK if we successfully parsed the settings, otherwise an appropriate HRESULT.
[[nodiscard]] HRESULT App::_TryLoadSettings(const bool saveOnLoad) noexcept
[[nodiscard]] HRESULT App::_TryLoadSettings() noexcept
{
HRESULT hr = E_FAIL;

try
{
auto newSettings = CascadiaSettings::LoadAll(saveOnLoad);
auto newSettings = CascadiaSettings::LoadAll();
_settings = std::move(newSettings);
const auto& warnings = _settings->GetWarnings();
hr = warnings.size() == 0 ? S_OK : S_FALSE;
Expand Down Expand Up @@ -733,7 +729,7 @@ namespace winrt::TerminalApp::implementation
// we should display the loading error.
// * We can't display the error now, because we might not have a
// UI yet. We'll display the error in _OnLoaded.
_settingsLoadedResult = _TryLoadSettings(true);
_settingsLoadedResult = _TryLoadSettings();

if (FAILED(_settingsLoadedResult))
{
Expand Down Expand Up @@ -813,7 +809,7 @@ namespace winrt::TerminalApp::implementation
// - don't change the settings (and don't actually apply the new settings)
// - don't persist them.
// - display a loading error
_settingsLoadedResult = _TryLoadSettings(false);
_settingsLoadedResult = _TryLoadSettings();

if (FAILED(_settingsLoadedResult))
{
Expand Down Expand Up @@ -1494,11 +1490,19 @@ namespace winrt::TerminalApp::implementation

const auto controlConnection = _CreateConnectionFromSettings(realGuid, controlSettings);

TermControl newControl{ controlSettings, controlConnection };

const int focusedTabIndex = _GetFocusedTabIndex();
auto focusedTab = _tabs[focusedTabIndex];

const auto canSplit = splitType == Pane::SplitState::Horizontal ? focusedTab->CanAddHorizontalSplit() :
focusedTab->CanAddVerticalSplit();

if (!canSplit)
{
return;
}

TermControl newControl{ controlSettings, controlConnection };

// Hookup our event handlers to the new terminal
_RegisterTerminalEvents(newControl, focusedTab);

Expand Down Expand Up @@ -1546,6 +1550,7 @@ namespace winrt::TerminalApp::implementation
}

Clipboard::SetContent(dataPack);
Clipboard::Flush();
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ namespace winrt::TerminalApp::implementation
void _ShowLoadWarningsDialog();
void _ShowLoadErrorsDialog(const winrt::hstring& titleKey, const winrt::hstring& contentKey);

[[nodiscard]] HRESULT _TryLoadSettings(const bool saveOnLoad) noexcept;
[[nodiscard]] HRESULT _TryLoadSettings() noexcept;
void _LoadSettings();
void _OpenSettings();

Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/CascadiaSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class TerminalApp::CascadiaSettings final
CascadiaSettings();
~CascadiaSettings();

static std::unique_ptr<CascadiaSettings> LoadAll(const bool saveOnLoad = true);
static std::unique_ptr<CascadiaSettings> LoadAll();
void SaveAll() const;

winrt::Microsoft::Terminal::Settings::TerminalSettings MakeSettings(std::optional<GUID> profileGuid) const;
Expand Down
21 changes: 1 addition & 20 deletions src/cascadia/TerminalApp/CascadiaSettingsSerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,9 @@ static constexpr std::string_view Utf8Bom{ u8"\uFEFF" };
// it will load the settings from our packaged localappdata. If we're
// running as an unpackaged application, it will read it from the path
// we've set under localappdata.
// Arguments:
// - saveOnLoad: If true, we'll write the settings back out after we load them,
// to make sure the schema is updated.
// Return Value:
// - a unique_ptr containing a new CascadiaSettings object.
std::unique_ptr<CascadiaSettings> CascadiaSettings::LoadAll(const bool saveOnLoad)
std::unique_ptr<CascadiaSettings> CascadiaSettings::LoadAll()
{
std::unique_ptr<CascadiaSettings> resultPtr;
std::optional<std::string> fileData = _ReadSettings();
Expand Down Expand Up @@ -70,22 +67,6 @@ std::unique_ptr<CascadiaSettings> CascadiaSettings::LoadAll(const bool saveOnLoa

// If this throws, the app will catch it and use the default settings (temporarily)
resultPtr->_ValidateSettings();

const bool foundWarnings = resultPtr->_warnings.size() > 0;

// Don't save on load if there were warnings - we tried to gracefully
// handle them.
if (saveOnLoad && !foundWarnings)
{
// Logically compare the json we've parsed from the file to what
// we'd serialize at runtime. If the values are different, then
// write the updated schema back out.
const Json::Value reserialized = resultPtr->ToJson();
if (reserialized != root)
{
resultPtr->SaveAll();
}
}
}
else
{
Expand Down
21 changes: 20 additions & 1 deletion src/cascadia/TerminalApp/GlobalAppSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ static constexpr std::string_view ShowTitleInTitlebarKey{ "showTerminalTitleInTi
static constexpr std::string_view RequestedThemeKey{ "requestedTheme" };
static constexpr std::string_view ShowTabsInTitlebarKey{ "showTabsInTitlebar" };
static constexpr std::string_view WordDelimitersKey{ "wordDelimiters" };
static constexpr std::string_view CopyOnSelectKey{ "copyOnSelect" };

static constexpr std::wstring_view LightThemeValue{ L"light" };
static constexpr std::wstring_view DarkThemeValue{ L"dark" };
Expand All @@ -39,7 +40,8 @@ GlobalAppSettings::GlobalAppSettings() :
_showTitleInTitlebar{ true },
_showTabsInTitlebar{ true },
_requestedTheme{ ElementTheme::Default },
_wordDelimiters{ DEFAULT_WORD_DELIMITERS }
_wordDelimiters{ DEFAULT_WORD_DELIMITERS },
_copyOnSelect{ false }
{
}

Expand Down Expand Up @@ -117,6 +119,16 @@ void GlobalAppSettings::SetWordDelimiters(const std::wstring wordDelimiters) noe
_wordDelimiters = wordDelimiters;
}

bool GlobalAppSettings::GetCopyOnSelect() const noexcept
{
return _copyOnSelect;
}

void GlobalAppSettings::SetCopyOnSelect(const bool copyOnSelect) noexcept
{
_copyOnSelect = copyOnSelect;
}

#pragma region ExperimentalSettings
bool GlobalAppSettings::GetShowTabsInTitlebar() const noexcept
{
Expand All @@ -141,6 +153,7 @@ void GlobalAppSettings::ApplyToSettings(TerminalSettings& settings) const noexce
settings.InitialRows(_initialRows);
settings.InitialCols(_initialCols);
settings.WordDelimiters(_wordDelimiters);
settings.CopyOnSelect(_copyOnSelect);
}

// Method Description:
Expand All @@ -160,6 +173,7 @@ Json::Value GlobalAppSettings::ToJson() const
jsonObject[JsonKey(ShowTitleInTitlebarKey)] = _showTitleInTitlebar;
jsonObject[JsonKey(ShowTabsInTitlebarKey)] = _showTabsInTitlebar;
jsonObject[JsonKey(WordDelimitersKey)] = winrt::to_string(_wordDelimiters);
jsonObject[JsonKey(CopyOnSelectKey)] = _copyOnSelect;
jsonObject[JsonKey(RequestedThemeKey)] = winrt::to_string(_SerializeTheme(_requestedTheme));
jsonObject[JsonKey(KeybindingsKey)] = AppKeyBindingsSerialization::ToJson(_keybindings);

Expand Down Expand Up @@ -210,6 +224,11 @@ GlobalAppSettings GlobalAppSettings::FromJson(const Json::Value& json)
result._wordDelimiters = GetWstringFromJson(wordDelimiters);
}

if (auto copyOnSelect{ json[JsonKey(CopyOnSelectKey)] })
{
result._copyOnSelect = copyOnSelect.asBool();
}

if (auto requestedTheme{ json[JsonKey(RequestedThemeKey)] })
{
result._requestedTheme = _ParseTheme(GetWstringFromJson(requestedTheme));
Expand Down
4 changes: 4 additions & 0 deletions src/cascadia/TerminalApp/GlobalAppSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class TerminalApp::GlobalAppSettings final
std::wstring GetWordDelimiters() const noexcept;
void SetWordDelimiters(const std::wstring wordDelimiters) noexcept;

bool GetCopyOnSelect() const noexcept;
void SetCopyOnSelect(const bool copyOnSelect) noexcept;

winrt::Windows::UI::Xaml::ElementTheme GetRequestedTheme() const noexcept;

Json::Value ToJson() const;
Expand All @@ -72,6 +75,7 @@ class TerminalApp::GlobalAppSettings final

bool _showTabsInTitlebar;
std::wstring _wordDelimiters;
bool _copyOnSelect;
winrt::Windows::UI::Xaml::ElementTheme _requestedTheme;

static winrt::Windows::UI::Xaml::ElementTheme _ParseTheme(const std::wstring& themeString) noexcept;
Expand Down
84 changes: 84 additions & 0 deletions src/cascadia/TerminalApp/Pane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,31 @@ void Pane::_ApplySplitDefinitions()
}
}

// Method Description:
// - Determines whether the pane can be split vertically
// Arguments:
// - splitType: what type of split we want to create.
// Return Value:
// - True if the pane can be split vertically. False otherwise.
bool Pane::CanSplitVertical()
{
if (!_IsLeaf())
{
if (_firstChild->_HasFocusedChild())
{
return _firstChild->CanSplitVertical();
}
else if (_secondChild->_HasFocusedChild())
{
return _secondChild->CanSplitVertical();
}

return false;
}

return _CanSplit(SplitState::Vertical);
}

// Method Description:
// - Vertically split the focused pane in our tree of panes, and place the given
// TermControl into the newly created pane. If we're the focused pane, then
Expand Down Expand Up @@ -806,6 +831,31 @@ void Pane::SplitVertical(const GUID& profile, const TermControl& control)
_Split(SplitState::Vertical, profile, control);
}

// Method Description:
// - Determines whether the pane can be split horizontally
// Arguments:
// - splitType: what type of split we want to create.
// Return Value:
// - True if the pane can be split horizontally. False otherwise.
bool Pane::CanSplitHorizontal()
{
if (!_IsLeaf())
{
if (_firstChild->_HasFocusedChild())
{
return _firstChild->CanSplitHorizontal();
}
else if (_secondChild->_HasFocusedChild())
{
return _secondChild->CanSplitHorizontal();
}

return false;
}

return _CanSplit(SplitState::Horizontal);
}

// Method Description:
// - Horizontally split the focused pane in our tree of panes, and place the given
// TermControl into the newly created pane. If we're the focused pane, then
Expand Down Expand Up @@ -834,6 +884,40 @@ void Pane::SplitHorizontal(const GUID& profile, const TermControl& control)
_Split(SplitState::Horizontal, profile, control);
}

// Method Description:
// - Determines whether the pane can be split.
// Arguments:
// - splitType: what type of split we want to create.
// Return Value:
// - True if the pane can be split. False otherwise.
bool Pane::_CanSplit(SplitState splitType)
{
const bool changeWidth = _splitState == SplitState::Vertical;

const Size actualSize{ gsl::narrow_cast<float>(_root.ActualWidth()),
gsl::narrow_cast<float>(_root.ActualHeight()) };

const Size minSize = _GetMinSize();

if (splitType == SplitState::Vertical)
{
const auto widthMinusSeparator = actualSize.Width - PaneSeparatorSize;
const auto newWidth = widthMinusSeparator * Half;

return newWidth > minSize.Width;
}

if (splitType == SplitState::Horizontal)
{
const auto heightMinusSeparator = actualSize.Height - PaneSeparatorSize;
const auto newHeight = heightMinusSeparator * Half;

return newHeight > minSize.Height;
}

return false;
}

// Method Description:
// - Does the bulk of the work of creating a new split. Initializes our UI,
// creates a new Pane to host the control, registers event handlers.
Expand Down
4 changes: 4 additions & 0 deletions src/cascadia/TerminalApp/Pane.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ class Pane : public std::enable_shared_from_this<Pane>
bool ResizePane(const winrt::TerminalApp::Direction& direction);
bool NavigateFocus(const winrt::TerminalApp::Direction& direction);

bool CanSplitHorizontal();
void SplitHorizontal(const GUID& profile, const winrt::Microsoft::Terminal::TerminalControl::TermControl& control);

bool CanSplitVertical();
void SplitVertical(const GUID& profile, const winrt::Microsoft::Terminal::TerminalControl::TermControl& control);

void Close();
Expand Down Expand Up @@ -79,6 +82,7 @@ class Pane : public std::enable_shared_from_this<Pane>
bool _HasFocusedChild() const noexcept;
void _SetupChildCloseHandlers();

bool _CanSplit(SplitState splitType);
void _Split(SplitState splitType, const GUID& profile, const winrt::Microsoft::Terminal::TerminalControl::TermControl& control);
void _CreateRowColDefinitions(const winrt::Windows::Foundation::Size& rootSize);
void _CreateSplitContent();
Expand Down
18 changes: 18 additions & 0 deletions src/cascadia/TerminalApp/Tab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ void Tab::Scroll(const int delta)
});
}

// Method Description:
// - Determines whether the focused pane has sufficient space to be split vertically.
// Return Value:
// - True if the focused pane can be split horizontally. False otherwise.
bool Tab::CanAddVerticalSplit()
{
return _rootPane->CanSplitVertical();
}

// Method Description:
// - Vertically split the focused pane in our tree of panes, and place the
// given TermControl into the newly created pane.
Expand All @@ -216,6 +225,15 @@ void Tab::AddVerticalSplit(const GUID& profile, TermControl& control)
_rootPane->SplitVertical(profile, control);
}

// Method Description:
// - Determines whether the focused pane has sufficient space to be split horizontally.
// Return Value:
// - True if the focused pane can be split horizontally. False otherwise.
bool Tab::CanAddHorizontalSplit()
{
return _rootPane->CanSplitHorizontal();
}

// Method Description:
// - Horizontally split the focused pane in our tree of panes, and place the
// given TermControl into the newly created pane.
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalApp/Tab.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ class Tab
void SetFocused(const bool focused);

void Scroll(const int delta);
bool CanAddVerticalSplit();
void AddVerticalSplit(const GUID& profile, winrt::Microsoft::Terminal::TerminalControl::TermControl& control);
bool CanAddHorizontalSplit();
void AddHorizontalSplit(const GUID& profile, winrt::Microsoft::Terminal::TerminalControl::TermControl& control);

void UpdateFocus();
Expand Down
Loading

0 comments on commit 870fed3

Please sign in to comment.