Skip to content

Commit

Permalink
cmd+enter is now a keyboard shortcut to a menu item, therefore users …
Browse files Browse the repository at this point in the history
…are now able to re-map the 'execute' command to whatever they would like. The issues with beeping on keypresses is now also fixed (mentioned in github issue #4
  • Loading branch information
kennethshawfriedman committed Aug 6, 2019
1 parent 55638df commit b00caa6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 60 deletions.
11 changes: 3 additions & 8 deletions Toski/Toski/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,13 @@ import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
@IBAction func executeCommand(_ sender: Any) {
NotificationCenter.default.post(name: NSNotification.Name.init(rawValue: "executeCommand"), object: nil)
}

@IBAction func executeCommand(_ sender: Any) {}

func applicationWillTerminate(_ aNotification: Notification) {

//Kills the Scheme Process
SchemeProcess.shared.terminate()


print("Schemer Program Ended")
print("Toski ended")
}
}
19 changes: 2 additions & 17 deletions Toski/Toski/CodeField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import Cocoa

class CodeField : NSTextView {

var parentVC : ViewController!

required init?(coder: NSCoder) {
super.init(coder: coder)

Expand Down Expand Up @@ -58,21 +60,4 @@ class CodeField : NSTextView {
let length = text.distance(from: r.lowerBound, to: r.upperBound)
return NSMakeRange(start, length)
}

//This function prevents the "error bell" sound from occuring on every keystroke
//This was first noticed in Toksi issue #4: https://github.com/kennethshawfriedman/Toski/issues/4
override func performKeyEquivalent(with event: NSEvent) -> Bool {
return true
}
}

// Helper function inserted by Swift 4.2 migrator.
fileprivate func convertToOptionalNSAttributedStringKeyDictionary(_ input: [String: Any]?) -> [NSAttributedString.Key: Any]? {
guard let input = input else { return nil }
return Dictionary(uniqueKeysWithValues: input.map { key, value in (NSAttributedString.Key(rawValue: key), value)})
}

// Helper function inserted by Swift 4.2 migrator.
fileprivate func convertFromNSAttributedStringKey(_ input: NSAttributedString.Key) -> String {
return input.rawValue
}
42 changes: 7 additions & 35 deletions Toski/Toski/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@ class ViewController: NSViewController {
//Setting Delegates
cf.delegate = self
cf.textStorage?.delegate = self

//watch for keydown
NSEvent.addLocalMonitorForEvents(matching: .keyDown) {
self.keyDown(with: $0)
return $0
}
cf.parentVC = self

//task.launchPath = "/usr/local/bin/mit-scheme" //this should eventually be detirmined per-machine (which is working in one of the playgrounds)
//The launchpath on my machine is "/usr/local/bin/mit-scheme", but if this is different on someone else's computer, i
Expand All @@ -62,6 +57,9 @@ class ViewController: NSViewController {

//The Results of a Scheme Execution come back from the REPL into this function:
outHandle.readabilityHandler = self.readingPipe

//notification to observe when someone pressed Cmd+Enter
NotificationCenter.default.addObserver(self, selector: #selector(executeCommand), name: NSNotification.Name(rawValue: "executeCommand"), object: nil)
}

//The Results of a Scheme Execution come back from the REPL into this function:
Expand Down Expand Up @@ -138,36 +136,10 @@ class ViewController: NSViewController {
//textfield can be edited as soon as Scheme as been launched
cf.isEditable = true
}

//called on every key-stroke of non-modifier keys
override func keyDown(with event: NSEvent) {

//not sure if this super call is needed. Didn't seem to hurt or help, but seems like it should be there
super.keyDown(with: event)

backspace = event.characters == "\u{7F}" //backspace is true if it was the backspace key
//Check if the command key is pressed, if it is: send to other function to handle
let commandKey:Bool = event.modifierFlags.contains(.command)
if (commandKey) {
let character:String = event.characters ?? ""
handleKeyPressWithCommand(with: character)
}
}

func handleKeyPressWithCommand(with character:String) {

print(character)
switch character {
case "\r": //this handles Cmd+enter
executeCommand()
break
default:
break
}
}


//This function is called on Cmd+Enter: it executes a call to Scheme Communication
func executeCommand() {
@objc func executeCommand() {
print("execute")
let dataToSubmit = SchemeComm.parseExecutionCommand(codingField: cf)
handleIn.write(dataToSubmit)
}
Expand Down

0 comments on commit b00caa6

Please sign in to comment.