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

Switch to TerminalSessionManager everywhere and fix small bug #1698

Merged
merged 1 commit into from
Jan 6, 2025
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
16 changes: 9 additions & 7 deletions src/contour/TerminalSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,8 @@ void TerminalSession::sendKeyEvent(Key key, Modifiers modifiers, KeyboardEventTy
if (auto const* actions =
config::apply(_config.inputMappings.value().keyMappings, key, modifiers, matchModeFlags()))
{
executeAllActions(*actions);
if (eventType == KeyboardEventType::Press)
executeAllActions(*actions);
return;
}
}
Expand Down Expand Up @@ -868,7 +869,8 @@ void TerminalSession::sendCharEvent(
config::apply(_config.inputMappings.value().charMappings, value, modifiers, matchModeFlags());
actions && !_terminal.inputHandler().isEditingSearch())
{
executeAllActions(*actions);
if (eventType == KeyboardEventType::Press)
executeAllActions(*actions);
return;
}
}
Expand Down Expand Up @@ -1440,7 +1442,7 @@ bool TerminalSession::operator()(actions::CreateNewTab)

bool TerminalSession::operator()(actions::CloseTab)
{
emit closeTab();
_manager->closeTab();
return true;
}

Expand All @@ -1464,25 +1466,25 @@ bool TerminalSession::operator()(actions::MoveTabToRight)

bool TerminalSession::operator()(actions::SwitchToTab const& event)
{
emit switchToTab(event.position);
_manager->switchToTab(event.position);
return true;
}

bool TerminalSession::operator()(actions::SwitchToPreviousTab)
{
emit switchToPreviousTab();
_manager->switchToPreviousTab();
return true;
}

bool TerminalSession::operator()(actions::SwitchToTabLeft)
{
emit switchToTabLeft();
_manager->switchToTabLeft();
return true;
}

bool TerminalSession::operator()(actions::SwitchToTabRight)
{
emit switchToTabRight();
_manager->switchToTabRight();
return true;
}

Expand Down
22 changes: 2 additions & 20 deletions src/contour/TerminalSessionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ std::unique_ptr<vtpty::Pty> TerminalSessionManager::createPty(std::optional<std:
profile->escapeSandbox.value());
}

TerminalSession* TerminalSessionManager::createSession()
{
return activateSession(createSessionInBackground());
}

TerminalSession* TerminalSessionManager::createSessionInBackground()
{
// TODO: Remove dependency on app-knowledge and pass shell / terminal-size instead.
Expand Down Expand Up @@ -101,9 +96,6 @@ void TerminalSessionManager::setSession(size_t index)
Require(index <= _sessions.size());
managerLog()(std::format("SET SESSION: index: {}, _sessions.size(): {}", index, _sessions.size()));

if (!isAllowedToChangeTabs())
return;

if (index < _sessions.size())
activateSession(_sessions[index]);
else
Expand All @@ -126,7 +118,6 @@ TerminalSession* TerminalSessionManager::activateSession(TerminalSession* sessio

_previousActiveSession = _activeSession;
_activeSession = session;
_lastTabChange = std::chrono::steady_clock::now();
updateStatusLine();

if (display)
Expand All @@ -150,9 +141,9 @@ TerminalSession* TerminalSessionManager::activateSession(TerminalSession* sessio
return session;
}

void TerminalSessionManager::addSession()
TerminalSession* TerminalSessionManager::createSession()
{
activateSession(createSessionInBackground(), true /*force resize on before display-attach*/);
return activateSession(createSessionInBackground(), true /*force resize on before display-attach*/);
}

void TerminalSessionManager::switchToPreviousTab()
Expand All @@ -161,9 +152,6 @@ void TerminalSessionManager::switchToPreviousTab()
getSessionIndexOf(_activeSession).value_or(-1),
getSessionIndexOf(_previousActiveSession).value_or(-1));

if (!isAllowedToChangeTabs())
return;

activateSession(_previousActiveSession);
}

Expand Down Expand Up @@ -207,9 +195,6 @@ void TerminalSessionManager::switchToTab(int position)
position - 1,
_sessions.size());

