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

Make some actions repeatable #1706

Merged
merged 1 commit into from
Jan 22, 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
4 changes: 4 additions & 0 deletions src/contour/Actions.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#pragma once

#include <crispy/assert.h>
#include <crispy/utils.h>

#include <format>
#include <optional>
Expand Down Expand Up @@ -151,6 +152,9 @@ using Action = std::variant<CancelSelection,
SwitchToTabLeft,
SwitchToTabRight>;

template <typename T>
concept NonRepeatableActionConcept = crispy::one_of<T, CreateNewTab, CloseTab>;
Yaraslaut marked this conversation as resolved.
Show resolved Hide resolved

std::optional<Action> fromString(std::string const& name);

namespace documentation
Expand Down
28 changes: 24 additions & 4 deletions src/contour/TerminalSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,28 @@ void TerminalSession::onScrollOffsetChanged(vtbackend::ScrollOffset value)
}
// }}}
// {{{ Input Events

void handleAction(auto const& actions, auto eventType, auto callback)
{
if (eventType == KeyboardEventType::Press)
callback(*actions);
else if (eventType == KeyboardEventType::Repeat)
{
// filter out actions that are not repeatable
std::vector<actions::Action> tmpActions;
auto set = crispy::overloaded {
[&]([[maybe_unused]] actions::NonRepeatableActionConcept auto const& action) {},
[&](auto const& action) { tmpActions.emplace_back(action); },
};

for (auto const& action: *actions)
{
std::visit(set, action);
}
callback(tmpActions);
}
}

void TerminalSession::sendKeyEvent(Key key, Modifiers modifiers, KeyboardEventType eventType, Timestamp now)
{
inputLog()("Key {} event received: {} {}", eventType, modifiers, key);
Expand All @@ -833,8 +855,7 @@ void TerminalSession::sendKeyEvent(Key key, Modifiers modifiers, KeyboardEventTy
if (auto const* actions =
config::apply(_config.inputMappings.value().keyMappings, key, modifiers, matchModeFlags()))
{
if (eventType == KeyboardEventType::Press)
executeAllActions(*actions);
handleAction(actions, eventType, [&](auto const& actions) { executeAllActions(actions); });
return;
}
}
Expand Down Expand Up @@ -869,8 +890,7 @@ void TerminalSession::sendCharEvent(
config::apply(_config.inputMappings.value().charMappings, value, modifiers, matchModeFlags());
actions && !_terminal.inputHandler().isEditingSearch())
{
if (eventType == KeyboardEventType::Press)
executeAllActions(*actions);
handleAction(actions, eventType, [&](auto const& actions) { executeAllActions(actions); });
return;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/crispy/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -560,4 +560,7 @@ struct overloaded: Ts...
template <class... Ts>
overloaded(Ts...) -> overloaded<Ts...>;

template <typename T, typename... Ts>
concept one_of = (std::same_as<T, Ts> || ...);

} // namespace crispy
Loading