Keep Focus & Selection

:warning: IMPORTANT: FOR TESTING PURPOSES ONLY

This script add-in adds the possibility to remember selected items and focus when navigating between folders and/or after a refresh. It works by saving the selected items and/or with focus including the tab path just before each folder change/refresh. It also has multi-lister support.

HOW TO INSTALL

Download the file below. Then run Prefs SCRIPTINSTALL and select the downloaded file.
Keep_Selection.js.txt (6.0 KB)

OPTIONS
There are some interesting options to set up first. In the Script Management window, select the script and click in the gear icon.

  • debug: Useful if you have some problems with the script or you want to see the process step by step.
  • ignored_paths: Paths to be ignored (not stored) by the script. One path per line. Note that all paths recognized by DOpus as shell type are automatically ignored.
  • max_paths: Maximum number of paths to remember. Note that the minimum is hardcoded to 2.
  • restore_mode: Choose between restoring only the item with focus, the entire selection or both.
  • sel_first_if: When an unstored path is opened, the script selects the first item in the folder to make keyboard navigation easier. This option can disable this.

SIDENOTE
You will probably want to disable the 'Automatically select first file in folder' option in Prefs PAGE="filedisplay" if you plan to use this script.

Any feedback is welcome, I hope you find it useful, and remember that the script may contain bugs.

CHANGELOG
v1.0 (2023-10-23) : Initial release
v1.0.1 (2023-10-24) :

  • Fixed the issue of the first item being selected after 2 consecutive refreshes without changes and the path being saved with items selected but without focus (e.g. selecting an entire group by clicking on the group header).

v1.0.2 (2023-10-25) :

  • If restore_mode=focus only but you have selected items without giving focus to one of them (e.g. selecting whole groups by clicking on their header), now the script will give the focus to the first selected item instead of saying that there is no item with focus
7 Likes

Awesome, many thanks. Working ok.

UPDATE:
2 issues

  • refresh, it always selects first file and the remembered selection whether the setting is on or off
  • refresh, it always selects - deselects - selects

Thanks for testing!!!

I can't reproduce what you say. Could you show me some visual evidence?
Here is how it works for me with restore_mode=both selection and focus
restore_mode_2

And this is when I change it to focus only
restore_mode_0

Maybe some other script or command you have causes that effect? Or do you have 'Automatically select first file in folder' option in Prefs PAGE="filedisplay" enabled?

Again, I can't reproduce it, and this one is even weirder, because if you look at the code, the script never uses any command to deselect. I do see that visually it seems that for an instant after opening/refreshing a folder, the first element will gain focus, but it's not something caused by the script (you can see similar effect with the script disabled), only that as the script makes a selection after that, the event is more noticeable. But regarding your question, no, the script doesn't do something like select, deselect and reselect.

Thanks for the video, it was very helpful!
The problem happens when selecting in a way that there is no item with focus (e.g. selecting by group). In that case, DOpus puts the focus on the first item .
If you pay attention in your video, the first time you update the folder, the selection is done correctly, and the focus is obtained by the first item (You can see a yellow frame appear in it. FWIW that's not done by the script). When you refresh a second time, (if no change is made to the selection) the first item already has the focus and so it is selected (as far as I have researched in the manual, there is no way to give focus to an item without selecting it). I have made some changes to mitigate that. You can try the new version and let me know how it works.
Note: You're still going to see that problem if restore_mode=focus only. The first time it will work correctly, but after that, the first item gets the focus (if no change is made to the selection). But TBH that scenario will be something very odd.
I still don't know if that DOpus behavior is by some enabled/disabled option or by design, but when I have more time I will look carefully in the manual.

New version (v1.0.2) is up

remenbers only last folder from selection after refesh, files are good for me.

and in every new folder it selects the first one.

Set sel_first_if to false (all the config options are explained above in the first post)

I'm sorry but I don't quite understand what you are trying to say here. Could you elaborate it further?

Ah, thanks! That's true

a scripts works only for files, not folders for me...

script

using version 1.0.2 I am still getting same issues

@fkast very strange indeed!
Does it happen in all the folders?
Don't you have some other script running in the background that responds to events?
It might be worth setting debug=true, restore_mode=both focus and selection. Then open the script log, select a defined number of folders and files, refresh and look in the log for these two lines:

OBFC=>Saving : some/path ---Vector size: x
OAFC=>Founded : some/path ---Items saved: y

y should be equal to the number of items you selected. x should be y + 1.

@galaxyhub from a quick view of your video, I can notice the following:

  • Every time you refresh, the video goes for a second to black...I'm not sure if it's something related to the video or indeed there is that delay every time DOpus reads a folder.
  • There are 2 of those black screen effects, which tells me that you updated 2 times, but the second one was also manual or a product of some script/command that automates that? I can't see that very clear in the video.
  • After the first refresh, you can see that the first item is not selected, but it does gain focus (something the script can't do). After the second refresh, evidently the first item is going to be selected because it had the focus.

After reading a folder, the first item always gets the focus. That frame effect on the first item can also be seen on my computer, but it is almost instantaneous, unlike yours.
My guess:

  • Then only after that, the script can select items/change the focus. That takes x amount of time. For the same thing to happen as in your case, I have to refresh 2 times in a row in a time less than x to make it save the selection partially or save the first item that had the focus at the beginning (believe me it's fast). In your case, as there is a longer delay, it could well be that x is a larger time span, and that's why if you refresh very quickly you get these problems with the script more often?

Now the important question:

  • What happens if you do the same but wait longer between refreshes? And in a regular use? FWIW there's no real need for refreshing 2 times in less than a second.

You can try to set debug=true and check which item is saved with focus and which item is retrieved with focus (look for the lines OBFC=>Focused item and OAFC=>Focus in the log).

values are correct, still selecting first item in lister as well

have turned off all other scripts, result is the same, Don't know what the black flash is about, I thought that was directory opus, (doing a clean install and starting from scratch would take some time and not something I want to do).
ezgif-4-ffbe1b6355

That was meant for @fkast :smile:

That's for you

So that delay when reading a folder indeed happens and is not something video related only?

can't really simulate regular use atm, beta testing this awsome product. But I did change 'restore_mode' to 'selected only' and is working okay now.

The blck flash is video related I think as comparing a directory refresh to the recorded video, the directory opus is not noticable.

Many thanks for quick response :slight_smile:

Lines 116-119, condition is wrong

if (focus)
	cmd.RunCommand('Select FROMSCRIPT');
else cmd.RunCommand('Select FROMSCRIPT SETFOCUS');