Reading right mouse clicks number of clicks - macos

I have been experimenting with mouse clicks. I am ok with left mouse clicks getting raw values etc etc. I now want to add right mouse clicks. I have setup a basic example. What i would like to achieve if there is one mouse right click it performs one function, and if there is two mouse right clicks it performs a different function. The problem is if you do two mouse clicks it obviously cannot differentiate between the two and so fire the one mouse click function before performing the second mouse function.
I was thinking of maybe using a timer of some sort to record the number of click. But i end up going round in circles as i just seem to start the timers over and over. I'm hoping some one might help. thanks for reading here is the code.
Xcode 8 Swift 3 Mac OSX Sierra NOT IOS.. NOT IOS
import Cocoa
class MainWindowController: NSWindowController {
#IBOutlet weak var MyView: NSView!
override func windowDidLoad() {
super.windowDidLoad()
//Initialize mouse for Right Click numberOfClicksRequired = 1
let recogRightClick1 = NSClickGestureRecognizer(target: self, action: #selector(oneMouseClick))
recogRightClick1.buttonMask = 0x2
recogRightClick1.numberOfClicksRequired = 1
MyView.addGestureRecognizer(recogRightClick1)
//Initialize mouse for Right ClicknumberOfClicksRequired = 2
let recogRightClick2 = NSClickGestureRecognizer(target: self, action: #selector(twoMouseClick(myrec:myRightClick:)))
recogRightClick2.buttonMask = 0x2
recogRightClick2.numberOfClicksRequired = 2
MyView.addGestureRecognizer(recogRightClick2)
}//EO Overide
func oneMouseClick(myrec: NSPanGestureRecognizer,myRightClick:NSClickGestureRecognizer){
let rightClick = myRightClick.state.rawValue
print("oneMouseClick",rightClick)
}
func twoMouseClick(myrec: NSPanGestureRecognizer,myRightClick:NSClickGestureRecognizer){
let rightClick = myRightClick.state.rawValue
print("twoMouseClick",rightClick)
}
}//EnD oF thE wORld
UPDATE
I have re generated the code following the advice given. Now the code reflects more of what i wanted to do. My only problem is that I would like all the mouse operations to be triggered only inside 'myView' rather than within the main window. I thought it might have something to do with first responder but that doesn't seem to work. Again any thought would be appreciated. Please excuse any bad code i'm self taught.
Xcode 8 Swift 3 Mac OSX Sierra NOT IOS.. NOT IOS
import Cocoa
class MainWindowController: NSWindowController {
#IBOutlet weak var myView: NSView!
var mouseX:CGFloat = 0
var mouseY:CGFloat = 0
override func windowDidLoad() {
myView.window?.becomeFirstResponder()
myView.window?.acceptsMouseMovedEvents = true
super.windowDidLoad()
myView.wantsLayer = true
myView.layer?.backgroundColor = CGColor(red: 0.05, green: 0.57, blue: 0.80, alpha: 0.6)
NSEvent.addLocalMonitorForEvents(matching:.leftMouseDown){
self.mouseEventFunction(data: 1)
return $0
}
NSEvent.addLocalMonitorForEvents(matching:.leftMouseUp){
self.mouseEventFunction(data:2)
return $0
}
NSEvent.addLocalMonitorForEvents(matching:.rightMouseDown){
self.mouseEventFunction(data: 3)
return $0
}
NSEvent.addLocalMonitorForEvents(matching:.rightMouseUp){
self.mouseEventFunction(data: 4)
return $0
}
NSEvent.addLocalMonitorForEvents(matching:.mouseMoved) {
self.mouseX = NSEvent.mouseLocation().x
self.mouseY = NSEvent.mouseLocation().y
return $0 }
}//EO Overide
func mouseEventFunction (data: Int){
switch data{
case 1 :
print("LeftMouseDown")
case 2 :
print("LeftMouseUp")
case 3 :
print("RightMouseDown")
case 3 :
print("RightMouseUP")
default: break
}
if data == 1 {print("mouseX",mouseX,"mouseY",mouseY)}
}//eo mouseEvent
}//EnD oF thE wORld
UPDATE 2
I have now updated subClassing the view controller, so the mouse clicks are now only working in myView. I'm still having problems with 'func mouseDragged' What i need to achieve is the bottom left of my view is x = 0 and Y = 0. I had a try with converting but thats not working. hoping someone might guide me. thanks for reading here is the updated code.
Xcode 8 Swift 3 Mac OSX Sierra NOT IOS.. NOT IOS
import Cocoa
class MainWindowController: NSWindowController {
#IBOutlet weak var myView: NSView!
override func windowDidLoad() {
myView.window?.becomeFirstResponder()
myView.window?.acceptsMouseMovedEvents = true
window?.contentView?.addSubview(myView)
super.windowDidLoad()
}//EO Overide
}//EnD oF thE wORld
class testView: NSView {
override func draw(_ dirtyRect: NSRect) {
let backgroundColor = NSColor.lightGray
backgroundColor.set()
NSBezierPath.fill(bounds)
}
override func mouseDragged(with theEvent: NSEvent) {
let myLocationInWindow = theEvent.locationInWindow
let location = convert(myLocationInWindow, to: self)
Swift.print("myLocationInWindow",myLocationInWindow,"location",location)
}
override func mouseDown(with theEvent: NSEvent) {
Swift.print("mouseDown")
}
override func mouseUp(with theEvent: NSEvent) {
Swift.print("mouseUp clickCount: \(theEvent.clickCount)")
}
}//eo testView

To define mouse inside view you use
let myLocationInWindow = theEvent.locationInWindow
let location = convert(myLocationInWindow, from: nil)
where nil is the window
here is the final code
import Cocoa
class MainWindowController: NSWindowController {
#IBOutlet weak var myView: NSView!
override func windowDidLoad() {
super.windowDidLoad()
}//EO Overide
}//EnD oF thE wORld
class testView: NSView {
override func draw(_ dirtyRect: NSRect) {
let backgroundColor = NSColor.lightGray
backgroundColor.set()
NSBezierPath.fill(bounds)
}
override func mouseDragged(with theEvent: NSEvent) {
let myLocationInWindow = theEvent.locationInWindow
let location = convert(myLocationInWindow, from: nil)
Swift.print("location",location)
}
override func mouseDown(with theEvent: NSEvent) {
Swift.print("mouseDown")
}
override func mouseUp(with theEvent: NSEvent) {
Swift.print("mouseUp clickCount: \(theEvent.clickCount)")
}
}//eo testView

Related

OSX Swift , Draw a simple line with a push of a button

Have looked a lot for this but nothing found to match.
Real simple I want to draw a line buy a push of a button.
I have a class which is from NSView to do the drawing, this monitors a var and if it changes to redraw the view.
I can get the mouse click to call the view by changing the value and it draws just fine. But I cant get the button to work. It calls the drawing method but the view is not updated ???
I know its got to be something simple, but I just cannot find it.
Thanks in advance.
Below is my View class and View Controller.
//
// LineDrawer.swift
// TestDraw3
//
// Created by Colin Wood on 21/04/17.
// Copyright © 2017 Colin Wood. All rights reserved.
//
import Cocoa
class LineDrawer : NSView {
// Vars
var newLinear = NSBezierPath()
public var QQ: Int = 0 { // every time QQ changes redraw information in QQ
didSet{
boom() // drawing method
}
}
// Methods
override func draw(_ dirtyRect: NSRect) {
NSColor.red.set()
newLinear.lineWidth = 5
newLinear.lineCapStyle = NSLineCapStyle.roundLineCapStyle
newLinear.stroke()
}
func boom(){ // draw a simple line
Swift.print("In BooM")
Swift.print(QQ)
let lastPt = NSPoint(x: QQ, y: 1)
newLinear.move(to: lastPt)
let newPt = NSPoint(x: QQ, y: 50)
newLinear.line(to: newPt)
needsDisplay = true
}
override func mouseDown(with theEvent: NSEvent) {
QQ = QQ + 7 // just add 7 so you can see it being drawn after didSet
}
}
//
// ViewController.swift
// TestDraw3
//
// Created by Colin Wood on 15/04/17.
// Copyright © 2017 Colin Wood. All rights reserved.
//
import Cocoa
class ViewController: NSViewController {
// Vars
#IBOutlet weak var textField: NSTextField!
// Methods
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
#IBAction func button(_ sender: Any) {
let v = LineDrawer()
v.QQ = 300 // change QQ to call didSet
}
}
You need to add your LineDrawer as a subview. A good place, to add it, is in the viewDidLoad method:
private var line: LinerDrawer!
override func viewDidLoad() {
super.viewDidLoad()
line = LinerDrawer()
line.frame = NSMakeRect(0, 0, 200, 200)
line.QQ = 300
line.isHidden = true
view.addSubview(line)
}
And then finally show it when you hit the button:
#IBAction func button(_ sender: Any) {
line.isHidden = false
}
You still need to work out the correct frame for your LineDrawer view.

