Skip to content

Commit 8987486

Browse files
authored
Add support for --fullscreen, --maximized (#6139)
## Summary of the Pull Request Adds two new flags to the `wt.exe` alias: * `--maximized,-M`: Launch the new Terminal window maximized. This flag cannot be combined with `--fullscreen`. * `--fullscreen,-F`: Launch the new Terminal window fullscreen. This flag cannot be combined with `--maximized`. ## References * This builds on the work done in #6060. * The cmdline args megathread: #4632 ## PR Checklist * [x] Closes #5801 * [x] I work here * [ ] Tests added/passed * [n/a] Requires documentation to be updated ## Detailed Description of the Pull Request / Additional comments * I had to move the commandline arg parsing up a layer from `TerminalPage` to `AppLogic`, because `AppLogic` controls the Terminal's settings, including launch mode settings. This seems like a reasonable change, to put both the settings from the file and the commandline in the same place. - **Most of the diff is that movement of code** * _"What happens when you try to pass both flags, like `wtd -M -F new-tab`?"_: ![image](https://user-images.githubusercontent.com/18356694/82679939-3cffde00-9c11-11ea-8d88-03ec7db83e59.png) ## Validation Steps Performed * Ran a bunch of commandlines to see what happened.
1 parent d92c829 commit 8987486

File tree

9 files changed

+160
-129
lines changed

9 files changed

+160
-129
lines changed

src/cascadia/TerminalApp/AppActionHandlers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ namespace winrt::TerminalApp::implementation
232232
void TerminalPage::_HandleToggleFullscreen(const IInspectable& /*sender*/,
233233
const TerminalApp::ActionEventArgs& args)
234234
{
235-
_ToggleFullscreen();
235+
ToggleFullscreen();
236236
args.Handled(true);
237237
}
238238
}

src/cascadia/TerminalApp/AppCommandlineArgs.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ int AppCommandlineArgs::_handleExit(const CLI::App& command, const CLI::Error& e
157157
// - <none>
158158
void AppCommandlineArgs::_buildParser()
159159
{
160+
// -v,--version: Displays version info
160161
auto versionCallback = [this](int64_t /*count*/) {
161162
if (const auto appLogic{ winrt::TerminalApp::implementation::AppLogic::Current() })
162163
{
@@ -173,6 +174,20 @@ void AppCommandlineArgs::_buildParser()
173174
};
174175
_app.add_flag_function("-v,--version", versionCallback, RS_A(L"CmdVersionDesc"));
175176

177+
// Maximized and Fullscreen flags
178+
// -M,--maximized: Maximizes the window on launch
179+
// -F,--fullscreen: Fullscreens the window on launch
180+
auto maximizedCallback = [this](int64_t /*count*/) {
181+
_launchMode = winrt::TerminalApp::LaunchMode::MaximizedMode;
182+
};
183+
auto fullscreenCallback = [this](int64_t /*count*/) {
184+
_launchMode = winrt::TerminalApp::LaunchMode::FullscreenMode;
185+
};
186+
auto maximized = _app.add_flag_function("-M,--maximized", maximizedCallback, RS_A(L"CmdMaximizedDesc"));
187+
auto fullscreen = _app.add_flag_function("-F,--fullscreen", fullscreenCallback, RS_A(L"CmdFullscreenDesc"));
188+
maximized->excludes(fullscreen);
189+
190+
// Subcommands
176191
_buildNewTabParser();
177192
_buildSplitPaneParser();
178193
_buildFocusTabParser();
@@ -410,6 +425,10 @@ void AppCommandlineArgs::_resetStateToDefault()
410425
_focusTabIndex = -1;
411426
_focusNextTab = false;
412427
_focusPrevTab = false;
428+
429+
// DON'T clear _launchMode here! This will get called once for every
430+
// subcommand, so we don't want `wt -F new-tab ; split-pane` clearing out
431+
// the "global" fullscreen flag (-F).
413432
}
414433

415434
// Function Description:
@@ -604,3 +623,8 @@ void AppCommandlineArgs::ValidateStartupCommands()
604623
_startupActions.push_front(*newTabAction);
605624
}
606625
}
626+
627+
std::optional<winrt::TerminalApp::LaunchMode> AppCommandlineArgs::GetLaunchMode() const noexcept
628+
{
629+
return _launchMode;
630+
}

src/cascadia/TerminalApp/AppCommandlineArgs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class TerminalApp::AppCommandlineArgs final
3838
const std::string& GetExitMessage();
3939
bool ShouldExitEarly() const noexcept;
4040

