All files / runtime/src/runtimes desktop.js

0% Statements 0/30
0% Branches 0/30
12.5% Functions 1/8
0% Lines 0/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                       
/**
 * Desktop Runtime - For Electron and Tauri applications
 * Extends browser runtime with desktop-specific capabilities
 */
 
import { BrowserRuntime } from './browser.js';
 
export class DesktopRuntime extends BrowserRuntime {
  constructor(options = {}) {
    super({
      enableFileSystem: true,
      enableNativeAPIs: true,
      ...options
    });
    
    this.desktopAPI = null;
    this.isElectron = typeof window !== 'undefined' && !!window.require;
    this.isTauri = typeof window !== 'undefined' && !!window.__TAURI__;
  }
 
  async initialize() {
    await super.initialize();
    
    // Initialize desktop APIs
    if (this.isElectron) {
      this.initializeElectron();
    } else if (this.isTauri) {
      this.initializeTauri();
    }
  }
 
  initializeElectron() {
    try {
      // Access Electron APIs if available
      this.desktopAPI = {
        platform: 'electron',
        fs: window.require ? window.require('fs') : null,
        path: window.require ? window.require('path') : null,
        ipcRenderer: window.require ? window.require('electron').ipcRenderer : null
      };
    } catch (error) {
      console.warn('Failed to initialize Electron APIs:', error);
    }
  }
 
  async initializeTauri() {
    try {
      // Access Tauri APIs if available
      if (window.__TAURI__) {
        this.desktopAPI = {
          platform: 'tauri',
          fs: window.__TAURI__.fs,
          path: window.__TAURI__.path,
          invoke: window.__TAURI__.invoke
        };
      }
    } catch (error) {
      console.warn('Failed to initialize Tauri APIs:', error);
    }
  }
 
  // Desktop-specific methods
  async readFile(path) {
    if (this.desktopAPI?.fs) {
      return await this.desktopAPI.fs.readTextFile(path);
    }
    throw new Error('File system access not available');
  }
 
  async writeFile(path, content) {
    if (this.desktopAPI?.fs) {
      return await this.desktopAPI.fs.writeTextFile(path, content);
    }
    throw new Error('File system access not available');
  }
 
  async showDialog(options) {
    if (this.isElectron && this.desktopAPI?.ipcRenderer) {
      return await this.desktopAPI.ipcRenderer.invoke('show-dialog', options);
    } else if (this.isTauri && this.desktopAPI?.invoke) {
      return await this.desktopAPI.invoke('show_dialog', options);
    }
    // No fallback dialog available in desktop environment
    console.warn('Desktop dialog not available, rejecting by default');
    return Promise.resolve(false);
  }
 
  static createApp(options = {}) {
    const runtime = new DesktopRuntime(options);
    return runtime.createApp(options);
  }
}