call a func in NSWindowController from custom class for an NSView

I have a "sheet" as NSWindowController in Swift, for OS X. I placed an NSView on the sheet, with a custom controller in a class called CustomView. Everything I do on the sheet and in the custom view controller works fine.
class MySheet: NSWindowController {
//...
}
class CustomView: NSView {
override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect) {
// Draw some things...
}
}
override func mouseDown(theEvent: NSEvent) {
// Change text on a label (or whatever) in MySheet()
}
}
Now I need to respond to user input in the CustomView, specifically, a mouse click on the view, and modify a label (or other control) back in MySheet(). This has to be very simple, but I don't know how to do it. The biggest stumbling block I have is that I don't have an instance of MySheet(); it is hidden in the way the system works. I can call back to a static func in MySheet(), but that won't let me change a label or other control through an #IBOutlet.
I know the above is a most likely a naive question, but would appreciate any help toward a solution.
Of course (!), this problem is supposed to be solved using a delegate, and when it is set up correctly it works fine. So I have like this:
protocol ClickableCurves {
func clickedOnCurve(index: Int)
}
class MySheet: NSWindowController, ClickableCurves {
private let lineColorCount = 16
func clickedOnCurve(index: Int) {
Swift.print("came back as \(index)")
}
override func windowDidLoad() {
super.windowDidLoad()
(curveView as! DefaultCurveAttrView).delegate = self
}
#IBOutlet weak var curveView: NSView!
}
class DefaultCurveAttrView: NSView {
var delegate: ClickableCurves? = nil
override func drawRect(dirtyRect: NSRect) {
super.drawRect(dirtyRect)
}
override func mouseDown(theEvent: NSEvent) {
let frameRelWindow = self.convertRect(bounds, toView: nil)
let x = theEvent.locationInWindow.x - frameRelWindow.origin.x
let y = theEvent.locationInWindow.y - frameRelWindow.origin.y
let xm = bounds.width / 10
let dy = bounds.height / CGFloat(lineColorCount + 1)
var yc: CGFloat = dy / 2
if x >= xm && x <= 9 * xm {
for i in 1 ... lineColorCount {
yc += dy
if yc > y || i == lineColorCount {
if delegate != nil {
delegate!.clickedOnCurve(i - 1)
}
break
}
}
}
}
}

