Skip to content

Commit

Permalink
🔀 Merge pull request #150 from `MrKai77/77-option-to-use-macos-center…
Browse files Browse the repository at this point in the history
…-instead-of-screen-center`

✨ #77 Option to use macOS center instead of screen center
  • Loading branch information
MrKai77 authored Jan 2, 2024
2 parents ce78626 + 4013446 commit 40b9cec
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 96 deletions.
16 changes: 16 additions & 0 deletions Loop.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
A8878A252AA3B2C800850A66 /* WindowTransformAnimation.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8878A242AA3B2C800850A66 /* WindowTransformAnimation.swift */; };
A88E83C52B37B354009D332F /* CGEvent+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A88E83C42B37B354009D332F /* CGEvent+Extensions.swift */; };
A8A2ABE72A3FB0370067B5A9 /* KeybindMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8A2ABE62A3FB0370067B5A9 /* KeybindMonitor.swift */; };
A8B5E1632B43726C00044D30 /* CustomWindowActionAnchor.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8B5E1622B43726C00044D30 /* CustomWindowActionAnchor.swift */; };
A8B5E1652B43728200044D30 /* CustomWindowActionMeasureSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8B5E1642B43728200044D30 /* CustomWindowActionMeasureSystem.swift */; };
A8BE09DB2B113FD700DBB242 /* KeycorderModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8BE09DA2B113FD700DBB242 /* KeycorderModel.swift */; };
A8D5A7D62A91384D004EA5BB /* DirectionSelectorSquareSegment.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8D5A7D52A91384D004EA5BB /* DirectionSelectorSquareSegment.swift */; };
A8D5A7D82A913862004EA5BB /* DirectionSelectorCircleSegment.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8D5A7D72A913862004EA5BB /* DirectionSelectorCircleSegment.swift */; };
Expand Down Expand Up @@ -129,6 +131,8 @@
A8878A242AA3B2C800850A66 /* WindowTransformAnimation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowTransformAnimation.swift; sourceTree = "<group>"; };
A88E83C42B37B354009D332F /* CGEvent+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CGEvent+Extensions.swift"; sourceTree = "<group>"; };
A8A2ABE62A3FB0370067B5A9 /* KeybindMonitor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeybindMonitor.swift; sourceTree = "<group>"; };
A8B5E1622B43726C00044D30 /* CustomWindowActionAnchor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomWindowActionAnchor.swift; sourceTree = "<group>"; };
A8B5E1642B43728200044D30 /* CustomWindowActionMeasureSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomWindowActionMeasureSystem.swift; sourceTree = "<group>"; };
A8BE09DA2B113FD700DBB242 /* KeycorderModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeycorderModel.swift; sourceTree = "<group>"; };
A8D5A7D52A91384D004EA5BB /* DirectionSelectorSquareSegment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DirectionSelectorSquareSegment.swift; sourceTree = "<group>"; };
A8D5A7D72A913862004EA5BB /* DirectionSelectorCircleSegment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DirectionSelectorCircleSegment.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -254,6 +258,7 @@
A8F0125A2AEDD7660017307F /* WindowAction.swift */,
A8330AD32A3AC27600673C8D /* WindowDirection.swift */,
A83E1C342ABFCA3200853FE9 /* WindowRecords.swift */,
A8B5E1612B43725B00044D30 /* Custom Window Sizes */,
);
path = "Window Management";
sourceTree = "<group>";
Expand Down Expand Up @@ -329,6 +334,15 @@
name = Frameworks;
sourceTree = "<group>";
};
A8B5E1612B43725B00044D30 /* Custom Window Sizes */ = {
isa = PBXGroup;
children = (
A8B5E1622B43726C00044D30 /* CustomWindowActionAnchor.swift */,
A8B5E1642B43728200044D30 /* CustomWindowActionMeasureSystem.swift */,
);
path = "Custom Window Sizes";
sourceTree = "<group>";
};
A8E59C2C297F5E9A0064D4BA = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -510,9 +524,11 @@
A8E1575F298654960005761C /* AboutView.swift in Sources */,
A8DCC98A2981F43F00D41065 /* PreviewSettingsView.swift in Sources */,
A87376F62AA288EB001890F4 /* Window.swift in Sources */,
A8B5E1632B43726C00044D30 /* CustomWindowActionAnchor.swift in Sources */,
A8055EC22AFEDE0B00459D13 /* Keycorder.swift in Sources */,
A85CB5852ACFA5F700BF63E6 /* AppDelegate.swift in Sources */,
A8330AC52A3AC15900673C8D /* Notification+Extensions.swift in Sources */,
A8B5E1652B43728200044D30 /* CustomWindowActionMeasureSystem.swift in Sources */,
A8878A252AA3B2C800850A66 /* WindowTransformAnimation.swift in Sources */,
A827409A2AB0208500B9BDC5 /* TriggerKeycorder.swift in Sources */,
A85FEEBC2AF15CDE00354D79 /* KeybindCustomizationViewItem.swift in Sources */,
Expand Down
18 changes: 11 additions & 7 deletions Loop/Managers/LoopManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,7 @@ class LoopManager: ObservableObject {
self.closeLoop(forceClose: true)
}

if self.currentlyPressedModifiers.contains(event.keyCode) {
self.currentlyPressedModifiers.remove(event.keyCode)
} else if event.modifierFlags.rawValue == 256 {
self.currentlyPressedModifiers = []
} else {
self.currentlyPressedModifiers.insert(event.keyCode)
}
processModifiers(event)

// Why sort the set? I have no idea. But it works much more reliably when sorted!
if self.currentlyPressedModifiers.sorted().contains(Defaults[.triggerKey].sorted()) {
Expand Down Expand Up @@ -243,6 +237,16 @@ class LoopManager: ObservableObject {
}
}

private func processModifiers(_ event: NSEvent) {
if self.currentlyPressedModifiers.contains(event.keyCode) {
self.currentlyPressedModifiers.remove(event.keyCode)
} else if event.modifierFlags.rawValue == 256 {
self.currentlyPressedModifiers = []
} else {
self.currentlyPressedModifiers.insert(event.keyCode)
}
}

private func openLoop() {
guard self.isLoopActive == false else { return }

Expand Down
8 changes: 0 additions & 8 deletions Loop/Preview Window/PreviewView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,13 @@ struct PreviewView: View {
self._currentAction = State(initialValue: startingAction)
}

@Default(.useSystemAccentColor) var useSystemAccentColor
@Default(.customAccentColor) var customAccentColor
@Default(.useGradient) var useGradient
@Default(.gradientColor) var gradientColor

@Default(.previewVisibility) var previewVisibility
@Default(.previewPadding) var previewPadding
@Default(.windowPadding) var windowPadding
@Default(.previewCornerRadius) var previewCornerRadius
@Default(.previewBorderThickness) var previewBorderThickness
@Default(.animationConfiguration) var animationConfiguration

@Default(.respectStageManager) var respectStageManager
@Default(.stageStripSize) var stageStripSize

var body: some View {
GeometryReader { geo in
ZStack {
Expand Down
11 changes: 8 additions & 3 deletions Loop/Settings/Keybindings/Custom Keybinds/AnchorPicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct AnchorPicker: View {
HStack {
selectorCircle(.left)
Spacer()
selectorCircle(.center)
selectorCircle([.center, .macOSCenter])
Spacer()
selectorCircle(.right)
}
Expand Down Expand Up @@ -58,8 +58,13 @@ struct AnchorPicker: View {

@ViewBuilder
func selectorCircle(_ anchor: CustomWindowActionAnchor) -> some View {
selectorCircle([anchor])
}

@ViewBuilder
func selectorCircle(_ anchor: [CustomWindowActionAnchor]) -> some View {
Button {
self.anchor = anchor
self.anchor = anchor.first
} label: {
Circle()
.foregroundStyle(Color.accentColor)
Expand All @@ -69,7 +74,7 @@ struct AnchorPicker: View {
.strokeBorder(.white.opacity(0.5), lineWidth: 1)
}
.overlay {
if self.anchor == anchor {
if anchor.contains(where: { $0 == self.anchor }) {
Circle()
.foregroundStyle(.white)
.frame(width: 6, height: 6)
Expand Down
31 changes: 31 additions & 0 deletions Loop/Settings/Keybindings/Custom Keybinds/CustomKeybindView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import SwiftUI
struct CustomKeybindView: View {
@Binding var action: WindowAction
@Binding var isSheetShown: Bool
@State var showingInfo: Bool = false

@FocusState private var focusedField: String?

Expand All @@ -27,6 +28,36 @@ struct CustomKeybindView: View {
.padding(-10)
}

if self.action.anchor == .center || self.action.anchor == .macOSCenter {
Section {
Toggle(
isOn: Binding(
get: { self.action.anchor == .macOSCenter },
set: { self.action.anchor = $0 ? .macOSCenter : .center }
)
) {
HStack {
Text("Use MacOS Center")
if let moreInformation = WindowDirection.macOSCenter.moreInformation {
Button(action: {
self.showingInfo.toggle()
}, label: {
Image(systemName: "info.circle")
.font(.title3)
.foregroundStyle(.secondary)
})
.buttonStyle(.plain)
.popover(isPresented: $showingInfo, arrowEdge: .bottom) {
Text(moreInformation)
.multilineTextAlignment(.center)
.padding(8)
}
}
}
}
}
}

Section("Window Size") {
Picker("Configure using", selection: $action.measureSystem) {
ForEach(CustomWindowActionMeasureSystem.allCases) { system in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct KeybindCustomizationViewItem: View {
.buttonStyle(.plain)
.popover(isPresented: $showingInfo, arrowEdge: .bottom) {
Text(moreInformation)
.multilineTextAlignment(.center)
.padding(8)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// CustomWindowActionAnchor.swift
// Loop
//
// Created by Kai Azim on 2024-01-01.
//

import SwiftUI

enum CustomWindowActionAnchor: Int, Codable, CaseIterable, Identifiable {
var id: Self { self }

case topLeft = 0
case top = 1
case topRight = 2
case right = 3
case bottomRight = 4
case bottom = 5
case bottomLeft = 6
case left = 7
case center = 8
case macOSCenter = 9

var label: Text {
switch self {
case .topLeft: Text("\(Image(systemName: "arrow.up.left")) Top Left")
case .top: Text("\(Image(systemName: "arrow.up")) Top")
case .topRight: Text("\(Image(systemName: "arrow.up.right")) Top Right")
case .right: Text("\(Image(systemName: "arrow.right")) Right")
case .bottomRight: Text("\(Image(systemName: "arrow.down.right")) Bottom Right")
case .bottom: Text("\(Image(systemName: "arrow.down")) Bottom")
case .bottomLeft: Text("\(Image(systemName: "arrow.down.left")) Bottom Left")
case .left: Text("\(Image(systemName: "arrow.left")) Left")
case .center: Text("\(Image(systemName: "scope")) Center")
case .macOSCenter: Text("\(Image(systemName: "scope")) MacOS Center")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// CustomWindowActionMeasureSystem.swift
// Loop
//
// Created by Kai Azim on 2024-01-01.
//

import SwiftUI

enum CustomWindowActionMeasureSystem: Int, Codable, CaseIterable, Identifiable {
var id: Self { self }

case pixels = 0
case percentage = 1

var label: Text {
switch self {
case .pixels:
Text("\(Image(systemName: "rectangle.checkered")) Pixels")
case .percentage:
Text("\(Image(systemName: "percent")) Percentages")
}
}

var postscript: String {
switch self {
case .pixels: "px"
case .percentage: "%"
}
}
}
68 changes: 13 additions & 55 deletions Loop/Window Management/WindowAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,14 @@ extension WindowAction {
xLocation = 0
case .center:
xLocation = (parentWidth / 2) - (previewWidth / 2)
case .macOSCenter :
xLocation = (parentWidth / 2) - (previewWidth / 2)
default:
xLocation = 0
}
}

if self.direction == .center {
if self.direction == .center || self.direction == .macOSCenter {
xLocation = (parentWidth / 2) - (previewWidth / 2)
}

Expand Down Expand Up @@ -309,6 +311,9 @@ extension WindowAction {
yLocation = (parentHeight / 2) - (previewHeight / 2)
case .center:
yLocation = (parentHeight / 2) - (previewHeight / 2)
case .macOSCenter:
let yOffset = WindowEngine.getMacOSCenterYOffset(previewHeight, screenHeight: parentHeight)
yLocation = (parentHeight / 2) - (previewHeight / 2) + yOffset
default:
yLocation = 0
}
Expand All @@ -318,6 +323,11 @@ extension WindowAction {
yLocation = (parentHeight / 2) - (previewHeight / 2)
}

if self.direction == .macOSCenter {
let yOffset = WindowEngine.getMacOSCenterYOffset(previewHeight, screenHeight: parentHeight)
yLocation = (parentHeight / 2) - (previewHeight / 2) + yOffset
}

return yLocation
}

Expand All @@ -335,7 +345,7 @@ extension WindowAction {
}
}

if self.direction == .center, let window = window {
if self.direction == .center || self.direction == .macOSCenter, let window = window {
width = window.frame.width
}

Expand All @@ -356,62 +366,10 @@ extension WindowAction {
}
}

if self.direction == .center, let window = window {
if self.direction == .center || self.direction == .macOSCenter, let window = window {
height = window.frame.height
}

return height
}
}

// MARK: - Custom Keybinds
enum CustomWindowActionMeasureSystem: Int, Codable, CaseIterable, Identifiable {
var id: Self { self }

case pixels = 0
case percentage = 1

var label: Text {
switch self {
case .pixels:
Text("\(Image(systemName: "rectangle.checkered")) Pixels")
case .percentage:
Text("\(Image(systemName: "percent")) Percentages")
}
}

var postscript: String {
switch self {
case .pixels: "px"
case .percentage: "%"
}
}
}

enum CustomWindowActionAnchor: Int, Codable, CaseIterable, Identifiable {
var id: Self { self }

case topLeft = 0
case top = 1
case topRight = 2
case right = 3
case bottomRight = 4
case bottom = 5
case bottomLeft = 6
case left = 7
case center = 8

var label: Text {
switch self {
case .topLeft: Text("\(Image(systemName: "arrow.up.left")) Top Left")
case .top: Text("\(Image(systemName: "arrow.up")) Top")
case .topRight: Text("\(Image(systemName: "arrow.up.right")) Top Right")
case .right: Text("\(Image(systemName: "arrow.right")) Right")
case .bottomRight: Text("\(Image(systemName: "arrow.down.right")) Bottom Right")
case .bottom: Text("\(Image(systemName: "arrow.down")) Bottom")
case .bottomLeft: Text("\(Image(systemName: "arrow.down.left")) Bottom Left")
case .left: Text("\(Image(systemName: "arrow.left")) Left")
case .center: Text("\(Image(systemName: "scope")) Center")
}
}
}
Loading

0 comments on commit 40b9cec

Please sign in to comment.