diff options
author | Karol Kosek <krkk@serenityos.org> | 2022-03-13 16:37:24 +0100 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2022-03-15 12:01:27 +0330 |
commit | dcd3d7fe22e66f05bb08e097dd959360adf55cb2 (patch) | |
tree | 6ae46995806f4c3773bf88624737aa3240a71747 /Userland | |
parent | db1236b336c6d8b2a1cb5c79a635c26ae018ec8d (diff) | |
download | serenity-dcd3d7fe22e66f05bb08e097dd959360adf55cb2.zip |
Spreadsheet: Set tab functions for every tab on setup
Previously, we were setting tab actions only for the active tab on a tab
change, and the same actions for the previous tab were removed.
Unfortunately, this also happened when making a new tab, which meant
that you could trick the cell editor to jump to the new sheet and start
writing there.
To fix this, every view will always have on_selection_changed
and on_selection_dropped assigned. I haven't seen much difference in
the memory usage, so I guess it'll be fine :)
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp | 28 |
1 files changed, 7 insertions, 21 deletions
diff --git a/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp b/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp index 05bfc18b6a..31fffa3da1 100644 --- a/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp +++ b/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp @@ -253,24 +253,13 @@ void SpreadsheetWidget::resize_event(GUI::ResizeEvent& event) void SpreadsheetWidget::setup_tabs(NonnullRefPtrVector<Sheet> new_sheets) { - RefPtr<GUI::Widget> first_tab_widget; for (auto& sheet : new_sheets) { - auto& tab = m_tab_widget->add_tab<SpreadsheetView>(sheet.name(), sheet); - if (!first_tab_widget) - first_tab_widget = &tab; - } - - auto change = [&](auto& selected_widget) { - if (m_selected_view) { - m_selected_view->on_selection_changed = nullptr; - m_selected_view->on_selection_dropped = nullptr; - } - m_selected_view = &static_cast<SpreadsheetView&>(selected_widget); - m_selected_view->model()->on_cell_data_change = [&](auto& cell, auto& previous_data) { + auto& new_view = m_tab_widget->add_tab<SpreadsheetView>(sheet.name(), sheet); + new_view.model()->on_cell_data_change = [&](auto& cell, auto& previous_data) { undo_stack().push(make<CellUndoCommand>(cell, previous_data)); window()->set_modified(true); }; - m_selected_view->on_selection_changed = [&](Vector<Position>&& selection) { + new_view.on_selection_changed = [&](Vector<Position>&& selection) { auto* sheet_ptr = m_selected_view->sheet_if_available(); // How did this even happen? VERIFY(sheet_ptr); @@ -335,7 +324,7 @@ void SpreadsheetWidget::setup_tabs(NonnullRefPtrVector<Sheet> new_sheets) }; static_cast<CellSyntaxHighlighter*>(const_cast<Syntax::Highlighter*>(m_cell_value_editor->syntax_highlighter()))->set_cell(&first_cell); }; - m_selected_view->on_selection_dropped = [&]() { + new_view.on_selection_dropped = [&]() { m_current_cell_label->set_enabled(false); m_current_cell_label->set_text({}); m_cell_value_editor->on_change = nullptr; @@ -349,13 +338,10 @@ void SpreadsheetWidget::setup_tabs(NonnullRefPtrVector<Sheet> new_sheets) static_cast<CellSyntaxHighlighter*>(const_cast<Syntax::Highlighter*>(m_cell_value_editor->syntax_highlighter()))->set_cell(nullptr); }; - }; - - if (first_tab_widget) - change(*first_tab_widget); + } - m_tab_widget->on_change = [change = move(change)](auto& selected_widget) { - change(selected_widget); + m_tab_widget->on_change = [this](auto& selected_widget) { + m_selected_view = &static_cast<SpreadsheetView&>(selected_widget); }; m_tab_widget->on_context_menu_request = [&](auto& widget, auto& event) { |