<commit_before>var React = require('react');
var ReactDOM = require('react-dom');
var AutoLaunch = require('auto-launch');
var path = require('path');

var _ = require('lodash');

import OptionsForm from './options-form';
import Preview from './preview';
import SaverList from './saver-list';

const shell = window.require('electron').shell;
const {BrowserWindow} = window.require('electron').remote;

(function() {
  var electron = window.require('electron');
  const {ipcRenderer} = window.require('electron');
  const {crashReporter} = window.require('electron');

  var remote = window.require('electron').remote;
  var savers = remote.getGlobal('savers');
  var appName = remote.getGlobal('APP_NAME');
  var appVersion = remote.getGlobal('APP_VERSION');
  var defaultSaverRepo = remote.getGlobal('SAVER_REPO');
  var appRepo = remote.getGlobal('APP_REPO');
  var updateAvailable = remote.getGlobal('NEW_RELEASE_AVAILABLE');

  
  var ravenUrl = remote.getGlobal('RAVEN_URL');
  if ( typeof(ravenUrl) !== "undefined" ) {
    console.log("RAVEN!", ravenUrl);
    Raven.config(ravenUrl).install();
  }
  
  crashReporter.start(remote.getGlobal('CRASH_REPORTER'));

  ipcRenderer.on('savers-updated', (event, arg) => {
    console.log("handle savers-updated event");
    renderList();

    var val = getCurrentScreensaver();
    var s = savers.getByKey(val);
    
    redraw(s);

  });
  
  const {dialog} = require('electron').remote;

  var appLauncher = new AutoLaunch({
	  name: appName
  });
    
  var saverOpts = {};
  var url_opts = {};
  
  // parse incoming URL params -- we'll get a link to the current screen images for previews here
  var tmpParams = new URLSearchParams(document.location.search);

  // the main app will pass us a screenshot URL, here it is
  var screenshot = decodeURIComponent(tmpParams.get("screenshot"));
  
  console.log("appLauncher " + appName);
  appLauncher.isEnabled().then(function(enabled){
    console.log("auto launch enabled?: " + enabled);
	  if (enabled) {
      document.querySelector("input[name=auto_start]").setAttribute("checked", "checked");
    }
  }).catch(function(err) {
    console.log("appLauncher error", err);
  });
  
  var el = document.querySelector("body > header div h1");
  if ( el ) {
    el.innerHTML = appName;
  }

  el = document.querySelector("body > header div .version");
  if ( el ) {
    el.innerHTML = appVersion;
  }
  
  // if the preview div didn't have a height, figure one out by getting
  // the width and making it proprtional to the main screen. at the moment,
  // the div will never have a height at this point unless someone specifically
  // hacks the CSS to make it work differently

  el = document.querySelector("[name=repo]");
  el.value = savers.getSource().repo;
  el.setAttribute("placeholder", defaultSaverRepo);


  document.querySelector("[name=localSource]").value = savers.getLocalSource();
  el = document.querySelector("select[name='delay'] option[value='" + savers.getDelay() + "']");
  if ( el !== null ) {
    el.setAttribute("selected", "selected");
  }

  el = document.querySelector("select[name='sleep'] option[value='" + savers.getSleep() + "']");
  if ( el !== null ) {
    el.setAttribute("selected", "selected");
  }
  
  if ( savers.getLock() === true ) {
    document.querySelector("input[name=lock_screen][type=checkbox]").setAttribute("checked", "checked");
  }

  if ( savers.getDisableOnBattery() === true ) {
    document.querySelector("input[name=disable_on_battery]").setAttribute("checked", "checked");
  }

  
  var getPreviewUrlOpts = function() {
    var tmp = {
      width: document.querySelector("#preview").offsetWidth,
      height: document.querySelector("#preview").offsetHeight,
      screenshot: screenshot,
      preview: 1
    };

    if ( tmp.height == 0 ) {
      var atomScreen = electron.screen;
      var size = atomScreen.getPrimaryDisplay().bounds;
      var ratio = size.height / size.width;
      tmp.height = tmp.width * ratio;
      //console.log("setting preview opts to", url_opts);
    }

    return tmp;
  };

  var loadPreview = function(s) {
    url_opts = getPreviewUrlOpts();

    ReactDOM.render(
      <Preview saver={s} url_opts={url_opts} saver_opts={saverOpts} />,
      document.getElementById('preview')
    );
  };

  var getCurrentScreensaver = function() {
    return document.querySelector("input[name=screensaver]:checked").value;
  };
  
  var optionsUpdated = function(data) {
    saverOpts = data;
    var current = getCurrentScreensaver();
    var s = savers.getByKey(current);
    redraw(s);        
  };
  
  var loadOptionsForm = function(s) {
    // hold a ref to the form so we can get values later
    // @todo - i assume this is a hack that doesn't match with react very well
    window.optionsFormRef = ReactDOM.render(
      <OptionsForm saver={s} onChange={optionsUpdated} />,
      document.getElementById('options')
    );
  };
  
  var closeWindow = function() {
    var window = remote.getCurrentWindow();
    window.close();
  };

  var redraw = function(s) {
    if ( typeof(s) === "undefined" ) {
      var current = getCurrentScreensaver();
      s = savers.getByKey(current);
      //console.log(current, s);
    }

    loadPreview(s);
    loadOptionsForm(s);
  };
<commit_msg>Don't render preview if missing<commit_after>var React = require('react');
var ReactDOM = require('react-dom');
var AutoLaunch = require('auto-launch');
var path = require('path');

var _ = require('lodash');

import OptionsForm from './options-form';
import Preview from './preview';
import SaverList from './saver-list';

const shell = window.require('electron').shell;
const {BrowserWindow} = window.require('electron').remote;

(function() {
  var electron = window.require('electron');
  const {ipcRenderer} = window.require('electron');
  const {crashReporter} = window.require('electron');

  var remote = window.require('electron').remote;
  var savers = remote.getGlobal('savers');
  var appName = remote.getGlobal('APP_NAME');
  var appVersion = remote.getGlobal('APP_VERSION');
  var defaultSaverRepo = remote.getGlobal('SAVER_REPO');
  var appRepo = remote.getGlobal('APP_REPO');
  var updateAvailable = remote.getGlobal('NEW_RELEASE_AVAILABLE');

  
  var ravenUrl = remote.getGlobal('RAVEN_URL');
  if ( typeof(ravenUrl) !== "undefined" ) {
    console.log("RAVEN!", ravenUrl);
    Raven.config(ravenUrl).install();
  }
  
  crashReporter.start(remote.getGlobal('CRASH_REPORTER'));

  ipcRenderer.on('savers-updated', (event, arg) => {
    console.log("handle savers-updated event");
    renderList();

    var val = getCurrentScreensaver();
    var s = savers.getByKey(val);
    
    redraw(s);

  });
  
  const {dialog} = require('electron').remote;

  var appLauncher = new AutoLaunch({
	  name: appName
  });
    
  var saverOpts = {};
  var url_opts = {};
  
  // parse incoming URL params -- we'll get a link to the current screen images for previews here
  var tmpParams = new URLSearchParams(document.location.search);

  // the main app will pass us a screenshot URL, here it is
  var screenshot = decodeURIComponent(tmpParams.get("screenshot"));
  
  console.log("appLauncher " + appName);
  appLauncher.isEnabled().then(function(enabled){
    console.log("auto launch enabled?: " + enabled);
	  if (enabled) {
      document.querySelector("input[name=auto_start]").setAttribute("checked", "checked");
    }
  }).catch(function(err) {
    console.log("appLauncher error", err);
  });
  
  var el = document.querySelector("body > header div h1");
  if ( el ) {
    el.innerHTML = appName;
  }

  el = document.querySelector("body > header div .version");
  if ( el ) {
    el.innerHTML = appVersion;
  }
  
  // if the preview div didn't have a height, figure one out by getting
  // the width and making it proprtional to the main screen. at the moment,
  // the div will never have a height at this point unless someone specifically
  // hacks the CSS to make it work differently

  el = document.querySelector("[name=repo]");
  el.value = savers.getSource().repo;
  el.setAttribute("placeholder", defaultSaverRepo);


  document.querySelector("[name=localSource]").value = savers.getLocalSource();
  el = document.querySelector("select[name='delay'] option[value='" + savers.getDelay() + "']");
  if ( el !== null ) {
    el.setAttribute("selected", "selected");
  }

  el = document.querySelector("select[name='sleep'] option[value='" + savers.getSleep() + "']");
  if ( el !== null ) {
    el.setAttribute("selected", "selected");
  }
  
  if ( savers.getLock() === true ) {
    document.querySelector("input[name=lock_screen][type=checkbox]").setAttribute("checked", "checked");
  }

  if ( savers.getDisableOnBattery() === true ) {
    document.querySelector("input[name=disable_on_battery]").setAttribute("checked", "checked");
  }

  
  var getPreviewUrlOpts = function() {
    var tmp = {
      width: document.querySelector("#preview").offsetWidth,
      height: document.querySelector("#preview").offsetHeight,
      screenshot: screenshot,
      preview: 1
    };

    if ( tmp.height == 0 ) {
      var atomScreen = electron.screen;
      var size = atomScreen.getPrimaryDisplay().bounds;
      var ratio = size.height / size.width;
      tmp.height = tmp.width * ratio;
      //console.log("setting preview opts to", url_opts);
    }

    return tmp;
  };

  var loadPreview = function(s) {
    url_opts = getPreviewUrlOpts();

    ReactDOM.render(
      <Preview saver={s} url_opts={url_opts} saver_opts={saverOpts} />,
      document.getElementById('preview')
    );
  };

  var getCurrentScreensaver = function() {
    return document.querySelector("input[name=screensaver]:checked").value;
  };
  
  var optionsUpdated = function(data) {
    saverOpts = data;
    var current = getCurrentScreensaver();
    var s = savers.getByKey(current);
    redraw(s);        
  };
  
  var loadOptionsForm = function(s) {
    // hold a ref to the form so we can get values later
    // @todo - i assume this is a hack that doesn't match with react very well
    window.optionsFormRef = ReactDOM.render(
      <OptionsForm saver={s} onChange={optionsUpdated} />,
      document.getElementById('options')
    );
  };
  
  var closeWindow = function() {
    var window = remote.getCurrentWindow();
    window.close();
  };

  var redraw = function(s) {
    if ( typeof(s) === "undefined" ) {
      var current = getCurrentScreensaver();
      s = savers.getByKey(current);
      //console.log(current, s);
    }

    if ( typeof(s) !== "undefined" ) {
      loadPreview(s);
      loadOptionsForm(s);
    }
  };

{'fraction_non_alphanumeric': 0.1479596644991047, 'fraction_numerical': 0.0005654509471303364, 'mean_word_length': 3.648269820411739, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '765072', 'n_tokens_mistral': 3276, 'n_tokens_neox': 3248, 'n_words': 753}