Selaa lähdekoodia

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 :)
Karol Kosek 3 vuotta sitten
vanhempi
commit
dcd3d7fe22
1 muutettua tiedostoa jossa 7 lisäystä ja 21 poistoa
  1. 7 21
      Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp

+ 7 - 21
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) {