From b00caa6fe2dc0ad734d774507ea00f55dbb1991d Mon Sep 17 00:00:00 2001 From: kennethshawfriedman Date: Mon, 5 Aug 2019 19:25:04 -0600 Subject: [PATCH] cmd+enter is now a keyboard shortcut to a menu item, therefore users 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 --- Toski/Toski/AppDelegate.swift | 11 +++------ Toski/Toski/CodeField.swift | 19 ++------------- Toski/Toski/ViewController.swift | 42 ++++++-------------------------- 3 files changed, 12 insertions(+), 60 deletions(-) diff --git a/Toski/Toski/AppDelegate.swift b/Toski/Toski/AppDelegate.swift index 1497945..8374f4a 100644 --- a/Toski/Toski/AppDelegate.swift +++ b/Toski/Toski/AppDelegate.swift @@ -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") } } diff --git a/Toski/Toski/CodeField.swift b/Toski/Toski/CodeField.swift index 184a84b..a8e1012 100644 --- a/Toski/Toski/CodeField.swift +++ b/Toski/Toski/CodeField.swift @@ -10,6 +10,8 @@ import Cocoa class CodeField : NSTextView { + var parentVC : ViewController! + required init?(coder: NSCoder) { super.init(coder: coder) @@ -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 } diff --git a/Toski/Toski/ViewController.swift b/Toski/Toski/ViewController.swift index ea81fa2..6fa9bec 100644 --- a/Toski/Toski/ViewController.swift +++ b/Toski/Toski/ViewController.swift @@ -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 @@ -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: @@ -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) }