Skip to content

Commit af6eb3c

Browse files
Added new snap-area for portrait top/bottom (#1675)
1 parent 62dc24c commit af6eb3c

File tree

3 files changed

+78
-24
lines changed

3 files changed

+78
-24
lines changed

Rectangle/Snapping/CompoundSnapArea/CompoundSnapArea.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import Foundation
1010

1111
enum CompoundSnapArea: Int, Codable {
1212

13-
case leftTopBottomHalf = -2, rightTopBottomHalf = -3, thirds = -4, portraitThirdsSide = -5, halves = -6, topSixths = -7, bottomSixths = -8, fourths = -9
13+
case leftTopBottomHalf = -2, rightTopBottomHalf = -3, thirds = -4, portraitThirdsSide = -5, halves = -6, topSixths = -7, bottomSixths = -8, fourths = -9, portraitTopBottomHalves = -10
1414

15-
static let all = [leftTopBottomHalf, rightTopBottomHalf, thirds, portraitThirdsSide, halves, topSixths, bottomSixths, fourths]
15+
static let all = [leftTopBottomHalf, rightTopBottomHalf, thirds, portraitThirdsSide, halves, topSixths, bottomSixths, fourths, portraitTopBottomHalves]
1616

1717
static let leftCompoundCalculation = LeftTopBottomHalfCalculation()
1818
static let rightCompoundCalculation = RightTopBottomHalfCalculation()
@@ -22,6 +22,7 @@ enum CompoundSnapArea: Int, Codable {
2222
static let topSixthsCalculation = TopSixthsCompoundCalculation()
2323
static let bottomSixthsCalculation = BottomSixthsCompoundCalculation()
2424
static let fourthsColumnCalculation = FourthsColumnCompoundCalculation()
25+
static let portraitTopBottomCalculation = TopBottomHalvesCalculation()
2526

2627
var displayName: String {
2728
switch self {
@@ -41,6 +42,8 @@ enum CompoundSnapArea: Int, Codable {
4142
return NSLocalizedString("Bottom sixths from corners; thirds", tableName: "Main", value: "", comment: "")
4243
case .fourths:
4344
return NSLocalizedString("Fourths columns", tableName: "Main", value: "", comment: "")
45+
case .portraitTopBottomHalves:
46+
return NSLocalizedString("Top/bottom halves", tableName: "Main", value: "", comment: "")
4447
}
4548
}
4649

@@ -62,6 +65,8 @@ enum CompoundSnapArea: Int, Codable {
6265
return Self.bottomSixthsCalculation
6366
case .fourths:
6467
return Self.fourthsColumnCalculation
68+
case .portraitTopBottomHalves:
69+
return Self.portraitTopBottomCalculation
6570
}
6671
}
6772

@@ -83,14 +88,16 @@ enum CompoundSnapArea: Int, Codable {
8388
return [.b]
8489
case .fourths:
8590
return [.t, .b]
91+
case .portraitTopBottomHalves:
92+
return [.l, .r]
8693
}
8794
}
8895

8996
var compatibleOrientation: [DisplayOrientation] {
9097
switch self {
9198
case .leftTopBottomHalf, .rightTopBottomHalf, .halves:
9299
return [.portrait, .landscape]
93-
case .portraitThirdsSide:
100+
case .portraitThirdsSide, .portraitTopBottomHalves:
94101
return [.portrait]
95102
case .thirds, .topSixths, .bottomSixths, .fourths:
96103
return [.landscape]

Rectangle/Snapping/CompoundSnapArea/HalvesCompoundCalculation.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,39 @@ struct LeftRightHalvesCompoundCalculation: CompoundSnapAreaCalculation {
6969
}
7070

7171
}
72+
73+
struct TopBottomHalvesCalculation: CompoundSnapAreaCalculation {
74+
private let marginTop = Defaults.snapEdgeMarginTop.cgFloat
75+
private let marginBottom = Defaults.snapEdgeMarginBottom.cgFloat
76+
private let ignoredSnapAreas = SnapAreaOption(rawValue: Defaults.ignoredSnapAreas.value)
77+
78+
func snapArea(cursorLocation loc: NSPoint, screen: NSScreen, directional: Directional, priorSnapArea: SnapArea?) -> SnapArea? {
79+
let frame = screen.frame
80+
let halfHeight = floor(frame.height / 2)
81+
let shortEdgeSize = Defaults.shortEdgeSnapAreaSize.cgFloat
82+
83+
if loc.y <= frame.minY + marginBottom + shortEdgeSize {
84+
let snapAreaOption: SnapAreaOption = loc.x < frame.midX ? .bottomLeftShort : .bottomRightShort
85+
if !ignoredSnapAreas.contains(snapAreaOption) {
86+
return SnapArea(screen: screen, directional: directional, action: .bottomHalf)
87+
}
88+
}
89+
90+
if loc.y >= frame.maxY - marginTop - shortEdgeSize {
91+
let snapAreaOption: SnapAreaOption = loc.x < frame.midX ? .topLeftShort : .topRightShort
92+
if !ignoredSnapAreas.contains(snapAreaOption) {
93+
return SnapArea(screen: screen, directional: directional, action: .topHalf)
94+
}
95+
}
96+
97+
if loc.y >= frame.minY && loc.y <= frame.minY + halfHeight {
98+
return SnapArea(screen: screen, directional: directional, action: .bottomHalf)
99+
}
100+
101+
if loc.y > frame.minY + halfHeight && loc.y <= frame.maxY {
102+
return SnapArea(screen: screen, directional: directional, action: .topHalf)
103+
}
104+
105+
return nil
106+
}
107+
}

Rectangle/mul.lproj/Main.xcstrings

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16207,7 +16207,24 @@
1620716207
"state" : "translated",
1620816208
"value" : "⋯"
1620916209
}
16210+
}
16211+
}
16212+
},
16213+
"Extra Shortcuts" : {
16214+
"extractionState" : "manual",
16215+
"localizations" : {
16216+
"en" : {
16217+
"stringUnit" : {
16218+
"state" : "new",
16219+
"value" : "Extra Shortcuts"
16220+
}
1621016221
},
16222+
"ru" : {
16223+
"stringUnit" : {
16224+
"state" : "translated",
16225+
"value" : "Дополнительные горячие клавиши"
16226+
}
16227+
}
1621116228
}
1621216229
},
1621316230
"F2S-fz-NVQ.title" : {
@@ -16768,23 +16785,6 @@
1676816785
}
1676916786
}
1677016787
},
16771-
"Extra Shortcuts" : {
16772-
"extractionState" : "manual",
16773-
"localizations" : {
16774-
"en" : {
16775-
"stringUnit" : {
16776-
"state" : "new",
16777-
"value" : "Extra Shortcuts"
16778-
}
16779-
},
16780-
"ru" : {
16781-
"stringUnit" : {
16782-
"state" : "translated",
16783-
"value" : "Дополнительные горячие клавиши"
16784-
}
16785-
},
16786-
}
16787-
},
1678816788
"F12-EV-Lfz.title" : {
1678916789
"comment" : "Class = \"NSTextFieldCell\"; title = \"First Third\"; ObjectID = \"F12-EV-Lfz\";",
1679016790
"extractionState" : "extracted_with_value",
@@ -27579,7 +27579,7 @@
2757927579
"state" : "translated",
2758027580
"value" : "Увеличить ширину"
2758127581
}
27582-
},
27582+
}
2758327583
}
2758427584
},
2758527585
"Lbh-J2-qVU.title" : {
@@ -39233,7 +39233,7 @@
3923339233
"state" : "translated",
3923439234
"value" : "Уменьшить ширину"
3923539235
}
39236-
},
39236+
}
3923739237
}
3923839238
},
3923939239
"snW-S8-Cw5.title" : {
@@ -41417,6 +41417,17 @@
4141741417
}
4141841418
}
4141941419
},
41420+
"Top/bottom halves" : {
41421+
"extractionState" : "manual",
41422+
"localizations" : {
41423+
"pl" : {
41424+
"stringUnit" : {
41425+
"state" : "translated",
41426+
"value" : "Górna/Dolna połowa"
41427+
}
41428+
}
41429+
}
41430+
},
4142041431
"tRr-pd-1PS.title" : {
4142141432
"comment" : "Class = \"NSMenuItem\"; title = \"Data Detectors\"; ObjectID = \"tRr-pd-1PS\";",
4142241433
"extractionState" : "manual",
@@ -46071,7 +46082,7 @@
4607146082
"state" : "translated",
4607246083
"value" : "Шаг ширины (px)"
4607346084
}
46074-
},
46085+
}
4607546086
}
4607646087
},
4607746088
"wpr-3q-Mcd.title" : {
@@ -50144,4 +50155,4 @@
5014450155
}
5014550156
},
5014650157
"version" : "1.0"
50147-
}
50158+
}

0 commit comments

Comments
 (0)