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 options to enable and disable read only mode #14995

Merged
merged 14 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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);
}
}
});
}

Comment on lines +1594 to +1621
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is all of this logic necessary? Why wouldn't this just be a

WalkTree([&](const auto& p){ 
    if (const auto& control{ p->GetTerminalControl() })
    {
        control.SetReadOnly(readOnlyState);
    }
});

Maybe I just haven't had enough coffee yet

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ill push my changes for this, I dont see any issue when ive tested it.

// 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 @@ -87,6 +87,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 @@ -1671,6 +1671,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 @@ -2925,6 +2925,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 @@ -89,6 +89,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 @@ -472,6 +472,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 @@ -408,6 +408,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