[LyX/master] Raise the window in single-instance mode

Enrico Forestieri forenr at lyx.org
Tue Apr 21 19:42:51 UTC 2020


commit a73c2c2b9342edd875810da700692d908f9ac624
Author: Enrico Forestieri <forenr at lyx.org>
Date:   Tue Apr 21 21:55:20 2020 +0200

    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 84deea3..585d1e1 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 9f4d3a5..c9f7625 100644
--- a/src/LyXAction.cpp
+++ b/src/LyXAction.cpp
@@ -4221,6 +4221,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.
  * \li Notion: This is not the action which should be bound to the arrow keys,
diff --git a/src/Server.cpp b/src/Server.cpp
index 5fb089d..dfc932c 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<string>::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 78e0817..382496e 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.


More information about the lyx-cvs mailing list