Skip to content

Recent list in dock does not show recent files/folders #74788

Closed

Description

related to #57272 but the dock context menu show seemingly random items, surely nothing recent (where is vscode) and apparently also duplicates (2x extensions)

Screenshot 2019-06-03 at 14 37 22

If we cannot get this to work properly can we at least add an option to not have this feature? It always disappoints me and leaves the sore feeling that our list is not good while other (mac) apps have this working flawless.

Activity

  1. jrieken commented on Jun 3, 2019

    @jrieken
    ContributorAuthor

    Compare dock with ctrl+r list

    Screenshot 2019-06-03 at 15 09 15

  2. bpasero commented on Jun 3, 2019

    @bpasero
    Contributor

    Discussed:

    • we should respect the max length as configured by macOS settings (if possible)
      image

    • we should not add entries that have the same name as other entries (follow up with Electron if this is actually just a missing API or a macOS dock limitation)

    • we can think about making the list smarter by just showing entries based on recency, though today we do not store any recency information with the files/folders, they are just 2 lists

  3. added
    macos
    workbench-os-integration
    help wanted
    good first issue
    on Jun 3, 2019
  4. bpasero commented on Jun 3, 2019

    @bpasero
    Contributor

    If someone wants to chime in, the code is in historyMainService#updateMacOSRecentDocuments()

  5. bill176 commented on Jun 4, 2019

    @bill176

    Hi. I am interested in working on this issue. I just want to make sure that I understood this correctly:

    • when we set the max length to, for example, 5, does that include both documents and folders or 5 of each?
  6. bpasero commented on Jun 4, 2019

    @bpasero
    Contributor

    Bill Zhang (@bill176) from my testing, e.g. setting this number to 5 I could only get 5 entries to show up in total.

    PS: we have to find the macOS settings key that controls this and then possibly can use systemPreferences.getUserDefault() to retrieve this setting, similar to here 👍

  7. bill176 commented on Jun 5, 2019

    @bill176

    Benjamin Pasero (@bpasero) Thanks for the information. However I couldn't seem to find the key associated to that setting. I have looked through domains com.apple.preference.general.plist and com.apple.systemPreferences.plist using the defaults command and none of the key-value-pair seems to correspond to the Recent items setting. I was wondering if this is a problem with my computer. I am running macOS 10.14.5.

  8. bpasero commented on Jun 5, 2019

    @bpasero
    Contributor

    Robo (@deepak1556) do you maybe have an idea where this setting could be found?

  9. connorshea commented on Jun 6, 2019

    @connorshea
    Contributor

    Does this help? https://www.blackbagtech.com/blog/2017/01/17/recent-items-in-macos-sierra/

    Not sure if this is accessible for third party applications, or if it's even the right value to be looking at, but the article mentions com.apple.LSShareFileList.MaxAmount.

  10. bpasero commented on Jun 6, 2019

    @bpasero
    Contributor

    Connor Shea (@connorshea) unfortunately I do not seem to be able to get that value from that string even though it looks very promising...

  11. 4 remaining items

  12. malingyan2017 commented on Jun 8, 2019

    @malingyan2017
    Contributor

    Benjamin Pasero (@bpasero) i follow this post https://stackoverflow.com/questions/17522286/is-there-a-way-to-get-all-values-in-nsuserdefaults, it doesn't include "com.apple.LSShareFileList.MaxAmount" as a key.

    I check Apple Pages and Microsoft Word behavior. Apple Pages most recent doc actually is at the bottom. Microsoft Word uses a secondary list and the most recent is at the top.
    Pages: Screen Shot 2019-06-08 at 12 17 49 AM Word: Screen Shot 2019-06-08 at 12 39 20 AM

    Also, if the user configured the number of entries to be larger than our maximum, the user would end up with a list of old entries, but not new.

    Fixed :)
    Screen Shot 2019-06-08 at 2 09 46 AM

  13. malingyan2017 commented on Jun 8, 2019

    @malingyan2017
    Contributor

    Benjamin Pasero (@bpasero) I think the reason of issue #38199 (Dock context menu is useless) was the different orders of macOS orders recent list (most recent at bottom, limit by config) and VSCode Recent list (most recent at top, infinite).

  14. bpasero commented on Jun 8, 2019

    @bpasero
    Contributor

    malingyan2017 when I read the Apple guidelines it reads:

    Place the most frequently used items at the top of a menu. When users click to open a menu, their focus is on the top area of that menu. Placing the most used items at the top of the menu ensures that people will always find the item they’re looking for.

    It seems that the way we do it is correct given that statement. I will check your updated PR. It would still be great if we could just read the setting. I am worried that we are potentially calling the native API to add entries a ton of times for all the files.

    i follow this post https://stackoverflow.com/questions/17522286/is-there-a-way-to-get-all-values-in-nsuserdefaults, it doesn't include "com.apple.LSShareFileList.MaxAmount" as a key.

    Do you see anything similar at least?

  15. bpasero commented on Jun 8, 2019

    @bpasero
    Contributor

    Hm, actually VLC on macOS also puts the most recent item to the bottom of the menu, that kind of convinces me that we are doing it wrong :)

    Update: also TextEdit! yeah we should change our sorting!

  16. malingyan2017 commented on Jun 9, 2019

    @malingyan2017
    Contributor

    Do you see anything similar at least?
    I don't.

    I take a step back, and look at the code for Windows. It calls app.addRecentDocument in HistoryMainService.addRecentlyOpened for file, and doesn't remove the entry for Windows in removeFromRecentlyOpened.

    Two questions:

    1. Can macOS do the same? In this way, just rely on macOS to manage the order/maxNum of Recent Document list.
    2. I don't have a Windows machine right now. Is there a reason not adding folder into Windows RecentDocument list? Is it a Windows limitation?

    I updated the PR to unify macOS and Windows. Not verified on Windows.

  17. bpasero commented on Jun 10, 2019

    @bpasero
    Contributor

    malingyan2017 I am not sure I understand your latest changes, I think the Windows task item list is perfectly fine. I just checked with other apps on Windows (e.g. Notepad, WordPad) and the order of documents is from top to bottom with the most recent document to the top (same as we do).

    For macOS I have a change ready now that I think works well based on your previous work, maybe you can take a look and adopt it for your PR so that we can merge it. The strategy is:

    • first add files, then workspaces
    • add files and workspaces in reverse order so that most recent entries appear to the bottom

    Given that, even with a small configured number of recent documents, we make sure that the most recent workspaces still appear (which imho are the most important entries).

    historyMainService.ts:

    /*---------------------------------------------------------------------------------------------
     *  Copyright (c) Microsoft Corporation. All rights reserved.
     *  Licensed under the MIT License. See License.txt in the project root for license information.
     *--------------------------------------------------------------------------------------------*/
    
    import * as nls from 'vs/nls';
    import * as arrays from 'vs/base/common/arrays';
    import { IStateService } from 'vs/platform/state/common/state';
    import { app } from 'electron';
    import { ILogService } from 'vs/platform/log/common/log';
    import { getBaseLabel, getPathLabel } from 'vs/base/common/labels';
    import { IPath } from 'vs/platform/windows/common/windows';
    import { Event as CommonEvent, Emitter } from 'vs/base/common/event';
    import { isWindows, isMacintosh } from 'vs/base/common/platform';
    import { IWorkspaceIdentifier, IWorkspacesMainService, ISingleFolderWorkspaceIdentifier, isSingleFolderWorkspaceIdentifier } from 'vs/platform/workspaces/common/workspaces';
    import { IHistoryMainService, IRecentlyOpened, isRecentWorkspace, isRecentFolder, IRecent, isRecentFile, IRecentFolder, IRecentWorkspace, IRecentFile } from 'vs/platform/history/common/history';
    import { ThrottledDelayer } from 'vs/base/common/async';
    import { isEqual as areResourcesEqual, dirname, originalFSPath, basename } from 'vs/base/common/resources';
    import { URI } from 'vs/base/common/uri';
    import { Schemas } from 'vs/base/common/network';
    import { IEnvironmentService } from 'vs/platform/environment/common/environment';
    import { getSimpleWorkspaceLabel } from 'vs/platform/label/common/label';
    import { toStoreData, restoreRecentlyOpened, RecentlyOpenedStorageData } from 'vs/platform/history/electron-main/historyStorage';
    import { exists } from 'vs/base/node/pfs';
    import { ServiceIdentifier } from 'vs/platform/instantiation/common/instantiation';
    import { ILifecycleService, LifecycleMainPhase } from 'vs/platform/lifecycle/electron-main/lifecycleMain';
    
    export class HistoryMainService implements IHistoryMainService {
    
    	private static readonly MAX_TOTAL_RECENT_ENTRIES = 100;
    
    	private static readonly MAX_MACOS_DOCK_RECENT_WORKSPACES = 7; // prefer more workspaces...
    	private static readonly MAX_MACOS_DOCK_RECENT_FILES = 3; // ...compared to files
    
    	// Exclude some very common files from the dock/taskbar
    	private static readonly COMMON_FILES_FILTER = [
    		'COMMIT_EDITMSG',
    		'MERGE_MSG'
    	];
    
    	private static readonly recentlyOpenedStorageKey = 'openedPathsList';
    
    	_serviceBrand: ServiceIdentifier<IHistoryMainService>;
    
    	private _onRecentlyOpenedChange = new Emitter<void>();
    	readonly onRecentlyOpenedChange: CommonEvent<void> = this._onRecentlyOpenedChange.event;
    
    	private macOSRecentDocumentsUpdater: ThrottledDelayer<void>;
    
    	constructor(
    		@IStateService private readonly stateService: IStateService,
    		@ILogService private readonly logService: ILogService,
    		@IWorkspacesMainService private readonly workspacesMainService: IWorkspacesMainService,
    		@IEnvironmentService private readonly environmentService: IEnvironmentService,
    		@ILifecycleService lifecycleService: ILifecycleService
    	) {
    		this.macOSRecentDocumentsUpdater = new ThrottledDelayer<void>(800);
    
    		lifecycleService.when(LifecycleMainPhase.AfterWindowOpen).then(() => this.handleWindowsJumpList());
    	}
    
    	private handleWindowsJumpList(): void {
    		if (!isWindows) {
    			return; // only on windows
    		}
    
    		this.updateWindowsJumpList();
    		this.onRecentlyOpenedChange(() => this.updateWindowsJumpList());
    	}
    
    	addRecentlyOpened(newlyAdded: IRecent[]): void {
    		const workspaces: Array<IRecentFolder | IRecentWorkspace> = [];
    		const files: IRecentFile[] = [];
    
    		for (let curr of newlyAdded) {
    
    			// Workspace
    			if (isRecentWorkspace(curr)) {
    				if (!this.workspacesMainService.isUntitledWorkspace(curr.workspace) && indexOfWorkspace(workspaces, curr.workspace) === -1) {
    					workspaces.push(curr);
    				}
    			}
    
    			// Folder
    			else if (isRecentFolder(curr)) {
    				if (indexOfFolder(workspaces, curr.folderUri) === -1) {
    					workspaces.push(curr);
    				}
    			}
    
    			// File
    			else {
    				const alreadyExistsInHistory = indexOfFile(files, curr.fileUri) >= 0;
    				const shouldBeFiltered = curr.fileUri.scheme === Schemas.file && HistoryMainService.COMMON_FILES_FILTER.indexOf(basename(curr.fileUri)) >= 0;
    
    				if (!alreadyExistsInHistory && !shouldBeFiltered) {
    					files.push(curr);
    
    					// Add to recent documents (Windows only, macOS later)
    					if (isWindows && curr.fileUri.scheme === Schemas.file) {
    						app.addRecentDocument(curr.fileUri.fsPath);
    					}
    				}
    			}
    		}
    
    		this.addEntriesFromStorage(workspaces, files);
    
    		if (workspaces.length > HistoryMainService.MAX_TOTAL_RECENT_ENTRIES) {
    			workspaces.length = HistoryMainService.MAX_TOTAL_RECENT_ENTRIES;
    		}
    
    		if (files.length > HistoryMainService.MAX_TOTAL_RECENT_ENTRIES) {
    			files.length = HistoryMainService.MAX_TOTAL_RECENT_ENTRIES;
    		}
    
    		this.saveRecentlyOpened({ workspaces, files });
    		this._onRecentlyOpenedChange.fire();
    
    		// Schedule update to recent documents on macOS dock
    		if (isMacintosh) {
    			this.macOSRecentDocumentsUpdater.trigger(() => this.updateMacOSRecentDocuments());
    		}
    	}
    
    	removeFromRecentlyOpened(toRemove: URI[]): void {
    		const keep = (recent: IRecent) => {
    			const uri = location(recent);
    			for (const r of toRemove) {
    				if (areResourcesEqual(r, uri)) {
    					return false;
    				}
    			}
    			return true;
    		};
    
    		const mru = this.getRecentlyOpened();
    		const workspaces = mru.workspaces.filter(keep);
    		const files = mru.files.filter(keep);
    
    		if (workspaces.length !== mru.workspaces.length || files.length !== mru.files.length) {
    			this.saveRecentlyOpened({ files, workspaces });
    			this._onRecentlyOpenedChange.fire();
    
    			// Schedule update to recent documents on macOS dock
    			if (isMacintosh) {
    				this.macOSRecentDocumentsUpdater.trigger(() => this.updateMacOSRecentDocuments());
    			}
    		}
    	}
    
    	private async updateMacOSRecentDocuments(): Promise<void> {
    		if (!isMacintosh) {
    			return;
    		}
    
    		// We clear all documents first to ensure an up-to-date view on the set. Since entries
    		// can get deleted on disk, this ensures that the list is always valid
    		app.clearRecentDocuments();
    
    		const mru = this.getRecentlyOpened();
    
    		// Collect max-N recent workspaces that are known to exist
    		const workspaceEntries: string[] = [];
    		for (let i = 0, entries = 0; i < mru.workspaces.length && entries < HistoryMainService.MAX_MACOS_DOCK_RECENT_WORKSPACES; i++) {
    			const loc = location(mru.workspaces[i]);
    			if (loc.scheme === Schemas.file) {
    				const workspacePath = originalFSPath(loc);
    				if (await exists(workspacePath)) {
    					workspaceEntries.push(workspacePath);
    					entries++;
    				}
    			}
    		}
    
    		// Collect max-N recent files that are known to exist
    		const fileEntries: string[] = [];
    		for (let i = 0, entries = 0; i < mru.files.length && entries < HistoryMainService.MAX_MACOS_DOCK_RECENT_FILES; i++) {
    			const loc = location(mru.files[i]);
    			if (loc.scheme === Schemas.file) {
    				const filePath = originalFSPath(loc);
    				if (
    					HistoryMainService.COMMON_FILES_FILTER.indexOf(basename(loc)) !== -1 || // skip some well known file entries
    					workspaceEntries.indexOf(filePath) !== -1								// prefer a workspace entry over a file entry (e.g. for .code-workspace)
    				) {
    					continue;
    				}
    
    				if (await exists(filePath)) {
    					fileEntries.push(filePath);
    					entries++;
    				}
    			}
    		}
    
    		// The apple guidelines (https://developer.apple.com/design/human-interface-guidelines/macos/menus/menu-anatomy/)
    		// explain that most recent entries should appear close to the interaction by the user (e.g. close to the
    		// mouse click). Most native macOS applications that add recent documents to the dock, show the most recent document
    		// to the bottom (because the dock menu is not appearing from top to bottom, but from the bottom to the top). As such
    		// we fill in the entries in reverse order so that the most recent shows up at the bottom of the menu.
    		//
    		// On top of that, the maximum number of documents can be configured by the user (defaults to 10). To ensure that
    		// we are not failing to show the most recent entries, we start by adding files first (in reverse order of recency)
    		// and then add folders (in reverse order of recency). Given that strategy, we can ensure that the most recent
    		// N folders are always appearing, even if the limit is low (https://github.com/microsoft/vscode/issues/74788)
    		fileEntries.reverse().forEach(fileEntry => app.addRecentDocument(fileEntry));
    		workspaceEntries.reverse().forEach(workspaceEntry => app.addRecentDocument(workspaceEntry));
    	}
    
    	clearRecentlyOpened(): void {
    		this.saveRecentlyOpened({ workspaces: [], files: [] });
    		app.clearRecentDocuments();
    
    		// Event
    		this._onRecentlyOpenedChange.fire();
    	}
    
    	getRecentlyOpened(currentWorkspace?: IWorkspaceIdentifier, currentFolder?: ISingleFolderWorkspaceIdentifier, currentFiles?: IPath[]): IRecentlyOpened {
    		const workspaces: Array<IRecentFolder | IRecentWorkspace> = [];
    		const files: IRecentFile[] = [];
    
    		// Add current workspace to beginning if set
    		if (currentWorkspace && !this.workspacesMainService.isUntitledWorkspace(currentWorkspace)) {
    			workspaces.push({ workspace: currentWorkspace });
    		}
    
    		if (currentFolder) {
    			workspaces.push({ folderUri: currentFolder });
    		}
    
    		// Add currently files to open to the beginning if any
    		if (currentFiles) {
    			for (let currentFile of currentFiles) {
    				const fileUri = currentFile.fileUri;
    				if (fileUri && indexOfFile(files, fileUri) === -1) {
    					files.push({ fileUri });
    				}
    			}
    		}
    
    		this.addEntriesFromStorage(workspaces, files);
    
    		return { workspaces, files };
    	}
    
    	private addEntriesFromStorage(workspaces: Array<IRecentFolder | IRecentWorkspace>, files: IRecentFile[]) {
    
    		// Get from storage
    		let recents = this.getRecentlyOpenedFromStorage();
    		for (let recent of recents.workspaces) {
    			let index = isRecentFolder(recent) ? indexOfFolder(workspaces, recent.folderUri) : indexOfWorkspace(workspaces, recent.workspace);
    			if (index >= 0) {
    				workspaces[index].label = workspaces[index].label || recent.label;
    			} else {
    				workspaces.push(recent);
    			}
    		}
    
    		for (let recent of recents.files) {
    			let index = indexOfFile(files, recent.fileUri);
    			if (index >= 0) {
    				files[index].label = files[index].label || recent.label;
    			} else {
    				files.push(recent);
    			}
    		}
    	}
    
    	private getRecentlyOpenedFromStorage(): IRecentlyOpened {
    		const storedRecents = this.stateService.getItem<RecentlyOpenedStorageData>(HistoryMainService.recentlyOpenedStorageKey);
    
    		return restoreRecentlyOpened(storedRecents);
    	}
    
    	private saveRecentlyOpened(recent: IRecentlyOpened): void {
    		const serialized = toStoreData(recent);
    
    		this.stateService.setItem(HistoryMainService.recentlyOpenedStorageKey, serialized);
    	}
    
    	updateWindowsJumpList(): void {
    		if (!isWindows) {
    			return; // only on windows
    		}
    
    		const jumpList: Electron.JumpListCategory[] = [];
    
    		// Tasks
    		jumpList.push({
    			type: 'tasks',
    			items: [
    				{
    					type: 'task',
    					title: nls.localize('newWindow', "New Window"),
    					description: nls.localize('newWindowDesc', "Opens a new window"),
    					program: process.execPath,
    					args: '-n', // force new window
    					iconPath: process.execPath,
    					iconIndex: 0
    				}
    			]
    		});
    
    		// Recent Workspaces
    		if (this.getRecentlyOpened().workspaces.length > 0) {
    
    			// The user might have meanwhile removed items from the jump list and we have to respect that
    			// so we need to update our list of recent paths with the choice of the user to not add them again
    			// Also: Windows will not show our custom category at all if there is any entry which was removed
    			// by the user! See https://github.com/Microsoft/vscode/issues/15052
    			let toRemove: URI[] = [];
    			for (let item of app.getJumpListSettings().removedItems) {
    				const args = item.args;
    				if (args) {
    					const match = /^--(folder|file)-uri\s+"([^"]+)"$/.exec(args);
    					if (match) {
    						toRemove.push(URI.parse(match[2]));
    					}
    				}
    			}
    			this.removeFromRecentlyOpened(toRemove);
    
    			// Add entries
    			jumpList.push({
    				type: 'custom',
    				name: nls.localize('recentFolders', "Recent Workspaces"),
    				items: arrays.coalesce(this.getRecentlyOpened().workspaces.slice(0, 7 /* limit number of entries here */).map(recent => {
    					const workspace = isRecentWorkspace(recent) ? recent.workspace : recent.folderUri;
    					const title = recent.label || getSimpleWorkspaceLabel(workspace, this.environmentService.untitledWorkspacesHome);
    
    					let description;
    					let args;
    					if (isSingleFolderWorkspaceIdentifier(workspace)) {
    						description = nls.localize('folderDesc', "{0} {1}", getBaseLabel(workspace), getPathLabel(dirname(workspace), this.environmentService));
    						args = `--folder-uri "${workspace.toString()}"`;
    					} else {
    						description = nls.localize('workspaceDesc', "{0} {1}", getBaseLabel(workspace.configPath), getPathLabel(dirname(workspace.configPath), this.environmentService));
    						args = `--file-uri "${workspace.configPath.toString()}"`;
    					}
    
    					return {
    						type: 'task',
    						title,
    						description,
    						program: process.execPath,
    						args,
    						iconPath: 'explorer.exe', // simulate folder icon
    						iconIndex: 0
    					};
    				}))
    			});
    		}
    
    		// Recent
    		jumpList.push({
    			type: 'recent' // this enables to show files in the "recent" category
    		});
    
    		try {
    			app.setJumpList(jumpList);
    		} catch (error) {
    			this.logService.warn('#setJumpList', error); // since setJumpList is relatively new API, make sure to guard for errors
    		}
    	}
    }
    
    function location(recent: IRecent): URI {
    	if (isRecentFolder(recent)) {
    		return recent.folderUri;
    	}
    
    	if (isRecentFile(recent)) {
    		return recent.fileUri;
    	}
    
    	return recent.workspace.configPath;
    }
    
    function indexOfWorkspace(arr: IRecent[], workspace: IWorkspaceIdentifier): number {
    	return arrays.firstIndex(arr, w => isRecentWorkspace(w) && w.workspace.id === workspace.id);
    }
    
    function indexOfFolder(arr: IRecent[], folderURI: ISingleFolderWorkspaceIdentifier): number {
    	return arrays.firstIndex(arr, f => isRecentFolder(f) && areResourcesEqual(f.folderUri, folderURI));
    }
    
    function indexOfFile(arr: IRecentFile[], fileURI: URI): number {
    	return arrays.firstIndex(arr, f => areResourcesEqual(f.fileUri, fileURI));
    }
  18. added
    bug
    on Jun 10, 2019
  19. added a commit that references this issue on Jun 10, 2019
    019d8a0
  20. malingyan2017 commented on Jun 11, 2019

    @malingyan2017
    Contributor

    Benjamin Pasero (@bpasero) thanks very much for the code. I made one change on top yours:
    Instead of using MAX_MACOS_DOCK_RECENT_FILES, use MAX_MACOS_DOCK_RECENT_ENTRIES_TOTAL. In this way, if worspace is less than 7, and macOS max limit is 10, we can have more than 3 files.

    Sorry, I was wrong about the Windows. Get a Windows machine, and find VSCode slit workspace and files nicely in the Recent Document List.

    Follow up on macOS question: I was asking if we can just simply using app.addRecentDocument to add entries (can be workspace or file) whenever a document is opened in VSCode. I think this will get your original suggestion ("we can think about making the list smarter by just showing entries based on recency..."). Is it a requirement to show two lists (group workspace together and show at the bottom, then files)?

  21. bpasero commented on Jun 12, 2019

    @bpasero
    Contributor

    Thanks, I merged this in. To summarise the changes:

    • reverted order so that most recent workspaces are shown to the bottom
    • avoid showing a workspace file as file entry if it also exists as workspace entry

    I decided not to filter duplicates in the list because native mac apps also do not do this and there does not seem to exist any API on macOS to distinguish them.

  22. locked and limited conversation to collaborators on Jul 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

buggood first issuehelp wantedmacosverifiedworkbench-os-integration

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions