From f2f443bd85c9d211152749e2981c9a6565ff78a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Tue, 12 Jun 2018 21:40:03 +0200 Subject: [PATCH] WIP --- src/directories.cc | 10 +++--- src/notebook.cc | 87 ++++++++++++++++++++++------------------------ src/notebook.h | 1 + src/source.cc | 2 -- src/source.h | 3 +- 5 files changed, 47 insertions(+), 56 deletions(-) diff --git a/src/directories.cc b/src/directories.cc index bcad6c1..eb48089 100644 --- a/src/directories.cc +++ b/src/directories.cc @@ -69,7 +69,7 @@ bool Directories::TreeStore::drag_data_received_vfunc(const TreeModel::Path &pat Terminal::get().print("Error: could not move file: "+ec.message()+'\n', true); return false; } - + auto new_file_path = target_path; for(size_t c=0;cfile_path.begin(); for(auto source_it=source_path.begin();source_it!=source_path.end();source_it++) file_it++; - auto new_file_path=target_path; for(;file_it!=view->file_path.end();file_it++) new_file_path/=*file_it; view->rename(new_file_path); @@ -88,7 +87,7 @@ bool Directories::TreeStore::drag_data_received_vfunc(const TreeModel::Path &pat break; } } - + Notebook::get().update_labels(new_file_path); Directories::get().update(); Directories::get().on_save_file(target_path); directories.select(target_path); @@ -263,7 +262,7 @@ Directories::Directories() : Gtk::ListViewText(1) { update(); on_save_file(target_path); select(target_path); - + auto new_file_path=target_path; for(size_t c=0;cfile_path.begin(); for(auto source_it=source_path.begin();source_it!=source_path.end();source_it++) file_it++; - auto new_file_path=target_path; for(;file_it!=view->file_path.end();file_it++) new_file_path/=*file_it; view->rename(new_file_path); @@ -291,7 +289,7 @@ Directories::Directories() : Gtk::ListViewText(1) { Terminal::get().print("Warning: language for "+target_path.string()+" has changed. Please reopen the file\n"); } } - + Notebook::get().update_labels(new_file_path); EntryBox::get().hide(); }); auto entry_it=EntryBox::get().entries.begin(); diff --git a/src/notebook.cc b/src/notebook.cc index 4870f0c..88e0889 100644 --- a/src/notebook.cc +++ b/src/notebook.cc @@ -114,6 +114,43 @@ std::vector &Notebook::get_views() { return source_views; } +void Notebook::update_labels(const boost::filesystem::path &introduced_file_name) { + std::vector> duplicates; + for (size_t c = 0; c < size(); ++c) { + if (source_views.at(c)->file_path.filename() == introduced_file_name.filename()) { + duplicates.emplace_back(std::make_pair(source_views[c]->file_path, c)); + } + } + std::sort(duplicates.begin(), duplicates.end(),[](const std::pair &a, const std::pair &b){ + return a.first > b.first; + }); + for (size_t c = 0; c < duplicates.size(); ++c) { + auto &duplicate = duplicates.at(c); + std::cout << duplicate.first << std::endl; + int diff = 0; + const auto parent_path1 = duplicate.first.parent_path(); + const auto index = c-1 == -1 ? duplicates.size()-1 : c-1; + const auto parent_path2 = duplicates.at(index).first.parent_path(); + auto it1 = parent_path1.end(); + const auto it1_end = parent_path1.begin(); + auto it2 = parent_path2.end(); + const auto it2_end = parent_path2.begin(); + while (it1 != it1_end && it2 != it2_end && *it1 == *it2) { + --it1; + --it2; + ++diff; + } + const auto current_view = source_views[duplicate.second]; + auto title = it1->string() + (diff > 1 ? "/…/" : "/") + current_view->file_path.filename().string(); + auto &tab_label = tab_labels.at(duplicate.second); + tab_label->label.set_text(title); + if (current_view->update_modified && current_view->get_buffer()->get_modified()) + current_view->update_modified(current_view); + tab_label->set_tooltip_text(filesystem::get_short_path(current_view->file_path).string()); + } + std::cout << std::endl; + //TODO update status +} void Notebook::open(const boost::filesystem::path &file_path_, size_t notebook_index) { auto file_path=filesystem::get_normal_path(file_path_); @@ -201,45 +238,6 @@ void Notebook::open(const boost::filesystem::path &file_path_, size_t notebook_i const auto title = view->get_buffer()->get_modified() ? tab_title + '*' : tab_title.substr(0, tab_title.size() - 1); label.set_text(title); }; - source_views.back()->update_tab_label = [this](Source::View *view) { - const auto update_label = [&](size_t index, const std::string &prepend) { - const auto current_view = source_views[index]; - auto title = prepend + current_view->file_path.filename().string(); - auto &tab_label = tab_labels.at(index); - tab_label->label.set_text(title); - if (current_view->update_modified && current_view->get_buffer()->get_modified()) - current_view->update_modified(current_view); - tab_label->set_tooltip_text(filesystem::get_short_path(current_view->file_path).string()); - }; - const auto file_name = view->file_path.filename(); - std::string prepend_current_view; - size_t current_view_index = 0; - for (size_t c = 0; c < size(); ++c) { - if (source_views[c] == view) { - current_view_index = c; - continue; - } - if (source_views[c]->file_path.filename() == file_name) { - int diff = 0; - const auto parent_path1 = view->file_path.parent_path(); - const auto parent_path2 = source_views[c]->file_path.parent_path(); - auto it1 = parent_path1.end(); - const auto it1_end = parent_path1.begin(); - auto it2 = parent_path2.end(); - const auto it2_end = parent_path2.begin(); - while (it1 != it1_end && it2 != it2_end && *it1 == *it2) { - --it1; - --it2; - ++diff; - } - if (prepend_current_view.empty()) - prepend_current_view = it1->string() + (diff > 0 ? "/…/" : "/"); - update_label(c, it2->string() + (diff > 0 ? "/…/" : "/")); - } - } - update_label(current_view_index, prepend_current_view); - update_status(view); - }; source_views.back()->update_status_diagnostics=[this](Source::View* view) { if(get_current_view()==view) { std::string diagnostic_info; @@ -324,9 +322,8 @@ void Notebook::open(const boost::filesystem::path &file_path_, size_t notebook_i close(index); })); - if(source_view->update_tab_label) - source_view->update_tab_label(source_view); - + update_labels(file_path); + source_view->get_buffer()->signal_modified_changed().connect([source_view]() { if (source_view->update_modified) source_view->update_modified(source_view); @@ -484,6 +481,7 @@ bool Notebook::save_current() { bool Notebook::close(size_t index) { if(auto view=get_view(index)) { + auto removed_path = view->file_path; if(view->get_buffer()->get_modified()){ if(!save_modified_dialog(index)) return false; @@ -537,10 +535,7 @@ bool Notebook::close(size_t index) { scrolled_windows.erase(scrolled_windows.begin()+index); hboxes.erase(hboxes.begin()+index); tab_labels.erase(tab_labels.begin()+index); - } - for (auto view : get_views()) { - if (view->update_tab_label) - view->update_tab_label(view); + Notebook::get().update_labels(removed_path); } return true; } diff --git a/src/notebook.h b/src/notebook.h index b9c36b8..1634f39 100644 --- a/src/notebook.h +++ b/src/notebook.h @@ -42,6 +42,7 @@ public: void next(); void previous(); void toggle_split(); + void update_labels(const boost::filesystem::path &introduced_file_name = ""); /// Hide/Show tabs. void toggle_tabs(); boost::filesystem::path get_current_folder(); diff --git a/src/source.cc b/src/source.cc index 740c893..9a0886b 100644 --- a/src/source.cc +++ b/src/source.cc @@ -629,8 +629,6 @@ void Source::View::rename(const boost::filesystem::path &path) { } if(update_status_file_path) update_status_file_path(this); - if(update_tab_label) - update_tab_label(this); } void Source::View::set_tab_char_and_size(char tab_char, unsigned tab_size) { diff --git a/src/source.h b/src/source.h index d43722e..a625704 100644 --- a/src/source.h +++ b/src/source.h @@ -98,8 +98,7 @@ namespace Source { void hide_tooltips() override; void hide_dialogs() override; - - std::function update_tab_label; + std::function update_modified; std::function update_status_location; std::function update_status_file_path;