Debian Codegraph @

src/qml/Main.qml

raw
1 /*
2 * SPDX-FileCopyrightText: 2020 George Florea Bănuș <[email protected]>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
7 pragma ComponentBehavior: Bound
8
9 import QtQuick
10 import QtQuick.Controls
11 import QtQuick.Layouts
12 import QtQuick.Dialogs
13
14 import org.kde.kirigami as Kirigami
15 import org.kde.config as KConfig
16
17 import org.kde.haruna
18 import org.kde.haruna.playlist
19 import org.kde.haruna.utilities
20 import org.kde.haruna.settings
21 import org.kde.haruna.youtube
22
23 ApplicationWindow {
24 id: window
25
26 property bool containsMouse: false
27
28 property int previousVisibility: Window.Windowed
29 property var acceptedSubtitleTypes: ["application/x-subrip", "text/x-ssa", "text/x-microdvd"]
30
31 visible: true
32 title: mpv.mediaTitle || i18nc("@title:window", "Haruna")
33 width: 1000
34 height: 600
35 minimumWidth: 400
36 minimumHeight: 200
37 color: Kirigami.Theme.backgroundColor
38
39 onVisibilityChanged: function(visibility) {
40 if (PlaybackSettings.pauseWhileMinimized) {
41 if (visibility === Window.Minimized) {
42 if (mpv.pause) {
43 mpv.preMinimizePlaybackState = MpvVideo.PlaybackState.Paused
44 } else {
45 mpv.preMinimizePlaybackState = MpvVideo.PlaybackState.Playing
46 }
47 mpv.pause = true
48 }
49 if (previousVisibility === Window.Minimized
50 && visibility === Window.Windowed | Window.Maximized | Window.FullScreen) {
51 if (mpv.preMinimizePlaybackState === MpvVideo.PlaybackState.Playing) {
52 mpv.pause = false
53 }
54 }
55 }
56
57 // used to restore window state, when exiting fullscreen,
58 // to the one it had before going fullscreen
59 if (visibility !== Window.FullScreen) {
60 previousVisibility = visibility
61 }
62 }
63
64 onClosing: {
65 const settingsWindow = settingsLoader.item as Window
66 const sortConfigWindow = advancedSortWindowLoader.item as PlaylistAdvancedSortWindow
67 settingsWindow?.close()
68 sortConfigWindow?.close()
69 }
70
71 header: Header {
72 id: header
73
74 m_mpv: mpv
75 m_menuBarLoader: menuBarLoader
76 m_recentFilesModel: recentFilesModel
77 m_settingsLoader: settingsLoader
78 }
79
80 menuBar: MenuBarLoader {
81 id: menuBarLoader
82
83 m_mpv: mpv
84 m_recentFilesModel: recentFilesModel
85 m_settingsLoader: settingsLoader
86 }
87
88 Connections {
89 target: GeneralSettings
90 function onOsdFontSizeChanged() {
91 osd.message("Test osd font size")
92 }
93 function onMaxRecentFilesChanged() {
94 recentFilesModel.getItems()
95 }
96 function onResizeWindowToVideoChanged() {
97 window.resizeWindow()
98 }
99 function onColorSchemeChanged() {
100 HarunaApp.activateColorScheme(GeneralSettings.colorScheme)
101 }
102 }
103 Connections {
104 target: AudioSettings
105 function onPreferredTrackChanged() {
106 mpv.audioId = AudioSettings.preferredTrack === 0
107 ? "auto"
108 : AudioSettings.preferredTrack
109 }
110 function onPreferredLanguageChanged() {
111 mpv.setProperty(MpvProperties.AudioLanguage, AudioSettings.preferredLanguage.replace(/\s+/g, ''))
112 }
113 function onReplayGainChanged() {
114 mpv.setProperty(MpvProperties.ReplayGain, AudioSettings.replayGain)
115 }
116 function onReplayGainPreampChanged() {
117 mpv.setProperty(MpvProperties.ReplayGainPreamp, AudioSettings.replayGainPreamp)
118 }
119 function onReplayGainPreventClipChanged() {
120 mpv.setProperty(MpvProperties.ReplayGainClip, !AudioSettings.replayGainPreventClip)
121 }
122 function onReplayGainFallbackChanged() {
123 mpv.setProperty(MpvProperties.ReplayGainFallback, AudioSettings.replayGainFallback)
124 }
125 }
126 Connections {
127 target: PlaybackSettings
128 function onYtdlFormatChanged() {
129 mpv.setProperty(MpvProperties.YtdlFormat, PlaybackSettings.ytdlFormat)
130 }
131 function onHWDecodingChanged() {
132 mpv.setProperty(MpvProperties.HardwareDecoding, PlaybackSettings.hWDecoding)
133 }
134 }
135 Connections {
136 target: SubtitlesSettings
137 function onAutoSelectSubtitlesChanged() {
138 mpv.selectSubtitleTrack()
139 }
140 function onPreferredLanguageChanged() {
141 mpv.setProperty(MpvProperties.SubtitleLanguage, SubtitlesSettings.preferredLanguage.replace(/\s+/g, ''))
142 }
143 function onPreferredTrackChanged() {
144 mpv.subtitleId = SubtitlesSettings.preferredTrack === 0
145 ? "auto"
146 : SubtitlesSettings.preferredTrack
147 }
148 function onAllowOnBlackBordersChanged() {
149 mpv.setProperty(MpvProperties.SubtitleUseMargins, SubtitlesSettings.allowOnBlackBorders ? "yes" : "no")
150 mpv.setProperty(MpvProperties.SubtitleAssForceMargins, SubtitlesSettings.allowOnBlackBorders ? "yes" : "no")
151 }
152 function onFontFamilyChanged() {
153 mpv.setProperty(MpvProperties.SubtitleFont, SubtitlesSettings.fontFamily)
154 }
155 function onFontSizeChanged() {
156 mpv.setProperty(MpvProperties.SubtitleFontSize, SubtitlesSettings.fontSize)
157 }
158 function onIsBoldChanged() {
159 mpv.setProperty(MpvProperties.SubtitleBold, SubtitlesSettings.isBold)
160 }
161 function onIsItalicChanged() {
162 mpv.setProperty(MpvProperties.SubtitleItalic, SubtitlesSettings.isItalic)
163 }
164 function onFontColorChanged() {
165 mpv.setProperty(MpvProperties.SubtitleColor, SubtitlesSettings.fontColor)
166 }
167 function onShadowColorChanged() {
168 mpv.setProperty(MpvProperties.SubtitleShadowColor, SubtitlesSettings.shadowColor)
169 }
170 function onShadowOffsetChanged() {
171 mpv.setProperty(MpvProperties.SubtitleShadowOffset, SubtitlesSettings.shadowOffset)
172 }
173 function onBorderColorChanged() {
174 mpv.setProperty(MpvProperties.SubtitleBorderColor, SubtitlesSettings.borderColor)
175 }
176 function onBorderSizeChanged() {
177 mpv.setProperty(MpvProperties.SubtitleBorderSize, SubtitlesSettings.borderSize)
178 }
179 function onBorderStyleChanged() {
180 mpv.setProperty(MpvProperties.SubtitleBorderStyle, SubtitlesSettings.borderStyle)
181 }
182 }
183 Connections {
184 target: VideoSettings
185 function onScreenshotTemplateChanged() {
186 mpv.setProperty(MpvProperties.ScreenshotTemplate, VideoSettings.screenshotTemplate)
187 }
188 function onScreenshotFormatChanged() {
189 mpv.setProperty(MpvProperties.ScreenshotFormat, VideoSettings.screenshotFormat)
190 }
191 }
192
193 Loader {
194 active: false
195 sourceComponent: KConfig.WindowStateSaver {
196 configGroupName: "MainWindow"
197 }
198 Component.onCompleted: active = GeneralSettings.rememberWindowGeometry
199 }
200
201 MpvVideo {
202 id: mpv
203
204 osd: osd
205 mouseActionsModel: mouseActionsModel
206
207 width: window.contentItem.width
208 height: window.isFullScreen()
209 ? window.contentItem.height
210 : window.contentItem.height - (footer.isFloating ? 0 : footer.height)
211 anchors.left: PlaylistSettings.overlayVideo
212 ? window.contentItem.left
213 : (PlaylistSettings.position === "left" ? playlist.right : window.contentItem.left)
214 anchors.right: PlaylistSettings.overlayVideo
215 ? window.contentItem.right
216 : (PlaylistSettings.position === "right" ? playlist.left : window.contentItem.right)
217 anchors.top: window.contentItem.top
218
219 onVideoReconfig: {
220 window.resizeWindow()
221 }
222
223 onAddToRecentFiles: function(url, openedFrom, name) {
224 recentFilesModel.addRecentFile(url, openedFrom, name)
225 }
226
227 Osd {
228 id: osd
229
230 active: mpv.isReady
231 maxWidth: mpv.width
232 }
233
234 SelectActionPopup {
235 id: triggerActionPopup
236
237 onActionSelected: function(actionName) {
238 HarunaApp.actions[actionName].trigger()
239 }
240 }
241 }
242
243 // extra space outside the playlist so that the playlist is not closed
244 // when the mouse leaves it by mistake (dragging items, resizing the playlist)
245 Item {
246 // when window width is very small, there is only 50 pixels not covered by the playlist
247 // in that case the extra space is reduced to 30 to allow the playlist to be closed with the mouse
248 width: playlist.width >= Window.window.width - 70 ? 30 : 50
249 height: playlist.height
250 anchors.right: PlaylistSettings.position === "right" ? playlist.left : undefined
251 anchors.left: PlaylistSettings.position === "left" ? playlist.right : undefined
252 visible: playlist.visible
253 HoverHandler {}
254 }
255
256 Playlist {
257 id: playlist
258
259 m_mpv: mpv
260 m_advancedSortWindowLoader: advancedSortWindowLoader
261 height: mpv.height
262 mainWindowWidth: window.width
263 fsScale: window.isFullScreen() && PlaylistSettings.bigFontFullscreen ? 1.36 : 1
264
265 Connections {
266 target: actions
267 function onTogglePlaylist() {
268 if (playlist.state === "visible") {
269 playlist.state = "hidden"
270 } else {
271 playlist.state = "visible"
272 }
273 }
274 }
275
276 Connections {
277 target: HarunaApp
278 function onQmlApplicationMouseLeave() {
279 if (PlaylistSettings.canToggleWithMouse && (window.isFullScreen() || window.isMaximized())) {
280 playlist.state = "hidden"
281 }
282 }
283 }
284
285 Connections {
286 target: mpv
287 function onOpenPlaylist() {
288 playlist.state = "visible"
289 }
290 function onClosePlaylist() {
291 playlist.state = "hidden"
292 }
293 }
294 }
295
296 Footer {
297 id: footer
298
299 anchors.bottom: window.contentItem.bottom
300
301 m_mpv: mpv
302 m_menuBarLoader: menuBarLoader
303 m_header: header
304 m_recentFilesModel: recentFilesModel
305 m_settingsLoader: settingsLoader
306 }
307
308 Actions {
309 id: actions
310
311 m_actionsModel: actionsModel
312 m_mpv: mpv
313 m_mpvContextMenuLoader: mpvContextMenuLoader
314 m_osd: osd
315 m_footer: footer
316 m_settingsLoader: settingsLoader
317 m_triggerActionPopup: triggerActionPopup
318 m_openUrlPopup: openUrlPopup
319
320 onOpenFileDialog: fileDialog.open()
321 onOpenSubtitleDialog: subtitlesFileDialog.open()
322 }
323
324 MouseActionsModel {
325 id: mouseActionsModel
326 }
327
328 ActionsModel {
329 id: actionsModel
330 }
331
332 ProxyActionsModel {
333 id: proxyActionsModel
334
335 sourceModel: actionsModel
336 }
337
338 CustomCommandsModel {
339 id: customCommandsModel
340
341 appActionsModel: actionsModel
342 Component.onCompleted: init()
343 }
344
345 RecentFilesModel {
346 id: recentFilesModel
347 }
348
349 RowLayout {
350 width: window.width * 0.8 > Kirigami.Units.gridUnit * 50
351 ? Kirigami.Units.gridUnit * 50
352 : window.width * 0.8
353 anchors.centerIn: parent
354
355 Kirigami.InlineMessage {
356 id: messageBox
357
358 Layout.fillWidth: true
359 Layout.fillHeight: true
360 type: Kirigami.MessageType.Error
361 showCloseButton: true
362
363 Connections {
364 target: MiscUtils
365 function onError(message) {
366 messageBox.visible = true
367 messageBox.text = message
368 }
369 }
370 }
371 }
372
373 MpvContextMenu {
374 id: mpvContextMenuLoader
375
376 m_mpv: mpv
377 }
378
379 SettingsWindow {
380 id: settingsLoader
381
382 m_mpv: mpv
383 m_proxyActionsModel: proxyActionsModel
384 m_customCommandsModel: customCommandsModel
385 m_mouseActionsModel: mouseActionsModel
386 }
387
388 Loader {
389 id: advancedSortWindowLoader
390 active: false
391 asynchronous: true
392 sourceComponent: PlaylistAdvancedSortWindow {
393 m_mpv: mpv
394 }
395
396 function openSortWindow() : void {
397 if (!advancedSortWindowLoader.active) {
398 advancedSortWindowLoader.active = true
399 advancedSortWindowLoader.loaded.connect(function() {
400 advancedSortWindowLoader.openSortWindow()
401 advancedSortWindowLoader.item.calculatePostLayout()
402 })
403 return
404 }
405
406 const advancedSortWindow = advancedSortWindowLoader.item as PlaylistAdvancedSortWindow
407 if (advancedSortWindow.visible) {
408 advancedSortWindow.raise()
409 } else {
410 advancedSortWindow.visible = true
411 }
412 }
413 }
414
415 Connections {
416 target: HarunaApp
417 function onQmlApplicationMouseLeave() {
418 window.containsMouse = false
419 }
420 function onQmlApplicationMouseEnter() {
421 window.containsMouse = true
422 }
423 function onOpenUrl(url) {
424 if (GeneralSettings.appendVideoToSingleInstance) {
425 let behavior = GeneralSettings.playNewFileInSingleInstance
426 ? PlaylistModel.AppendAndPlay
427 : PlaylistModel.Append
428
429 mpv.defaultFilterProxyModel.addItem(url.toString(), behavior)
430 return
431 }
432
433 window.openFile(url, RecentFilesModel.OpenedFrom.ExternalApp)
434 }
435 function onOpenRecentFile(url) {
436 window.openFile(url, RecentFilesModel.OpenedFrom.Other)
437 }
438 }
439
440 FileDialog {
441 id: fileDialog
442
443 title: i18nc("@title:window", "Select File")
444 currentFolder: GeneralSettings.fileDialogLastLocation
445 fileMode: FileDialog.OpenFile
446
447 onAccepted: {
448 window.openFile(fileDialog.selectedFile, RecentFilesModel.OpenedFrom.OpenAction)
449 mpv.focus = true
450
451 GeneralSettings.fileDialogLastLocation = PathUtils.parentUrl(fileDialog.selectedFile)
452 GeneralSettings.save()
453 }
454 onRejected: mpv.focus = true
455 onVisibleChanged: {
456 HarunaApp.actionsEnabled = !visible
457 }
458 }
459
460 FileDialog {
461 id: subtitlesFileDialog
462
463 title: i18nc("@title:window", "Select Subtitles File")
464 currentFolder: PathUtils.parentUrl(mpv.currentUrl)
465 fileMode: FileDialog.OpenFile
466 nameFilters: ["Subtitles (*.srt *.ssa *.ass *.sub)"]
467
468 onAccepted: {
469 if (window.acceptedSubtitleTypes.includes(MiscUtils.mimeType(subtitlesFileDialog.selectedFile))) {
470 mpv.addSubtitles(subtitlesFileDialog.selectedFile)
471 }
472 }
473 onRejected: mpv.focus = true
474 onVisibleChanged: {
475 HarunaApp.actionsEnabled = !visible
476 }
477 }
478
479 YouTube {
480 id: youtube
481 }
482
483 InputPopup {
484 id: openUrlPopup
485
486 x: 10
487 y: 10
488 width: Math.min(window.width * 0.9, 600)
489 lastText: GeneralSettings.lastUrl
490 buttonText: i18nc("@action:button", "Open")
491 warningText: youtube.hasYoutubeDl()
492 ? ""
493 : i18nc("@info", "Neither <a href=\"https://github.com/yt-dlp/yt-dlp\">yt-dlp</a> nor <a href=\"https://github.com/ytdl-org/youtube-dl\">youtube-dl</a> was found.")
494
495 onSubmitted: function(url) {
496 window.openFile(youtube.normalizeUrl(url), RecentFilesModel.OpenedFrom.OpenAction)
497
498 GeneralSettings.lastUrl = url
499 GeneralSettings.save()
500 }
501 }
502
503 Component.onCompleted: {
504 HarunaApp.activateColorScheme(GeneralSettings.colorScheme)
505
506 const hasCommandLineFile = HarunaApp.url(0).toString() !== ""
507 const hasLastPlayedFile = GeneralSettings.lastPlayedFile !== ""
508 const hasFileToOpen = hasCommandLineFile || (PlaybackSettings.openLastPlayedFile && hasLastPlayedFile)
509 if (GeneralSettings.fullscreenOnStartUp && hasFileToOpen) {
510 toggleFullScreen()
511 }
512 }
513
514 function openFile(path: string, openedFrom: int) : void {
515 recentFilesModel.addRecentFile(path, openedFrom)
516 mpv.defaultFilterProxyModel.addItem(path, PlaylistModel.Clear)
517 }
518
519 function isFullScreen() : bool {
520 return window.visibility === Window.FullScreen
521 }
522
523 function isMaximized() : bool {
524 return window.visibility === Window.Maximized
525 }
526
527 function toggleFullScreen() : void {
528 if (!isFullScreen()) {
529 window.showFullScreen()
530 } else {
531 exitFullscreen()
532 }
533 }
534
535 function exitFullscreen() : void {
536 if (window.previousVisibility === Window.Maximized) {
537 window.show()
538 window.showMaximized()
539 } else {
540 window.showNormal()
541 }
542 }
543
544 function resizeWindow() : void {
545 if (SystemUtils.isPlatformWayland() || !GeneralSettings.resizeWindowToVideo || isFullScreen()) {
546 return
547 }
548
549 window.width = mpv.videoWidth
550 window.height = mpv.videoHeight
551 + (footer.isFloating ? 0 : footer.height)
552 + (header.visible ? header.height : 0)
553 + (menuBar.visible ? menuBar.height : 0)
554 }
555 }