diff --git a/src/juci.cc b/src/juci.cc index 4b50c38..cd9e3de 100644 --- a/src/juci.cc +++ b/src/juci.cc @@ -82,7 +82,7 @@ void Application::on_activate() { } for(size_t i = 0; i < files.size(); ++i) { - Notebook::get().open(files[i].first, files[i].second); + Notebook::get().open(files[i].first, files[i].second == 0 ? Notebook::Position::left : Notebook::Position::right); if(i < file_offsets.size()) { if(auto view = Notebook::get().get_current_view()) { view->place_cursor_at_line_offset(file_offsets[i].first, file_offsets[i].second); @@ -104,10 +104,11 @@ void Application::on_activate() { } } - while(Gtk::Main::events_pending()) - Gtk::Main::iteration(); - for(auto view : Notebook::get().get_views()) - view->scroll_to(view->get_buffer()->get_insert(), 0.0, 1.0, 0.5); + Glib::signal_idle().connect([] { + for(auto view : Notebook::get().get_views()) + view->scroll_to(view->get_buffer()->get_insert(), 0.0, 1.0, 0.5); + return false; + }); } void Application::on_startup() { diff --git a/src/notebook.cc b/src/notebook.cc index 55f3318..761ba8f 100644 --- a/src/notebook.cc +++ b/src/notebook.cc @@ -103,18 +103,18 @@ std::vector &Notebook::get_views() { return source_views; } -void Notebook::open(const boost::filesystem::path &file_path_, size_t notebook_index, bool copy) { +void Notebook::open(const boost::filesystem::path &file_path_, Position position) { auto file_path = filesystem::get_normal_path(file_path_); - if(notebook_index == 1 && !split) + if((position == Position::right || position == Position::split) && !split) toggle_split(); // Use canonical path to follow symbolic links - boost::system::error_code ec; - auto canonical_file_path = boost::filesystem::canonical(file_path, ec); - if(ec) - canonical_file_path = file_path; - if(!copy) { + if(position == Position::infer) { + boost::system::error_code ec; + auto canonical_file_path = boost::filesystem::canonical(file_path, ec); + if(ec) + canonical_file_path = file_path; for(size_t c = 0; c < size(); c++) { bool equal; { @@ -165,17 +165,11 @@ void Notebook::open(const boost::filesystem::path &file_path_, size_t notebook_i auto view = source_views.back(); - if(copy) { + if(position == Position::split) { auto previous_view = get_current_view(); if(previous_view) { view->replace_text(previous_view->get_buffer()->get_text()); - if(!split) - toggle_split(); - else { - auto pair = get_notebook_page(get_index(previous_view)); - if(pair.second != -1) - notebook_index = pair.first == 0 ? 1 : 0; - } + position = get_notebook_page(get_index(previous_view)).first == 0 ? Position::right : Position::left; } } @@ -184,16 +178,15 @@ void Notebook::open(const boost::filesystem::path &file_path_, size_t notebook_i view->scroll_to_cursor_delayed = [this](Source::BaseView *view, bool center, bool show_tooltips) { if(!show_tooltips) view->hide_tooltips(); - while(Gtk::Main::events_pending()) - Gtk::Main::iteration(); - if(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->hide_tooltips(); - } + Glib::signal_idle().connect([this, view, center] { + if(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()); + } + return false; + }); }; view->update_status_location = [this](Source::BaseView *view) { if(get_current_view() == view) { @@ -434,16 +427,17 @@ void Notebook::open(const boost::filesystem::path &file_path_, size_t notebook_i return false; }); - if(notebook_index == static_cast(-1)) { + if(position == Position::infer) { if(!split) - notebook_index = 0; + position = Position::left; else if(notebooks[0].get_n_pages() == 0) - notebook_index = 0; + position = Position::left; else if(notebooks[1].get_n_pages() == 0) - notebook_index = 1; + position = Position::right; else if(last_view) - notebook_index = get_notebook_page(get_index(last_view)).first; + position = get_notebook_page(get_index(last_view)).first == 0 ? Position::left : Position::right; } + size_t notebook_index = position == Position::right ? 1 : 0; auto ¬ebook = notebooks[notebook_index]; notebook.append_page(*hboxes.back(), *tab_labels.back()); diff --git a/src/notebook.h b/src/notebook.h index e730a03..4405dae 100644 --- a/src/notebook.h +++ b/src/notebook.h @@ -34,7 +34,8 @@ public: Source::View *get_current_view(); std::vector &get_views(); - void open(const boost::filesystem::path &file_path, size_t notebook_index = -1, bool copy = false); + enum class Position { left, right, infer, split }; + void open(const boost::filesystem::path &file_path, Position position = Position::infer); void open_uri(const std::string &uri); void configure(size_t index); bool save(size_t index); diff --git a/src/window.cc b/src/window.cc index 4c5ca00..7371d3c 100644 --- a/src/window.cc +++ b/src/window.cc @@ -1435,7 +1435,7 @@ void Window::set_menu_actions() { auto iter = view->get_buffer()->get_insert()->get_iter(); - Notebook::get().open(view->file_path, -1, true); + Notebook::get().open(view->file_path, Notebook::Position::split); auto new_view = Notebook::get().get_current_view(); new_view->place_cursor_at_line_offset(iter.get_line(), iter.get_line_offset()); new_view->scroll_to_cursor_delayed(new_view, true, false); diff --git a/tests/stubs/notebook.cc b/tests/stubs/notebook.cc index 53b682f..f045b8f 100644 --- a/tests/stubs/notebook.cc +++ b/tests/stubs/notebook.cc @@ -6,4 +6,4 @@ Source::View *Notebook::get_current_view() { return nullptr; } -void Notebook::open(const boost::filesystem::path &file_path, size_t notebook_index, bool copy) {} +void Notebook::open(const boost::filesystem::path &file_path, Position position) {}