Skip to content

feat(focus): Add functionality to focus on running instance #13196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 1, 2025

Conversation

miguelro20
Copy link
Contributor

Closes #13129
This issue consisted on focusing a running instance of Jabref when the user tried to open another instance of the program. To do so, the first step was to modify the handleMultipleAppInstances function, to return an option from an enum containing CONTINUE, SHUTDOWN, FOCUS to determine what to do next. If there is no instance, CONTINUE is selected and one boots up, if an instance is running, FOCUS is returned. Focus is a function created inside UiCommands. In the launcher if FOCUS is returned it triggers RemoteClient's sendFocus function, which sends the message FOCUS and its outlined inside RemoteMessage. Then the RemoteListenerServer picks up this FOCUS message inside of a case within a switch, which calls a function from the RemoteMessageHandler called handlerFocus() and being overridden inside CLIMessageHandler, and sends the platform a list of UiCommands, consisting of one UiCommand.Focus (This because JabRefFrame expects a list of UiCommands not just one UiCommand). This UiCommand is then picked up in JabRefFrame, which then calls the toFront() and requestFocus() functions to show the running instance.
This makes the user see the already running instance, and at the same time handles all the functions needed to send Focus from the Launcher to the JabRefFrame.

Running JabRef Instance from IntelliJ

Screenshot 2025-05-30 at 12 50 51 p m

Message when running ./gradlew jabgui:run in Terminal with running instance

Screenshot 2025-05-30 at 12 55 32 p m

Mandatory checks

  • I own the copyright of the code submitted and I license it under the MIT license
  • [/] Change in CHANGELOG.md described in a way that is understandable for the average user (if change is visible to the user)
  • [/] Tests created for changes (if applicable)
  • Manually tested changed features in running JabRef (always required)
  • Screenshots added in PR description (if change is visible to the user)
  • Checked developer's documentation: Is the information available and up to date? If not, I outlined it in this pull request.
  • Checked documentation: Is the information available and up to date? If not, I created an issue at https://github.com/JabRef/user-documentation/issues or, even better, I submitted a pull request to the documentation repository.

@@ -140,25 +153,27 @@ private static boolean handleMultipleAppInstances(String[] args, RemotePreferenc
LOGGER.debug("Pinging other instance succeeded.");
if (args.length == 0) {
// There is already a server out there, avoid showing log "Passing arguments" while no arguments are provided.
LOGGER.warn("This JabRef instance is already running. Please switch to that instance.");
LOGGER.warn("A JabRef instance is already running. Switching to that instance.");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The log message uses an exclamation mark, which is discouraged as it can imply shouting. A period should be used instead to maintain a professional tone.

Comment on lines +20 to +23
/**
* Request the running instance to focus its window when a second instance tries to execute. No message content.
*/
FOCUS
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JavaDoc comment for the FOCUS constant is trivial and restates the obvious without adding new information or reasoning, which violates the guideline for non-trivial comments.

@@ -3,4 +3,6 @@
@FunctionalInterface
public interface RemoteMessageHandler {
void handleCommandLineArguments(String[] message);

default void handleFocus() { }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The patch reformats the code without adding new statements, which violates the guideline that code should not be reformatted only because of syntax.

*
* @return true if successful, false otherwise.
*/
public boolean sendFocus() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new public method 'sendFocus' should not return 'null'. It should use 'java.util.Optional' to handle cases where a return value might be absent.

Comment on lines 647 to 648
mainStage.toFront();
mainStage.requestFocus();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@InAnYan I think, it is either or here.

VS.Code just lets blink the app in the task bar (which is requestFocus IMHO). Here, the current instance is brought to front. I assume that requestFocus is made in addition to ensure it works?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the requestFocus statement - and it worked, too.

Copy link
Member

@koppor koppor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides the additional requestFocus statement, it looks goot to me.

Copy link
Member

@koppor koppor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@miguelro20 Please add a CHANGELOG.md entry. Please remove mainStage.requestFocus();, because ...toFront is enough (if you have other reasons, please share)

Then, this is good to go IMHO

Copy link

trag-bot bot commented Jun 1, 2025

@trag-bot didn't find any issues in the code! ✅✨

@miguelro20
Copy link
Contributor Author

@koppor I believe I've made all the necessary changes, you were right about the requestFocus, it was redundant

@koppor koppor added this pull request to the merge queue Jun 1, 2025
Merged via the queue into JabRef:main with commit 0ea5480 Jun 1, 2025
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Focus on running instance
2 participants