Skip to content

Commit

Permalink
feat: center cursor in focused app (#16)
Browse files Browse the repository at this point in the history
Resolves #12
  • Loading branch information
wojciech-kulik authored Jan 24, 2025
1 parent bc287cf commit 2223a0f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 7 deletions.
8 changes: 4 additions & 4 deletions FlashSpace.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 5;
CURRENT_PROJECT_VERSION = 6;
DEVELOPMENT_TEAM = ZL6756Q8Q2;
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
Expand All @@ -437,7 +437,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 14.0;
MARKETING_VERSION = 0.4.5;
MARKETING_VERSION = 0.5.6;
PRODUCT_BUNDLE_IDENTIFIER = pl.wojciechkulik.FlashSpace.dev;
PRODUCT_NAME = "$(TARGET_NAME)-Dev";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand All @@ -454,7 +454,7 @@
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 5;
CURRENT_PROJECT_VERSION = 6;
DEVELOPMENT_TEAM = JT56B63DU5;
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
Expand All @@ -468,7 +468,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 14.0;
MARKETING_VERSION = 0.4.5;
MARKETING_VERSION = 0.5.6;
PRODUCT_BUNDLE_IDENTIFIER = pl.wojciechkulik.FlashSpace;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down
7 changes: 7 additions & 0 deletions FlashSpace/Focus/FocusManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ final class FocusManager {
toFocus?.window.focus()
toFocus?.app.activate()
toFocus?.window.focus()
centerCursorIfNeeded(in: toFocus?.frame)
}

private func centerCursorIfNeeded(in frame: CGRect?) {
guard settingsRepository.centerCursorOnFocusChange, let frame else { return }

CGWarpMouseCursorPosition(CGPoint(x: frame.midX, y: frame.midY))
}

private func getFocusedAppIndex() -> (Int, [String])? {
Expand Down
4 changes: 4 additions & 0 deletions FlashSpace/Settings/FocusSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ struct FocusSettingsView: View {
Toggle("Enable Focus Manager", isOn: $settings.enableFocusManagement)
}

Section {
Toggle("Center Cursor In Focused App", isOn: $settings.centerCursorOnFocusChange)
}

Section {
hotkey("Focus Left", for: $settings.focusLeft)
hotkey("Focus Right", for: $settings.focusRight)
Expand Down
20 changes: 18 additions & 2 deletions FlashSpace/Settings/SettingsRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import Foundation

struct AppSettings: Codable {
var enableFocusManagement: Bool?
var centerCursorOnFocusChange: Bool?
var focusLeft: HotKeyShortcut?
var focusRight: HotKeyShortcut?
var focusUp: HotKeyShortcut?
var focusDown: HotKeyShortcut?
var focusNextWorkspaceApp: HotKeyShortcut?
var focusPreviousWorkspaceApp: HotKeyShortcut?

var centerCursorOnWorkspaceChange: Bool?
var switchToPreviousWorkspace: HotKeyShortcut?
var switchToNextWorkspace: HotKeyShortcut?
var unassignFocusedApp: HotKeyShortcut?
Expand All @@ -30,7 +32,11 @@ final class SettingsRepository: ObservableObject {

// MARK: - Focus Manager

@Published var enableFocusManagement: Bool = true {
@Published var enableFocusManagement: Bool = false {
didSet { updateSettings() }
}

@Published var centerCursorOnFocusChange: Bool = false {
didSet { updateSettings() }
}

Expand Down Expand Up @@ -60,6 +66,10 @@ final class SettingsRepository: ObservableObject {

// MARK: - Workspaces

@Published var centerCursorOnWorkspaceChange: Bool = false {
didSet { updateSettings() }
}

@Published var switchToPreviousWorkspace: HotKeyShortcut? {
didSet { updateSettings() }
}
Expand Down Expand Up @@ -108,15 +118,19 @@ final class SettingsRepository: ObservableObject {

currentSettings = AppSettings(
enableFocusManagement: enableFocusManagement,
centerCursorOnFocusChange: centerCursorOnFocusChange,
focusLeft: focusLeft,
focusRight: focusRight,
focusUp: focusUp,
focusDown: focusDown,
focusNextWorkspaceApp: focusNextWorkspaceApp,
focusPreviousWorkspaceApp: focusPreviousWorkspaceApp,

centerCursorOnWorkspaceChange: centerCursorOnWorkspaceChange,
switchToPreviousWorkspace: switchToPreviousWorkspace,
switchToNextWorkspace: switchToNextWorkspace,
unassignFocusedApp: unassignFocusedApp,

enableIntegrations: enableIntegrations,
runScriptOnWorkspaceChange: runScriptOnWorkspaceChange
)
Expand All @@ -139,14 +153,16 @@ final class SettingsRepository: ObservableObject {

currentSettings = settings

enableFocusManagement = settings.enableFocusManagement ?? true
enableFocusManagement = settings.enableFocusManagement ?? false
centerCursorOnFocusChange = settings.centerCursorOnFocusChange ?? false
focusLeft = settings.focusLeft
focusRight = settings.focusRight
focusUp = settings.focusUp
focusDown = settings.focusDown
focusNextWorkspaceApp = settings.focusNextWorkspaceApp
focusPreviousWorkspaceApp = settings.focusPreviousWorkspaceApp

centerCursorOnWorkspaceChange = settings.centerCursorOnWorkspaceChange ?? false
switchToPreviousWorkspace = settings.switchToPreviousWorkspace
switchToNextWorkspace = settings.switchToNextWorkspace
unassignFocusedApp = settings.unassignFocusedApp
Expand Down
7 changes: 7 additions & 0 deletions FlashSpace/Settings/WorkspacesSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ struct WorkspacesSettingsView: View {

var body: some View {
Form {
Section(
footer: Text("This feature is triggered when the workspace is changed using hotkeys.")
.foregroundStyle(.secondary)
) {
Toggle("Center Cursor In Focused App", isOn: $settings.centerCursorOnWorkspaceChange)
}

Section(
footer: Text("These shortcuts cycle through workspaces on the display with the cursor.")
.foregroundStyle(.secondary)
Expand Down
10 changes: 9 additions & 1 deletion FlashSpace/Workspaces/WorkspaceManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ final class WorkspaceManager {
if setFocus {
let appToFocus = appsToShow.first { $0.localizedName == workspace.appToFocus }
let lastApp = appsToShow.first { $0.localizedName == workspace.apps.last }
(appToFocus ?? lastApp)?.focus()
let toFocus = appToFocus ?? lastApp
toFocus?.focus()
centerCursorIfNeeded(in: toFocus?.getFrame())
}
}

Expand All @@ -109,6 +111,12 @@ final class WorkspaceManager {
app.hide()
}
}

private func centerCursorIfNeeded(in frame: CGRect?) {
guard settingsRepository.centerCursorOnWorkspaceChange, let frame else { return }

CGWarpMouseCursorPosition(CGPoint(x: frame.midX, y: frame.midY))
}
}

extension WorkspaceManager {
Expand Down

0 comments on commit 2223a0f

Please sign in to comment.