main.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import sys
  2. import os
  3. import asyncio
  4. import logging
  5. from pathlib import Path
  6. from PySide6.QtCore import QUrl, QTimer
  7. from PySide6.QtGui import QGuiApplication
  8. from PySide6.QtQml import QQmlApplicationEngine, qmlRegisterType
  9. from qasync import QEventLoop
  10. from backend import Backend
  11. from models.pattern_model import PatternModel
  12. from models.playlist_model import PlaylistModel
  13. from png_cache_manager import ensure_png_cache_startup
  14. # Configure logging
  15. logging.basicConfig(
  16. level=logging.INFO,
  17. format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
  18. )
  19. logger = logging.getLogger(__name__)
  20. async def startup_tasks():
  21. """Run async startup tasks"""
  22. logger.info("🚀 Starting dune-weaver-touch async initialization...")
  23. # Ensure PNG cache is available for all WebP previews
  24. try:
  25. logger.info("🎨 Checking PNG preview cache...")
  26. png_cache_success = await ensure_png_cache_startup()
  27. if png_cache_success:
  28. logger.info("✅ PNG cache check completed successfully")
  29. else:
  30. logger.warning("⚠️ PNG cache check completed with warnings")
  31. except Exception as e:
  32. logger.error(f"❌ PNG cache check failed: {e}")
  33. logger.info("✨ dune-weaver-touch startup tasks completed")
  34. def main():
  35. # Enable virtual keyboard
  36. os.environ['QT_IM_MODULE'] = 'qtvirtualkeyboard'
  37. app = QGuiApplication(sys.argv)
  38. # Setup async event loop
  39. loop = QEventLoop(app)
  40. asyncio.set_event_loop(loop)
  41. # Register types
  42. qmlRegisterType(Backend, "DuneWeaver", 1, 0, "Backend")
  43. qmlRegisterType(PatternModel, "DuneWeaver", 1, 0, "PatternModel")
  44. qmlRegisterType(PlaylistModel, "DuneWeaver", 1, 0, "PlaylistModel")
  45. # Load QML
  46. engine = QQmlApplicationEngine()
  47. qml_file = Path(__file__).parent / "qml" / "main.qml"
  48. engine.load(QUrl.fromLocalFile(str(qml_file)))
  49. if not engine.rootObjects():
  50. return -1
  51. # Schedule startup tasks after a brief delay to ensure event loop is running
  52. def schedule_startup():
  53. try:
  54. # Check if we're in an event loop context
  55. current_loop = asyncio.get_running_loop()
  56. current_loop.create_task(startup_tasks())
  57. except RuntimeError:
  58. # No running loop, create task directly
  59. asyncio.create_task(startup_tasks())
  60. # Use QTimer to delay startup tasks
  61. startup_timer = QTimer()
  62. startup_timer.timeout.connect(schedule_startup)
  63. startup_timer.setSingleShot(True)
  64. startup_timer.start(100) # 100ms delay
  65. with loop:
  66. loop.run_forever()
  67. return 0
  68. if __name__ == "__main__":
  69. sys.exit(main())