From 775760d5a3536dd63b172765a67b82ade4aad981 Mon Sep 17 00:00:00 2001 From: Enrico Forestieri Date: Tue, 21 Apr 2020 21:55:20 +0200 Subject: [PATCH] Raise the window in single-instance mode Other than issuing a warning on the console, now the LyX window is raised when in single-instance mode and no files to load are specified. In this way, the user is aware that something happened after double clicking the program icon. To support this functionality the new lfun window-raise has been introduced. --- src/FuncCode.h | 1 + src/LyXAction.cpp | 12 ++++++++++++ src/Server.cpp | 15 +++++++++++++-- src/frontends/qt/GuiView.cpp | 7 +++++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/FuncCode.h b/src/FuncCode.h index 84deea34df..585d1e1580 100644 --- a/src/FuncCode.h +++ b/src/FuncCode.h @@ -489,6 +489,7 @@ enum FuncCode // 380 LFUN_MASTER_BUFFER_FORALL, // spitz 20191231 LFUN_IF_RELATIVES, // spitz 20200102 + LFUN_WINDOW_RAISE, // forenr, 20202104 LFUN_LASTACTION // end of the table }; diff --git a/src/LyXAction.cpp b/src/LyXAction.cpp index 9f4d3a5589..c9f76258f8 100644 --- a/src/LyXAction.cpp +++ b/src/LyXAction.cpp @@ -4220,6 +4220,18 @@ void LyXAction::init() */ { LFUN_WINDOW_NEW, "window-new", NoBuffer, Buffer }, +/*! + * \var lyx::FuncCode lyx::LFUN_WINDOW_RAISE + * \li Action: Raises the LyX window. + * \li Notion: Brings the LyX window to the front. Such behavior is allowed + on Windows only when no other application has focus. + This action is used when LyX is in single instance mode. + * \li Syntax: window-raise + * \li Origin: forenr, 21 Apr 2020 + * \endvar + */ + { LFUN_WINDOW_RAISE, "window-raise", ReadOnly | NoBuffer, Hidden }, + /*! * \var lyx::FuncCode lyx::LFUN_WORD_BACKWARD * \li Action: Moves the cursor to the logically previous beginning of a word. diff --git a/src/Server.cpp b/src/Server.cpp index 5fb089d758..dfc932c674 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -1002,14 +1002,25 @@ struct Sleep : QThread bool LyXComm::loadFilesInOtherInstance() { + int pipefd; + FileName const pipe(inPipeName()); + if (theFilesToLoad().empty()) { LYXERR0("LyX is already running in another instance\n" "and 'use single instance' is active."); + // Wait a while for the other instance to reset the connection + Sleep::millisec(200); + pipefd = ::open(pipe.toFilesystemEncoding().c_str(), O_WRONLY); + if (pipefd >= 0) { + string const cmd = "LYXCMD:pipe:window-raise\n"; + if (::write(pipefd, cmd.c_str(), cmd.length()) < 0) + LYXERR0("Cannot communicate with running instance!"); + ::close(pipefd); + } return true; } - int pipefd; + int loaded_files = 0; - FileName const pipe(inPipeName()); vector::iterator it = theFilesToLoad().begin(); while (it != theFilesToLoad().end()) { FileName fname = fileSearch(string(), os::internal_path(*it), diff --git a/src/frontends/qt/GuiView.cpp b/src/frontends/qt/GuiView.cpp index 78e0817cdd..382496e3e5 100644 --- a/src/frontends/qt/GuiView.cpp +++ b/src/frontends/qt/GuiView.cpp @@ -2312,6 +2312,7 @@ bool GuiView::getStatus(FuncRequest const & cmd, FuncStatus & flag) case LFUN_SERVER_GOTO_FILE_ROW: case LFUN_LYX_ACTIVATE: + case LFUN_WINDOW_RAISE: break; case LFUN_FORWARD_SEARCH: enable = !(lyxrc.forward_search_dvi.empty() && lyxrc.forward_search_pdf.empty()); @@ -4502,6 +4503,12 @@ void GuiView::dispatch(FuncRequest const & cmd, DispatchResult & dr) activateWindow(); break; + case LFUN_WINDOW_RAISE: + raise(); + activateWindow(); + showNormal(); + break; + case LFUN_FORWARD_SEARCH: { // it seems safe to assume we have a document buffer, since // getStatus wants one. -- 2.39.5