电子:关闭w X vs右键单击停靠并退出

时间:2016-01-26 06:51:24

标签: javascript electron

在我的Electron应用程序中,我想做一些在其他OSX应用程序中经常执行的操作。那是......我想不要关闭红色X的应用程序点击右上角。但是,如果他们右键单击Dock中的应用程序图标,然后说退出,那么我想退出该应用程序。我该怎么做?

我尝试过使用rendererProcess中的onbeforeunload事件以及browserWindow.on("close", fn)事件来尝试阻止这种情况。问题是他们都提交了onbeforeunload事件。我无法分辨出被点击的红色X和右键单击的停靠栏图标之间的区别,并被告知要退出。你能帮忙的话,我会很高兴。还有其他人在Electron for OSX中做过这个吗?

7 个答案:

答案 0 :(得分:9)

试试这个

if (process.platform === 'darwin') {
  var forceQuit = false;
  app.on('before-quit', function() {
    forceQuit = true;
  });
  mainWindow.on('close', function(event) {
    if (!forceQuit) {
      event.preventDefault();

      /*
       * your process here
       */
    }
  });
}

答案 1 :(得分:2)

这是唯一对我有用的答案:

//CLICK ON S TO SAVE FRAMES TO FOLDER


int series = 50; //
PImage[] collection = new PImage[series];

PImage imageKey,imageKey2, imageKeyHUE, imageKeySUM, imageKeySAT; //target image alias with ready operations
int imageWidth = 800;
int leftAlign = 850  ;

void setup()
{
size(1200,600);
background(255);
frameRate(random(1,10.0));

for ( int i = 0; i< collection.length; i++ )
{
collection[i] = loadImage( "Image_"+ i + ".jpg" );  
}

//_____________________________________________TARGET IMAGE AND NAME TEXT

textSize(10);
fill(0);
text("target image", leftAlign, 220);
textSize(15);
text("central london", leftAlign, 240);
text("comparison methods", leftAlign, 290);
//_____________________________________________________________________BUTTONS
imageKey = loadImage("Image_0.jpg");
imageKey.resize(240, 180);
image(imageKey, leftAlign,25);

   imageKeySAT= loadImage("Image_0.jpg");
    imageKeySAT.resize(60,60);
    imageKeySAT = saturation(imageKeySAT);
    image(imageKeySAT, leftAlign+140,300);

      imageKeySUM = loadImage("Image_0.jpg");
      imageKeySUM.resize(60,60);
      imageKeySUM = sum(imageKeySUM);
      image(imageKeySUM, leftAlign+70,300);

          imageKeyHUE = loadImage("Image_0.jpg");
          imageKeyHUE.resize(60,60);
          imageKeyHUE = hue(imageKeyHUE);
          image(imageKeyHUE, leftAlign,300);

          textSize(20);
          text("CLICK HERE TO", leftAlign, 430);
          text("STOP AT WINNER", leftAlign, 450);



}


void draw()
{

//______________________________________________SHOW IMAGES ARRAY
image(collection[int(random(0,series))],0,0);

//______________________________________________HISTOGRAMS
histogramhue();
histogramsat();
histogrambright();

//______________________________________________SUM METHOD
//float Vector_Approach(PImage sumSatin){


//}

}


void keyPressed()
{
  if(key=='s') saveFrame("images/image-######.jpg");
}


PImage difference(PImage satin)
{
  colorMode(HSB);

    satin.loadPixels();
    imageKey.loadPixels();

    PImage satout = createImage(satin.width, satin.height, RGB);

    satout.loadPixels();
    for(int i = imageWidth; i<satout.pixels.length-imageWidth; i++)
    {
        float x1 = brightness(imageKey.pixels[i]);
        float b0 = brightness(satin.pixels[i]);
       // float y1 = brightness(satin.pixels[i+1]);

        float value = x1-b0;

        satout.pixels[i] = color(0,0,value);

    }
    satout.updatePixels();

    return satout;    
}

