Skip to content

Commit

Permalink
Fixed bug when clicking on scrolled Qt tabs.
Browse files Browse the repository at this point in the history
The way Qt emits signals and handles clicked tabs can result in a double-emission of "tab_clicked",
resulting in the wrong buffer being shown.
Also ensure mouse modifiers are passed for all emissions of "tab_clicked" on GTK.
  • Loading branch information
orbitalquark committed Dec 29, 2022
1 parent 43a1860 commit e44373b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/textadept_gtk.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static bool window_keypress(GtkWidget *_, GdkEventKey *event, void *__) {
// Generates a 'tab_clicked' event.
static void tab_changed(GtkNotebook *_, GtkWidget *__, int tab_num, void *___) {
current_tab = tab_num;
if (!tab_sync) emit("tab_clicked", LUA_TNUMBER, tab_num + 1, LUA_TNUMBER, 1, -1);
if (!tab_sync) emit("tab_clicked", LUA_TNUMBER, tab_num + 1, LUA_TNUMBER, 1, LUA_TNUMBER, 0, -1);
}

// Signal for reordering tabs.
Expand Down
21 changes: 15 additions & 6 deletions src/textadept_qt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -650,17 +650,26 @@ class FindKeypressHandler : public QObject {
Textadept::Textadept(QWidget *parent) : QMainWindow{parent}, ui{new Ui::Textadept} {
ui->setupUi(this);

connect(ui->tabbar, &QTabBar::tabBarClicked, this, [](int index) {
Qt::MouseButtons button = QApplication::mouseButtons();
auto modifiers = []() {
Qt::KeyboardModifiers mods = QApplication::keyboardModifiers();
int modifiers = (mods & Qt::ShiftModifier ? SCMOD_SHIFT : 0) |
return (mods & Qt::ShiftModifier ? SCMOD_SHIFT : 0) |
(mods & Qt::ControlModifier ? SCMOD_CTRL : 0) | (mods & Qt::AltModifier ? SCMOD_ALT : 0) |
(mods & Qt::MetaModifier ? SCMOD_META : 0);
emit("tab_clicked", LUA_TNUMBER, index + 1, LUA_TNUMBER, button, LUA_TNUMBER, modifiers, -1);
};
connect(ui->tabbar, &QTabBar::tabBarClicked, this, [this, modifiers](int index) {
Qt::MouseButtons button = QApplication::mouseButtons();
// Qt emits tabBarClicked before updating the current tab for left button clicks.
// If the "tab_clicked" event were to be emitted here, it could update the current tab, and
// then Qt could do so again, but with the wrong tab index. Instead, only emit "tab_clicked"
// here under certain conditions, relying on currentChanged to do so otherwise.
if (button == Qt::LeftButton && index != ui->tabbar->currentIndex()) return;
emit("tab_clicked", LUA_TNUMBER, index + 1, LUA_TNUMBER, button, LUA_TNUMBER, modifiers(), -1);
if (button == Qt::RightButton) show_context_menu("tab_context_menu", nullptr);
});
connect(ui->tabbar, &QTabBar::currentChanged, this,
[](int index) { emit("tab_clicked", LUA_TNUMBER, index + 1, -1); });
connect(ui->tabbar, &QTabBar::currentChanged, this, [modifiers](int index) {
emit("tab_clicked", LUA_TNUMBER, index + 1, LUA_TNUMBER, Qt::LeftButton, LUA_TNUMBER,
modifiers(), -1);
});
connect(ui->tabbar, &QTabBar::tabMoved, this,
[](int from, int to) { move_buffer(from + 1, to + 1, false); });
connect(ui->tabbar, &QTabBar::tabCloseRequested, this,
Expand Down

0 comments on commit e44373b

Please sign in to comment.