41+
std::optional<winrt::TerminalApp::LaunchMode> GetLaunchMode() const noexcept;
42+
4143
private:
4244
static const std::wregex _commandDelimiterRegex;
4345

@@ -76,6 +78,8 @@ class TerminalApp::AppCommandlineArgs final
7678
int _focusTabIndex{ -1 };
7779
bool _focusNextTab{ false };
7880
bool _focusPrevTab{ false };
81+
82+
std::optional<winrt::TerminalApp::LaunchMode> _launchMode{ std::nullopt };
7983
// Are you adding more args here? Make sure to reset them in _resetStateToDefault
8084

8185
std::deque<winrt::TerminalApp::ActionAndArgs> _startupActions;

src/cascadia/TerminalApp/AppLogic.cpp

Lines changed: 108 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,17 @@ namespace winrt::TerminalApp::implementation
242242

243243
_root->SetSettings(_settings, false);
244244
_root->Loaded({ this, &AppLogic::_OnLoaded });
245+
_root->Initialized([this](auto&&, auto&&) {
246+
// GH#288 - When we finish initialization, if the user wanted us
247+
// launched _fullscreen_, toggle fullscreen mode. This will make sure
248+
// that the window size is _first_ set up as something sensible, so
249+
// leaving fullscreen returns to a reasonable size.
250+
const auto launchMode = this->GetLaunchMode();
251+
if (launchMode == LaunchMode::FullscreenMode)
252+
{
253+
_root->ToggleFullscreen();
254+
}
255+
});
245256
_root->Create();
246257

247258
_ApplyTheme(_settings->GlobalSettings().GetTheme());
@@ -529,7 +540,13 @@ namespace winrt::TerminalApp::implementation
529540
LoadSettings();
530541
}
531542

532-
return _settings->GlobalSettings().GetLaunchMode();
543+
// GH#4620/#5801 - If the user passed --maximized or --fullscreen on the
544+
// commandline, then use that to override the value from the settings.
545+
const auto valueFromSettings = _settings->GlobalSettings().GetLaunchMode();
546+
const auto valueFromCommandlineArgs = _appArgs.GetLaunchMode();
547+
return valueFromCommandlineArgs.has_value() ?
548+
valueFromCommandlineArgs.value() :
549+
valueFromSettings;
533550
}
534551

535552
// Method Description:
@@ -934,31 +951,108 @@ namespace winrt::TerminalApp::implementation
934951
}
935952
}
936953

937-
int32_t AppLogic::SetStartupCommandline(array_view<const winrt::hstring> actions)
954+
// Method Description:
955+
// - Sets the initial commandline to process on startup, and attempts to
956+
// parse it. Commands will be parsed into a list of ShortcutActions that
957+
// will be processed on TerminalPage::Create().
958+
// - This function will have no effective result after Create() is called.
959+
// - This function returns 0, unless a there was a non-zero result from
960+
// trying to parse one of the commands provided. In that case, no commands
961+
// after the failing command will be parsed, and the non-zero code
962+
// returned.
963+
// Arguments:
964+
// - args: an array of strings to process as a commandline. These args can contain spaces
965+
// Return Value:
966+
// - the result of the first command who's parsing returned a non-zero code,
967+
// or 0. (see AppLogic::_ParseArgs)
968+
int32_t AppLogic::SetStartupCommandline(array_view<const winrt::hstring> args)
938969
{
939-
if (_root)
970+
const auto result = _ParseArgs(args);
971+
if (result == 0)
940972
{
941-
return _root->SetStartupCommandline(actions);
973+
_appArgs.ValidateStartupCommands();
974+
_root->SetStartupActions(_appArgs.GetStartupActions());
942975
}
943-
return 0;
976+
977+
return result;
944978
}
945979