void mouseReleased(){
//______________________________________________BUTTON OVER
for ( int i = 0; i< collection.length; i++ )
  if (mouseX > leftAlign && mouseX < (leftAlign + 60) && mouseY > 300 && mouseY < 360){

            collection[i] = loadImage( "Image_"+ i + ".jpg" );  
            collection[i] = hue(collection[i]); histogramhue();
                  noStroke(); fill(255); rect(leftAlign,360,200,40); fill(0);
                  textSize(10);text("comparison by hue", leftAlign, 380);

} else if (mouseX > (leftAlign + 70) && mouseX < (leftAlign + 130) && mouseY > 300 && mouseY < 360)
{

            collection[i] = loadImage( "Image_"+ i + ".jpg" );  
            collection[i] = sum(collection[i]);
                  noStroke(); fill(255); rect(leftAlign,360,200,40); fill(0);
                  textSize(10);text("comparison by sum", leftAlign, 380);

}else if (mouseX > (leftAlign + 140) && mouseX < (leftAlign + 200) && mouseY > 300 && mouseY < 360)
{

          collection[i] = loadImage( "Image_"+ i + ".jpg" );  
          collection[i] = saturation(collection[i]);
                  noStroke(); fill(255); rect(leftAlign,360,200,40); fill(0);
                  textSize(10);text("comparison by saturation", leftAlign, 380);


}else if (mouseX > leftAlign && mouseX < 1200 && mouseY > 340 && mouseY < 600)
{

          collection[i] = loadImage( "Image_"+ i + ".jpg" );  
          collection[i] = difference(collection[i]);
                  noStroke(); fill(255); rect(leftAlign,360,200,40); fill(0);
                  textSize(10);text("WINNER IMAGE!!!!", leftAlign, 380);
}else{

    collection[i] = loadImage( "Image_"+ i + ".jpg" );  
    noStroke(); fill(255); rect(leftAlign,360,200,40); fill(0);

}
}



/*          HSB PImage Methods             */

 //HUE              ------->    /** CHOSEN METHOD**/
 //SATURATION       ------->    /** CHOSEN METHOD**/
 //SUM              ------->    /** CHOSEN METHOD**/





PImage hue(PImage satin)
{
      colorMode(HSB);
      satin.loadPixels();

      PImage satout = createImage(satin.width, satin.height, HSB);

      satout.loadPixels();

      for (int j = 0; j < satout.pixels.length; j++)
      {
      satout.pixels[j] = color(hue(satin.pixels[j]),255,255);
      }

satout.updatePixels();

return satout;
} 

PImage saturation(PImage satin)
{
      colorMode(HSB);
      satin.loadPixels();
      PImage satout = createImage(satin.width, satin.height, RGB);
      satout.loadPixels();
      for (int j = 0; j < satout.pixels.length; j++)
      {
      satout.pixels[j] = color(saturation(satin.pixels[j]));
      }

satout.updatePixels();
//colorMode(RGB);
return satout;
} 



PImage sum(PImage satin)
{
  colorMode(HSB);

    satin.loadPixels();

    PImage satout = createImage(satin.width, satin.height, RGB);

    satout.loadPixels();
    for(int i = imageWidth; i<satout.pixels.length-imageWidth; i++)
    {
        float b0 = brightness(satin.pixels[i]);
        float x1 = brightness(satin.pixels[i-1]);
        float y1 = brightness(satin.pixels[i-imageWidth]);

        float xdiff = b0-x1;
        float ydiff = b0-y1;

        float value = (510 + xdiff + ydiff)/3;

        satout.pixels[i] = color(0,0,value);

    }
    satout.updatePixels();

    return satout;    
}

//REFERENCE HISTOGRAM TAKEN FROM A PROGRAMMING HANDBOOK FOR VISUAL DESIGNERS AND ARTISTS BY BEN FRY ET AL

void histogramhue(){
PImage img = loadImage("Image_0.jpg");
int[] hist = new int[600];

// Calculate the histogram
for (int i = 0; i < img.width; i++) {
  for (int j = 0; j < img.height; j++) {
    int hue = int(hue(get(i, j)));
    hist[hue]++; 
  }
}

int histMax = max(hist);

stroke(255,250); strokeWeight(5);
// Draw half of the histogram (skip every second value)
for (int i = 0; i < img.width; i += 20) {
  int which = int(map(i, 0, img.width, 0, 255));
  int y = int(map(hist[which], 0, histMax, img.height, 0));
  line(i, img.height, i, y);
}}


void histogramsat(){
PImage img = loadImage("Image_0.jpg");

int[] hist = new int[600];

for (int i = 0; i < img.width; i++) {
  for (int j = 0; j < img.height; j++) {
    int sat = int(saturation(get(i, j)));
    hist[sat]++; 
  }
}

int histMax = max(hist);

stroke(255,150);strokeWeight(10);
for (int i = 0; i < img.width; i += 20) {
  int which = int(map(i, 0, img.width, 0, 255));
  int y = int(map(hist[which], 0, histMax, img.height, 0));
  line(i, img.height, i, y);
}}

