Skip to content

Commit

Permalink
#62 Ability to hide windows
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKai77 committed Oct 24, 2023
1 parent 26ad7e5 commit 57a8fea
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"info" : {
"author" : "xcode",
"version" : 1
},
"symbols" : [
{
"filename" : "custom.rectangle.inset.filled.slash.svg",
"idiom" : "universal"
}
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion Loop/MenuBar/MenuBarResizeButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct MenuBarResizeButton: View {
}
} label: {
HStack {
if let image = direction.image {
if let image = direction.menuBarImage {
image
}
Text(name)
Expand Down
15 changes: 11 additions & 4 deletions Loop/Preview Window/PreviewView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ struct PreviewView: View {
.bottomThird,
.bottomTwoThirds,
.noAction,
.lastDirection:
.lastDirection,
.hide:
Rectangle()
.frame(height: currentResizeDirection == .bottomThird ? geo.size.height / 3 * 2 : nil)
default:
Expand All @@ -59,7 +60,8 @@ struct PreviewView: View {
.rightThird,
.rightTwoThirds,
.noAction,
.lastDirection:
.lastDirection,
.hide:
Rectangle()
.frame(width: currentResizeDirection == .rightThird ? geo.size.width / 3 * 2 : nil)
default:
Expand Down Expand Up @@ -92,6 +94,8 @@ struct PreviewView: View {
height: currentResizeDirection == .initialFrame ? 0 : nil)
.frame(width: currentResizeDirection == .lastDirection ? 0 : nil,
height: currentResizeDirection == .lastDirection ? 0 : nil)
.frame(width: currentResizeDirection == .hide ? 0 : nil,
height: currentResizeDirection == .hide ? 0 : nil)

.frame(width: currentResizeDirection == .center ?
(window?.size.width ?? 10) - previewPadding + previewBorderThickness / 2 : nil,
Expand All @@ -112,7 +116,8 @@ struct PreviewView: View {
.leftThird,
.leftTwoThirds,
.noAction,
.lastDirection:
.lastDirection,
.hide:
Rectangle()
.frame(width: currentResizeDirection == .leftThird ? geo.size.width / 3 * 2 : nil)
default:
Expand All @@ -129,7 +134,8 @@ struct PreviewView: View {
.topThird,
.topTwoThirds,
.noAction,
.lastDirection:
.lastDirection,
.hide:
Rectangle()
.frame(height: currentResizeDirection == .topThird ? geo.size.height / 3 * 2 : nil)
default:
Expand All @@ -139,6 +145,7 @@ struct PreviewView: View {
}
.foregroundColor(.clear)
.opacity(currentResizeDirection == .noAction ? 0 : 1)
.opacity(currentResizeDirection == .hide ? 0 : 1)
.animation(.interpolatingSpring(stiffness: 250, damping: 25), value: currentResizeDirection)
.onReceive(.directionChanged) { obj in
if let direction = obj.userInfo?["direction"] as? WindowDirection, !self.previewMode, !direction.cyclable {
Expand Down
12 changes: 8 additions & 4 deletions Loop/Radial Menu/RadialMenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,15 @@ struct RadialMenuView: View {
}
}

if frontmostWindow == nil && previewMode == false {
Image("custom.macwindow.trianglebadge.exclamationmark")
.foregroundStyle(Color.getLoopAccent(tone: .normal))
.font(Font.system(size: 20, weight: .bold))
Group {
if frontmostWindow == nil && previewMode == false {
Image("custom.macwindow.trianglebadge.exclamationmark")
} else if let image = self.currentResizeDirection.radialMenuImage {
image
}
}
.foregroundStyle(Color.getLoopAccent(tone: .normal))
.font(Font.system(size: 20, weight: .bold))
}
.frame(width: radialMenuSize, height: radialMenuSize)

Expand Down
16 changes: 13 additions & 3 deletions Loop/Window Management/Window.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import SwiftUI
class Window {
let axWindow: AXUIElement
let cgWindowID: CGWindowID
let nsRunningApplication: NSRunningApplication?
var processID: pid_t

init?(element: AXUIElement, pid: pid_t? = nil) {
Expand All @@ -22,6 +23,10 @@ class Window {
self.processID = pid!
}

self.nsRunningApplication = NSWorkspace.shared.runningApplications.first(where: {
$0.processIdentifier == pid!
})

// Set self's CGWindowID
var windowId = CGWindowID(0)
let result = _AXUIElementGetWindow(self.axWindow, &windowId)
Expand Down Expand Up @@ -66,12 +71,17 @@ class Window {
}

var isHidden: Bool {
let result = self.axWindow.getValue(.hidden) as? NSNumber
return result?.boolValue ?? false
return self.nsRunningApplication?.isHidden ?? false
}
@discardableResult
func setHidden(_ state: Bool) -> Bool {
return self.axWindow.setValue(.hidden, value: state)
var result = false
if state {
result = self.nsRunningApplication?.hide() ?? false
} else {
result = self.nsRunningApplication?.unhide() ?? false
}
return result
}

var isMinimized: Bool {
Expand Down
9 changes: 8 additions & 1 deletion Loop/Window Management/WindowDirection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ enum WindowDirection: CaseIterable, Identifiable {
}
}

var image: Image? {
var menuBarImage: Image? {
switch self {
case .maximize: Image(systemName: "rectangle.inset.filled")
case .fullscreen: Image(systemName: "rectangle.fill")
Expand Down Expand Up @@ -211,6 +211,13 @@ enum WindowDirection: CaseIterable, Identifiable {
}
}

var radialMenuImage: Image? {
switch self {
case .hide: Image("custom.rectangle.inset.filled.slash")
default: nil
}
}

var edgesTouchingScreen: [Edge] {
switch self {
case .maximize: [.top, .bottom, .leading, .trailing]
Expand Down
12 changes: 3 additions & 9 deletions Loop/Window Management/WindowEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,14 @@ struct WindowEngine {
}

if direction == .hide {
if window.isMinimized {
window.setMinimized(false)
if window.isHidden {
window.setHidden(false)
} else {
window.setMinimized(true)
window.setHidden(true)
}

WindowRecords.recordDirection(window, direction)
return
}

window.setFullscreen(false)
window.setMinimized(false)

let oldWindowFrame = window.frame
guard let screenFrame = screen.safeScreenFrame, let currentWindowFrame = WindowEngine.generateWindowFrame(
Expand Down Expand Up @@ -104,8 +100,6 @@ struct WindowEngine {
#endif

return window

return nil
}

static var windowList: [Window] {
Expand Down

0 comments on commit 57a8fea

Please sign in to comment.