Replace NSViewController under Swift2 Storyboard MAC OSX

I am new to Mac OSX and with Apple promoting the fact that the bodies of code are becoming similar decided to tell the folk I am writing code for we should be able to do a Mac OSX version. iPhone and iPad versions are all good and about to release second version so no issues there.
So I am subclassing NSWindowController to get access to the Toolbar and worked out how to remove and add items on the toolbar, but for the life of me I can not get one NSViewController (firstViewController) to dismiss and bring up the second NSViewController (secondViewController) in the same NSWindowController.
So the 2 issues are that
1. I want to be able to performSegueWithIdentifier from the first NSViewController in code and
2. bring up the second NSViewController by replacing the first NSViewController in the same NSWindowController.
If I add a button to the firstViewController and put a segue to the secondViewController then when I select the button the secondViewController comes up just fine but in a seperate window not the same NSWindowController that I want it to and the firstViewController does not get replaced but stays in the NSWindowController.
So I know the segue idea will work but its not working in code and when I do insert the segue from a button it works but into a seperate NSViewController that is not part of the NSWindowController.
I am trying to find some programming guide from Apple on the issue but no luck so far.
Here is an overview from my Storyboard:
Here is my NSWindowController subclassed and the func loginToMe2Team is trigger from the NSToolBar and its working just find as the print statements show up on the console.
import Cocoa
class me2teamWindowsController: NSWindowController {
#IBOutlet var mySignUp : NSToolbarItem!
#IBOutlet var myToolbar : NSToolbar!
let controller = ViewController()
override func windowDidLoad() {
super.windowDidLoad()
print("window loaded")
}
override func windowWillLoad() {
print("window will load")
}
#IBAction func logInToMe2Team(sender: AnyObject){
controller.LogIn() //THIS IS THE FUNC I AM TESTING WITH
}
#IBAction func signUpToMe2Team(sender: AnyObject){
controller.signUp()
}
Here is my NSViewController subclassed with the func LogIn. Its getting selected just fine but the performSegueWithIdentifier is not. And I did cut and past the Identifier to make absolutely sure it was the same.
import Cocoa
import WebKit
class ViewController: NSViewController {
#IBOutlet weak var theWebPage: WebView!
#IBOutlet weak var progressIndicator: NSProgressIndicator!
override func viewDidLoad() {
super.viewDidLoad()
let urlString = "https://thewebpage.com.au"
self.theWebPage.mainFrame.loadRequest(NSURLRequest(URL: NSURL(string: urlString)!))
}
override func viewDidAppear() {
}
func LogIn() {
print("I logged in")
self.performSegueWithIdentifier("goToTeamPage", sender: self)
//THIS IS THE BIT THATS NOT WORKING
}
func signUp() {
print("I have to sign up now")
}
override var representedObject: AnyObject? {
didSet {
}
}
func webView(sender: WebView!, didStartProvisionalLoadForFrame frame: WebFrame!)
{
self.progressIndicator.startAnimation(self)
}
func webView(sender: WebView!, didFinishLoadForFrame frame: WebFrame!)
{
self.progressIndicator.stopAnimation(self)
}
}
You need to use a custom segue class (or possibly NSTabViewController if it’s enough for your needs). Set the segue’s type to Custom, with your class name specified:
…and implement it. With no animation, it’s simple:
class ReplaceSegue: NSStoryboardSegue {
override func perform() {
if let src = self.sourceController as? NSViewController,
let dest = self.destinationController as? NSViewController,
let window = src.view.window {
// this updates the content and adjusts window size
window.contentViewController = dest
}
}
}
In my case, I was using a sheet and wanted to transition to a different sheet with a different size, so I needed to do more:
class ReplaceSheetSegue: NSStoryboardSegue {
override func perform() {
if let src = self.sourceController as? NSViewController,
let dest = self.destinationController as? NSViewController,
let window = src.view.window {
// calculate new frame:
var rect = window.frameRectForContentRect(dest.view.frame)
rect.origin.x += (src.view.frame.width - dest.view.frame.width) / 2
rect.origin.y += src.view.frame.height - dest.view.frame.height
// don’t shrink visible content, prevent minsize from intervening:
window.contentViewController = nil
// animate resizing (TODO: crossover blending):
window.setFrame(window.convertRectToScreen(rect), display: true, animate: true)
// set new controller
window.contentViewController = dest
}
}
}

