-
Notifications
You must be signed in to change notification settings - Fork 44
/
Studio.kt
363 lines (340 loc) · 15.7 KB
/
Studio.kt
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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package com.typedb.studio
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.key.KeyEvent
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.type
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.ApplicationScope
import androidx.compose.ui.window.LocalWindowExceptionHandlerFactory
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.WindowExceptionHandler
import androidx.compose.ui.window.WindowExceptionHandlerFactory
import androidx.compose.ui.window.WindowPlacement
import androidx.compose.ui.window.WindowPosition
import androidx.compose.ui.window.rememberWindowState
import com.typedb.studio.framework.common.Context.LocalTitleBarHeight
import com.typedb.studio.framework.common.Context.LocalWindow
import com.typedb.studio.framework.common.Context.LocalWindowContext
import com.typedb.studio.framework.common.KeyMapper
import com.typedb.studio.framework.common.Util.toDP
import com.typedb.studio.framework.common.WindowContext
import com.typedb.studio.framework.common.theme.Theme
import com.typedb.studio.framework.common.theme.Theme.DIALOG_PADDING
import com.typedb.studio.framework.material.Browsers
import com.typedb.studio.framework.material.ConfirmationDialog
import com.typedb.studio.framework.material.Form.RowSpacer
import com.typedb.studio.framework.material.Form.SelectableText
import com.typedb.studio.framework.material.Form.Text
import com.typedb.studio.framework.material.Form.TextButton
import com.typedb.studio.framework.material.Frame
import com.typedb.studio.framework.material.Notifications
import com.typedb.studio.framework.material.Pages
import com.typedb.studio.framework.material.Separator
import com.typedb.studio.module.StatusBar
import com.typedb.studio.module.Toolbar
import com.typedb.studio.module.connection.DatabaseDialog
import com.typedb.studio.module.connection.ServerDialog
import com.typedb.studio.module.preference.PreferenceDialog
import com.typedb.studio.module.project.FilePage
import com.typedb.studio.module.project.ProjectBrowser
import com.typedb.studio.module.project.ProjectDialog
import com.typedb.studio.module.type.TypeBrowser
import com.typedb.studio.module.type.TypeDialog
import com.typedb.studio.module.type.TypeEditor
import com.typedb.studio.module.user.UpdateDefaultPasswordDialog
import com.typedb.studio.resources.version.Version
import com.typedb.studio.service.Service
import com.typedb.studio.service.common.util.Label
import com.typedb.studio.service.common.util.Message
import com.typedb.studio.service.common.util.Sentence
import com.typedb.studio.service.project.FileState
import com.typedb.studio.service.schema.ThingTypeState
import com.vaticle.typedb.common.collection.Either
import io.sentry.Sentry
import io.sentry.protocol.User
import java.awt.Window
import java.awt.event.WindowEvent
import java.net.InetAddress
import java.net.NetworkInterface
import java.nio.ByteBuffer
import java.security.MessageDigest
import java.util.Arrays
import javax.swing.UIManager
import kotlin.system.exitProcess
import kotlinx.coroutines.runBlocking
import mu.KotlinLogging
object Studio {
private const val VATICLE_BOT_ICON = "resources/icons/vaticle/vaticle-bot-32px.png"
private val ERROR_WINDOW_WIDTH = 1000.dp
private val ERROR_WINDOW_HEIGHT = 610.dp
private val ERROR_WINDOW_CONTENT_PADDING = 10.dp
private val ERROR_WINDOW_LABEL_WIDTH = 40.dp
private val LOGGER = KotlinLogging.logger {}
private var error: Throwable? by mutableStateOf(null)
private var quit: Boolean by mutableStateOf(false)
private val browsers = listOf(
ProjectBrowser(true, 1),
TypeBrowser(true, 2),
// RuleBrowser(false, 3),
// UserBrowser(false, 4),
// RoleBrowser(false, 5),
)
@OptIn(ExperimentalComposeUiApi::class)
private object ExceptionHandler : WindowExceptionHandlerFactory {
override fun exceptionHandler(window: Window) = WindowExceptionHandler {
error = it
window.dispatchEvent(WindowEvent(window, WindowEvent.WINDOW_CLOSING))
throw it
}
}
@Composable
private fun MainWindow(exitApplicationFn: () -> Unit) {
fun confirmClose() = Service.confirmation.submit(
title = Label.CONFIRM_QUITTING_APPLICATION,
message = Sentence.CONFIRM_QUITING_APPLICATION,
onConfirm = { quit = true; exitApplicationFn() }
// TODO: we don't want to call exitApplication() for MacOS
)
// TODO: we want no title bar, by passing undecorated=true, but it seems to cause intermittent crashes on startup
// (see #40). Test if they occur when running the distribution, or only with bazel run :studio-bin-*
Window(
title = getMainWindowTitle(),
state = rememberWindowState(WindowPlacement.Maximized),
icon = painterResource(VATICLE_BOT_ICON),
onPreviewKeyEvent = { handlePreviewKeyEvent(it, ::confirmClose) },
onKeyEvent = { handleKeyEvent(it) },
onCloseRequest = { if (error != null) exitApplicationFn() else confirmClose() },
) {
CompositionLocalProvider(LocalWindow provides window) {
val windowCtx = WindowContext.Compose(window)
MainWindowContent(windowCtx)
CompositionLocalProvider(LocalWindowContext provides windowCtx) {
Notifications.MayShowPopup()
ConfirmationDialog.MayShowDialog()
ServerDialog.MayShowDialogs()
DatabaseDialog.MayShowDialogs()
PreferenceDialog.MayShowDialogs()
ProjectDialog.MayShowDialogs(window)
TypeDialog.MayShowDialogs()
UpdateDefaultPasswordDialog.MayShowDialogs()
}
}
}
}
@Composable
private fun MainWindowContent(windowCtx: WindowContext) {
var titleBarHeight by remember { mutableStateOf(0.dp) }
val density = LocalDensity.current.density
Column(Modifier.fillMaxSize().background(Theme.studio.backgroundMedium).onGloballyPositioned {
titleBarHeight = windowCtx.height.dp - toDP(it.size.height, density)
}) {
CompositionLocalProvider(LocalWindowContext provides windowCtx) {
CompositionLocalProvider(LocalTitleBarHeight provides titleBarHeight) {
Toolbar.Layout()
Separator.Horizontal()
Frame.Row(
modifier = Modifier.fillMaxWidth().weight(1f),
separator = Frame.SeparatorArgs(Separator.WEIGHT),
Frame.Pane(
id = Browsers.javaClass.name,
minSize = Browsers.MIN_WIDTH,
initSize = Either.first(Browsers.DEFAULT_WIDTH)
) { Browsers.Layout(browsers, it, Browsers.Position.LEFT) },
Frame.Pane(
id = Pages.javaClass.name,
minSize = Pages.MIN_WIDTH,
initSize = Either.second(1f)
) {
Pages.Layout(enabled = Service.project.current != null) {
when (it) {
is FileState -> FilePage.create(it)
is ThingTypeState<*, *> -> TypeEditor.create(it)
else -> throw IllegalStateException("Unrecognised pageable type")
}
}
}
)
Separator.Horizontal()
StatusBar.Layout()
}
}
}
}
private fun getMainWindowTitle(): String {
val projectName = Service.project.current?.directory?.name
val pageName = Service.pages.active?.windowTitle
return Label.TYPEDB_STUDIO + ((pageName ?: projectName)?.let { " — $it" } ?: "")
}
private fun handlePreviewKeyEvent(event: KeyEvent, onClose: () -> Unit): Boolean = handleEvent(event) {
when (it) {
KeyMapper.Command.QUIT -> {
onClose()
true
}
else -> false
}
}
private fun handleKeyEvent(event: KeyEvent): Boolean = handleEvent(event) {
when (it) {
KeyMapper.Command.NEW_PAGE -> {
Service.project.tryCreateUntitledFile()?.tryOpen()
true
}
else -> false
}
}
private fun handleEvent(event: KeyEvent, function: (KeyMapper.Command) -> Boolean): Boolean {
return if (event.type == KeyEventType.KeyUp) false
else KeyMapper.CURRENT.map(event)?.let { function(it) } ?: false
}
@Composable
private fun ErrorWindow(exception: Throwable, onClose: () -> Unit) {
Window(
title = Label.TYPEDB_STUDIO_APPLICATION_ERROR,
onCloseRequest = { onClose() },
state = rememberWindowState(
placement = WindowPlacement.Floating,
position = WindowPosition.Aligned(Alignment.Center),
size = DpSize(ERROR_WINDOW_WIDTH, ERROR_WINDOW_HEIGHT),
)
) {
val clipboard = LocalClipboardManager.current
val labelModifier = Modifier.width(ERROR_WINDOW_LABEL_WIDTH)
val labelStyle = Theme.typography.body1.copy(fontWeight = FontWeight.Bold)
val contentColor = Theme.studio.errorStroke
val contentModifier = Modifier.fillMaxWidth().border(1.dp, Theme.studio.border)
.background(Theme.studio.backgroundDark).padding(horizontal = ERROR_WINDOW_CONTENT_PADDING)
fun exceptionText(): String =
"${Label.TITLE}: ${exception.message}\n${Label.TRACE}: ${exception.stackTraceToString()}"
CompositionLocalProvider(LocalWindow provides window) {
CompositionLocalProvider(LocalWindowContext provides WindowContext.Compose(window)) {
Column(
modifier = Modifier.fillMaxSize().background(Theme.studio.backgroundMedium)
.padding(DIALOG_PADDING),
verticalArrangement = Arrangement.spacedBy(DIALOG_PADDING)
) {
Row(verticalAlignment = Alignment.CenterVertically) {
Text(value = "${Label.TITLE}:", modifier = labelModifier, textStyle = labelStyle)
exception.message?.let { SelectableText(value = it, color = contentColor) }
}
Row(Modifier.weight(1f)) {
Text(value = "${Label.TRACE}:", modifier = labelModifier, textStyle = labelStyle)
SelectableText(
value = exception.stackTraceToString(), color = contentColor, modifier = contentModifier
)
}
Row {
Spacer(Modifier.weight(1f))
TextButton(text = Label.COPY) { clipboard.setText(AnnotatedString(exceptionText())) }
RowSpacer()
TextButton(text = Label.QUIT) { quit = true; onClose() }
RowSpacer()
TextButton(text = Label.REOPEN, onClick = onClose)
}
}
}
}
}
}
private fun addShutdownHook() {
Runtime.getRuntime().addShutdownHook(object : Thread() {
override fun run(): Unit = runBlocking {
LOGGER.info { Label.CLOSING_TYPEDB_STUDIO }
Service.driver.close()
}
})
}
private fun setConfigurations() {
// Enable anti-aliasing
System.setProperty("awt.useSystemAAFontSettings", "on")
System.setProperty("swing.aatext", "true")
// Enable native Windows UI style
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()) // Set UI style for Windows
}
private fun configureDiagnostics(enabled: Boolean) {
val releaseName = "TypeDB Studio@" + Version.VERSION;
Sentry.init { options ->
options.setDsn("https://9c327cb98a925974587f98adb192a89b@o4506315929812992.ingest.sentry.io/4506355166806016")
options.setEnableTracing(true)
options.setSendDefaultPii(false)
options.setRelease(releaseName)
options.setEnabled(enabled)
}
val user = User();
user.setUsername(userID());
Sentry.setUser(user);
}
private fun userID(): String {
return try {
val mac = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()).hardwareAddress
val macHash = MessageDigest.getInstance("SHA-256").digest(mac)
val truncatedHash = Arrays.copyOfRange(macHash, 0, 8)
String.format("%X", ByteBuffer.wrap(truncatedHash).long)
} catch (e: java.lang.Exception) {
"_0"
}
}
@OptIn(ExperimentalComposeUiApi::class)
private fun application(window: @Composable ApplicationScope.() -> Unit) {
androidx.compose.ui.window.application(exitProcessOnExit = false) {
Theme.Material {
CompositionLocalProvider(
LocalWindowExceptionHandlerFactory provides ExceptionHandler
) { window() }
}
}
}
@JvmStatic
fun main(args: Array<String>) {
try {
addShutdownHook()
setConfigurations()
Message.loadClasses()
Service.data.initialise()
configureDiagnostics(Service.preference.diagnosticsReportingEnabled);
while (!quit) {
application { MainWindow(::exitApplication) }
error?.let { exception ->
Sentry.captureException(exception)
LOGGER.error(exception.message, exception)
application { ErrorWindow(exception) { error = null; exitApplication() } }
}
}
exitProcess(0)
} catch (exception: Exception) {
Sentry.captureException(exception)
LOGGER.error(exception.message, exception)
exitProcess(1)
}
}
}