-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathScaleController.swift
More file actions
121 lines (92 loc) · 3.39 KB
/
ScaleController.swift
File metadata and controls
121 lines (92 loc) · 3.39 KB
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
//
// ScaleController.swift
// Free Ruler
//
// Created by Kevin Meziere on 1/26/25.
// Copyright © 2025 Free Ruler. All rights reserved.
//
import Cocoa
class ScaleController: NSWindowController, NSWindowDelegate,
NSTextFieldDelegate, NotificationPoster
{
@IBOutlet weak var xScaleTextField: NSTextField!
@IBOutlet weak var yScaleTextField: NSTextField!
@IBOutlet weak var lockedRatioButton: NSButton!
var observers: [NSKeyValueObservation] = []
public var xRulerWidth: CGFloat = 0
public var yRulerHeight: CGFloat = 0
var lockedAspectRatio: Bool = true
override var windowNibName: String {
return "ScaleController"
}
override func windowDidLoad() {
super.windowDidLoad()
window?.isMovableByWindowBackground = true
lockedAspectRatio = prefs.xscale == prefs.yscale
}
func windowDidBecomeKey(_ notification: Notification) {
lockedRatioButton.state = lockedAspectRatio ? .on : .off
xScaleTextField.stringValue = String(
format: "%.5f", xRulerWidth * prefs.xscale)
yScaleTextField.stringValue = String(
format: "%.5f", yRulerHeight * prefs.yscale)
}
override func showWindow(_ sender: Any?) {
window?.makeKeyAndOrderFront(sender)
window?.center()
}
override func windowWillLoad() {
}
@IBAction func xValueChanged(_ sender: NSTextField) {
}
func controlTextDidChange(_ obj: Notification) {
let textField = obj.object as! NSTextField
// https://stackoverflow.com/a/52311371
var stringValue = textField.stringValue
let charSet = NSCharacterSet(charactersIn: "1234567890.").inverted
let chars = textField.stringValue.components(separatedBy: charSet)
stringValue = chars.joined()
// Second step : only one '.'
let comma = NSCharacterSet(charactersIn: ".")
let chuncks = stringValue.components(separatedBy: comma as CharacterSet)
switch chuncks.count {
case 0:
stringValue = ""
case 1:
stringValue = "\(chuncks[0])"
default:
stringValue = "\(chuncks[0]).\(chuncks[1])"
}
// replace string
textField.stringValue = stringValue
if textField.identifier?.rawValue == "xscale" {
if lockedRatioButton.state == .on {
yScaleTextField.stringValue = String(
format: "%.5f",
(CGFloat(yRulerHeight * Double(stringValue)!) / xRulerWidth)
)
}
}
if textField.identifier?.rawValue == "yscale" {
if lockedRatioButton.state == .on {
xScaleTextField.stringValue = String(
format: "%.5f",
(CGFloat(xRulerWidth * Double(stringValue)!) / yRulerHeight)
)
}
}
}
@IBAction func lockRatio(_ sender: Any) {
if lockedRatioButton.state == .on {
yScaleTextField.stringValue = String(
format: "%.5f",
(CGFloat(yRulerHeight * Double(xScaleTextField.stringValue)!)
/ xRulerWidth))
}
}
@IBAction func saveScale(_ sender: Any) {
prefs.xscale = Double(xScaleTextField.stringValue)! / xRulerWidth
prefs.yscale = Double(yScaleTextField.stringValue)! / yRulerHeight
self.window?.close()
}
}