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

Add centerOnLaunch setting #9036

Merged
7 commits merged into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
10 changes: 10 additions & 0 deletions src/cascadia/TerminalApp/AppLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,16 @@ namespace winrt::TerminalApp::implementation
};
}

bool AppLogic::CenterOnLaunch()
{
if (!_loadedInitialSettings)
{
// Load settings if we haven't already
LoadSettings();
}
return _settings.GlobalSettings().CenterOnLaunch();
}

winrt::Windows::UI::Xaml::ElementTheme AppLogic::GetRequestedTheme()
{
if (!_loadedInitialSettings)
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalApp/AppLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace winrt::TerminalApp::implementation
bool AlwaysOnTop() const;

Windows::Foundation::Size GetLaunchDimensions(uint32_t dpi);
bool CenterOnLaunch();
TerminalApp::InitialPosition GetInitialPosition(int64_t defaultInitialX, int64_t defaultInitialY);
winrt::Windows::UI::Xaml::ElementTheme GetRequestedTheme();
Microsoft::Terminal::Settings::Model::LaunchMode GetLaunchMode();
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalApp/AppLogic.idl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace TerminalApp
Boolean AlwaysOnTop { get; };

Windows.Foundation.Size GetLaunchDimensions(UInt32 dpi);
Boolean CenterOnLaunch { get; };

InitialPosition GetInitialPosition(Int64 defaultInitialX, Int64 defaultInitialY);
Windows.UI.Xaml.ElementTheme GetRequestedTheme();
Expand Down
5 changes: 5 additions & 0 deletions src/cascadia/TerminalSettingsModel/GlobalAppSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ 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 CenterOnLaunchKey{ "centerOnLaunch" };
static constexpr std::string_view ShowTitleInTitlebarKey{ "showTerminalTitleInTitlebar" };
static constexpr std::string_view ThemeKey{ "theme" };
static constexpr std::string_view TabWidthModeKey{ "tabWidthMode" };
Expand Down Expand Up @@ -105,6 +106,7 @@ winrt::com_ptr<GlobalAppSettings> GlobalAppSettings::Copy() const
globals->_WarnAboutLargePaste = _WarnAboutLargePaste;
globals->_WarnAboutMultiLinePaste = _WarnAboutMultiLinePaste;
globals->_InitialPosition = _InitialPosition;
globals->_CenterOnLaunch = _CenterOnLaunch;
globals->_LaunchMode = _LaunchMode;
globals->_SnapToGridOnResize = _SnapToGridOnResize;
globals->_ForceFullRepaintRendering = _ForceFullRepaintRendering;
Expand Down Expand Up @@ -260,6 +262,8 @@ void GlobalAppSettings::LayerJson(const Json::Value& json)

JsonUtils::GetValueForKey(json, InitialPositionKey, _InitialPosition);

JsonUtils::GetValueForKey(json, CenterOnLaunchKey, _CenterOnLaunch);

JsonUtils::GetValueForKey(json, ShowTitleInTitlebarKey, _ShowTitleInTitlebar);

JsonUtils::GetValueForKey(json, ShowTabsInTitlebarKey, _ShowTabsInTitlebar);
Expand Down Expand Up @@ -379,6 +383,7 @@ Json::Value GlobalAppSettings::ToJson() const
JsonUtils::SetValueForKey(json, InitialRowsKey, _InitialRows);
JsonUtils::SetValueForKey(json, InitialColsKey, _InitialCols);
JsonUtils::SetValueForKey(json, InitialPositionKey, _InitialPosition);
JsonUtils::SetValueForKey(json, CenterOnLaunchKey, _CenterOnLaunch);
JsonUtils::SetValueForKey(json, ShowTitleInTitlebarKey, _ShowTitleInTitlebar);
JsonUtils::SetValueForKey(json, ShowTabsInTitlebarKey, _ShowTabsInTitlebar);
JsonUtils::SetValueForKey(json, WordDelimitersKey, _WordDelimiters);
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/GlobalAppSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
GETSET_SETTING(bool, WarnAboutLargePaste, true);
GETSET_SETTING(bool, WarnAboutMultiLinePaste, true);
GETSET_SETTING(Model::LaunchPosition, InitialPosition, nullptr, nullptr);
GETSET_SETTING(bool, CenterOnLaunch, false);
GETSET_SETTING(Model::LaunchMode, LaunchMode, LaunchMode::DefaultMode);
GETSET_SETTING(bool, SnapToGridOnResize, true);
GETSET_SETTING(bool, ForceFullRepaintRendering, false);
Expand Down
4 changes: 4 additions & 0 deletions src/cascadia/TerminalSettingsModel/GlobalAppSettings.idl
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ namespace Microsoft.Terminal.Settings.Model
void ClearInitialPosition();
LaunchPosition InitialPosition;

Boolean HasCenterOnLaunch();
void ClearCenterOnLaunch();
Boolean CenterOnLaunch;

Boolean HasLaunchMode();
void ClearLaunchMode();
LaunchMode LaunchMode;
Expand Down
22 changes: 20 additions & 2 deletions src/cascadia/WindowsTerminal/AppHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ void AppHost::_HandleCreateWindow(const HWND hwnd, RECT proposedRect, LaunchMode

// Acquire the actual initial position
auto initialPos = _logic.GetInitialPosition(proposedRect.left, proposedRect.top);
const auto centerOnLaunch = _logic.CenterOnLaunch();
proposedRect.left = static_cast<long>(initialPos.X);
proposedRect.top = static_cast<long>(initialPos.Y);

Expand Down Expand Up @@ -374,11 +375,28 @@ void AppHost::_HandleCreateWindow(const HWND hwnd, RECT proposedRect, LaunchMode
adjustedWidth = islandWidth + nonClientSize.cx;
adjustedHeight = islandHeight + nonClientSize.cy;

const COORD origin{ gsl::narrow<short>(proposedRect.left),
gsl::narrow<short>(proposedRect.top) };
const COORD dimensions{ Utils::ClampToShortMax(adjustedWidth, 1),
Utils::ClampToShortMax(adjustedHeight, 1) };

if (centerOnLaunch)
{
// Find nearest monitor for the position that we've actually settled on
HMONITOR hMonNearest = MonitorFromRect(&proposedRect, MONITOR_DEFAULTTONEAREST);
MONITORINFO nearestMonitorInfo;
nearestMonitorInfo.cbSize = sizeof(MONITORINFO);
// Get monitor dimensions:
GetMonitorInfo(hMonNearest, &nearestMonitorInfo);
const COORD desktopDimensions{ gsl::narrow<short>(nearestMonitorInfo.rcWork.right - nearestMonitorInfo.rcWork.left),
gsl::narrow<short>(nearestMonitorInfo.rcWork.bottom - nearestMonitorInfo.rcWork.top) };
// Move our proposed location into the center of that specific monitor.
proposedRect.left = nearestMonitorInfo.rcWork.left +
((desktopDimensions.X / 2) - (dimensions.X / 2));
proposedRect.top = nearestMonitorInfo.rcWork.top +
((desktopDimensions.Y / 2) - (dimensions.Y / 2));
}
const COORD origin{ gsl::narrow<short>(proposedRect.left),
gsl::narrow<short>(proposedRect.top) };

const auto newPos = Viewport::FromDimensions(origin, dimensions);
bool succeeded = SetWindowPos(hwnd,
nullptr,
Expand Down