NSClickGestureRecognizer not working on NSStatusItem

Trying to recognize a right click on a NSStatusItem I got a suggestion ( Thanks to Zoff Dino ) to use a NSClickGestureRecognizer for that. But for some bizarre reason it isn't working as it should be. I am able to recognize a left click (buttonMask = 0x1) but not a right-click (buttonMask = 0x2). This is how I would like it to work but it isn't:
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
if let button = statusItem.button {
// Add right click functionality
let gesture = NSClickGestureRecognizer()
gesture.buttonMask = 0x2 // right mouse
gesture.target = self
gesture.action = "rightClickAction:"
button.addGestureRecognizer(gesture)
}}
func rightClickAction(sender: NSGestureRecognizer) {
if let button = sender.view as? NSButton {
NSLog("rightClick")
}
}
UPDATE:
I still did not manage to gets to work. Somehow it doesn't react on a right click (but changing the code on a left click) does. I guess some really simple issues are occurring that seem to block it from working. Even stranger is the fact that gesture.buttonMask = 0x1 works on the left click.
An alternative solution rather than NSClickGestureRecognizer is to attach a custom view to the status bar and handle the event from there.
The small disadvantage is you have to take care of the drawing and menu delegate methods.
Here a simple example:
Create a file StatusItemView a subclass of NSView
import Cocoa
class StatusItemView: NSView, NSMenuDelegate {
//MARK: - Variables
weak var statusItem : NSStatusItem!
var menuVisible = false
var image : NSImage! {
didSet {
if image != nil {
statusItem.length = image.size.width
needsDisplay = true
}
}
}
//MARK: - Override functions
override func mouseDown(theEvent: NSEvent) {
if let hasMenu = menu {
hasMenu.delegate = self
statusItem.popUpStatusItemMenu(hasMenu)
needsDisplay = true
}
}
override func rightMouseDown(theEvent: NSEvent) {
Swift.print(theEvent)
}
//MARK: - NSMenuDelegate
func menuWillOpen(menu: NSMenu) {
menuVisible = true
needsDisplay = true
}
func menuDidClose(menu: NSMenu) {
menuVisible = false
menu.delegate = nil
needsDisplay = true
}
//MARK: - DrawRect
override func drawRect(dirtyRect: NSRect) {
statusItem.drawStatusBarBackgroundInRect(bounds, withHighlight:menuVisible)
let origin = NSMakePoint(2.0, 3.0) // adjust origin if necessary
image?.drawAtPoint(origin, fromRect: dirtyRect, operation: .CompositeSourceOver, fraction: 1.0)
}
}
In AppDelegate you need a reference to the custom menu and an instance variable for the NSStatusItem instance
#IBOutlet weak var menu : NSMenu!
var statusItem : NSStatusItem!
In applicationDidFinishLaunching create the view and attach it to the status item. Be aware to set the image of the view after attaching it to make sure the width is considered.
func applicationDidFinishLaunching(aNotification: NSNotification) {
statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-1) // NSVariableStatusItemLength)
let statusItemView = StatusItemView(frame: NSRect(x: 0.0, y: 0.0, width: statusItem.length, height: 22.0))
statusItemView.statusItem = statusItem;
statusItemView.menu = menu
statusItem.view = statusItemView
statusItemView.image = NSImage(named: NSImageNameStatusAvailable)
}
The special case control-click to trigger the right-click function is not implemented.

