Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable setting an initial position and maximization launch for Terminal #2817

Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
9e85571
Spec version 1 for set terminal initial position
KaiyuWang16 Sep 3, 2019
dbbba4e
Fix the spec title
KaiyuWang16 Sep 3, 2019
5a919c7
Add a new function to read initial position properties
KaiyuWang16 Sep 4, 2019
5e8dc81
Merge branch 'master' into dev/kawa/1043-set-initial-position-for-the…
KaiyuWang16 Sep 4, 2019
5e947ac
Test version - 9-6
KaiyuWang16 Sep 6, 2019
8fbe307
spec for code review updates
KaiyuWang16 Sep 6, 2019
f5926f8
Code change 9-11
KaiyuWang16 Sep 12, 2019
6c59f37
temporary changes 9 12
KaiyuWang16 Sep 12, 2019
e2999b1
Enable setting an initial position for terminal and maximization launch
KaiyuWang16 Sep 19, 2019
6206ecb
remove empty lines
KaiyuWang16 Sep 19, 2019
524f589
remove empty lines
KaiyuWang16 Sep 19, 2019
bea181f
Support resize when the Terminal Window hangs off the screen
KaiyuWang16 Sep 23, 2019
c60b074
Merge branch 'master' into dev/kawa/1043-set-initial-position-for-the…
KaiyuWang16 Sep 24, 2019
621c2c9
sync to master, fix conflict and typing warnings
KaiyuWang16 Sep 24, 2019
e285ac4
Remove unreferenced parameter to fix warnings
KaiyuWang16 Sep 24, 2019
9fc29b6
fix format issues
KaiyuWang16 Sep 24, 2019
9520adb
remove support for launch minimization
KaiyuWang16 Sep 24, 2019
1a24936
Code review changes
KaiyuWang16 Sep 27, 2019
9433b72
remove spec in this PR
KaiyuWang16 Sep 27, 2019
a682e6d
Move launchMode0 string serializarion to GlobalAppSettings
KaiyuWang16 Sep 28, 2019
1a330d7
remove useless default value definition and fix typos
KaiyuWang16 Sep 30, 2019
3ba5eb8
Merge branch 'master' into dev/kawa/1043-set-initial-position-for-the…
KaiyuWang16 Sep 30, 2019
a7cc388
Use std::optional<> for initial X/Y value, fix format issue
KaiyuWang16 Oct 10, 2019
c700753
Combine the initial X/Y position into one string property
KaiyuWang16 Oct 11, 2019
bba2b9b
Change the return type of AppHost::_HandleCreateWindow back to void
KaiyuWang16 Oct 11, 2019
929e8ae
Elinamate useless bools for initialX/Y
KaiyuWang16 Oct 12, 2019
1cf8f83
remove useless setters in GlobalAppSettings
KaiyuWang16 Oct 14, 2019
e39b110
Remove useless variables in GlobalAppSettings
KaiyuWang16 Oct 15, 2019
5cf1037
Merge remote-tracking branch 'github/master' into HEAD
DHowett Oct 17, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/cascadia/TerminalApp/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,60 @@ namespace winrt::TerminalApp::implementation
return TermControl::GetProposedDimensions(settings, dpi);
}

// Method Description:
// - Get the launch mode in json settings file. Now there
// two launch mode: default, maximized. Default means the window
// will launch according to the launch dimensions provided. Maximized
// means the window will launch as a maximized window
// Arguments:
// - <none>
// Return Value:
// - LaunchMode enum that indicates the launch mode
LaunchMode App::GetLaunchMode()
{
if (!_loadedInitialSettings)
{
// Load settings if we haven't already
LoadSettings();
}

return _settings->GlobalSettings().GetLaunchMode();
}

// Method Description:
// - Get the user defined initial position from Json settings file.
// This position represents the top left corner of the Terminal window.
// This setting is optional, if not provided, we will use the system
// default size, which is provided in IslandWindow::MakeWindow.
// Arguments:
// - defaultInitialX: the system default x coordinate value
// - defaultInitialY: the system defualt y coordinate value
// Return Value:
// - a point containing the requested initial position in pixels.
winrt::Windows::Foundation::Point App::GetLaunchInitialPositions(int32_t defaultInitialX, int32_t defaultInitialY)
{
if (!_loadedInitialSettings)
{
// Load settings if we haven't already
LoadSettings();
}

winrt::Windows::Foundation::Point point((float)defaultInitialX, (float)defaultInitialY);

auto initialX = _settings->GlobalSettings().GetInitialX();
auto initialY = _settings->GlobalSettings().GetInitialY();
if (initialX.has_value())
{
point.X = gsl::narrow_cast<float>(initialX.value());
}
if (initialY.has_value())
{
point.Y = gsl::narrow_cast<float>(initialY.value());
}

return point;
}

