]> khamidou — GitHub mirrors - electron.git/commitdiff
Use cross-platform callbacks in the dialog API.
authorCheng Zhao <zcbenz@gmail.com>
Sun, 5 May 2013 12:24:20 +0000 (20:24 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Sun, 5 May 2013 12:24:20 +0000 (20:24 +0800)
browser/api/lib/dialog.coffee
renderer/api/lib/dialog.coffee

index c278bc32b9e72faeef913f5ebef55d02df6df9de..20f83d053c2a7db474a3a8b6fa286f220a9ea295 100644 (file)
@@ -5,18 +5,32 @@ ipc = require 'ipc'
 FileDialog = binding.FileDialog
 FileDialog.prototype.__proto__ = EventEmitter.prototype
 
-callbacksInfo = {}
+class CallbacksRegistry
+  @nextId = 0
+  @callbacks = {}
 
-fileDialog = new FileDialog
+  @add: (callback) ->
+    @callbacks[++@nextId] = callback
+    @nextId
+
+  @get: (id) ->
+    @callbacks[id]
+
+  @call: (id, args...) ->
+    @callbacks[id].call global, args...
 
-onSelected = (event, id, paths...) ->
-  {processId, routingId} = callbacksInfo[id]
-  delete callbacksInfo[id]
+  @remove: (id) ->
+    delete @callbacks[id]
 
-  ipc.sendChannel processId, routingId, 'ATOM_RENDERER_DIALOG', id, paths...
+fileDialog = new FileDialog
+
+fileDialog.on 'selected', (event, callbackId, paths...) ->
+  CallbacksRegistry.call callbackId, 'selected', paths...
+  CallbacksRegistry.remove callbackId
 
-fileDialog.on 'selected', onSelected
-fileDialog.on 'cancelled', onSelected
+fileDialog.on 'cancelled', (event, callbackId) ->
+  CallbacksRegistry.call callbackId, 'cancelled'
+  CallbacksRegistry.remove callbackId
 
 validateOptions = (options) ->
   return false unless typeof options is 'object'
@@ -30,19 +44,20 @@ validateOptions = (options) ->
   options.defaultPath = '' unless options.defaultPath?
   options.fileTypeIndex = 0 unless options.fileTypeIndex?
   options.defaultExtension = '' unless options.defaultExtension?
-
   true
 
-ipc.on 'ATOM_BROWSER_FILE_DIALOG', (processId, routingId, callbackId, type, title, options) ->
+selectFileWrap = (window, options, callback, type, title) ->
+  throw new TypeError('Need Window object') unless window.constructor?.name is 'Window'
+
   options = {} unless options?
   options.type = type
   options.title = title unless options.title?
 
   throw new TypeError('Bad arguments') unless validateOptions options
 
-  callbacksInfo[callbackId] = processId: processId, routingId: routingId
+  callbackId = CallbacksRegistry.add callback
 
-  fileDialog.selectFile processId, routingId,
+  fileDialog.selectFile window.getProcessId(), window.getRoutingId(),
                         options.type,
                         options.title,
                         options.defaultPath,
@@ -50,3 +65,16 @@ ipc.on 'ATOM_BROWSER_FILE_DIALOG', (processId, routingId, callbackId, type, titl
                         options.fileTypeIndex,
                         options.defaultExtension,
                         callbackId
+
+module.exports =
+  openFolder: (args...) ->
+    selectFileWrap args..., 1, 'Open Folder'
+
+  saveAs: (args...) ->
+    selectFileWrap args..., 2, 'Save As'
+
+  openFile: (args...) ->
+    selectFileWrap args..., 3, 'Open File'
+
+  openMultiFiles: (args...) ->
+    selectFileWrap args..., 4, 'Open Files'
index 6ac381f9b6e34fe04423dc44746602a66e87cf89..8fd20a70032f3c9c86d66823dea0a39255e84ddb 100644 (file)
@@ -1,41 +1,10 @@
-ipc = require 'ipc'
+remote = require 'remote'
+dialog = remote.require 'dialog'
 
-callbackId = 0
-callbacks = {}
-
-storeCallback = (callback) ->
-  throw new TypeError('Bad argument') unless typeof callback is 'function'
-
-  ++callbackId
-  callbacks[callbackId] = callback
-  callbackId
-
-makeCallback = (id, args...) ->
-  callbacks[id].call global, args...
-  delete callbacks[id]
-
-# Force loading dialog code in browser.
-remote.require 'dialog'
-
-ipc.on 'ATOM_RENDERER_DIALOG', (id, args...) ->
-  makeCallback(id, args...)
-
-callFileDialogs = (options, callback, args...) ->
-  if typeof options is 'function'
-    callback = options
-    options = {}
-
-  ipc.sendChannel 'ATOM_BROWSER_FILE_DIALOG', storeCallback(callback), args..., options
+win = remote.getCurrentWindow()
 
 module.exports =
-  openFolder: (options, callback) ->
-    callFileDialogs options, callback, 1, 'Open Folder'
-
-  saveAs: (options, callback) ->
-    callFileDialogs options, callback, 2, 'Save As'
-
-  openFile: (options, callback) ->
-    callFileDialogs options, callback, 3, 'Open File'
-
-  openMultiFiles: (options, callback) ->
-    callFileDialogs options, callback, 4, 'Open Files'
+  openFolder: (args...) -> dialog.openFolder win, args...
+  saveAs: (args...) -> dialog.saveAs win, args...
+  openFile: (args...) -> dialog.openFile win, args...
+  openMultiFiles: (args...) -> dialog.openMultiFiles win, args...