-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6361718
commit 93996c9
Showing
6 changed files
with
120 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3481,6 +3481,9 @@ | |
} | ||
} | ||
} | ||
}, | ||
"Import from Rectangle" : { | ||
|
||
}, | ||
"In a galaxy far, far away... still no updates!" : { | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// | ||
// TranslationLayer.swift | ||
// Loop | ||
// | ||
// Created by Kami on 8/7/2024. | ||
// | ||
|
||
import AppKit | ||
import Defaults | ||
import Foundation | ||
|
||
/// Represents a keyboard shortcut configuration for a Rectangle action. | ||
struct RectangleShortcut: Codable { | ||
let keyCode: Int | ||
let modifierFlags: Int | ||
} | ||
|
||
/// Represents the configuration of Rectangle app shortcuts. | ||
struct RectangleConfig: Codable { | ||
let shortcuts: [String: RectangleShortcut] | ||
} | ||
|
||
/// Translates the RectangleConfig to an array of WindowActions for Loop. | ||
/// - Parameter rectangleConfig: The RectangleConfig instance to translate. (this works w/ both normal and pro) | ||
/// - Returns: An array of WindowAction instances corresponding to the RectangleConfig. | ||
func translateRectangleConfigToWindowActions(rectangleConfig: RectangleConfig) -> [WindowAction] { | ||
// Maps Rectangle direction keys to Loop's WindowDirection enum. | ||
let directionMapping: [String: WindowDirection] = [ | ||
"bottomHalf": .bottomHalf, | ||
"bottomRight": .bottomRightQuarter, | ||
"center": .center, | ||
"larger": .larger, | ||
"leftHalf": .leftHalf, | ||
"maximize": .maximize, | ||
"nextDisplay": .nextScreen, | ||
"previousDisplay": .previousScreen, | ||
"restore": .undo, | ||
"rightHalf": .rightHalf, | ||
"smaller": .smaller, | ||
"topHalf": .topHalf, | ||
"topLeft": .topLeftQuarter, | ||
"topRight": .topRightQuarter | ||
] | ||
|
||
// Converts the Rectangle shortcuts into Loop's WindowActions. | ||
return rectangleConfig.shortcuts.compactMap { direction, shortcut in | ||
guard let loopDirection = directionMapping[direction], !direction.contains("Todo") else { return nil } | ||
return WindowAction( | ||
loopDirection, | ||
keybind: Set([CGKeyCode(shortcut.keyCode)]), // Converts the integer keyCode to CGKeyCode. | ||
name: direction.capitalized.replacingOccurrences(of: " ", with: "") + "Cycle" | ||
) | ||
} | ||
} | ||
|
||
/// Initiates the import process for the RectangleConfig.json file. | ||
func importRectangleConfig() { | ||
let openPanel = NSOpenPanel() | ||
openPanel.prompt = "Select Rectangle Config File" | ||
openPanel.allowedContentTypes = [.json] | ||
|
||
// Presents a file open panel to the user. | ||
openPanel.begin { response in | ||
guard response == .OK, let selectedFile = openPanel.url else { return } | ||
|
||
// Attempts to decode the selected file into a RectangleConfig object. | ||
if let rectangleConfig = try? JSONDecoder().decode(RectangleConfig.self, from: Data(contentsOf: selectedFile)) { | ||
let windowActions = translateRectangleConfigToWindowActions(rectangleConfig: rectangleConfig) | ||
saveWindowActions(windowActions) | ||
} else { | ||
print("Error reading or translating RectangleConfig.json") | ||
} | ||
} | ||
} | ||
|
||
/// Saves the translated WindowActions into Loop's configuration and posts a notification. | ||
/// - Parameter windowActions: The array of WindowActions to save. | ||
func saveWindowActions(_ windowActions: [WindowAction]) { | ||
for action in windowActions { | ||
print("Direction: \(action.direction), Keybind: \(action.keybind), Name: \(action.name ?? "")") | ||
} | ||
|
||
// Stores the WindowActions into Loop's configuration. | ||
Defaults[.keybinds] = windowActions | ||
|
||
// Post a notification after saving the new keybinds | ||
NotificationCenter.default.post(name: .keybindsUpdated, object: nil) | ||
} | ||
|
||
/// Starts the import process for Rectangle configuration. | ||
func initiateImportProcess() { | ||
importRectangleConfig() | ||
} |