Skip to content

Commit

Permalink
#254 Add Preserve window size option in custom window action
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKai77 committed Mar 22, 2024
1 parent 206159f commit 8bd0ea2
Show file tree
Hide file tree
Showing 20 changed files with 35 additions and 1,034 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

19 changes: 17 additions & 2 deletions Loop/Settings/Keybindings/Custom Keybinds/CustomKeybindView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ struct CustomKeybindView: View {
ForEach(CustomWindowActionMeasureSystem.allCases) { system in
system.label
.tag(system as CustomWindowActionMeasureSystem?)
.if(self.action.preserveSize ?? false) { view in
view.foregroundStyle(.secondary)
}
}
}
.onChange(of: action.measureSystem) { _ in
Expand All @@ -79,10 +82,11 @@ struct CustomKeybindView: View {
}
}
}
.disabled(self.action.preserveSize ?? false)

CrispValueAdjuster(
"Width",
value: Binding<Double>( // Width is an optional
value: Binding<Double>(
get: { self.action.width ?? 0 },
set: { self.action.width = $0 }
),
Expand All @@ -92,10 +96,11 @@ struct CustomKeybindView: View {
postscript: action.measureSystem?.postscript ?? "",
lowerClamp: true
)
.disabled(self.action.preserveSize ?? false)

CrispValueAdjuster(
"Height",
value: Binding<Double>( // Height is an optional
value: Binding<Double>(
get: { self.action.height ?? 0 },
set: { self.action.height = $0 }
),
Expand All @@ -105,13 +110,23 @@ struct CustomKeybindView: View {
postscript: action.measureSystem?.postscript ?? "",
lowerClamp: true
)
.disabled(self.action.preserveSize ?? false)

Toggle(
"Preserve window size",
isOn: Binding(
get: { self.action.preserveSize ?? false },
set: { self.action.preserveSize = $0 }
)
)
}

Section {
HStack {
Text("Preview window size")
Spacer()
PreviewWindowButton(self.$action)
.disabled(self.action.preserveSize ?? false)
}
}
}
Expand Down
6 changes: 0 additions & 6 deletions Loop/Settings/Keybindings/KeybindingsSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,6 @@ struct KeybindingsSettingsView: View {
}
}

Menu("Positioning") {
ForEach(WindowDirection.positioning) { direction in
newDirectionButton(direction)
}
}

Menu("More") {
ForEach(WindowDirection.more) { direction in
newDirectionButton(direction)
Expand Down
64 changes: 18 additions & 46 deletions Loop/Window Management/WindowAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ struct WindowAction: Codable, Identifiable, Hashable, Equatable, Defaults.Serial
var anchor: CustomWindowActionAnchor?
var width: Double?
var height: Double?
var preserveSize: Bool?

var cycle: [WindowAction]?

Expand All @@ -74,53 +75,24 @@ struct WindowAction: Codable, Identifiable, Hashable, Equatable, Defaults.Serial
result.origin.y = bounds.height * frameMultiplyValues.minY
result.size.width = bounds.width * frameMultiplyValues.width
result.size.height = bounds.height * frameMultiplyValues.height
} else if direction.isPositioning {
guard let screenFrame = NSScreen.screenWithMouse?.visibleFrame else { return result }
let windowSize = window.size
result.size.width = windowSize.width / screenFrame.width
result.size.height = windowSize.height / screenFrame.height

switch direction {
case .macOSCenter:
let yOffset = WindowEngine.getMacOSCenterYOffset(result.height, screenHeight: bounds.height)
result.origin.x = bounds.midX - result.width / 2
result.origin.y = (bounds.midY - result.height / 2) + yOffset
case .center:
result.origin.x = bounds.midX - result.width / 2
result.origin.y = bounds.midY - result.height / 2
case .topLeft:
break
case .top:
result.origin.x = bounds.midX - result.width / 2
case .topRight:
result.origin.x = bounds.maxX - result.width
case .right:
result.origin.x = bounds.maxX - result.width
result.origin.y = bounds.midY - result.height / 2
case .bottomRight:
result.origin.x = bounds.maxX - result.width
result.origin.y = bounds.maxY - result.height
case .bottom:
result.origin.x = bounds.midX - result.width / 2
result.origin.y = bounds.maxY - result.height
case .bottomLeft:
result.origin.y = bounds.maxY - result.height
case .left:
result.origin.y = bounds.midY - result.height / 2
default:
break
}
} else if direction == .custom {
switch measureSystem {
case .pixels:
guard let screenFrame = NSScreen.screenWithMouse?.frame else { return result }
result.size.width = (width ?? screenFrame.width) / screenFrame.width
result.size.height = (height ?? screenFrame.height) / screenFrame.height
case .percentage:
result.size.width = bounds.width * ((width ?? 0) / 100.0)
result.size.height = bounds.height * ((height ?? 0) / 100.0)
case .none:
break
if let preserveSize, preserveSize {
guard let screenFrame = NSScreen.screenWithMouse?.visibleFrame else { return result }
let windowSize = window.size
result.size.width = windowSize.width / screenFrame.width
result.size.height = windowSize.height / screenFrame.height
} else {
switch measureSystem {
case .pixels:
guard let screenFrame = NSScreen.screenWithMouse?.frame else { return result }
result.size.width = (width ?? screenFrame.width) / screenFrame.width
result.size.height = (height ?? screenFrame.height) / screenFrame.height
case .percentage:
result.size.width = bounds.width * ((width ?? 0) / 100.0)
result.size.height = bounds.height * ((height ?? 0) / 100.0)
case .none:
break
}
}

switch anchor {
Expand Down
28 changes: 0 additions & 28 deletions Loop/Window Management/WindowDirection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,9 @@ enum WindowDirection: String, CaseIterable, Identifiable, Codable {
case initialFrame = "InitialFrame"
case hide = "Hide"
case minimize = "Minimize"

// Window translation (size won't be altered)
case macOSCenter = "MacOSCenter"
case center = "Center"

case topLeft = "TopLeft"
case top = "Top"
case topRight = "TopRight"
case right = "Right"
case bottomRight = "BottomRight"
case bottom = "Bottom"
case bottomLeft = "BottomLeft"
case left = "Left"

// To cycle through directions
case cycleTop = "CycleTop"
case cycleBottom = "CycleBottom"
Expand Down Expand Up @@ -97,9 +86,6 @@ enum WindowDirection: String, CaseIterable, Identifiable, Codable {
static var screenSwitching: [WindowDirection] {
[.nextScreen, .previousScreen]
}
static var positioning: [WindowDirection] {
[.center, .macOSCenter, .topLeft, .top, .topRight, .right, .bottomRight, .bottom, .bottomLeft, .left]
}
static var more: [WindowDirection] {
[.initialFrame, .undo, .custom, .cycle]
}
Expand All @@ -108,10 +94,6 @@ enum WindowDirection: String, CaseIterable, Identifiable, Codable {
WindowDirection.cyclable.contains(self)
}

var isPositioning: Bool {
WindowDirection.positioning.contains(self)
}

var willChangeScreen: Bool {
WindowDirection.screenSwitching.contains(self)
}
Expand Down Expand Up @@ -197,19 +179,9 @@ enum WindowDirection: String, CaseIterable, Identifiable, Codable {
case .initialFrame: Image("custom.backward.end.alt.fill.2.rectangle")
case .hide: Image("custom.rectangle.slash")
case .minimize: Image("custom.arrow.down.right.and.arrow.up.left.rectangle")

case .center: Image("custom.rectangle.center.inset.inset.filled")
case .macOSCenter: Image("custom.rectangle.center.inset.inset.filled")

case .topLeft: Image("custom.arrow.up.left.rectangle")
case .top: Image("custom.arrow.up.rectangle")
case .topRight: Image("custom.arrow.up.right.rectangle")
case .right: Image("custom.arrow.right.rectangle")
case .bottomRight: Image("custom.arrow.down.right.rectangle")
case .bottom: Image("custom.arrow.down.rectangle")
case .bottomLeft: Image("custom.arrow.down.left.rectangle")
case .left: Image("custom.arrow.left.rectangle")

case .cycleTop: Image(systemName: "rectangle.tophalf.inset.filled")
case .cycleBottom: Image(systemName: "rectangle.bottomhalf.inset.filled")
case .cycleRight: Image(systemName: "rectangle.righthalf.inset.filled")
Expand Down

0 comments on commit 8bd0ea2

Please sign in to comment.