@@ -5,18 +5,33 @@ const { app, ipcMain } = require("electron");
5
5
const { setOptions } = require ( "../../config/plugins" ) ;
6
6
const { injectCSS } = require ( "../utils" ) ;
7
7
8
- let isInPiPMode = false ;
8
+ let isInPiP = false ;
9
9
let originalPosition ;
10
10
let originalSize ;
11
+ let originalFullScreen ;
12
+ let originalMaximized ;
11
13
12
- const pipPosition = [ 10 , 10 ] ;
13
- const pipSize = [ 450 , 275 ] ;
14
+ let win ;
15
+ let options ;
14
16
15
- const togglePiP = async ( win ) => {
16
- isInPiPMode = ! isInPiPMode ;
17
- setOptions ( "picture-in-picture" , { isInPiP : isInPiPMode } ) ;
17
+ const pipPosition = ( ) => ( options . savePosition && options [ "pip-position" ] ) || [ 10 , 10 ] ;
18
+ const pipSize = ( ) => ( options . saveSize && options [ "pip-size" ] ) || [ 450 , 275 ] ;
18
19
19
- if ( isInPiPMode ) {
20
+ const setLocalOptions = ( _options ) => {
21
+ options = { ...options , ..._options } ;
22
+ setOptions ( "picture-in-picture" , _options ) ;
23
+ }
24
+
25
+ const togglePiP = async ( ) => {
26
+ isInPiP = ! isInPiP ;
27
+ setLocalOptions ( { isInPiP } ) ;
28
+
29
+ if ( isInPiP ) {
30
+ originalFullScreen = win . isFullScreen ( ) ;
31
+ if ( originalFullScreen ) win . setFullScreen ( false ) ;
32
+ originalMaximized = win . isMaximized ( ) ;
33
+ if ( originalMaximized ) win . unmaximize ( ) ;
34
+
20
35
originalPosition = win . getPosition ( ) ;
21
36
originalSize = win . getSize ( ) ;
22
37
@@ -26,6 +41,13 @@ const togglePiP = async (win) => {
26
41
await win . webContents . executeJavaScript (
27
42
// Go fullscreen
28
43
`
44
+ var exitButton = document.querySelector(".exit-fullscreen-button");
45
+ exitButton.replaceWith(exitButton.cloneNode(true));
46
+ document.querySelector(".exit-fullscreen-button").onclick = () => togglePictureInPicture();
47
+
48
+ var onPlayerDblClick = document.querySelector('#player').onDoubleClick_
49
+ document.querySelector('#player').onDoubleClick_ = () => {};
50
+ document.querySelector('#expanding-menu').onmouseleave = () => document.querySelector('.middle-controls').click();
29
51
if (!document.querySelector("ytmusic-player-page").playerPageOpen_) {
30
52
document.querySelector(".toggle-player-page-button").click();
31
53
}
@@ -47,33 +69,49 @@ const togglePiP = async (win) => {
47
69
await win . webContents . executeJavaScript (
48
70
// Exit fullscreen
49
71
`
72
+ document.querySelector('#player').onDoubleClick_ = onPlayerDblClick;
73
+ document.querySelector('#expanding-menu').onmouseleave = undefined;
74
+ document.querySelector(".exit-fullscreen-button").replaceWith(exitButton);
50
75
document.querySelector(".exit-fullscreen-button").click();
51
76
document.querySelector("ytmusic-player-bar").classList.remove("pip");
52
77
`
53
78
) ;
54
79
55
80
win . setVisibleOnAllWorkspaces ( false ) ;
56
81
win . setAlwaysOnTop ( false ) ;
82
+
83
+ if ( originalFullScreen ) win . setFullScreen ( true ) ;
84
+ if ( originalMaximized ) win . maximize ( ) ;
57
85
}
58
86
59
- const [ x , y ] = isInPiPMode ? pipPosition : originalPosition ;
60
- const [ w , h ] = isInPiPMode ? pipSize : originalSize ;
87
+ const [ x , y ] = isInPiP ? pipPosition ( ) : originalPosition ;
88
+ const [ w , h ] = isInPiP ? pipSize ( ) : originalSize ;
61
89
win . setPosition ( x , y ) ;
62
90
win . setSize ( w , h ) ;
63
91
64
- win . setWindowButtonVisibility ?. ( ! isInPiPMode ) ;
92
+ win . setWindowButtonVisibility ?. ( ! isInPiP ) ;
65
93
} ;
66
94
67
- module . exports = ( win ) => {
95
+ const blockShortcutsInPiP = ( event , input ) => {
96
+ const key = input . key . toLowerCase ( ) ;
97
+
98
+ if ( key === "f" ) {
99
+ event . preventDefault ( ) ;
100
+ } else if ( key === 'escape' ) {
101
+ togglePiP ( ) ;
102
+ event . preventDefault ( ) ;
103
+ } ;
104
+ } ;
105
+
106
+ module . exports = ( _win , _options ) => {
107
+ options ??= _options ;
108
+ win ??= _win ;
109
+ setLocalOptions ( { isInPiP } ) ;
68
110
injectCSS ( win . webContents , path . join ( __dirname , "style.css" ) ) ;
69
111
ipcMain . on ( "picture-in-picture" , async ( ) => {
70
- await togglePiP ( win ) ;
112
+ await togglePiP ( ) ;
71
113
} ) ;
72
114
} ;
73
115
74
- const blockShortcutsInPiP = ( event , input ) => {
75
- const blockedShortcuts = [ "f" , "escape" ] ;
76
- if ( blockedShortcuts . includes ( input . key . toLowerCase ( ) ) ) {
77
- event . preventDefault ( ) ;
78
- }
79
- } ;
116
+ module . exports . setOptions = setLocalOptions ;
117
+
0 commit comments