-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccelerationWindowController.m
executable file
·124 lines (100 loc) · 3.94 KB
/
AccelerationWindowController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// AccelerationWindowController.m
// Mousy
//
// Created by Basil Shkara on 22/08/09.
// Copyright 2009 Oiled Machine Pty Ltd. All rights reserved.
//
// Note: This is basically SetMouseAcclSample from Apple.
// http://developer.apple.com/Samplecode/SetMouseAcclSample/index.html
//
#import "AccelerationWindowController.h"
// Amount divided by 10 as increment/decrement amount
static NSUInteger kStandardIncrement = 1;
// Corresponds to the maximum setting for the slider in the nib file
static NSUInteger kMaxSpeedSetting = 4;
@implementation AccelerationWindowController
@synthesize slider = slider_;
@synthesize textField = textField_;
@synthesize mouseInterface = mouseInterface_;
@synthesize accelerationValue = accelerationValue_;
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self setSlider:nil];
[self setTextField:nil];
[self setMouseInterface:nil];
[super dealloc];
}
- (void)awakeFromNib {
MouseInterface *mouseInterface = [[MouseInterface alloc] init];
self.mouseInterface = mouseInterface;
[mouseInterface release];
// Store the original value to restore if needed
self.accelerationValue = [self.mouseInterface mouseDefaultSpeed];
// Promote original values into View
[self.slider setFloatValue:self.accelerationValue];
[self.textField setFloatValue:self.accelerationValue];
// Register for notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowDidBecomeKey:)
name:NSWindowDidBecomeKeyNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowDidResignActive:)
name:NSWindowDidResignMainNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillTerminate:)
name:NSApplicationWillTerminateNotification object:nil];
}
// User wants to set the currently selected acceleration value as the default
- (IBAction)setMouseSpeedAsDefault:(id)sender {
self.accelerationValue = [self.slider floatValue];
}
- (IBAction)updateMouseSpeed:(id)sender {
// Read the current mouse value from the slider
CGFloat value = [self.slider floatValue];
// Set the text value of the mouse setting in the window
[self.textField setFloatValue:value];
// Set this value as the mouse speed
[self.mouseInterface setMouseSpeed:value];
}
- (IBAction)incrementMouseSpeed:(id)sender {
CGFloat value = [self.slider floatValue] * 10 + kStandardIncrement + 0.1;
NSInteger newValue = value;
value = newValue;
value = value / 10; // cannot use value = newValue / 10, since (newValue / 10) will force integer math
if (value > kMaxSpeedSetting) {
value = kMaxSpeedSetting;
}
[self.slider setFloatValue:value];
[self.textField setFloatValue:value];
[self.mouseInterface setMouseSpeed:value];
}
- (IBAction)decrementMouseSpeed:(id)sender {
CGFloat value = [self.slider floatValue] * 10 - kStandardIncrement + 0.1;
NSInteger newValue = value;
if (newValue < 0) {
newValue = 0;
}
value = newValue;
value = value / 10; // cannot use value = newValue / 10, since (newValue / 10) will force integer math
[self.slider setFloatValue:value];
[self.textField setFloatValue:value];
[self.mouseInterface setMouseSpeed:value];
}
#pragma mark -
#pragma mark Observer methods
// Restore the mouse acceleration setting to whatever is in slider
- (void)windowDidBecomeKey:(NSNotification *)aNotification {
CGFloat value = [self.slider floatValue];
[self.textField setFloatValue:value];
[self.mouseInterface setMouseSpeed:value];
}
// Restore the mouse acceleration setting to whatever the original value was
- (void)windowDidResignActive:(NSNotification *)aNotification {
[self.mouseInterface setMouseSpeed:self.accelerationValue];
}
// Restore the mouse acceleration setting to whatever the original value was
- (void)applicationWillTerminate:(NSNotification *)aNotification {
[self.mouseInterface setMouseSpeed:self.accelerationValue];
}
@end