File size: 9,936 Bytes
3a08226 | 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 | // Gesture Controller - Android 14 compliant gesture handling
class GestureController {
constructor() {
this.minSwipeDistance = 60;
this.edgeSwipeWidth = 40;
this.gestureArea = document.documentElement;
this.isTracking = false;
this.startY = 0;
this.startX = 0;
this.currentGesture = null;
this.vibrationEnabled = true;
this.init();
}
init() {
this.setupTouchHandlers();
this.setupAndroidGestures();
this.listenForSettings();
}
setupTouchHandlers() {
// Use passive listeners for performance
document.addEventListener('touchstart', (e) => this.onTouchStart(e), { passive: true });
document.addEventListener('touchmove', (e) => this.onTouchMove(e), { passive: true });
document.addEventListener('touchend', (e) => this.onTouchEnd(e), { passive: true });
document.addEventListener('touchcancel', (e) => this.onTouchCancel(e), { passive: true });
// Prevent default only for specific gestures
document.addEventListener('touchmove', (e) => {
if (this.isTracking && this.shouldPreventScroll()) {
e.preventDefault();
}
}, { passive: false });
}
setupAndroidGestures() {
// Register with Android system if available
if (window.Android?.GestureManager?.registerCallback) {
window.Android.GestureManager.registerCallback((gesture) => {
this.handleAndroidGesture(gesture);
});
}
}
listenForSettings() {
window.addEventListener('settings-updated', (e) => {
if (e.detail.settings) {
const settings = e.detail.settings;
this.vibrationEnabled = settings['haptic_feedback'] !== '0';
}
});
}
onTouchStart(e) {
const touch = e.touches[0];
this.startX = touch.clientX;
this.startY = touch.clientY;
this.startTime = Date.now();
this.isTracking = true;
this.currentGesture = null;
// Detect gesture zone
const screenHeight = window.innerHeight;
const screenWidth = window.innerWidth;
this.gestureType = this.detectGestureZone(this.startX, this.startY, screenWidth, screenHeight);
// Send to worker for processing
if (window.workerBridge) {
window.workerBridge.processGesture('TOUCH_START', {
x: this.startX,
y: this.startY,
time: this.startTime
});
}
}
onTouchMove(e) {
if (!this.isTracking) return;
const touch = e.touches[0];
const deltaX = touch.clientX - this.startX;
const deltaY = touch.clientY - this.startY;
// Send to worker
if (window.workerBridge) {
window.workerBridge.processGesture('TOUCH_MOVE', {
x: touch.clientX,
y: touch.clientY,
time: Date.now()
});
}
// Live gesture detection for UI feedback
this.updateGesturePreview(deltaX, deltaY);
}
onTouchEnd(e) {
if (!this.isTracking) return;
const touch = e.changedTouches[0];
const deltaX = touch.clientX - this.startX;
const deltaY = touch.clientY - this.startY;
const deltaTime = Date.now() - this.startTime;
this.isTracking = false;
// Send to worker
if (window.workerBridge) {
window.workerBridge.processGesture('TOUCH_END', {
x: touch.clientX,
y: touch.clientY,
time: Date.now()
});
}
// Local gesture detection for immediate response
const gesture = this.detectGesture(deltaX, deltaY, deltaTime);
if (gesture) {
this.executeGesture(gesture);
}
this.hideGesturePreview();
}
onTouchCancel(e) {
this.isTracking = false;
this.hideGesturePreview();
}
detectGestureZone(x, y, width, height) {
// Android 14 gesture zones
if (y < 50) {
if (x < width * 0.4) return 'STATUS_NOTIFICATIONS';
if (x > width * 0.6) return 'STATUS_QUICK_SETTINGS';
return 'STATUS_CLOCK';
}
if (y > height - 20 && x > width * 0.2 && x < width * 0.8) {
return 'HOME_INDICATOR';
}
if (x < this.edgeSwipeWidth) return 'EDGE_BACK';
if (x > width - this.edgeSwipeWidth) return 'EDGE_FORWARD';
return 'CONTENT';
}
detectGesture(deltaX, deltaY, deltaTime) {
const absX = Math.abs(deltaX);
const absY = Math.abs(deltaY);
const velocityX = absX / deltaTime;
const velocityY = absY / deltaTime;
const minVelocity = 0.3; // px/ms
// Home gesture (bottom swipe up)
if (this.gestureType === 'HOME_INDICATOR' && deltaY < -this.minSwipeDistance) {
return { type: 'HOME', intensity: Math.min(absY / 200, 1) };
}
// Back gesture (edge swipe)
if (this.gestureType === 'EDGE_BACK' && deltaX > this.minSwipeDistance) {
return { type: 'BACK', edge: 'left' };
}
// Quick settings (top-right swipe down)
if (this.gestureType === 'STATUS_QUICK_SETTINGS' && deltaY > this.minSwipeDistance) {
return { type: 'QUICK_SETTINGS' };
}
// Notifications (top-left swipe down)
if (this.gestureType === 'STATUS_NOTIFICATIONS' && deltaY > this.minSwipeDistance) {
return { type: 'NOTIFICATIONS' };
}
// App drawer (home screen swipe up)
if (absY > absX && deltaY < -this.minSwipeDistance && velocityY > minVelocity) {
if (document.getElementById('main-screen')?.contains(document.elementFromPoint(this.startX, this.startY))) {
return { type: 'APP_DRAWER' };
}
}
// General swipes
if (absX > absY && absX > this.minSwipeDistance && velocityX > minVelocity) {
return { type: deltaX > 0 ? 'SWIPE_RIGHT' : 'SWIPE_LEFT' };
}
return null;
}
executeGesture(gesture) {
if (this.vibrationEnabled) {
this.hapticFeedback(gesture.type);
}
switch (gesture.type) {
case 'HOME':
this.goHome();
break;
case 'BACK':
this.goBack();
break;
case 'QUICK_SETTINGS':
case 'APP_DRAWER':
this.togglePanel('settings-panel');
break;
case 'NOTIFICATIONS':
this.togglePanel('notification-shade');
break;
}
// Dispatch event for components
window.dispatchEvent(new CustomEvent('gesture-executed', { detail: gesture }));
}
goHome() {
// Close all panels
['server-panel', 'settings-panel', 'notification-shade', 'app-drawer'].forEach(id => {
const el = document.getElementById(id);
if (el) {
el.classList.add('-translate-y-full', '-translate-x-full', 'translate-y-full');
el.classList.remove('translate-y-0', 'translate-x-0', 'translate-y-full');
}
});
// Reset main screen
const main = document.getElementById('main-screen');
if (main) {
main.style.transform = '';
}
}
goBack() {
// Close topmost panel or go back in history
const panels = ['notification-shade', 'settings-panel', 'server-panel', 'app-drawer'];
for (const id of panels) {
const el = document.getElementById(id);
if (el && this.isPanelOpen(el)) {
this.closePanel(el);
return;
}
}
// Fallback to browser back
if (history.length > 1) {
history.back();
}
}
togglePanel(id) {
const el = document.getElementById(id);
if (!el) return;
if (this.isPanelOpen(el)) {
this.closePanel(el);
} else {
this.openPanel(el);
}
}
openPanel(el) {
const id = el.id;
if (id === 'notification-shade') {
el.classList.remove('-translate-y-full');
el.classList.add('translate-y-0');
} else if (id === 'server-panel') {
el.classList.remove('-translate-x-full');
el.classList.add('translate-x-0');
} else {
el.classList.remove('translate-y-full');
el.classList.add('translate-y-0');
}
}
closePanel(el) {
const id = el.id;
if (id === 'notification-shade') {
el.classList.add('-translate-y-full');
el.classList.remove('translate-y-0');
} else if (id === 'server-panel') {
el.classList.add('-translate-x-full');
el.classList.remove('translate-x-0');
} else {
el.classList.add('translate-y-full');
el.classList.remove('translate-y-0');
}
}
isPanelOpen(el) {
return !el.classList.contains('-translate-y-full') &&
!el.classList.contains('-translate-x-full') &&
!el.classList.contains('translate-y-full');
}
hapticFeedback(type) {
const patterns = {
'HOME': [0, 20, 10, 20],
'BACK': [0, 15],
'QUICK_SETTINGS': [0, 10, 40, 10],
'NOTIFICATIONS': [0, 10, 40, 10],
'default': [0, 20]
};
if (navigator.vibrate) {
navigator.vibrate(patterns[type] || patterns.default);
}
// Also notify Android
if (window.Android?.Haptic?.perform) {
window.Android.Haptic.perform(type.toLowerCase());
}
}
updateGesturePreview(deltaX, deltaY) {
// Visual feedback during gesture
const indicator = document.getElementById('gesture-indicator');
if (!indicator) return;
const progress = Math.min(Math.abs(deltaY) / 200, 1);
indicator.style.opacity = progress;
indicator.style.transform = `translateY(${Math.min(deltaY * 0.3, 50)}px)`;
}
hideGesturePreview() {
const indicator = document.getElementById('gesture-indicator');
if (indicator) {
indicator.style.opacity = '0';
}
}
shouldPreventScroll() {
return ['HOME_INDICATOR', 'STATUS_NOTIFICATIONS', 'STATUS_QUICK_SETTINGS'].includes(this.gestureType);
}
handleAndroidGesture(gesture) {
// Handle gestures from Android system
this.executeGesture({ type: gesture.type });
}
}
// Initialize when DOM ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => {
window.gestureController = new GestureController();
});
} else {
window.gestureController = new GestureController();
} |