diff --git a/src/notebook.cc b/src/notebook.cc index f973961..3d310c0 100644 --- a/src/notebook.cc +++ b/src/notebook.cc @@ -116,6 +116,18 @@ void Notebook::open(const boost::filesystem::path &file_path) { else source_views.emplace_back(new Source::GenericView(file_path, project_path, language)); + source_views.back()->scroll_to_cursor_delayed=[this](Source::View* view, bool center, bool show_tooltips) { + while(g_main_context_pending(NULL)) + g_main_context_iteration(NULL, false); + if(get_current_page()!=-1 && get_current_view()==view) { + if(center) + view->scroll_to(view->get_buffer()->get_insert(), 0.0, 1.0, 0.5); + else + view->scroll_to(view->get_buffer()->get_insert()); + if(!show_tooltips) + view->delayed_tooltips_connection.disconnect(); + } + }; source_views.back()->on_update_status=[this](Source::View* view, const std::string &status_text) { if(get_current_page()!=-1 && get_current_view()==view) status.set_text(status_text+" "); diff --git a/src/source.cc b/src/source.cc index 6a7d4ee..a74607b 100644 --- a/src/source.cc +++ b/src/source.cc @@ -349,6 +349,7 @@ Source::View::View(const boost::filesystem::path &file_path, const boost::filesy auto cursor_line_offset=iter.get_line_offset(); get_buffer()->set_text(stdout_stream.str()); + get_source_buffer()->end_user_action(); cursor_line_nr=std::min(cursor_line_nr, get_buffer()->get_line_count()-1); if(cursor_line_nr>=0) { @@ -359,11 +360,8 @@ Source::View::View(const boost::filesystem::path &file_path, const boost::filesy iter.forward_char(); } get_buffer()->place_cursor(iter); - while(g_main_context_pending(NULL)) //TODO: minor: might crash if the buffer is saved and closed really fast right after doing auto indent - g_main_context_iteration(NULL, false); - scroll_to(get_buffer()->get_insert(), 0.0, 1.0, 0.5); + scroll_to_cursor_delayed(this, true, false); } - get_source_buffer()->end_user_action(); } }; } @@ -748,12 +746,13 @@ void Source::View::paste() { } } get_buffer()->place_cursor(get_buffer()->get_insert()->get_iter()); - scroll_to(get_buffer()->get_insert()); get_source_buffer()->end_user_action(); + scroll_to_cursor_delayed(this, false, false); } else { Gtk::Clipboard::get()->set_text(text); get_buffer()->paste_clipboard(Gtk::Clipboard::get()); + scroll_to_cursor_delayed(this, false, false); } } diff --git a/src/source.h b/src/source.h index f0dde90..feffbbf 100644 --- a/src/source.h +++ b/src/source.h @@ -92,6 +92,7 @@ namespace Source { Gtk::TextIter get_iter_for_dialog(); sigc::connection delayed_tooltips_connection; + std::function scroll_to_cursor_delayed=[](View* view, bool center, bool show_tooltips) {}; std::function on_update_status; std::function on_update_info; void set_status(const std::string &status); diff --git a/src/window.cc b/src/window.cc index 76081ff..c50ee81 100644 --- a/src/window.cc +++ b/src/window.cc @@ -344,10 +344,7 @@ void Window::set_menu_actions() { if(notebook.get_current_page()!=-1) { auto view=notebook.get_current_view(); - while(g_main_context_pending(NULL)) - g_main_context_iteration(NULL, false); - if(notebook.get_current_page()!=-1 && notebook.get_current_view()==view) - view->scroll_to(view->get_buffer()->get_insert(), 0.0, 1.0, 0.5); + view->scroll_to_cursor_delayed(view, true, false); } }); @@ -415,12 +412,7 @@ void Window::set_menu_actions() { index=std::min(index, end_line_index); view->get_buffer()->place_cursor(view->get_buffer()->get_iter_at_line_index(line, index)); - while(g_main_context_pending(NULL)) - g_main_context_iteration(NULL, false); - if(notebook.get_current_page()!=-1 && notebook.get_current_view()==view) { - view->scroll_to(view->get_buffer()->get_insert(), 0.0, 1.0, 0.5); - view->delayed_tooltips_connection.disconnect(); - } + view->scroll_to_cursor_delayed(view, true, false); } } } @@ -709,10 +701,7 @@ void Window::set_menu_actions() { line_index=0; view->get_buffer()->place_cursor(view->get_buffer()->get_iter_at_line_index(line_nr, line_index)); - while(g_main_context_pending(NULL)) - g_main_context_iteration(NULL, false); - if(notebook.get_current_page()!=-1 && notebook.get_current_view()==view) - view->scroll_to(view->get_buffer()->get_insert(), 0.0, 1.0, 0.5); + view->scroll_to_cursor_delayed(view, true, true); } Project::debug_update_stop(); } @@ -984,10 +973,7 @@ void Window::goto_line_entry() { line--; view->get_buffer()->place_cursor(view->get_buffer()->get_iter_at_line(line)); - while(g_main_context_pending(NULL)) - g_main_context_iteration(NULL, false); - if(notebook.get_current_page()!=-1 && notebook.get_current_view()==view) - view->scroll_to(view->get_buffer()->get_insert(), 0.0, 1.0, 0.5); + view->scroll_to_cursor_delayed(view, true, false); } } catch(const std::exception &e) {}