/
configStore.ts
214 lines (185 loc) · 7.5 KB
/
configStore.ts
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import Store from 'electron-store';
// eslint-disable-next-line import/no-extraneous-dependencies
import electron from 'electron';
import { join, dirname } from 'path';
import { pathExists } from 'fs-extra';
import { KeyBinding, Config } from '../../types.js';
import logger from './logger.js';
import { isWindows } from './util.js';
const { app } = electron;
const defaultKeyBindings: KeyBinding[] = [
{ keys: 'plus', action: 'addSegment' },
{ keys: 'space', action: 'togglePlayResetSpeed' },
{ keys: 'k', action: 'togglePlayNoResetSpeed' },
{ keys: 'j', action: 'reducePlaybackRate' },
{ keys: 'shift+j', action: 'reducePlaybackRateMore' },
{ keys: 'l', action: 'increasePlaybackRate' },
{ keys: 'shift+l', action: 'increasePlaybackRateMore' },
{ keys: 'z', action: 'timelineToggleComfortZoom' },
{ keys: ',', action: 'seekPreviousFrame' },
{ keys: '.', action: 'seekNextFrame' },
{ keys: 'c', action: 'captureSnapshot' },
{ keys: 'i', action: 'setCutStart' },
{ keys: 'o', action: 'setCutEnd' },
{ keys: 'backspace', action: 'removeCurrentSegment' },
{ keys: 'd', action: 'cleanupFilesDialog' },
{ keys: 'b', action: 'splitCurrentSegment' },
{ keys: 'r', action: 'increaseRotation' },
{ keys: 'g', action: 'goToTimecode' },
{ keys: 'left', action: 'seekBackwards' },
{ keys: 'ctrl+left', action: 'seekBackwardsPercent' },
{ keys: 'command+left', action: 'seekBackwardsPercent' },
{ keys: 'alt+left', action: 'seekBackwardsKeyframe' },
{ keys: 'shift+left', action: 'jumpCutStart' },
{ keys: 'right', action: 'seekForwards' },
{ keys: 'ctrl+right', action: 'seekForwardsPercent' },
{ keys: 'command+right', action: 'seekForwardsPercent' },
{ keys: 'alt+right', action: 'seekForwardsKeyframe' },
{ keys: 'shift+right', action: 'jumpCutEnd' },
{ keys: 'ctrl+home', action: 'jumpTimelineStart' },
{ keys: 'ctrl+end', action: 'jumpTimelineEnd' },
{ keys: 'pageup', action: 'jumpFirstSegment' },
{ keys: 'up', action: 'jumpPrevSegment' },
{ keys: 'ctrl+up', action: 'timelineZoomIn' },
{ keys: 'command+up', action: 'timelineZoomIn' },
{ keys: 'shift+up', action: 'batchPreviousFile' },
{ keys: 'ctrl+shift+up', action: 'batchOpenPreviousFile' },
{ keys: 'pagedown', action: 'jumpLastSegment' },
{ keys: 'down', action: 'jumpNextSegment' },
{ keys: 'ctrl+down', action: 'timelineZoomOut' },
{ keys: 'command+down', action: 'timelineZoomOut' },
{ keys: 'shift+down', action: 'batchNextFile' },
{ keys: 'ctrl+shift+down', action: 'batchOpenNextFile' },
{ keys: 'shift+enter', action: 'batchOpenSelectedFile' },
// https://github.com/mifi/lossless-cut/issues/610
{ keys: 'ctrl+z', action: 'undo' },
{ keys: 'command+z', action: 'undo' },
{ keys: 'ctrl+shift+z', action: 'redo' },
{ keys: 'command+shift+z', action: 'redo' },
{ keys: 'ctrl+c', action: 'copySegmentsToClipboard' },
{ keys: 'command+c', action: 'copySegmentsToClipboard' },
{ keys: 'f', action: 'toggleFullscreenVideo' },
{ keys: 'enter', action: 'labelCurrentSegment' },
{ keys: 'e', action: 'export' },
{ keys: 'shift+/', action: 'toggleKeyboardShortcuts' },
{ keys: 'escape', action: 'closeActiveScreen' },
{ keys: 'alt+up', action: 'increaseVolume' },
{ keys: 'alt+down', action: 'decreaseVolume' },
];
const defaults: Config = {
captureFormat: 'jpeg',
customOutDir: undefined,
keyframeCut: true,
autoMerge: false,
autoDeleteMergedSegments: true,
segmentsToChaptersOnly: false,
enableSmartCut: false,
timecodeFormat: 'timecodeWithDecimalFraction',
invertCutSegments: false,
autoExportExtraStreams: true,
exportConfirmEnabled: true,
askBeforeClose: false,
enableAskForImportChapters: true,
enableAskForFileOpenAction: true,
playbackVolume: 1,
autoSaveProjectFile: true,
wheelSensitivity: 0.2,
language: undefined,
ffmpegExperimental: false,
preserveMovData: false,
movFastStart: true,
avoidNegativeTs: 'make_zero',
hideNotifications: undefined,
autoLoadTimecode: false,
segmentsToChapters: false,
preserveMetadataOnMerge: false,
simpleMode: true,
outSegTemplate: undefined,
keyboardSeekAccFactor: 1.03,
keyboardNormalSeekSpeed: 1,
treatInputFileModifiedTimeAsStart: true,
treatOutputFileModifiedTimeAsStart: true,
outFormatLocked: undefined,
safeOutputFileName: true,
windowBounds: undefined,
enableAutoHtml5ify: true,
keyBindings: defaultKeyBindings,
customFfPath: undefined,
storeProjectInWorkingDir: true,
enableOverwriteOutput: true,
mouseWheelZoomModifierKey: 'ctrl',
captureFrameMethod: 'videotag', // we don't default to ffmpeg because ffmpeg might choose a frame slightly off
captureFrameQuality: 0.95,
captureFrameFileNameFormat: 'timestamp',
enableNativeHevc: true,
enableUpdateCheck: true,
cleanupChoices: {
trashTmpFiles: true, askForCleanup: true, closeFile: true, cleanupAfterExport: false,
},
allowMultipleInstances: false,
darkMode: true,
preferStrongColors: false,
outputFileNameMinZeroPadding: 1,
cutFromAdjustmentFrames: 0,
invertTimelineScroll: undefined,
};
// For portable app: https://github.com/mifi/lossless-cut/issues/645
async function getCustomStoragePath() {
try {
if (!isWindows || process.windowsStore) return undefined;
// https://github.com/mifi/lossless-cut/issues/645#issuecomment-1001363314
// https://stackoverflow.com/questions/46307797/how-to-get-the-original-path-of-a-portable-electron-app
// https://github.com/electron-userland/electron-builder/blob/master/docs/configuration/nsis.md
const customStorageDir = process.env['PORTABLE_EXECUTABLE_DIR'] || dirname(app.getPath('exe'));
const customConfigPath = join(customStorageDir, 'config.json');
if (await pathExists(customConfigPath)) return customStorageDir;
return undefined;
} catch (err) {
logger.error('Failed to get custom storage path', err);
return undefined;
}
}
let store: Store;
export function get<T extends keyof Config>(key: T): Config[T] {
return store.get(key);
}
export function set<T extends keyof Config>(key: T, val: Config[T]) {
if (val === undefined) store.delete(key);
else store.set(key, val);
}
export function reset<T extends keyof Config>(key: T) {
set(key, defaults[key]);
}
async function tryCreateStore({ customStoragePath }: { customStoragePath: string | undefined }) {
for (let i = 0; i < 5; i += 1) {
try {
store = new Store({
defaults,
...(customStoragePath != null ? { cwd: customStoragePath } : {}),
});
return;
} catch (err) {
// eslint-disable-next-line no-await-in-loop
await new Promise((r) => setTimeout(r, 2000));
logger.error('Failed to create config store, retrying', err);
}
}
throw new Error('Timed out while creating config store');
}
export async function init() {
const customStoragePath = await getCustomStoragePath();
if (customStoragePath) logger.info('customStoragePath', customStoragePath);
await tryCreateStore({ customStoragePath });
// migrate old configs:
const enableTransferTimestamps = store.get('enableTransferTimestamps'); // todo remove after a while
if (enableTransferTimestamps != null) {
logger.info('Migrating enableTransferTimestamps');
store.delete('enableTransferTimestamps');
set('treatOutputFileModifiedTimeAsStart', enableTransferTimestamps ? true : undefined);
}
const cleanupChoices = store.get('cleanupChoices'); // todo remove after a while
if (cleanupChoices != null && cleanupChoices.closeFile == null) {
logger.info('Migrating cleanupChoices.closeFile');
set('cleanupChoices', { ...cleanupChoices, closeFile: true });
}
}