Skip to content

Commit

Permalink
Merge pull request #1673 from contour-terminal/improvements/tabs
Browse files Browse the repository at this point in the history
Some Improvements to the tabs
  • Loading branch information
christianparpart authored Dec 25, 2024
2 parents 46f74e9 + 455d08b commit 96a01b6
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 21 deletions.
66 changes: 51 additions & 15 deletions src/contour/TerminalSessionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <QtQml/QQmlEngine>

#include <algorithm>
#include <filesystem>
#include <mutex>
#include <string>

using namespace std::string_literals;
Expand All @@ -27,13 +29,15 @@ TerminalSessionManager::TerminalSessionManager(ContourGuiApp& app): _app { app }
{
}

std::unique_ptr<vtpty::Pty> TerminalSessionManager::createPty()
std::unique_ptr<vtpty::Pty> TerminalSessionManager::createPty(std::optional<std::string> cwd)
{
auto const& profile = _app.config().profile(_app.profileName());
#if defined(VTPTY_LIBSSH2)
if (!profile->ssh.value().hostname.empty())
return make_unique<vtpty::SshSession>(profile->ssh.value());
#endif
if (cwd)
profile->shell.value().workingDirectory = std::filesystem::path(cwd.value());
return make_unique<vtpty::Process>(profile->shell.value(),
vtpty::createPty(profile->terminalSize.value(), nullopt),
profile->escapeSandbox.value());
Expand All @@ -43,7 +47,32 @@ TerminalSession* TerminalSessionManager::createSession()
{
// TODO: Remove dependency on app-knowledge and pass shell / terminal-size instead.
// The GuiApp *or* (Global)Config could be made a global to be accessable from within QML.
auto* session = new TerminalSession(createPty(), _app);
//

#if !defined(_WIN32)
auto ptyPath = [this]() -> std::optional<std::string> {
if (_activeSession)
{
auto& terminal = _activeSession->terminal();
if (auto const* ptyProcess = dynamic_cast<vtpty::Process const*>(&terminal.device()))
return ptyProcess->workingDirectory();
}
return std::nullopt;
}();
#else
std::optional<std::string> ptyPath = std::nullopt;
if (_activeSession)
{
auto& terminal = _activeSession->terminal();
{
auto _l = std::scoped_lock { terminal };
ptyPath = terminal.currentWorkingDirectory();
}
}
#endif

auto* session = new TerminalSession(createPty(ptyPath), _app);
managerLog()("CREATE SESSION, new session: {}", (void*) session);

_sessions.push_back(session);

Expand All @@ -57,6 +86,7 @@ TerminalSession* TerminalSessionManager::createSession()

// we can close application right after session has been created
_lastTabChange = std::chrono::steady_clock::now() - std::chrono::seconds(1);
_activeSession = session;
return session;
}

Expand All @@ -72,21 +102,19 @@ void TerminalSessionManager::setSession(size_t index)
if (index < _sessions.size())
_activeSession = _sessions[index];
else
_activeSession = createSession();
createSession();

if (oldSession == _activeSession)
return;

if (oldSession)
oldSession->detachDisplay(*display);

Require(display != nullptr);
auto const pixels = display->pixelSize();
auto const totalPageSize = display->calculatePageSize() + _activeSession->terminal().statusLineHeight();

display->setSession(_activeSession);
_activeSession->terminal().resizeScreen(totalPageSize, pixels);
updateStatusLine();

_lastTabChange = std::chrono::steady_clock::now();
}

Expand All @@ -106,16 +134,9 @@ void TerminalSessionManager::switchToTabLeft()
{
setSession(currentSessionIndex - 1);
}
}

void TerminalSessionManager::switchToTab(int position)
{
managerLog()(std::format(
"switchToTab from {} to {} (out of {})", getCurrentSessionIndex(), position - 1, _sessions.size()));

if (1 <= position && position <= static_cast<int>(_sessions.size()))
else // wrap
{
setSession(position - 1);
setSession(_sessions.size() - 1);
}
}

Expand All @@ -129,6 +150,21 @@ void TerminalSessionManager::switchToTabRight()
{
setSession(currentSessionIndex + 1);
}
else // wrap
{
setSession(0);
}
}

void TerminalSessionManager::switchToTab(int position)
{
managerLog()(std::format(
"switchToTab from {} to {} (out of {})", getCurrentSessionIndex(), position - 1, _sessions.size()));

if (1 <= position && position <= static_cast<int>(_sessions.size()))
{
setSession(position - 1);
}
}

void TerminalSessionManager::closeTab()
Expand Down
4 changes: 2 additions & 2 deletions src/contour/TerminalSessionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class TerminalSessionManager: public QAbstractListModel
TerminalSession* getSession() { return _sessions[0]; }

private:
std::unique_ptr<vtpty::Pty> createPty();
std::unique_ptr<vtpty::Pty> createPty(std::optional<std::string> cwd);
[[nodiscard]] auto getCurrentSessionIndex() const
{
return [](auto const& sessions, auto const& activeSession) {
Expand Down Expand Up @@ -87,7 +87,7 @@ class TerminalSessionManager: public QAbstractListModel
TerminalSession* _activeSession = nullptr;
std::vector<TerminalSession*> _sessions;
std::chrono::time_point<std::chrono::steady_clock> _lastTabChange;
std::chrono::milliseconds _timeBetweenTabSwitches { 100 };
std::chrono::milliseconds _timeBetweenTabSwitches { 10 };
};

} // namespace contour
Expand Down
10 changes: 7 additions & 3 deletions src/contour/display/TerminalDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ void TerminalDisplay::setSession(TerminalSession* newSession)
window()->setFlag(Qt::FramelessWindowHint, !profile().showTitleBar.value());

if (!_renderer)
{

_renderer = make_unique<vtrasterizer::Renderer>(
_session->profile().terminalSize.value(),
sanitizeFontDescription(profile().fonts.value(), fontDPI()),
Expand All @@ -323,9 +325,11 @@ void TerminalDisplay::setSession(TerminalSession* newSession)
// TODO: , WindowMargin(windowMargin_.left, windowMargin_.bottom);
);

applyFontDPI();
updateImplicitSize();
updateMinimumSize();
// setup once with the renderer creation
applyFontDPI();
updateImplicitSize();
updateMinimumSize();
}

_session->attachDisplay(*this); // NB: Requires Renderer to be instanciated to retrieve grid metrics.

Expand Down
2 changes: 1 addition & 1 deletion src/vtrasterizer/TextClusterGrouper_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <libunicode/convert.h>

#include <range/v3/to_container.hpp>
#include <range/v3/range/conversion.hpp>
#include <range/v3/view/join.hpp>
#include <range/v3/view/transform.hpp>

Expand Down

0 comments on commit 96a01b6

Please sign in to comment.