void histogrambright(){
PImage img = loadImage("Image_0.jpg");

int[] hist = new int[600];


for (int i = 0; i < img.width; i++) {
  for (int j = 0; j < img.height; j++) {
    int bright = int(brightness(get(i, j)));
    hist[bright]++; 
  }
}
int histMax = max(hist);

stroke(255, 150);strokeWeight(20);
for (int i = 0; i < img.width; i += 20) {
  int which = int(map(i, 0, img.width, 0, 255));
  int y = int(map(hist[which], 0, histMax, img.height, 0));
  line(i, img.height, i, y);
}}

通过https://discuss.atom.io/t/how-to-catch-the-event-of-clicking-the-app-windows-close-button-in-electron-app/21425/8

答案 2 :(得分:0)

经过多看,我发现了以下解决方案。当您右键单击停靠栏并选择Quit时,在此之前触发rendererProcess中的onbeforeunload,它将首先在应用程序本身上触发close事件。因此,在rendererProcess中,您有一个onbeforeunload侦听器。而且你告诉那个永远都是假的。从该事件返回false将阻止窗口永远卸载/关闭。然后在mainProcess中添加app.on('close',fn)侦听器。该侦听器可以向rendererProcess发送一个事件,告诉它允许关闭。也许您可以设置全局allowClose = true或其他内容。然后在onbeforeunload中,如果allowClose为真,则添加逻辑以不返回true。

答案 3 :(得分:0)

在主要流程中查看@BeforeClass public static void beforeClass() { BasicConfigurator.resetConfiguration(); BasicConfigurator.configure(); } app事件import numpy as np myA = np.array([7, 4, 5, 8, 3, 10]) n = 2 val = 5 myA[np.where(myA > val)[0][:n]] = 0 print(myA) 。此事件通常用于退出Linux和Windows上的应用程序,但不用于OS X(例如,请参阅Electron&#39; s window-all-closed)。在OS X上,如果当前没有窗口打开,您可能还应该处理Quick Start Tutorial事件以打开一个新窗口。

答案 4 :(得分:0)

看一下电子quick start guide

请注意以下两个解决方案需要在main.js中实现,而不是在html页面上执行的JS。

特定窗口关闭

如果要在特定BrowserWindow关闭时执行代码:

mainWindow.on('closed', function() {
    // Your code to be executed before "really" stopping the app
});

关闭所有窗口

如果要在所有窗口关闭时执行代码(app API):

app.on('window-all-closed', function() {
    // do stuff here
});

答案 5 :(得分:0)

您需要在public void Evaluate(int SpreadMax) { SpreadMax = 1; UIElementOut.SliceCount = 1; for (var i = 0; i < SpreadMax; i++) { if (UIElementOut == null || !(UIElementOut[i] is TabControl)) UIElementOut[i] = new TabControl(); var uiElement = (TabControl)UIElementOut[i]; uiElement.Height = 200; uiElement.Width = 500; } Grid grid = new Grid(); var listOfElements = new List<UIElements> { new Button {Background = Brushes.Tomato, Content = "Click Me"}, new Button {Background = Brushes.Yellow, Content = "Click Me"}, new Button {Background = Brushes.Green, Content = "Click Me"}, new Button {Background = Brushes.Blue, Content = "Click Me"} }; listOfElements.ForEach(button => grid.Children.Add(button)); ((TabControl)UIElementOut[0]).Items.Add(new TabItem { Header = "Objects", Content = grid }); } 文件中处理此问题,在main.js事件中检查它是否为darwin平台,并在window-all-closed事件上重新创建窗口。< / p>

activate

更多信息/示例:https://github.com/atom/electron-quick-start/blob/master/main.js

答案 6 :(得分:0)

我就是这样解决的,而且效果很好。

import { app } from "electron";
let window: any;
let forceQuit = false;

app.on("ready", () => {
    window = //YOUR BROWSER WINDOW
    window.on("close", e => {
      if (process.platform === "darwin" && forceQuit) {
        window = null;
      } else {
        e.preventDefault();
        app.hide();
      }
});

app.on("activate", function() {
    app.show();
});

app.on("before-quit", function(event) {
    if (!forceQuit) {
      event.preventDefault();
      forceQuit = true;
      app.quit();
    }
});