bool App::GetShowTabsInTitlebar()
{
if (!_loadedInitialSettings)
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalApp/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ namespace winrt::TerminalApp::implementation
void LoadSettings();

Windows::Foundation::Point GetLaunchDimensions(uint32_t dpi);
winrt::Windows::Foundation::Point GetLaunchInitialPositions(int32_t defaultInitialX, int32_t defaultInitialY);
LaunchMode GetLaunchMode();
bool GetShowTabsInTitlebar();

Windows::UI::Xaml::UIElement GetRoot() noexcept;
Expand Down
10 changes: 9 additions & 1 deletion src/cascadia/TerminalApp/App.idl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@

namespace TerminalApp
{
enum LaunchMode
{
DefaultMode,
MaximizedMode,
};

delegate void LastTabClosedEventArgs();
[default_interface] runtimeclass App : Microsoft.Toolkit.Win32.UI.XamlHost.XamlApplication
{

App();

void Initialize();
Expand All @@ -18,12 +25,13 @@ namespace TerminalApp
void Create();

void LoadSettings();

Windows.UI.Xaml.UIElement GetRoot();

String Title { get; };

Windows.Foundation.Point GetLaunchDimensions(UInt32 dpi);
Windows.Foundation.Point GetLaunchInitialPositions(Int32 defaultInitialX, Int32 defaultInitialY);
LaunchMode GetLaunchMode();
Boolean GetShowTabsInTitlebar();
void TitlebarClicked();

Expand Down
157 changes: 154 additions & 3 deletions src/cascadia/TerminalApp/GlobalAppSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "../../inc/DefaultSettings.h"
#include "Utils.h"
#include "JsonUtils.h"
#include <sstream>

using namespace TerminalApp;
using namespace winrt::Microsoft::Terminal::Settings;
Expand All @@ -20,12 +21,15 @@ static constexpr std::string_view DefaultProfileKey{ "defaultProfile" };
static constexpr std::string_view AlwaysShowTabsKey{ "alwaysShowTabs" };
static constexpr std::string_view InitialRowsKey{ "initialRows" };
static constexpr std::string_view InitialColsKey{ "initialCols" };
static constexpr std::string_view InitialPositionKey{ "initialPosition" };
static constexpr std::string_view ShowTitleInTitlebarKey{ "showTerminalTitleInTitlebar" };
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::string_view LaunchModeKey{ "launchMode" };
static constexpr std::wstring_view DefaultLaunchModeValue{ L"default" };
static constexpr std::wstring_view MaximizedLaunchModeValue{ L"maximized" };
static constexpr std::wstring_view LightThemeValue{ L"light" };
static constexpr std::wstring_view DarkThemeValue{ L"dark" };
static constexpr std::wstring_view SystemThemeValue{ L"system" };
Expand All @@ -37,11 +41,14 @@ GlobalAppSettings::GlobalAppSettings() :
_alwaysShowTabs{ true },
_initialRows{ DEFAULT_ROWS },
_initialCols{ DEFAULT_COLS },
_initialX{},
_initialY{},
_showTitleInTitlebar{ true },
_showTabsInTitlebar{ true },
_requestedTheme{ ElementTheme::Default },
_wordDelimiters{ DEFAULT_WORD_DELIMITERS },
_copyOnSelect{ false }
_copyOnSelect{ false },
_launchMode{ LaunchMode::DefaultMode }
{
}

Expand Down Expand Up @@ -124,6 +131,16 @@ void GlobalAppSettings::SetCopyOnSelect(const bool copyOnSelect) noexcept
_copyOnSelect = copyOnSelect;
}

LaunchMode GlobalAppSettings::GetLaunchMode() const noexcept
{
return _launchMode;
}

void GlobalAppSettings::SetLaunchMode(const LaunchMode launchMode)
{
_launchMode = launchMode;
}

#pragma region ExperimentalSettings
bool GlobalAppSettings::GetShowTabsInTitlebar() const noexcept
{
Expand All @@ -134,6 +151,17 @@ void GlobalAppSettings::SetShowTabsInTitlebar(const bool showTabsInTitlebar) noe
{
_showTabsInTitlebar = showTabsInTitlebar;
}

std::optional<int32_t> GlobalAppSettings::GetInitialX() const noexcept
{
return _initialX;
}

std::optional<int32_t> GlobalAppSettings::GetInitialY() const noexcept
{
return _initialY;
}

#pragma endregion

// Method Description:
Expand All @@ -147,6 +175,7 @@ void GlobalAppSettings::ApplyToSettings(TerminalSettings& settings) const noexce
settings.KeyBindings(GetKeybindings());
settings.InitialRows(_initialRows);
settings.InitialCols(_initialCols);

settings.WordDelimiters(_wordDelimiters);
settings.CopyOnSelect(_copyOnSelect);
}
Expand All @@ -164,11 +193,13 @@ Json::Value GlobalAppSettings::ToJson() const
jsonObject[JsonKey(DefaultProfileKey)] = winrt::to_string(Utils::GuidToString(_defaultProfile));
jsonObject[JsonKey(InitialRowsKey)] = _initialRows;
jsonObject[JsonKey(InitialColsKey)] = _initialCols;
jsonObject[JsonKey(InitialPositionKey)] = _SerializeInitialPosition(_initialX, _initialY);
jsonObject[JsonKey(AlwaysShowTabsKey)] = _alwaysShowTabs;
jsonObject[JsonKey(ShowTitleInTitlebarKey)] = _showTitleInTitlebar;
jsonObject[JsonKey(ShowTabsInTitlebarKey)] = _showTabsInTitlebar;
jsonObject[JsonKey(WordDelimitersKey)] = winrt::to_string(_wordDelimiters);
jsonObject[JsonKey(CopyOnSelectKey)] = _copyOnSelect;
jsonObject[JsonKey(LaunchModeKey)] = winrt::to_string(_SerializeLaunchMode(_launchMode));
jsonObject[JsonKey(RequestedThemeKey)] = winrt::to_string(_SerializeTheme(_requestedTheme));
jsonObject[JsonKey(KeybindingsKey)] = _keybindings->ToJson();

Expand Down Expand Up @@ -208,7 +239,10 @@ void GlobalAppSettings::LayerJson(const Json::Value& json)
{
_initialCols = initialCols.asInt();
}

if (auto initialPosition{ json[JsonKey(InitialPositionKey)] })
{
_ParseInitialPosition(GetWstringFromJson(initialPosition), _initialX, _initialY);
}
if (auto showTitleInTitlebar{ json[JsonKey(ShowTitleInTitlebarKey)] })
{
_showTitleInTitlebar = showTitleInTitlebar.asBool();
Expand All @@ -229,6 +263,11 @@ void GlobalAppSettings::LayerJson(const Json::Value& json)
_copyOnSelect = copyOnSelect.asBool();
}

if (auto launchMode{ json[JsonKey(LaunchModeKey)] })
{
_launchMode = _ParseLaunchMode(GetWstringFromJson(launchMode));
}

if (auto requestedTheme{ json[JsonKey(RequestedThemeKey)] })
{
_requestedTheme = _ParseTheme(GetWstringFromJson(requestedTheme));
Expand Down Expand Up @@ -280,3 +319,115 @@ std::wstring_view GlobalAppSettings::_SerializeTheme(const ElementTheme theme) n
return SystemThemeValue;
}
}

// Method Description:
// - Helper function for converting the initial position string into
// 2 coordinate values. We allow users to only provide one coordinate,
// thus, we use comma as the separater:
// (100, 100): standard input string
// (, 100), (100, ): if a value is missing, we set this value as a default
// (,): both x and y are set to default
// (abc, 100): if a value is not valid, we treat it as default
// (100, 100, 100): we only read the first two values, this is equivalent to (100, 100)
// Arguments:
// - initialPosition: the initial position string from json
// initialX: reference to the _initialX member
// initialY: reference to the _initialY member
// Return Value:
// - None
void GlobalAppSettings::_ParseInitialPosition(const std::wstring& initialPosition,
std::optional<int32_t>& initialX,
std::optional<int32_t>& initialY) noexcept
{
const wchar_t singleCharDelim = L',';
std::wstringstream tokenStream(initialPosition.c_str());
std::wstring token;
uint8_t initialPosIndex = 0;
size_t* idx = nullptr;

// Get initial position values till we run out of delimiter separated values in the stream
// or we hit max number of allowable values (= 2)
// Non-numeral values or empty string will be caught as exception and we do not assign them
for (; std::getline(tokenStream, token, singleCharDelim) && (initialPosIndex < 2); initialPosIndex++)
{
try
{
int32_t position = std::stoi(token, idx);
if (initialPosIndex == 0)
{
initialX.emplace(position);
}

if (initialPosIndex == 1)
{
initialY.emplace(position);
}
}
catch (...)
{
// Do nothing
}
}
}

// Method Description:
// - Helper function for converting X/Y initial positions to a string
// value.
// Arguments:
// - initialX: reference to the _initialX member
// initialY: reference to the _initialY member
// Return Value:
// - The concatenated string for the the current initialX and initialY
std::string GlobalAppSettings::_SerializeInitialPosition(const std::optional<int32_t>& initialX,
const std::optional<int32_t>& initialY) noexcept
{
std::string serializedInitialPos = "(";
if (initialX.has_value())
{
serializedInitialPos += initialX.value();
}

serializedInitialPos += ", ";

if (initialY.has_value())
{
serializedInitialPos += initialY.value();
}

return serializedInitialPos;
}

// Method Description:
// - Helper function for converting the user-specified launch mode
// to a LaunchMode enum value
// Arguments:
// - launchModeString: The string value from the settings file to parse
// Return Value:
// - The corresponding enum value which maps to the string provided by the user
LaunchMode GlobalAppSettings::_ParseLaunchMode(const std::wstring& launchModeString) noexcept
{
if (launchModeString == MaximizedLaunchModeValue)
{
return LaunchMode::MaximizedMode;
}

return LaunchMode::DefaultMode;
}

// Method Description:
// - Helper function for converting a LaunchMode to its corresponding string
// value.
// Arguments:
// - launchMode: The enum value to convert to a string.
// Return Value:
// - The string value for the given LaunchMode
std::wstring_view GlobalAppSettings::_SerializeLaunchMode(const LaunchMode launchMode) noexcept
{
switch (launchMode)
{
case LaunchMode::MaximizedMode:
return MaximizedLaunchModeValue;
default:
return DefaultLaunchModeValue;
}
}
22 changes: 22 additions & 0 deletions src/cascadia/TerminalApp/GlobalAppSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ class TerminalApp::GlobalAppSettings final
bool GetCopyOnSelect() const noexcept;
void SetCopyOnSelect(const bool copyOnSelect) noexcept;

std::optional<int32_t> GetInitialX() const noexcept;

std::optional<int32_t> GetInitialY() const noexcept;

winrt::TerminalApp::LaunchMode GetLaunchMode() const noexcept;
void SetLaunchMode(const winrt::TerminalApp::LaunchMode launchMode);

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

Json::Value ToJson() const;
Expand All @@ -75,6 +82,9 @@ class TerminalApp::GlobalAppSettings final
int32_t _initialRows;
int32_t _initialCols;

std::optional<int32_t> _initialX;
std::optional<int32_t> _initialY;

bool _showStatusline;
bool _alwaysShowTabs;
bool _showTitleInTitlebar;
Expand All @@ -84,8 +94,20 @@ class TerminalApp::GlobalAppSettings final
bool _copyOnSelect;
winrt::Windows::UI::Xaml::ElementTheme _requestedTheme;

winrt::TerminalApp::LaunchMode _launchMode;

static winrt::Windows::UI::Xaml::ElementTheme _ParseTheme(const std::wstring& themeString) noexcept;
static std::wstring_view _SerializeTheme(const winrt::Windows::UI::Xaml::ElementTheme theme) noexcept;

static void _ParseInitialPosition(const std::wstring& initialPosition,
std::optional<int32_t>& initialX,
std::optional<int32_t>& initialY) noexcept;

static std::string _SerializeInitialPosition(const std::optional<int32_t>& initialX,
const std::optional<int32_t>& initialY) noexcept;

static std::wstring_view _SerializeLaunchMode(const winrt::TerminalApp::LaunchMode launchMode) noexcept;
static winrt::TerminalApp::LaunchMode _ParseLaunchMode(const std::wstring& launchModeString) noexcept;

friend class TerminalAppLocalTests::SettingsTests;
};
Loading