Skip to content
Snippets Groups Projects
main.js 2.16 KiB
Newer Older
  • Learn to ignore specific revisions
  • Bye's avatar
    Bye committed
    // import {app, BrowserWindow, ipcMain} from "electron";
    const {app, BrowserWindow, ipcMain, Notification} = require("electron");
    const path = require("path");
    const Player =  require("@jellybrick/mpris-service");
    
    
    Bye's avatar
    Bye committed
    if (require('electron-squirrel-startup')) app.quit();
    
    
    Bye's avatar
    Bye committed
    let currentSong = {
      "title":  "",
      "artist": ""
    }
    
    let win;
    let player;
    
    Bye's avatar
    Bye committed
    
    
    Bye's avatar
    Bye committed
    if (process.platform === "linux"){
    
    Bye's avatar
    Bye committed
      player = new Player(
        {
          name: "waves",
          identity: "waves",
          supportedUriSchemes: [],
          supportedMimeTypes: [],
          supportedInterfaces: ['player']
        }
      );
    
    Bye's avatar
    Bye committed
    
    
    Bye's avatar
    Bye committed
      player.canSeek = false;
      player.canGoNext = false;
      player.canGoPrevious = false;
    }
    
    Bye's avatar
    Bye committed
    
    function updatePlayer (event, args) {
      console.log(args)
    
    Bye's avatar
    Bye committed
      if (process.platform === "linux") {
        player.metadata = {
          'mpris:trackid': player.objectPath('track/') + args.id,
          'mpris:length': args.duration * 1000 * 1000,
          'mpris:artUrl': args.artUrl,
          'xesam:title': args.title,
          'xesam:album': args.album,
          'xesam:artist': [args.artist]
        };
    
        player.getPosition = function () {
          return (Date.now() - args.startTime) * 1000
        }
    
    Bye's avatar
    Bye committed
    
    
    Bye's avatar
    Bye committed
        win.setProgressBar((Date.now() - args.startTime) / args.duration)
    
    Bye's avatar
    Bye committed
        console.log((Date.now() - args.startTime) / args.duration)
    
    
    Bye's avatar
    Bye committed
        player.playbackStatus = args.playingStatus;
      }
    
    Bye's avatar
    Bye committed
    
      console.error(args.title !== currentSong.title || args.artist !== currentSong.artist)
      if (args.title !== currentSong.title || args.artist !== currentSong.artist) {
        currentSong = {title: args.title, artist: args.artist}
        new Notification(
          {
            title: args.title,
            body:  args.artist
          }
        ).show()
      }
    
    
    Bye's avatar
    Bye committed
    }
    
    
    function createWindow () {
    
    Bye's avatar
    Bye committed
      win = new BrowserWindow({
    
    Bye's avatar
    Bye committed
        width: 800,
        height: 600,
    
        titleBarStyle: false,
    
        webPreferences: {
          preload: path.join(__dirname, 'preload.js')
        }
      })
    
    
    Bye's avatar
    Bye committed
      // win.setProgressBar(0.69)
    
    Bye's avatar
    Bye committed
    
      win.loadFile('index.html')
    }
    
    app.whenReady().then(() => {
      ipcMain.on('update-metadata', updatePlayer);
      createWindow()
      app.on('activate', () => {
        if (BrowserWindow.getAllWindows().length === 0) createWindow()
      })
    })
    
    
    Bye's avatar
    Bye committed
    if (process.platform === "linux") {
      player.on('quit', function () {
        app.exit();
      });
    }