Skip to content

Commit

Permalink
Add options to enable and disable read only mode (#14995)
Browse files Browse the repository at this point in the history
## Summary of the Pull Request
PR adds functionality to enable or disable readOnly mode within panes.
This functionality is different to toggling as if you call the same
functionality twice, it will not toggle between states.

## References and Relevant Issues
- Closes #14415
- Documentation MicrosoftDocs/terminal#645

## Validation Steps Performed
- Checked readOnly is enabled when command triggered
- Checked readOnly is enabled when command triggered while read only
already enabled
- Checked readOnly is disabled when command triggered while read only is
enabled
- Checked readOnly stays disabled when command triggered while read only
is disabled
- Checked above with multiple tabs and split panes

## PR Checklist
- [ ] Closes #14415
- [X] Tests added/passed
- [x] Documentation updated
- If checked, please file a pull request on [our docs
repo](https://github.com/MicrosoftDocs/terminal) and link it here:
MicrosoftDocs/terminal#645
- [X] Schema updated (if necessary)

---------

Co-authored-by: Mike Griese <migrie@microsoft.com>
(cherry picked from commit 2acdc9d)
Service-Card-Id: 89002012
Service-Version: 1.17
  • Loading branch information
2 people authored and DHowett committed Apr 25, 2023
1 parent d48870c commit a1882ce
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/cascadia/TerminalApp/AppActionHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,28 @@ namespace winrt::TerminalApp::implementation
args.Handled(true);
}

void TerminalPage::_HandleEnablePaneReadOnly(const IInspectable& /*sender*/,
const ActionEventArgs& args)
{
if (const auto activeTab{ _GetFocusedTabImpl() })
{
activeTab->SetPaneReadOnly(true);
}

args.Handled(true);
}

void TerminalPage::_HandleDisablePaneReadOnly(const IInspectable& /*sender*/,
const ActionEventArgs& args)
{
if (const auto activeTab{ _GetFocusedTabImpl() })
{
activeTab->SetPaneReadOnly(false);
}

args.Handled(true);
}

void TerminalPage::_HandleScrollUpPage(const IInspectable& /*sender*/,
const ActionEventArgs& args)
{
Expand Down
36 changes: 34 additions & 2 deletions src/cascadia/TerminalApp/TerminalTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1563,14 +1563,14 @@ namespace winrt::TerminalApp::implementation
{
auto hasReadOnly = false;
auto allReadOnly = true;
_activePane->WalkTree([&](auto p) {
_activePane->WalkTree([&](const auto& p) {
if (const auto& control{ p->GetTerminalControl() })
{
hasReadOnly |= control.ReadOnly();
allReadOnly &= control.ReadOnly();
}
});
_activePane->WalkTree([&](auto p) {
_activePane->WalkTree([&](const auto& p) {
if (const auto& control{ p->GetTerminalControl() })
{
// If all controls have the same read only state then just toggle
Expand All @@ -1587,6 +1587,38 @@ namespace winrt::TerminalApp::implementation
});
}

// Method Description:
// - Set read-only mode on the active pane
// - If a parent pane is selected, this will ensure that all children have
// the same read-only status.
void TerminalTab::SetPaneReadOnly(const bool readOnlyState)
{
auto hasReadOnly = false;
auto allReadOnly = true;
_activePane->WalkTree([&](const auto& p) {
if (const auto& control{ p->GetTerminalControl() })
{
hasReadOnly |= control.ReadOnly();
allReadOnly &= control.ReadOnly();
}
});
_activePane->WalkTree([&](const auto& p) {
if (const auto& control{ p->GetTerminalControl() })
{
// If all controls have the same read only state then just disable
if (allReadOnly || !hasReadOnly)
{
control.SetReadOnly(readOnlyState);
}
// otherwise set to all read only.
else if (!control.ReadOnly())
{
control.SetReadOnly(readOnlyState);
}
}
});
}

// Method Description:
// - Calculates if the tab is read-only.
// The tab is considered read-only if one of the panes is read-only.
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalApp/TerminalTab.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ namespace winrt::TerminalApp::implementation
int GetLeafPaneCount() const noexcept;

void TogglePaneReadOnly();
void SetPaneReadOnly(const bool readOnlyState);

std::shared_ptr<Pane> GetActivePane() const;
winrt::TerminalApp::TaskbarState GetCombinedTaskbarState() const;

Expand Down
5 changes: 5 additions & 0 deletions src/cascadia/TerminalControl/ControlCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1667,6 +1667,11 @@ namespace winrt::Microsoft::Terminal::Control::implementation
_isReadOnly = !_isReadOnly;
}

void ControlCore::SetReadOnlyMode(const bool readOnlyState)
{
_isReadOnly = readOnlyState;
}

void ControlCore::_raiseReadOnlyWarning()
{
auto noticeArgs = winrt::make<NoticeEventArgs>(NoticeLevel::Info, RS_(L"TermControlReadOnly"));
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalControl/ControlCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation

bool IsInReadOnlyMode() const;
void ToggleReadOnlyMode();
void SetReadOnlyMode(const bool readOnlyState);

hstring ReadEntireBuffer() const;

Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalControl/ControlCore.idl
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ namespace Microsoft.Terminal.Control

void ToggleShaderEffects();
void ToggleReadOnlyMode();
void SetReadOnlyMode(Boolean readOnlyState);

Microsoft.Terminal.Core.Point CursorPosition { get; };
void ResumeRendering();
Expand Down
8 changes: 8 additions & 0 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2869,6 +2869,14 @@ namespace winrt::Microsoft::Terminal::Control::implementation
_ReadOnlyChangedHandlers(*this, winrt::box_value(_core.IsInReadOnlyMode()));
}

// Method Description:
// - Sets the read-only flag, raises event describing the value change
void TermControl::SetReadOnly(const bool readOnlyState)
{
_core.SetReadOnlyMode(readOnlyState);
_ReadOnlyChangedHandlers(*this, winrt::box_value(_core.IsInReadOnlyMode()));
}

// Method Description:
// - Handle a mouse exited event, specifically clearing last hovered cell
// and removing selection from hyper link if exists
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalControl/TermControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation

bool ReadOnly() const noexcept;
void ToggleReadOnly();
void SetReadOnly(const bool readOnlyState);

static Control::MouseButtonState GetPressedMouseButtons(const winrt::Windows::UI::Input::PointerPoint point);
static unsigned int GetPointerUpdateKind(const winrt::Windows::UI::Input::PointerPoint point);
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalControl/TermControl.idl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ namespace Microsoft.Terminal.Control

Boolean ReadOnly { get; };
void ToggleReadOnly();
void SetReadOnly(Boolean readOnlyState);

String ReadEntireBuffer();

Expand Down
4 changes: 4 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionAndArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ static constexpr std::string_view MoveTabKey{ "moveTab" };
static constexpr std::string_view BreakIntoDebuggerKey{ "breakIntoDebugger" };
static constexpr std::string_view FindMatchKey{ "findMatch" };
static constexpr std::string_view TogglePaneReadOnlyKey{ "toggleReadOnlyMode" };
static constexpr std::string_view EnablePaneReadOnlyKey{ "enableReadOnlyMode" };
static constexpr std::string_view DisablePaneReadOnlyKey{ "disableReadOnlyMode" };
static constexpr std::string_view NewWindowKey{ "newWindow" };
static constexpr std::string_view IdentifyWindowKey{ "identifyWindow" };
static constexpr std::string_view IdentifyWindowsKey{ "identifyWindows" };
Expand Down Expand Up @@ -387,6 +389,8 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{ ShortcutAction::BreakIntoDebugger, RS_(L"BreakIntoDebuggerCommandKey") },
{ ShortcutAction::FindMatch, L"" }, // Intentionally omitted, must be generated by GenerateName
{ ShortcutAction::TogglePaneReadOnly, RS_(L"TogglePaneReadOnlyCommandKey") },
{ ShortcutAction::EnablePaneReadOnly, RS_(L"EnablePaneReadOnlyCommandKey") },
{ ShortcutAction::DisablePaneReadOnly, RS_(L"DisablePaneReadOnlyCommandKey") },
{ ShortcutAction::NewWindow, RS_(L"NewWindowCommandKey") },
{ ShortcutAction::IdentifyWindow, RS_(L"IdentifyWindowCommandKey") },
{ ShortcutAction::IdentifyWindows, RS_(L"IdentifyWindowsCommandKey") },
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalSettingsModel/AllShortcutActions.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
ON_ALL_ACTIONS(MoveTab) \
ON_ALL_ACTIONS(BreakIntoDebugger) \
ON_ALL_ACTIONS(TogglePaneReadOnly) \
ON_ALL_ACTIONS(EnablePaneReadOnly) \
ON_ALL_ACTIONS(DisablePaneReadOnly) \
ON_ALL_ACTIONS(FindMatch) \
ON_ALL_ACTIONS(NewWindow) \
ON_ALL_ACTIONS(IdentifyWindow) \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,12 @@
<data name="TogglePaneReadOnlyCommandKey" xml:space="preserve">
<value>Toggle pane read-only mode</value>
</data>
<data name="EnablePaneReadOnlyCommandKey" xml:space="preserve">
<value>Enable pane read-only mode</value>
</data>
<data name="DisablePaneReadOnlyCommandKey" xml:space="preserve">
<value>Disable pane read-only mode</value>
</data>
<data name="ToggleShaderEffectsCommandKey" xml:space="preserve">
<value>Toggle terminal visual effects</value>
</data>
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalSettingsModel/defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,8 @@
{ "command": "togglePaneZoom" },
{ "command": "toggleSplitOrientation" },
{ "command": "toggleReadOnlyMode" },
{ "command": "enableReadOnlyMode" },
{ "command": "disableReadOnlyMode" },
{ "command": { "action": "movePane", "index": 0 } },
{ "command": { "action": "movePane", "index": 1 } },
{ "command": { "action": "movePane", "index": 2 } },
Expand Down

0 comments on commit a1882ce

Please sign in to comment.