Skip to content

Commit

Permalink
Merge pull request #1628 from contour-terminal/maintenance/cleanups
Browse files Browse the repository at this point in the history
simplify, cleanup, and improve window size/resize handling
  • Loading branch information
christianparpart authored Oct 7, 2024
2 parents a502b10 + ad84c20 commit 6da571c
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 147 deletions.
7 changes: 4 additions & 3 deletions metainfo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,17 @@
<release version="0.5.2" urgency="medium" type="development">
<description>
<ul>
<li>Add terminal tabs (#90)</li>
<li>Add binding to exit normal mode with `Esc` (#1604)</li>
<li>Add config option to switch into insert mode after yank (#1604)</li>
<li>Improves window size/resize handling on HiDPI monitor settings (#1628)</li>
</ul>
</description>
</release>

<release version="0.5.1" urgency="medium" date="2024-09-30">
<description>
<ul>
<li>Add terminal tabs (#90)</li>
<li>Add binding to exit normal mode with `Esc` (#1604)</li>
<li>Add config option to switch into insert mode after yank (#1604)</li>
<li>Fixes vi-mode motions like `viW`, `yiW`, `oiW` as well as `B` and `W`</li>
<li>Fixes rendered backend loading from config</li>
<li>Fixes mouse wheel scrolling on High-DPI screens feeling too slow</li>
Expand Down
7 changes: 7 additions & 0 deletions src/contour/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,13 @@ struct WindowMargins
VerticalMargin vertical { 0 };
};

constexpr WindowMargins operator*(WindowMargins const& margin, double factor) noexcept
{
return WindowMargins {
.horizontal = HorizontalMargin { static_cast<unsigned>(*margin.horizontal * factor) },
};
}

template <typename T, documentation::StringLiteral doc>
struct ConfigEntry
{
Expand Down
43 changes: 19 additions & 24 deletions src/contour/TerminalSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,20 +235,15 @@ void TerminalSession::detachDisplay(display::TerminalDisplay& display)

void TerminalSession::attachDisplay(display::TerminalDisplay& newDisplay)
{
sessionLog()("Attaching display.");
// newDisplay.setSession(*this); // NB: we're being called by newDisplay!
_display = &newDisplay;
sessionLog()("Attaching session to display {}x{}.", newDisplay.width(), newDisplay.height());

setContentScale(newDisplay.contentScale());
// We're being called by newDisplay!
_display = &newDisplay;

{
// NB: Inform connected TTY and local Screen instance about initial cell pixel size.
auto const pixels = _display->cellSize() * _terminal.pageSize();
// auto const pixels =
// ImageSize { _display->cellSize().width * boxed_cast<Width>(_terminal.pageSize().columns),
// _display->cellSize().height * boxed_cast<Height>(_terminal.pageSize().lines) };
auto const l = scoped_lock { _terminal };
_terminal.resizeScreen(_terminal.pageSize(), pixels);
_terminal.resizeScreen(_terminal.pageSize(), _display->pixelSize());
_terminal.setRefreshRate(_display->refreshRate());
}

Expand All @@ -268,10 +263,10 @@ void TerminalSession::scheduleRedraw()

void TerminalSession::start()
{
sessionLog()("Starting terminal session.");
// ensure that we start only once
if (!_screenUpdateThread)
{
sessionLog()("Starting terminal session.");
_terminal.device().start();
_screenUpdateThread = make_unique<std::thread>(bind(&TerminalSession::mainLoop, this));
_exitWatcherThread->start(QThread::LowPriority);
Expand Down Expand Up @@ -548,7 +543,7 @@ void TerminalSession::copyToClipboard(std::string_view data)
if (!_display)
return;

_display->post([this, data = string(data)]() { _display->copyToClipboard(data); });
_display->post([data = string(data)]() { display::TerminalDisplay::copyToClipboard(data); });
}

void TerminalSession::openDocument(std::string_view fileOrUrl)
Expand Down Expand Up @@ -705,10 +700,10 @@ void TerminalSession::requestWindowResize(LineCount lines, ColumnCount columns)
_display->post([this, lines, columns]() { _display->resizeWindow(lines, columns); });
}

void TerminalSession::adaptToWidgetSize()
void TerminalSession::resizeTerminalToDisplaySize()
{
if (_display)
_display->post([this]() { _display->adaptToWidgetSize(); });
_display->post([this]() { _display->resizeTerminalToDisplaySize(); });
}

void TerminalSession::requestWindowResize(Width width, Height height)
Expand Down Expand Up @@ -803,17 +798,18 @@ void TerminalSession::sendCharEvent(
modifiers,
crispy::escape(unicode::convert_to<char>(value)));

assert(_display != nullptr);

if (_terminatedAndWaitingForKeyPress && eventType == KeyboardEventType::Press)
if (_display)
{
sessionLog()("Terminated and waiting for key press. Closing display.");
_display->closeDisplay();
return;
}
if (_terminatedAndWaitingForKeyPress && eventType == KeyboardEventType::Press)
{
sessionLog()("Terminated and waiting for key press. Closing display.");
_display->closeDisplay();
return;
}

if (_profile.mouseHideWhileTyping.value())
_display->setMouseCursorShape(MouseCursorShape::Hidden);
if (_profile.mouseHideWhileTyping.value())
_display->setMouseCursorShape(MouseCursorShape::Hidden);
}

if (eventType != KeyboardEventType::Release)
{
Expand Down Expand Up @@ -1498,7 +1494,6 @@ void TerminalSession::activateProfile(string const& newProfileName)
_profileName = newProfileName;
_profile = *newProfile;
configureTerminal();
configureDisplay();
}

void TerminalSession::configureTerminal()
Expand Down Expand Up @@ -1571,7 +1566,7 @@ void TerminalSession::configureDisplay()

_terminal.setRefreshRate(_display->refreshRate());
_display->setFonts(_profile.fonts.value());
adaptToWidgetSize();
resizeTerminalToDisplaySize();

_display->setHyperlinkDecoration(_profile.hyperlinkDecorationNormal.value(),
_profile.hyperlinkDecorationHover.value());
Expand Down
6 changes: 1 addition & 5 deletions src/contour/TerminalSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,6 @@ class TerminalSession: public QAbstractItemModel, public vtbackend::Terminal::Ev
config::Config const& config() const noexcept { return _config; }
config::TerminalProfile const& profile() const noexcept { return _profile; }

double contentScale() const noexcept { return _contentScale; }
void setContentScale(double value) noexcept { _contentScale = value; }

vtpty::Pty& pty() noexcept { return _terminal.device(); }
vtbackend::Terminal& terminal() noexcept { return _terminal; }
vtbackend::Terminal const& terminal() const noexcept { return _terminal; }
Expand All @@ -245,7 +242,7 @@ class TerminalSession: public QAbstractItemModel, public vtbackend::Terminal::Ev
Q_INVOKABLE void applyPendingFontChange(bool allow, bool remember);
Q_INVOKABLE void executePendingBufferCapture(bool allow, bool remember);
Q_INVOKABLE void executeShowHostWritableStatusLine(bool allow, bool remember);
Q_INVOKABLE void adaptToWidgetSize();
Q_INVOKABLE void resizeTerminalToDisplaySize();

void updateColorPreference(vtbackend::ColorPreference preference);

Expand Down Expand Up @@ -438,7 +435,6 @@ class TerminalSession: public QAbstractItemModel, public vtbackend::Terminal::Ev
config::Config _config;
std::string _profileName;
config::TerminalProfile _profile;
double _contentScale = 1.0;
ContourGuiApp& _app;
vtbackend::ColorPreference _currentColorPreference;

Expand Down
24 changes: 13 additions & 11 deletions src/contour/TerminalSessionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,32 +53,34 @@ TerminalSession* TerminalSessionManager::createSession()
// sessions. This will work around it, by explicitly claiming ownership of the object.
QQmlEngine::setObjectOwnership(session, QQmlEngine::CppOwnership);

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

void TerminalSessionManager::setSession(size_t index)
{

Require(index <= _sessions.size());
managerLog()(std::format("SET SESSION: index: {}, _sessions.size(): {}", index, _sessions.size()));
if (!isAllowedToChangeTabs())
return;

auto const pixels = display->pixelSize();
auto const totalPageSize = display->calculatePageSize() + _activeSession->terminal().statusLineHeight();
auto* oldSession = _activeSession;

_activeSession->detachDisplay(*display);
if (index < _sessions.size())
{
_activeSession = _sessions[index];
}
else
{
createSession();
_activeSession->terminal().resizeScreen(totalPageSize, pixels);
}
_activeSession = 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);
Expand Down
31 changes: 17 additions & 14 deletions src/contour/TerminalSessionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,23 @@ class TerminalSessionManager: public QAbstractListModel

void updateStatusLine()
{
const auto currentSessionIndex = getCurrentSessionIndex();
_activeSession->terminal().setGuiTabInfoForStatusLine([&]() {
std::string tabInfo;
for (size_t i = 0; i < _sessions.size(); ++i)
{
if (std::cmp_equal(i, currentSessionIndex))
tabInfo += "[";
tabInfo += std::to_string(i + 1);
if (std::cmp_equal(i, currentSessionIndex))
tabInfo += "]";
tabInfo += " ";
}
return tabInfo;
}());
if (!_activeSession)
return;

_activeSession->terminal().setGuiTabInfoForStatusLine(
[this, currentSessionIndex = getCurrentSessionIndex()]() {
std::string tabInfo;
for (size_t i = 0; i < _sessions.size(); ++i)
{
if (std::cmp_equal(i, currentSessionIndex))
tabInfo += "[";
tabInfo += std::to_string(i + 1);
if (std::cmp_equal(i, currentSessionIndex))
tabInfo += "]";
tabInfo += " ";
}
return tabInfo;
}());
}

bool isAllowedToChangeTabs()
Expand Down
Loading

0 comments on commit 6da571c

Please sign in to comment.