osx.coffee is dead.

Split Chrome into App and Window, in app.coffee and window.coffee
This commit is contained in:
Chris Wanstrath 2011-09-04 14:17:46 -07:00
parent de2ffdf25d
commit 804b67f8df
10 changed files with 138 additions and 125 deletions

View File

@ -1,7 +1,7 @@
$ = require 'jquery'
_ = require 'underscore'
{Chrome} = require 'osx'
{activeWindow} = require 'app'
File = require 'fs'
Editor = require 'editor'
@ -30,7 +30,7 @@ exports.toggle = ->
if @showing
$('#project').parent().remove()
else
Chrome.addPane 'left', @html
activeWindow.addPane 'left', @html
@reload()
@showing = not @showing

View File

@ -1,8 +1,7 @@
$ = require 'jquery'
_ = require 'underscore'
{Chrome} = require 'osx'
File = require 'fs'
{activeWindow} = require 'app'
{bindKey} = require 'editor'
@ -26,7 +25,7 @@ bindKey 'toggleTabs', 'Command-Ctrl-T', (env) ->
module.exports = tabs =
showTabs: ->
Chrome.addPane 'top', require 'tabs/tabs.html'
activeWindow.addPane 'top', require 'tabs/tabs.html'
$('#tabs').parents('.pane').css height: 'inherit'
css = $('<style id="tabs-style"></style>').html require 'tabs/tabs.css'
$('head').append css

43
src/app.coffee Normal file
View File

@ -0,0 +1,43 @@
_ = require 'underscore'
Window = require 'window'
module.exports = App =
windows: []
root: OSX.NSBundle.mainBundle.resourcePath
activeWindow: null
setActiveWindow: (window) ->
@activeWindow = window
@windows.push window if window not in @windows
# path - Optional. The String path to the file to base it on.
newWindow: (path) ->
c = OSX.AtomWindowController.alloc.initWithWindowNibName "AtomWindow"
c.window
c.window.makeKeyAndOrderFront null
# Returns null or a file path.
openPanel: ->
panel = OSX.NSOpenPanel.openPanel
panel.setCanChooseDirectories true
if panel.runModal isnt OSX.NSFileHandlingPanelOKButton
return null
panel.filenames.lastObject
openURL: (url) ->
window.location = url
@activeWindow.setTitle _.last url.replace(/\/$/,'').split '/'
# Returns null or a file path.
savePanel: ->
panel = OSX.NSSavePanel.savePanel
if panel.runModal isnt OSX.NSFileHandlingPanelOKButton
return null
panel.filenames.lastObject
writeToPasteboard: (text) ->
pb = OSX.NSPasteboard.generalPasteboard
pb.declareTypes_owner [OSX.NSStringPboardType], null
pb.setString_forType text, OSX.NSStringPboardType

View File

@ -8,7 +8,7 @@ __jsc__.evalJSString_withScriptPath code, path
# TODO: turn these into real unit tests
OSX.NSLog 'require tests:'
OSX.NSLog require.resolve 'underscore'
OSX.NSLog require.resolve 'osx'
OSX.NSLog require.resolve 'app'
OSX.NSLog require.resolve 'tabs/tabs'
[ fn, window.__filename ] = [ __filename, "#{root}/src/bootstrap.js" ]

View File

@ -3,8 +3,9 @@
$ = require 'jquery'
_ = require 'underscore'
{Chrome} = require 'osx'
File = require 'fs'
App = require 'app'
activeWindow = App.activeWindow
ace = require 'ace/ace'
canon = require 'pilot/canon'
@ -16,31 +17,31 @@ editor.getSession().setTabSize 2
filename = null
editor.getSession().on 'change', ->
Chrome.setDirty true
activeWindow.setDirty true
save = ->
File.write filename, editor.getSession().getValue()
Chrome.setDirty false
activeWindow.setDirty false
editor._emit 'save', { filename }
exports.open = open = (path) ->
filename = path
if File.isDirectory filename
File.changeWorkingDirectory filename
Chrome.title _.last filename.split '/'
activeWindow.setTitle _.last filename.split '/'
editor.getSession().setValue ""
Chrome.setDirty false
activeWindow.setDirty false
else
if /png|jpe?g|gif/i.test filename
Chrome.openURL filename
App.openURL filename
else
Chrome.title _.last filename.split '/'
activeWindow.setTitle _.last filename.split '/'
editor.getSession().setValue File.read filename
Chrome.setDirty false
activeWindow.setDirty false
editor._emit 'open', { filename }
saveAs = ->
if file = Chrome.savePanel()
if file = App.savePanel()
filename = file
Chrome.title _.last filename.split '/'
activeWindow.setTitle _.last filename.split '/'
save()
exports.bindKey = bindKey = (name, shortcut, callback) ->
canon.addCommand
@ -59,12 +60,12 @@ exports.resize = (timeout=1) ->
exports.resize(200)
bindKey 'open', 'Command-O', (env, args, request) ->
if file = Chrome.openPanel()
if file = App.openPanel()
open file
bindKey 'openURL', 'Command-Shift-O', (env, args, request) ->
if url = prompt "Enter URL:"
Chrome.openURL url
App.openURL url
bindKey 'saveAs', 'Command-Shift-S', (env, args, request) ->
saveAs()
@ -73,15 +74,15 @@ bindKey 'save', 'Command-S', (env, args, request) ->
if filename then save() else saveAs()
bindKey 'new', 'Command-N', (env, args, request) ->
Chrome.createWindow()
App.newWindow()
bindKey 'copy', 'Command-C', (env, args, request) ->
text = editor.getSession().doc.getTextRange editor.getSelectionRange()
Chrome.writeToPasteboard text
App.writeToPasteboard text
bindKey 'cut', 'Command-X', (env, args, request) ->
text = editor.getSession().doc.getTextRange editor.getSelectionRange()
Chrome.writeToPasteboard text
App.writeToPasteboard text
editor.session.remove editor.getSelectionRange()
bindKey 'eval', 'Command-R', (env, args, request) ->
@ -118,15 +119,12 @@ bindKey 'home', 'Alt-Shift-,', (env) ->
bindKey 'end', 'Alt-Shift-.', (env) ->
env.editor.navigateFileEnd()
bindKey 'fullscreen', 'Command-Shift-Return', (env) ->
Chrome.toggleFullscreen()
bindKey 'console', 'Command-Ctrl-k', (env) ->
Chrome.inspector().showConsole(1)
activeWindow.inspector().showConsole(1)
bindKey 'reload', 'Command-Ctrl-r', (env) ->
Chrome.createWindow()
WindowController.close()
App.newWindow()
activeWindow.close()
# this should go in coffee.coffee or something
bindKey 'consolelog', 'Ctrl-L', (env) ->

