Skip to content
2 changes: 2 additions & 0 deletions Loop/App/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
LoopSocketManager.shared.start()
WindowDragManager.shared.addObservers()
StashManager.shared.start()
InitialWindowFrameManager.shared.start()
AccessibilityManager.requestAccess()

// Wait for the app to settle before showing the update window
Expand Down Expand Up @@ -180,6 +181,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
LoopManager.shared.shutdown()
WindowDragManager.shared.shutdown()
StashManager.shared.shutdown()
InitialWindowFrameManager.shared.shutdown()
LoopSocketManager.shared.shutdown()
return .terminateNow
}
Expand Down
3 changes: 3 additions & 0 deletions Loop/Extensions/Defaults+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ extension Defaults.Keys {
/// Excluded apps
static let excludedApps = Key<[URL]>("excludedApps", default: [])

/// Maps an app's bundle identifier to the custom window action that sets its initial size and position.
static let initialWindowFrames = Key<[String: WindowAction]>("initialWindowFrames", default: [:])

// About
#if RELEASE
static let includeDevelopmentVersions = Key<Bool>("includeDevelopmentVersions", default: false)
Expand Down
11 changes: 11 additions & 0 deletions Loop/Settings Window/Settings/Behavior/BehaviorConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ struct BehaviorConfigurationView: View {

@State private var isPaddingConfigurationViewPresented = false

@State private var isPlacementPresented = false

var body: some View {
LuminareForm {
generalSection
Expand Down Expand Up @@ -91,6 +93,15 @@ struct BehaviorConfigurationView: View {
}
.luminareModalCornerRadius(24)
}

LuminareButton("Initial Window Placement", "Configure…") {
isPlacementPresented = true
}
.luminareModal(isPresented: $isPlacementPresented) {
InitialWindowPlacementConfigurationView(isPresented: $isPlacementPresented)
.frame(width: 400)
}
.luminareModalCornerRadius(24)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
//
// InitialWindowPlacementConfigurationView.swift
// Loop
//

import Defaults
import Luminare
import SwiftUI

struct InitialWindowPlacementConfigurationView: View {
@Binding var isPresented: Bool
@Default(.initialWindowFrames) private var frames
@State private var editingID: String?
@State private var draft = WindowAction(.custom)
@State private var selectedIDs = Set<String>()
@State private var pickerError: String?

var body: some View {
if let editingID {
CustomActionConfigurationView(
action: $draft,
// Done saves the edited placement and returns to the app list in the same modal.
isPresented: Binding(
get: { true },
set: { presented in
guard !presented else { return }
frames[editingID] = draft
selectedIDs = [editingID]
self.editingID = nil
}
),
placementAppName: appName(editingID)
)
} else {
LuminareForm {
Text("Initial Window Placement")
.font(.headline)

LuminareSection {
LuminareButtonRow {
Button("Add") { chooseApp() }
Button("Remove", role: .destructive) {
frames = frames.filter { !selectedIDs.contains($0.key) }
selectedIDs.removeAll()
}
.disabled(selectedIDs.isEmpty)
.keyboardShortcut(.delete)
}
.luminareRoundingBehavior(top: true)

LuminareList(items: .constant(frames.keys.sorted()), selection: $selectedIDs, id: \.self) { item in
let id = item.wrappedValue
HStack(spacing: 8) {
if let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: id) {
Image(nsImage: NSWorkspace.shared.icon(forFile: url.path))
.resizable()
.frame(width: 24, height: 24)
} else {
Image(systemName: "app")
.frame(width: 24, height: 24)
}

Text(verbatim: appName(id))
Spacer()

Button {
edit(id)
} label: {
Image(systemName: "slider.horizontal.3")
}
.buttonStyle(.plain)
.accessibilityLabel("Edit placement")
.help("Edit placement")
}
.padding(.horizontal, 12)
.moveDisabled(true)
} emptyView: {
VStack {
Text("No configured apps")
.font(.title3)
Text("Click “Add” to add an app")
.font(.caption)
}
.foregroundStyle(.secondary)
.padding()
}
.luminareListFixedHeight(until: 250)
.luminareRoundingBehavior(bottom: true)
}

if let pickerError {
Text(verbatim: pickerError)
.foregroundStyle(.secondary)
}

Button("Done") { isPresented = false }
.buttonStyle(.luminare(overrideUseMainStyle: true))
}
}
}

private func appName(_ id: String) -> String {
guard let url = NSWorkspace.shared.urlForApplication(withBundleIdentifier: id) else {
return id
}
return FileManager.default.displayName(atPath: url.path)
}

private func chooseApp() {
let panel = NSOpenPanel()
panel.allowedContentTypes = [.application]
panel.directoryURL = FileManager.default.urls(for: .applicationDirectory, in: .localDomainMask).first
pickerError = nil
guard panel.runModal() == .OK, let url = panel.url else {
return
}
guard let id = Bundle(url: url)?.bundleIdentifier else {
pickerError = String(localized: "This app has no bundle identifier.")
return
}
guard InitialWindowFrames.allowsPlacement(bundleID: id, url: url) else {
pickerError = String(localized: "Loop skips window placement for this app.")
return
}
edit(id)
}

private func edit(_ id: String) {
draft = frames[id] ?? WindowAction(.custom)
draft.direction = .custom
draft.sizeMode = .custom
editingID = id
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ struct CustomActionConfigurationView: View {
}
}

private let placementAppName: String?

private let anchors: [CustomWindowActionAnchor] = [
.topLeft, .top, .topRight, .left, .center, .right, .bottomLeft, .bottom, .bottomRight
]
Expand All @@ -47,7 +49,8 @@ struct CustomActionConfigurationView: View {
private let previewController = PreviewController()
private let screenSize: CGSize = NSScreen.main?.frame.size ?? NSScreen.screens[0].frame.size

init(action: Binding<WindowAction>, isPresented: Binding<Bool>) {
init(action: Binding<WindowAction>, isPresented: Binding<Bool>, placementAppName: String? = nil) {
self.placementAppName = placementAppName
_windowAction = action
_isPresented = isPresented
_action = State(initialValue: action.wrappedValue)
Expand All @@ -70,16 +73,21 @@ struct CustomActionConfigurationView: View {

@ViewBuilder
private func configurationSections() -> some View {
LuminareSection(outerPadding: 0) {
LuminareTextField(
WindowDirection.custom.name,
text: Binding(
get: { action.name ?? "" },
set: { action.name = $0 }
if let placementAppName {
Text(verbatim: placementAppName)
.font(.headline)
} else {
LuminareSection(outerPadding: 0) {
LuminareTextField(
WindowDirection.custom.name,
text: Binding(
get: { action.name ?? "" },
set: { action.name = $0 }
)
)
)
.luminareFilledStates(.none)
.luminareBorderedStates(.none)
.luminareFilledStates(.none)
.luminareBorderedStates(.none)
}
}

LuminareSection(outerPadding: 0) {
Expand Down Expand Up @@ -287,32 +295,34 @@ struct CustomActionConfigurationView: View {

@ViewBuilder
private func sizeConfiguration() -> some View {
LuminarePicker(
elements: CustomWindowActionSizeMode.allCases,
selection: Binding(
get: {
action.sizeMode ?? .custom
},
set: { newValue in
withAnimation(luminareAnimation) {
action.sizeMode = newValue
if placementAppName == nil {
LuminarePicker(
elements: CustomWindowActionSizeMode.allCases,
selection: Binding(
get: {
action.sizeMode ?? .custom
},
set: { newValue in
withAnimation(luminareAnimation) {
action.sizeMode = newValue
}
}
),
columns: 3
) { mode in
VStack(spacing: 4) {
mode.image
Text(mode.name)
}
),
columns: 3
) { mode in
VStack(spacing: 4) {
mode.image
Text(mode.name)
.padding(.vertical, 15)
.compositingGroup()
}
.padding(.vertical, 15)
.compositingGroup()
.luminareContentSize(hasFixedHeight: true)
.luminareRoundingBehavior(
top: true,
bottom: action.sizeMode != .custom
)
}
.luminareContentSize(hasFixedHeight: true)
.luminareRoundingBehavior(
top: true,
bottom: action.sizeMode != .custom
)

if action.sizeMode ?? .custom == .custom {
LuminareSlider(
Expand Down
Loading