Skip to content

Commit 9b6346c

Browse files
committed
Add label next to the GridSize slide, and improve efficiency
1 parent 888c58e commit 9b6346c

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

GridGuide/GridGuideApp.swift

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@ struct GridGuideApp: App {
4141

4242
class AppDelegate: NSObject, NSApplicationDelegate {
4343
private var statusItem: NSStatusItem!
44-
@objc dynamic var gridDensity: Double = 50 {
44+
private var valueLabel: NSTextField!
45+
@objc dynamic var gridSize: Double = 50 {
4546
didSet {
4647
NotificationCenter.default.post(
4748
name: NSNotification.Name("gridDensityChanged"),
4849
object: nil,
49-
userInfo: ["value": gridDensity]
50+
userInfo: ["value": gridSize]
5051
)
52+
valueLabel?.stringValue = "\(Int(gridSize))"
5153
}
5254
}
5355

@@ -65,16 +67,28 @@ class AppDelegate: NSObject, NSApplicationDelegate {
6567
}
6668
let menu = NSMenu()
6769

68-
// Add slider item
69-
let sliderItem = NSMenuItem()
70-
let slider = NSSlider(value: gridDensity,
71-
minValue: 20,
70+
// Add slider item with label
71+
let menuItem = NSMenuItem()
72+
let containerView = NSView(frame: NSRect(x: 0, y: 0, width: 200, height: 40))
73+
74+
let slider = NSSlider(value: gridSize,
75+
minValue: 40,
7276
maxValue: 100,
7377
target: self,
7478
action: #selector(sliderChanged(_:)))
75-
slider.frame = NSRect(x: 20, y: 0, width: 150, height: 20)
76-
sliderItem.view = slider
77-
menu.addItem(sliderItem)
79+
slider.frame = NSRect(x: 20, y: 10, width: 130, height: 20)
80+
81+
valueLabel = NSTextField(frame: NSRect(x: 160, y: 10, width: 30, height: 20))
82+
valueLabel.isEditable = false
83+
valueLabel.isBordered = false
84+
valueLabel.backgroundColor = .clear
85+
valueLabel.stringValue = "\(Int(gridSize))"
86+
87+
containerView.addSubview(slider)
88+
containerView.addSubview(valueLabel)
89+
90+
menuItem.view = containerView
91+
menu.addItem(menuItem)
7892

7993
menu.addItem(NSMenuItem.separator())
8094
menu.addItem(NSMenuItem(title: "Quit", action: #selector(quit), keyEquivalent: "q"))
@@ -86,6 +100,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
86100
}
87101

88102
@objc func sliderChanged(_ sender: NSSlider) {
89-
gridDensity = sender.doubleValue
103+
gridSize = round(sender.doubleValue)
90104
}
91105
}

GridGuide/GridGuideView.swift

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@ import SwiftUI
99
import AppKit
1010

1111
struct GridGuideView: View {
12-
@State private var gridSize: CGFloat = 50
12+
@State private var gridSize: Int = 50
13+
let screenWidth = NSScreen.main?.visibleFrame.width ?? 1440
14+
let screenHeight = NSScreen.main?.visibleFrame.height ?? 900
15+
1316
@State private var numColumns: Int = 30
1417
@State private var numRows: Int = 30
1518

19+
init() {
20+
_numColumns = State(initialValue: Int(screenWidth) / gridSize)
21+
_numRows = State(initialValue: Int(screenHeight) / gridSize)
22+
}
23+
1624
var body: some View {
1725
GeometryReader { proxy in
1826
VStack(spacing: 0) {
@@ -27,10 +35,9 @@ struct GridGuideView: View {
2735
}
2836
.onReceive(NotificationCenter.default.publisher(for: .gridDensityChanged)) { notification in
2937
if let value = notification.userInfo?["value"] as? Double {
30-
self.gridSize = CGFloat(value)
31-
self.numColumns = Int(ceil(NSScreen.main?.visibleFrame.width ?? 1440) / value)
32-
self.numRows = Int(ceil(NSScreen.main?.visibleFrame.height ?? 900) / value)
33-
print("Updated gridSize: \(gridSize), numColumns: \(numColumns), numRows: \(numRows)")
38+
self.gridSize = Int(value)
39+
self.numColumns = Int(screenWidth / value)
40+
self.numRows = Int(screenHeight / value)
3441
}
3542
}
3643
.allowsHitTesting(false)

0 commit comments

Comments
 (0)