946-
winrt::hstring AppLogic::ParseCommandlineMessage()
980+
// Method Description:
981+
// - Attempts to parse an array of commandline args into a list of
982+
// commands to execute, and then parses these commands. As commands are
983+
// successfully parsed, they will generate ShortcutActions for us to be
984+
// able to execute. If we fail to parse any commands, we'll return the
985+
// error code from the failure to parse that command, and stop processing
986+
// additional commands.
987+
// Arguments:
988+
// - args: an array of strings to process as a commandline. These args can contain spaces
989+
// Return Value:
990+
// - 0 if the commandline was successfully parsed
991+
int AppLogic::_ParseArgs(winrt::array_view<const hstring>& args)
947992
{
948-
if (_root)
993+
auto commands = ::TerminalApp::AppCommandlineArgs::BuildCommands(args);
994+
995+
for (auto& cmdBlob : commands)
949996
{
950-
return _root->ParseCommandlineMessage();
997+
// On one hand, it seems like we should be able to have one
998+
// AppCommandlineArgs for parsing all of them, and collect the
999+
// results one at a time.
1000+
//
1001+
// On the other hand, re-using a CLI::App seems to leave state from
1002+
// previous parsings around, so we could get mysterious behavior
1003+
// where one command affects the values of the next.
1004+
//
1005+
// From https://cliutils.github.io/CLI11/book/chapters/options.html:
1006+
// > If that option is not given, CLI11 will not touch the initial
1007+
// > value. This allows you to set up defaults by simply setting
1008+
// > your value beforehand.
1009+
//
1010+
// So we pretty much need the to either manually reset the state
1011+
// each command, or build new ones.
1012+
const auto result = _appArgs.ParseCommand(cmdBlob);
1013+
1014+
// If this succeeded, result will be 0. Otherwise, the caller should
1015+
// exit(result), to exit the program.
1016+
if (result != 0)
1017+
{
1018+
return result;
1019+
}
9511020
}
952-
return { L"" };
1021+
1022+
// If all the args were successfully parsed, we'll have some commands
1023+
// built in _appArgs, which we'll use when the application starts up.
1024+
return 0;
9531025
}
9541026

1027+
// Method Description:
1028+
// - If there were any errors parsing the commandline that was used to
1029+
// initialize the terminal, this will return a string containing that
1030+
// message. If there were no errors, this message will be blank.
1031+
// - If the user requested help on any command (using --help), this will
1032+
// contain the help message.
1033+
// - If the user requested the version number (using --version), this will
1034+
// contain the version string.
1035+
// Arguments:
1036+
// - <none>
1037+
// Return Value:
1038+
// - the help text or error message for the provided commandline, if one
1039+
// exists, otherwise the empty string.
1040+
winrt::hstring AppLogic::ParseCommandlineMessage()
1041+
{
1042+
return winrt::to_hstring(_appArgs.GetExitMessage());
1043+
}
1044+
1045+
// Method Description:
1046+
// - Returns true if we should exit the application before even starting the
1047+
// window. We might want to do this if we're displaying an error message or
1048+
// the version string, or if we want to open the settings file.
1049+
// Arguments:
1050+
// - <none>
1051+
// Return Value:
1052+
// - true iff we should exit the application before even starting the window
9551053
bool AppLogic::ShouldExitEarly()
9561054
{
957-
if (_root)
958-
{
959-
return _root->ShouldExitEarly();
960-
}
961-
return false;
1055+
return _appArgs.ShouldExitEarly();
9621056
}
9631057

9641058
winrt::hstring AppLogic::ApplicationDisplayName() const

src/cascadia/TerminalApp/AppLogic.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ namespace winrt::TerminalApp::implementation
7676

7777
std::atomic<bool> _settingsReloadQueued{ false };
7878

79+
::TerminalApp::AppCommandlineArgs _appArgs;
80+
int _ParseArgs(winrt::array_view<const hstring>& args);
81+
7982
fire_and_forget _ShowDialog(const winrt::Windows::Foundation::IInspectable& sender, winrt::Windows::UI::Xaml::Controls::ContentDialog dialog);
8083
void _ShowLoadErrorsDialog(const winrt::hstring& titleKey, const winrt::hstring& contentKey, HRESULT settingsLoadedResult);
8184
void _ShowLoadWarningsDialog();

src/cascadia/TerminalApp/Resources/en-US/Resources.resw

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,12 @@
259259
<data name="CmdVersionDesc" xml:space="preserve">
260260
<value>Display the application version</value>
261261
</data>
262+
<data name="CmdMaximizedDesc" xml:space="preserve">
263+
<value>Launch the window maximized</value>
264+
</data>
265+
<data name="CmdFullscreenDesc" xml:space="preserve">
266+
<value>Launch the window in fullscreen mode</value>
267+
</data>
262268
<data name="NewTabSplitButton.[using:Windows.UI.Xaml.Automation]AutomationProperties.HelpText" xml:space="preserve">
263269
<value>Press the button to open a new terminal tab with your default profile. Open the flyout to select which profile you want to open.</value>
264270
</data>

0 commit comments

Comments
 (0)