forked from pulsar-edit/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reopen-project-menu-manager.js
182 lines (165 loc) · 5.08 KB
/
reopen-project-menu-manager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const { CompositeDisposable } = require('event-kit');
const path = require('path');
const TASK_DESCRIPTION_MAX_LENGTH = 260;
module.exports = class ReopenProjectMenuManager {
constructor({ menu, commands, history, config, open }) {
this.menuManager = menu;
this.historyManager = history;
this.config = config;
this.open = open;
this.projects = [];
this.subscriptions = new CompositeDisposable();
this.subscriptions.add(
history.onDidChangeProjects(this.update.bind(this)),
config.onDidChange(
'core.reopenProjectMenuCount',
({ oldValue, newValue }) => {
this.update();
}
),
commands.add('atom-workspace', {
'application:reopen-project': this.reopenProjectCommand.bind(this)
})
);
this.applyWindowsJumpListRemovals();
}
reopenProjectCommand(e) {
if (e.detail != null && e.detail.index != null) {
this.open(this.projects[e.detail.index].paths);
} else {
this.createReopenProjectListView();
}
}
createReopenProjectListView() {
if (this.reopenProjectListView == null) {
const ReopenProjectListView = require('./reopen-project-list-view');
this.reopenProjectListView = new ReopenProjectListView(paths => {
if (paths != null) {
this.open(paths);
}
});
}
this.reopenProjectListView.toggle();
}
update() {
this.disposeProjectMenu();
this.projects = this.historyManager
.getProjects()
.slice(0, this.config.get('core.reopenProjectMenuCount'));
const newMenu = ReopenProjectMenuManager.createProjectsMenu(this.projects);
this.lastProjectMenu = this.menuManager.add([newMenu]);
this.updateWindowsJumpList();
}
static taskDescription(paths) {
const description = paths
.map(path => `${ReopenProjectMenuManager.betterBaseName(path)} (${path})`)
.join(' ');
if (description.length > TASK_DESCRIPTION_MAX_LENGTH) {
return description.substring(0, TASK_DESCRIPTION_MAX_LENGTH - 3) + "..."
} else {
return description;
}
}
// Windows users can right-click Pulsar taskbar and remove project from the jump list.
// We have to honor that or the group stops working. As we only get a partial list
// each time we remove them from history entirely.
async applyWindowsJumpListRemovals() {
if (process.platform !== 'win32') return;
if (this.app === undefined) {
this.app = require('electron').remote.app;
}
const removed = this.app
.getJumpListSettings()
.removedItems.map(i => i.description);
if (removed.length === 0) return;
for (let project of this.historyManager.getProjects()) {
if (
removed.includes(
ReopenProjectMenuManager.taskDescription(project.paths)
)
) {
await this.historyManager.removeProject(project.paths);
}
}
}
updateWindowsJumpList() {
if (process.platform !== 'win32') return;
if (this.app === undefined) {
this.app = require('electron').remote.app;
}
this.app.setJumpList([
{
type: 'custom',
name: 'Recent Projects',
items: this.projects.map(project => ({
type: 'task',
title: project.paths
.map(ReopenProjectMenuManager.betterBaseName)
.join(', '),
description: ReopenProjectMenuManager.taskDescription(project.paths),
program: process.execPath,
args: project.paths.map(path => `"${path}"`).join(' '),
iconPath: path.join(
path.dirname(process.execPath),
'resources',
'cli',
'folder.ico'
),
iconIndex: 0
}))
},
{ type: 'recent' },
{
items: [
{
type: 'task',
title: 'New Window',
program: process.execPath,
args: '--new-window',
description: 'Opens a new Pulsar window'
}
]
}
]);
}
dispose() {
this.subscriptions.dispose();
this.disposeProjectMenu();
if (this.reopenProjectListView != null) {
this.reopenProjectListView.dispose();
}
}
disposeProjectMenu() {
if (this.lastProjectMenu) {
this.lastProjectMenu.dispose();
this.lastProjectMenu = null;
}
}
static createProjectsMenu(projects) {
return {
label: 'File',
id: 'File',
submenu: [
{
label: 'Reopen Project',
id: 'Reopen Project',
submenu: projects.map((project, index) => ({
label: this.createLabel(project),
command: 'application:reopen-project',
commandDetail: { index: index, paths: project.paths }
}))
}
]
};
}
static createLabel(project) {
return project.paths.length === 1
? project.paths[0]
: project.paths.map(this.betterBaseName).join(', ');
}
static betterBaseName(directory) {
// Handles Windows roots better than path.basename which returns '' for 'd:' and 'd:\'
const match = directory.match(/^([a-z]:)[\\]?$/i);
return match ? match[1] + '\\' : path.basename(directory);
}
};