-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreload.js
More file actions
160 lines (140 loc) · 4.33 KB
/
preload.js
File metadata and controls
160 lines (140 loc) · 4.33 KB
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
const { contextBridge, ipcRenderer } = require('electron');
window.addEventListener('DOMContentLoaded', () => {
// Push content down
const bodyStyle = document.createElement('style');
bodyStyle.textContent = `
body {
margin-top: 32px !important;
}
`;
document.head.appendChild(bodyStyle);
// Create the drag region (always present, fully transparent)
const dragRegion = document.createElement('div');
Object.assign(dragRegion.style, {
position: 'fixed',
top: '0',
left: '0',
right: '0',
height: '32px',
webkitAppRegion: 'drag',
zIndex: '2147483647',
pointerEvents: 'auto', // Changed to auto to detect mouse events
});
// Create the visual indicator (shows on hover)
const visualIndicator = document.createElement('div');
// Add styles for the visual indicator
const indicatorStyle = document.createElement('style');
indicatorStyle.textContent = `
.drag-indicator {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 32px;
background-color: rgba(0, 0, 0, 0.5);
opacity: 0;
z-index: 2147483646;
pointer-events: none;
}
.drag-indicator.fade-out {
transition: opacity 200ms ease-out 100ms;
}
`;
document.head.appendChild(indicatorStyle);
Object.assign(visualIndicator, {
className: 'drag-indicator'
});
// --- Global mouse tracking logic (from preload.mockfix.js) ---
let trackingTimeoutId = null;
let isInDragRegion = false;
let windowBounds = null;
let isWindowFocused = document.hasFocus();
const TRACKING_INTERVAL = 100; // Mouse position check interval
// Helper to update window bounds (for multi-display/resize support)
const updateWindowBounds = async () => {
windowBounds = await ipcRenderer.invoke('get-window-bounds');
};
// Check if mouse is in drag region
const checkMouseInDragRegion = async (x, y) => {
if (!windowBounds) return false;
const withinX = x >= windowBounds.x && x <= windowBounds.x + windowBounds.width;
const withinY = y >= windowBounds.y && y <= windowBounds.y + 32; // 32px drag region
return withinX && withinY;
};
const startTracking = () => {
const track = async () => {
try {
const globalPos = await ipcRenderer.invoke('get-cursor-position');
const inRegion = await checkMouseInDragRegion(globalPos.x, globalPos.y);
isInDragRegion = inRegion;
if (inRegion) {
visualIndicator.classList.remove('fade-out');
visualIndicator.style.opacity = '1';
} else {
visualIndicator.classList.add('fade-out');
visualIndicator.style.opacity = '0';
}
} catch (error) {
// Ignore errors, keep tracking
}
trackingTimeoutId = setTimeout(track, TRACKING_INTERVAL);
};
if (!trackingTimeoutId) track();
};
const stopTracking = () => {
if (trackingTimeoutId) {
clearTimeout(trackingTimeoutId);
trackingTimeoutId = null;
}
};
// Keep windowBounds up-to-date after move/resize
ipcRenderer.on('window-moved-or-resized', () => {
updateWindowBounds();
// Only start tracking if window is focused
if (isWindowFocused) {
startTracking();
}
});
// Event listeners to control tracking
window.addEventListener('mouseenter', () => {
updateWindowBounds();
startTracking();
});
window.addEventListener('mouseleave', () => {
stopTracking();
isInDragRegion = false;
visualIndicator.style.opacity = '0';
});
window.addEventListener('focus', () => {
isWindowFocused = true;
updateWindowBounds();
startTracking();
});
window.addEventListener('blur', () => {
isWindowFocused = false;
stopTracking();
isInDragRegion = false;
visualIndicator.style.opacity = '0';
});
// Insert elements when body is ready
const insertElements = () => {
if (document.body) {
document.body.insertBefore(visualIndicator, document.body.firstChild);
document.body.insertBefore(dragRegion, document.body.firstChild);
return true;
}
return false;
};
// Try to insert immediately or wait for body
if (!insertElements()) {
const observer = new MutationObserver(() => {
if (insertElements()) {
observer.disconnect();
}
});
observer.observe(document.documentElement, {
childList: true,
subtree: true
});
}
});