Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved selectInput/Output/Folder #1818

Closed
wants to merge 4 commits into from

Conversation

fukuchi
Copy link

@fukuchi fukuchi commented May 26, 2013

This patch allows you to call selectInput() and similar functions in previous manner (synchronous method), and improves the flexibility of the new callback-based method.

This pull request consists of two feature-enhancement patches. One allows you to pass an additional object to callback function, and the other serves selectInput(String) in previous manner. Let me explain the former patch at first.

Passing an object to the callback function

Example:
selectOutput("Save as...", "saverFunction", a_pg_graphic);
selectInput("Load CVS...", "loadCVSFile", target_table);

Background:
Passing an object to a callback function is a well-known technique to:

  • identify the caller
  • passing an object (or an array containing multiple objects)

Previously selectInput/Output/Folder just returned a file name and the caller was responsible to handle the file name. Now callback function has to handle the file name out of the context.

Instead of writing a number of callback functions (e.g. saveTable1(), saveTable2()...) or using a global variable, you can just pass an argument to a single function.

Simulating previous behavior of selectInput/Output/Folder

Since Processing-2.0b7, selectInput() requires to pass a callback function and runs asynchronously. This is mandatory to make Processing stable, but many developers are confused. (e.g. #1254, 1)

This patch allows you to call selectInput and its friends in the same manner to Processing 1.5. You can call selectInput() then it returns a string when the user finished to select a file, as it is in Processing 1.5. It uses a synchronized queue to pass the file name from the callback function to the caller, that means this patch does not stop any other thread.

Test code attached:

class FileChooserOpener implements Runnable
{
  private int type;

  FileChooserOpener(int n) {
    type = n;
  };

  public void run() {
    for (int i=0; i<10; i++) {
      switch(type) {
      default:
      case 0:
        println(selectInput());
        break;
      case 1:
        println(selectOutput());
        break;
      case 2:
        println(selectFolder());
        break;
      }
    }
  }
}

Thread[] threads;

void setup()
{
  threads = new Thread[100];
  for (int i=0; i<100; i++) {
    Thread thread = new Thread(new FileChooserOpener(int(random(3))));
    thread.start();
  }
}

@benfry
Copy link
Contributor

benfry commented May 10, 2014

Sorry, it's just not possible to do a synchronous file selection dialog because of the way in which Processing sketches interact with the event thread. That's why we made such a drastic change to the API (that broke code, books, etc), because it was simply not possible to make it behave correctly.

@benfry benfry closed this May 10, 2014
@fukuchi
Copy link
Author

fukuchi commented May 11, 2014

Hello Ben,

Okay, I'll tackle this problem again because the current implementation is so painful for the rest of us.

By the way, there's no clear reason disclosed why the first patch (allowing to pass an object to the caller) was rejected. Do you think we should use global variables to pass the context data to callback function instead of the proposed method?

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants