/
BisqApp.java
442 lines (384 loc) · 18.2 KB
/
BisqApp.java
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.desktop.app;
import bisq.desktop.common.view.CachingViewLoader;
import bisq.desktop.common.view.View;
import bisq.desktop.common.view.ViewLoader;
import bisq.desktop.components.AutoTooltipLabel;
import bisq.desktop.main.MainView;
import bisq.desktop.main.debug.DebugView;
import bisq.desktop.main.overlays.popups.Popup;
import bisq.desktop.main.overlays.windows.EmptyWalletWindow;
import bisq.desktop.main.overlays.windows.FilterWindow;
import bisq.desktop.main.overlays.windows.ManualPayoutTxWindow;
import bisq.desktop.main.overlays.windows.SendAlertMessageWindow;
import bisq.desktop.main.overlays.windows.ShowWalletDataWindow;
import bisq.desktop.util.ImageUtil;
import bisq.core.alert.AlertManager;
import bisq.core.app.AppOptionKeys;
import bisq.core.app.AvoidStandbyModeService;
import bisq.core.app.BisqEnvironment;
import bisq.core.btc.wallet.BtcWalletService;
import bisq.core.btc.wallet.WalletsManager;
import bisq.core.dao.governance.voteresult.MissingDataRequestService;
import bisq.core.filter.FilterManager;
import bisq.core.locale.Res;
import bisq.core.offer.OpenOfferManager;
import bisq.core.user.Preferences;
import bisq.common.UserThread;
import bisq.common.app.DevEnv;
import bisq.common.app.Log;
import bisq.common.setup.GracefulShutDownHandler;
import bisq.common.setup.UncaughtExceptionHandler;
import bisq.common.util.Profiler;
import bisq.common.util.Utilities;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.name.Names;
import org.reactfx.EventStreams;
import javafx.application.Application;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import static bisq.desktop.util.Layout.INITIAL_WINDOW_HEIGHT;
import static bisq.desktop.util.Layout.INITIAL_WINDOW_WIDTH;
import static bisq.desktop.util.Layout.MIN_WINDOW_HEIGHT;
import static bisq.desktop.util.Layout.MIN_WINDOW_WIDTH;
@Slf4j
public class BisqApp extends Application implements UncaughtExceptionHandler {
private static final long LOG_MEMORY_PERIOD_MIN = 10;
@Setter
private static Consumer<Application> appLaunchedHandler;
@Getter
private static Runnable shutDownHandler;
@Setter
private Injector injector;
@Setter
private GracefulShutDownHandler gracefulShutDownHandler;
private Stage stage;
private boolean popupOpened;
private Scene scene;
private boolean shutDownRequested;
public BisqApp() {
shutDownHandler = this::stop;
}
///////////////////////////////////////////////////////////////////////////////////////////
// JavaFx Application implementation
///////////////////////////////////////////////////////////////////////////////////////////
// NOTE: This method is not called on the JavaFX Application Thread.
@Override
public void init() {
}
@Override
public void start(Stage stage) {
this.stage = stage;
appLaunchedHandler.accept(this);
}
public void startApplication(Runnable onUiReadyHandler) {
try {
MainView mainView = loadMainView(injector);
mainView.setOnUiReadyHandler(onUiReadyHandler);
scene = createAndConfigScene(mainView, injector);
setupStage(scene);
injector.getInstance(AvoidStandbyModeService.class).init();
UserThread.runPeriodically(() -> Profiler.printSystemLoad(log), LOG_MEMORY_PERIOD_MIN, TimeUnit.MINUTES);
} catch (Throwable throwable) {
log.error("Error during app init", throwable);
handleUncaughtException(throwable, false);
}
}
@Override
public void stop() {
if (!shutDownRequested) {
new Popup<>().headLine(Res.get("popup.shutDownInProgress.headline"))
.backgroundInfo(Res.get("popup.shutDownInProgress.msg"))
.hideCloseButton()
.useAnimation(false)
.show();
UserThread.runAfter(() -> {
gracefulShutDownHandler.gracefulShutDown(() -> {
log.debug("App shutdown complete");
});
}, 200, TimeUnit.MILLISECONDS);
shutDownRequested = true;
}
}
///////////////////////////////////////////////////////////////////////////////////////////
// UncaughtExceptionHandler implementation
///////////////////////////////////////////////////////////////////////////////////////////
@Override
public void handleUncaughtException(Throwable throwable, boolean doShutDown) {
if (!shutDownRequested) {
if (scene == null) {
log.warn("Scene not available yet, we create a new scene. The bug might be caused by an exception in a constructor or by a circular dependency in Guice. throwable=" + throwable.toString());
scene = new Scene(new StackPane(), 1000, 650);
scene.getStylesheets().setAll(
"/bisq/desktop/bisq.css",
"/bisq/desktop/images.css");
stage.setScene(scene);
stage.show();
}
try {
try {
if (!popupOpened) {
String message = throwable.getMessage();
popupOpened = true;
if (message != null)
new Popup<>().error(message).onClose(() -> popupOpened = false).show();
else
new Popup<>().error(throwable.toString()).onClose(() -> popupOpened = false).show();
}
} catch (Throwable throwable3) {
log.error("Error at displaying Throwable.");
throwable3.printStackTrace();
}
if (doShutDown)
stop();
} catch (Throwable throwable2) {
// If printStackTrace cause a further exception we don't pass the throwable to the Popup.
log.error(throwable2.toString());
if (doShutDown)
stop();
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////
// Private
///////////////////////////////////////////////////////////////////////////////////////////
private Scene createAndConfigScene(MainView mainView, Injector injector) {
Rectangle maxWindowBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
Scene scene = new Scene(mainView.getRoot(),
maxWindowBounds.width < INITIAL_WINDOW_WIDTH ?
(maxWindowBounds.width < MIN_WINDOW_WIDTH ? MIN_WINDOW_WIDTH : maxWindowBounds.width) :
INITIAL_WINDOW_WIDTH,
maxWindowBounds.height < INITIAL_WINDOW_HEIGHT ?
(maxWindowBounds.height < MIN_WINDOW_HEIGHT ? MIN_WINDOW_HEIGHT : maxWindowBounds.height) :
INITIAL_WINDOW_HEIGHT);
addSceneKeyEventHandler(scene, injector);
loadSceneStyles(scene, injector);
injector.getInstance(Preferences.class).getUseDarkThemeProperty().addListener((ov) -> {
loadSceneStyles(scene, injector);
});
return scene;
}
private void setupStage(Scene scene) {
// configure the system tray
SystemTray.create(stage, shutDownHandler);
stage.setOnCloseRequest(event -> {
event.consume();
shutDownByUser();
});
// configure the primary stage
String appName = injector.getInstance(Key.get(String.class, Names.named(AppOptionKeys.APP_NAME_KEY)));
if (BisqEnvironment.getBaseCurrencyNetwork().isTestnet())
appName += " [TESTNET]";
else if (BisqEnvironment.getBaseCurrencyNetwork().isRegtest())
appName += " [REGTEST]";
stage.setTitle(appName);
stage.setScene(scene);
stage.setMinWidth(MIN_WINDOW_WIDTH);
stage.setMinHeight(MIN_WINDOW_HEIGHT);
// on Windows the title icon is also used as task bar icon in a larger size
// on Linux no title icon is supported but also a large task bar icon is derived from that title icon
String iconPath;
if (Utilities.isOSX())
iconPath = ImageUtil.isRetina() ? "/images/window_icon@2x.png" : "/images/window_icon.png";
else if (Utilities.isWindows())
iconPath = "/images/task_bar_icon_windows.png";
else
iconPath = "/images/task_bar_icon_linux.png";
stage.getIcons().add(new Image(getClass().getResourceAsStream(iconPath)));
// make the UI visible
stage.show();
}
private MainView loadMainView(Injector injector) {
CachingViewLoader viewLoader = injector.getInstance(CachingViewLoader.class);
return (MainView) viewLoader.load(MainView.class);
}
private void addSceneKeyEventHandler(Scene scene, Injector injector) {
scene.addEventHandler(KeyEvent.KEY_RELEASED, keyEvent -> {
if (Utilities.isCtrlPressed(KeyCode.W, keyEvent) ||
Utilities.isCtrlPressed(KeyCode.Q, keyEvent)) {
shutDownByUser();
} else {
if (Utilities.isAltOrCtrlPressed(KeyCode.E, keyEvent)) {
showBtcEmergencyWalletPopup(injector);
} else if (Utilities.isAltOrCtrlPressed(KeyCode.B, keyEvent)) {
showBsqEmergencyWalletPopup(injector);
} else if (Utilities.isAltOrCtrlPressed(KeyCode.M, keyEvent)) {
showSendAlertMessagePopup(injector);
} else if (Utilities.isAltOrCtrlPressed(KeyCode.F, keyEvent)) {
showFilterPopup(injector);
} else if (Utilities.isAltOrCtrlPressed(KeyCode.UP, keyEvent)) {
log.warn("We re-published all proposalPayloads and blindVotePayloads to the P2P network.");
injector.getInstance(MissingDataRequestService.class).reRepublishAllGovernanceData();
} else if (Utilities.isAltOrCtrlPressed(KeyCode.T, keyEvent)) {
// Toggle between show tor logs and only show warnings. Helpful in case of connection problems
String pattern = "org.berndpruenster.netlayer";
Level logLevel = ((Logger) LoggerFactory.getLogger(pattern)).getLevel();
if (logLevel != Level.DEBUG) {
log.info("Set log level for org.berndpruenster.netlayer classes to DEBUG");
Log.setCustomLogLevel(pattern, Level.DEBUG);
} else {
log.info("Set log level for org.berndpruenster.netlayer classes to WARN");
Log.setCustomLogLevel(pattern, Level.WARN);
}
} else if (Utilities.isAltOrCtrlPressed(KeyCode.J, keyEvent)) {
WalletsManager walletsManager = injector.getInstance(WalletsManager.class);
if (walletsManager.areWalletsAvailable())
new ShowWalletDataWindow(walletsManager).show();
else
new Popup<>().warning(Res.get("popup.warning.walletNotInitialized")).show();
} else if (Utilities.isAltOrCtrlPressed(KeyCode.G, keyEvent)) {
if (injector.getInstance(BtcWalletService.class).isWalletReady())
injector.getInstance(ManualPayoutTxWindow.class).show();
else
new Popup<>().warning(Res.get("popup.warning.walletNotInitialized")).show();
} else if (DevEnv.isDevMode()) {
// dev ode only
if (Utilities.isAltOrCtrlPressed(KeyCode.P, keyEvent)) {
showFPSWindow(scene);
} else if (Utilities.isAltOrCtrlPressed(KeyCode.Z, keyEvent)) {
showDebugWindow(scene, injector);
} else if (Utilities.isAltOrCtrlPressed(KeyCode.D, keyEvent)) {
toggleDarkTheme(scene, injector);
}
}
}
});
}
private void toggleDarkTheme(Scene scene, Injector injector) {
boolean useDarkTheme = ! injector.getInstance(Preferences.class).isUseDarkTheme();
injector.getInstance(Preferences.class).setUseDarkTheme(useDarkTheme);
loadSceneStyles(scene, injector);
}
private void loadSceneStyles(Scene scene, Injector injector) {
Boolean useDarkTheme = injector.getInstance(Preferences.class).isUseDarkTheme();
String themeCss = useDarkTheme ? "theme-dark.css" : "theme-light.css";
String cssPath = "/bisq/desktop/styles/";
// String cssBaseDir = "/home/peter/Work/bisq/bisq/desktop/build/resources/main/bisq/desktop/styles/";
//
// scene.getStylesheets().setAll(
// "file://" + cssBaseDir + themeCss,
// "file://" + cssBaseDir + "bisq.css");
scene.getStylesheets().setAll(
cssPath + themeCss,
cssPath + "bisq.css"
);
}
private void shutDownByUser() {
if (injector.getInstance(OpenOfferManager.class).getObservableList().isEmpty()) {
// No open offers, so no need to show the popup.
stop();
return;
}
// We show a popup to inform user that open offers will be removed if Bisq is not running.
String key = "showOpenOfferWarnPopupAtShutDown";
if (injector.getInstance(Preferences.class).showAgain(key) && !DevEnv.isDevMode()) {
new Popup<>().information(Res.get("popup.info.shutDownWithOpenOffers"))
.dontShowAgainId(key)
.useShutDownButton()
.closeButtonText(Res.get("shared.cancel"))
.show();
} else {
stop();
}
}
private void showSendAlertMessagePopup(Injector injector) {
AlertManager alertManager = injector.getInstance(AlertManager.class);
boolean useDevPrivilegeKeys = injector.getInstance(Key.get(Boolean.class, Names.named(AppOptionKeys.USE_DEV_PRIVILEGE_KEYS)));
new SendAlertMessageWindow(useDevPrivilegeKeys)
.onAddAlertMessage(alertManager::addAlertMessageIfKeyIsValid)
.onRemoveAlertMessage(alertManager::removeAlertMessageIfKeyIsValid)
.show();
}
private void showFilterPopup(Injector injector) {
FilterManager filterManager = injector.getInstance(FilterManager.class);
boolean useDevPrivilegeKeys = injector.getInstance(Key.get(Boolean.class, Names.named(AppOptionKeys.USE_DEV_PRIVILEGE_KEYS)));
new FilterWindow(filterManager, useDevPrivilegeKeys)
.onAddFilter(filterManager::addFilterMessageIfKeyIsValid)
.onRemoveFilter(filterManager::removeFilterMessageIfKeyIsValid)
.show();
}
private void showBtcEmergencyWalletPopup(Injector injector) {
EmptyWalletWindow emptyWalletWindow = injector.getInstance(EmptyWalletWindow.class);
emptyWalletWindow.setIsBtc(true);
emptyWalletWindow.show();
}
private void showBsqEmergencyWalletPopup(Injector injector) {
EmptyWalletWindow emptyWalletWindow = injector.getInstance(EmptyWalletWindow.class);
emptyWalletWindow.setIsBtc(false);
emptyWalletWindow.show();
}
// Used for debugging trade process
private void showDebugWindow(Scene scene, Injector injector) {
ViewLoader viewLoader = injector.getInstance(ViewLoader.class);
View debugView = viewLoader.load(DebugView.class);
Parent parent = (Parent) debugView.getRoot();
Stage stage = new Stage();
stage.setScene(new Scene(parent));
stage.setTitle("Debug window"); // Don't translate, just for dev
stage.initModality(Modality.NONE);
stage.initStyle(StageStyle.UTILITY);
stage.initOwner(scene.getWindow());
stage.setX(this.stage.getX() + this.stage.getWidth() + 10);
stage.setY(this.stage.getY());
stage.show();
}
private void showFPSWindow(Scene scene) {
Label label = new AutoTooltipLabel();
EventStreams.animationTicks()
.latestN(100)
.map(ticks -> {
int n = ticks.size() - 1;
return n * 1_000_000_000.0 / (ticks.get(n) - ticks.get(0));
})
.map(d -> String.format("FPS: %.3f", d)) // Don't translate, just for dev
.feedTo(label.textProperty());
Pane root = new StackPane();
root.getChildren().add(label);
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.setTitle("FPS"); // Don't translate, just for dev
stage.initModality(Modality.NONE);
stage.initStyle(StageStyle.UTILITY);
stage.initOwner(scene.getWindow());
stage.setX(this.stage.getX() + this.stage.getWidth() + 10);
stage.setY(this.stage.getY());
stage.setWidth(200);
stage.setHeight(100);
stage.show();
}
}