View File

@ -1,91 +0,0 @@
# This is the CoffeeScript API that wraps all of Cocoa.
$ = require 'jquery'
_ = require 'underscore'
jscocoa = require 'jscocoa'
Editor = require 'editor'
File = require 'fs'
# Handles the UI chrome
Chrome =
addPane: (position, html) ->
verticalDiv = $('#app-vertical')
horizontalDiv = $('#app-horizontal')
el = document.createElement "div"
el.setAttribute 'class', "pane " + position
el.innerHTML = html
el.addEventListener 'DOMNodeInsertedIntoDocument', ->
Editor.resize()
, false
el.addEventListener 'DOMNodeRemovedFromDocument', ->
Editor.resize()
, false
switch position
when 'top', 'main'
verticalDiv.prepend el
when 'left'
horizontalDiv.prepend el
when 'bottom'
verticalDiv.append el
when 'right'
horizontalDiv.append el
else
throw "I DON'T KNOW HOW TO DEAL WITH #{position}"
inspector:->
@_inspector ?= WindowController.webView.inspector
# path - Optional. The String path to the file to base it on.
createWindow: (path) ->
c = OSX.AtomWindowController.alloc.initWithWindowNibName "AtomWindow"
c.window
c.window.makeKeyAndOrderFront null
# Set the active window's dirty status.
setDirty: (bool) ->
Chrome.activeWindow().setDocumentEdited bool
# Returns a boolean
dirty: ->
Chrome.activeWindow().isDocumentEdited()
# Returns the active NSWindow object
activeWindow: ->
OSX.NSApplication.sharedApplication.keyWindow
# Returns null or a file path.
openPanel: ->
panel = OSX.NSOpenPanel.openPanel
panel.setCanChooseDirectories true
if panel.runModal isnt OSX.NSFileHandlingPanelOKButton
return null
panel.filenames.lastObject
# Returns null or a file path.
savePanel: ->
panel = OSX.NSSavePanel.savePanel
if panel.runModal isnt OSX.NSFileHandlingPanelOKButton
return null
panel.filenames.lastObject
writeToPasteboard: (text) ->
pb = OSX.NSPasteboard.generalPasteboard
pb.declareTypes_owner [OSX.NSStringPboardType], null
pb.setString_forType text, OSX.NSStringPboardType
openURL: (url) ->
window.location = url
Chrome.title _.last url.replace(/\/$/,'').split '/'
title: (text) ->
WindowController.window.title = text
appRoot: ->
OSX.NSBundle.mainBundle.resourcePath
exports.Chrome = Chrome

View File

@ -1,10 +1,10 @@
$ = require 'jquery'
_ = require 'underscore'
{Chrome} = require 'osx'
File = require 'fs'
App = require 'app'
_.map File.list(Chrome.appRoot() + "/plugins"), (plugin) ->
_.map File.list(App.root + "/plugins"), (plugin) ->
require plugin
if css = File.read "~/.atomicity/twilight.css"

6
src/startup.coffee Normal file
View File

@ -0,0 +1,6 @@
App = require 'app'
Window = require 'window'
App.setActiveWindow new Window controller: WindowController
require 'editor'
require 'plugins'

62
src/window.coffee Normal file
View File

@ -0,0 +1,62 @@
$ = require 'jquery'
module.exports =
class Window
controller: null
document: null
nswindow: null
panes: []
constructor: (options={}) ->
@controller = options.controller
@document = options.document
@nswindow = options.controller?.window
addPane: (position, html) ->
Editor = require 'editor'
verticalDiv = $('#app-vertical')
horizontalDiv = $('#app-horizontal')
el = document.createElement "div"
el.setAttribute 'class', "pane " + position
el.innerHTML = html
el.addEventListener 'DOMNodeInsertedIntoDocument', ->
Editor.resize()
, false
el.addEventListener 'DOMNodeRemovedFromDocument', ->
Editor.resize()
, false
switch position
when 'top', 'main'
verticalDiv.prepend el
when 'left'
horizontalDiv.prepend el
when 'bottom'
verticalDiv.append el
when 'right'
horizontalDiv.append el
else
throw "I DON'T KNOW HOW TO DEAL WITH #{position}"
close: ->
@controller.close()
isDirty: ->
@nswindow.isDocumentEdited()
# Set the active window's dirty status.
setDirty: (bool) ->
@nswindow.setDocumentEdited bool
inspector:->
@_inspector ?= WindowController.webView.inspector
title: ->
@nswindow.title
setTitle: (title) ->
@nswindow.title = title

View File

@ -67,11 +67,7 @@
</body>
<script>
require('underscore');
require('jquery');
require('coffee-script');
require('editor');
require('plugins');
require('startup');
</script>
</html>