if (!isAllowedToChangeTabs())
return;

if (1 <= position && position <= static_cast<int>(_sessions.size()))
activateSession(_sessions[position - 1]);
}
Expand Down Expand Up @@ -272,9 +257,6 @@ void TerminalSessionManager::removeSession(TerminalSession& thatSession)
{
managerLog()("REMOVE SESSION: session: {}, _sessions.size(): {}", (void*) &thatSession, _sessions.size());

if (!isAllowedToChangeTabs())
return;

if (&thatSession == _activeSession && _previousActiveSession)
activateSession(_previousActiveSession);

Expand Down
32 changes: 8 additions & 24 deletions src/contour/TerminalSessionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <QtCore/QAbstractListModel>
#include <QtQml/QQmlEngine>

#include <chrono>
#include <vector>

namespace contour
Expand All @@ -31,16 +30,15 @@ class TerminalSessionManager: public QAbstractListModel
contour::TerminalSession* activateSession(TerminalSession* session, bool isNewSession = false);

Q_INVOKABLE contour::TerminalSession* createSession();
Q_INVOKABLE void addSession();

Q_INVOKABLE void switchToPreviousTab();
Q_INVOKABLE void switchToTabLeft();
Q_INVOKABLE void switchToTabRight();
Q_INVOKABLE void switchToTab(int position);
Q_INVOKABLE void closeTab();
Q_INVOKABLE void moveTabTo(int position);
Q_INVOKABLE void moveTabToLeft(TerminalSession* session);
Q_INVOKABLE void moveTabToRight(TerminalSession* session);
void switchToPreviousTab();
void switchToTabLeft();
void switchToTabRight();
void switchToTab(int position);
void closeTab();
void moveTabTo(int position);
void moveTabToLeft(TerminalSession* session);
void moveTabToRight(TerminalSession* session);

void setSession(size_t index);

Expand Down Expand Up @@ -82,25 +80,11 @@ class TerminalSessionManager: public QAbstractListModel
});
}

[[nodiscard]] bool isAllowedToChangeTabs() const
{
// QML for some reason sends multiple signals requests in a row, so we need to ignore them.
auto now = std::chrono::steady_clock::now();
if (abs(now - _lastTabChange) < _timeBetweenTabSwitches)
{
managerLog()("Ignoring change request due to too frequent change requests.");
return false;
}
return true;
}

ContourGuiApp& _app;
std::chrono::seconds _earlyExitThreshold;
TerminalSession* _activeSession = nullptr;
TerminalSession* _previousActiveSession = nullptr;
std::vector<TerminalSession*> _sessions;
std::chrono::time_point<std::chrono::steady_clock> _lastTabChange;
std::chrono::milliseconds _timeBetweenTabSwitches { 50 };
};

} // namespace contour
Expand Down
19 changes: 0 additions & 19 deletions src/contour/ui.template/Terminal.qml.in
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,6 @@ ContourTerminal

session: terminalSessions.createSession()

signal switchToTab(int index)
signal switchToPreviousTab
signal switchToTabLeft
signal switchToTabRight
signal closeTab

onSwitchToPreviousTab: terminalSessions.switchToPreviousTab()
onSwitchToTabLeft: terminalSessions.switchToTabLeft()
onSwitchToTabRight: terminalSessions.switchToTabRight()
onCloseTab: terminalSessions.closeTab()
onSwitchToTab: (i) => terminalSessions.switchToTab(i)

Rectangle {
id : backgroundColor
anchors.centerIn: parent
Expand Down Expand Up @@ -288,12 +276,5 @@ ContourTerminal
vt.requestPermissionForPasteLargeFile.connect(requestLargeFilePaste.open);
forceActiveFocus();

// TAB handling
vt.createNewTab.connect(onCreateNewTab);
vt.closeTab.connect(closeTab);
vt.switchToTab.connect(switchToTab);
vt.switchToPreviousTab.connect(switchToPreviousTab);
vt.switchToTabLeft.connect(switchToTabLeft);
vt.switchToTabRight.connect(switchToTabRight);
}
}
Loading