Is it possible to change value by scrolling OSX trackpad (Swift)?

Is it possible to change a value in Cocoa depending on the direction of the trackpad scroll/ magic mouse 'wheel' scroll etc.
For example
var value = 0
func startsScrolling() {
if direction == up {
value += 5
} else if direction == down {
value -= 5
}
}
This would continuous so as you scroll up on the trackpad the direction is down and so it takes 5 off every time the fingers move up, and vice versa.
Thanks and sorry for the bad pseudo code.
You can create a custom window and override scrollWheel NSEvent. You can check the scrollingDeltaX and scrollingDeltaY properties.
class CustomWindow: NSWindow {
override func scrollWheel(theEvent: NSEvent) {
println( "Mouse Scroll scrollingDeltaX = \(theEvent.scrollingDeltaX)" )
println( "Mouse Scroll scrollingDeltaY = \(Int(theEvent.scrollingDeltaY))" )
}
}
If you are using storyboards you have to override scrollWheel NSEvent of your viewController:
import Cocoa
class ViewController: NSViewController {
#IBOutlet weak var strDeltaX: NSTextField!
#IBOutlet weak var strDeltaY: NSTextField!
#IBOutlet weak var strScrollingDeltaX: NSTextField!
#IBOutlet weak var strScrollingDeltaY: NSTextField!
#IBOutlet weak var strValueX: NSTextField!
#IBOutlet weak var strValueY: NSTextField!
var xValue:CGFloat = 0
var yValue:CGFloat = 0
override func viewDidLoad() {
super.viewDidLoad()
}
override var representedObject: AnyObject? {
didSet {
}
}
override func scrollWheel(theEvent: NSEvent) {
strDeltaX.stringValue = "deltaX: \(theEvent.deltaX)"
strDeltaY.stringValue = "deltaY: \(theEvent.deltaY)"
strScrollingDeltaX.stringValue = "scrollingDeltaX: \(theEvent.scrollingDeltaX)"
strScrollingDeltaY.stringValue = "scrollingDeltaY: \(theEvent.scrollingDeltaY)"
xValue += theEvent.scrollingDeltaX
yValue += theEvent.scrollingDeltaY
strValueX.stringValue = "xValue: \(xValue)"
strValueY.stringValue = "yValue: \(yValue)"
}
}
First you need to add a scroll view to your ViewController and link it. You can make it transparent if you don't want it to show. If you have buttons on your ViewController you can send it to back. I've converted code from objective-c. More power to you :)
var pointNow: CGPoint?
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
pointNow = scrollView.contentOffset
}
func scrollViewDidScroll(scrollView: UIScrollView) {
if scrollView.contentOffset.y < pointNow!.y {
println("down")
}
else {
if scrollView.contentOffset.y > pointNow!.y {
println("up")
}
}
}

Resources