-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.component.ts
120 lines (110 loc) · 4.58 KB
/
app.component.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
import { Component, ElementRef, HostListener, OnInit } from '@angular/core';
import { MatIconRegistry } from '@angular/material';
import { DomSanitizer } from '@angular/platform-browser';
import { OverlayContainer } from '@angular/cdk/overlay';
import { ProjectService } from './project/project.service';
import { Project } from './project/project.model';
import { ChromeExtensionInterfaceService } from './core/chrome-extension-interface/chrome-extension-interface.service';
import { ShortcutService } from './core/shortcut/shortcut.service';
import { ConfigService } from './core/config/config.service';
import { blendInOutAnimation } from './ui/animations/blend-in-out.ani';
import { LayoutService } from './core/layout/layout.service';
import { ElectronService } from 'ngx-electron';
import { IPC_APP_READY, IPC_ERROR, IPC_TRANSFER_SETTINGS_REQUESTED, IPC_TRANSFER_SETTINGS_TO_ELECTRON } from '../ipc-events.const';
import { SnackService } from './core/snack/snack.service';
import { IS_ELECTRON } from './app.constants';
import { GoogleDriveSyncService } from './core/google/google-drive-sync.service';
import { SwUpdate } from '@angular/service-worker';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
animations: [blendInOutAnimation]
})
export class AppComponent implements OnInit {
private _currentTheme: string;
constructor(
private _configService: ConfigService,
private _shortcutService: ShortcutService,
private _matIconRegistry: MatIconRegistry,
private _domSanitizer: DomSanitizer,
private _overlayContainer: OverlayContainer,
private _projectService: ProjectService,
private _electronService: ElectronService,
private _googleDriveSyncService: GoogleDriveSyncService,
private _snackService: SnackService,
private _chromeExtensionInterface: ChromeExtensionInterfaceService,
private _swUpdate: SwUpdate,
private _el: ElementRef,
public layoutService: LayoutService,
) {
this._matIconRegistry.addSvgIcon(
`sp`,
this._domSanitizer.bypassSecurityTrustResourceUrl(`assets/icons/sp.svg`)
);
// INIT Services and global handlers
this._googleDriveSyncService.init();
this._chromeExtensionInterface.init();
if (IS_ELECTRON) {
this._electronService.ipcRenderer.send(IPC_APP_READY);
this._initElectronErrorHandler();
this._electronService.ipcRenderer.on(IPC_TRANSFER_SETTINGS_REQUESTED, () => {
this._electronService.ipcRenderer.send(IPC_TRANSFER_SETTINGS_TO_ELECTRON, this._configService.cfg);
});
} else {
if (this._swUpdate.isEnabled) {
this._swUpdate.available.subscribe(() => {
if (confirm('New version available. Load New Version?')) {
window.location.reload();
}
});
}
window.addEventListener('beforeunload', (e) => {
if (this._configService.cfg.misc.isConfirmBeforeExit) {
e.preventDefault();
e.returnValue = '';
}
});
}
}
@HostListener('document:keydown', ['$event']) onKeyDown(ev: KeyboardEvent) {
this._shortcutService.handleKeyDown(ev);
}
ngOnInit() {
this._projectService.currentProject$.subscribe((currentProject: Project) => {
this._setTheme(currentProject.isDarkTheme, currentProject.themeColor);
});
}
private _setTheme(isDarkTheme: boolean, theme: string) {
if (this._currentTheme) {
this._overlayContainer.getContainerElement().classList.remove(this._currentTheme);
this._el.nativeElement.classList.remove(this._currentTheme);
}
this._overlayContainer.getContainerElement().classList.add(theme);
this._el.nativeElement.classList.add(theme);
if (isDarkTheme) {
this._el.nativeElement.classList.remove('isLightTheme');
this._overlayContainer.getContainerElement().classList.remove('isLightTheme');
this._el.nativeElement.classList.add('isDarkTheme');
this._overlayContainer.getContainerElement().classList.add('isDarkTheme');
} else {
this._el.nativeElement.classList.remove('isDarkTheme');
this._overlayContainer.getContainerElement().classList.remove('isDarkTheme');
this._el.nativeElement.classList.add('isLightTheme');
this._overlayContainer.getContainerElement().classList.add('isLightTheme');
}
this._currentTheme = theme;
}
private _initElectronErrorHandler() {
this._electronService.ipcRenderer.on(IPC_ERROR, (ev, data: {
error: string,
stack: any,
}) => {
this._snackService.open({
message: data.error,
type: 'ERROR'
});
console.error(data);
});
}
}