Skip to content

Commit

Permalink
Move menubarOrigins to IndicatorsController
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Jan 24, 2025
1 parent 9df8efc commit d3bfe7e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 61 deletions.
61 changes: 53 additions & 8 deletions src/ShowyEdge/swift/IndicatorsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import Combine
import SettingsAccess
import SwiftUI

@MainActor
class IndicatorsController {
private var userSettings: UserSettings

private var windows: [NSWindow] = []
private var menubarOrigins: [CGPoint] = []
private var cancellables = Set<AnyCancellable>()

init(userSettings: UserSettings) {
Expand All @@ -18,17 +20,24 @@ class IndicatorsController {
) { [weak self] _ in
guard let self = self else { return }

self.updateWindowFrames()
Task { @MainActor in
self.updateMenubarOrigins()
self.updateWindowFrames()
}
}

// TODO: It does not work, change polling
NotificationCenter.default.addObserver(
forName: WorkspaceData.fullScreenModeChanged,
forName: NSWorkspace.activeSpaceDidChangeNotification,
object: nil,
queue: .main
) { [weak self] _ in
guard let self = self else { return }

self.updateWindowFrames()
Task { @MainActor in
self.updateMenubarOrigins()
self.updateWindowFrames()
}
}

NotificationCenter.default.addObserver(
Expand All @@ -38,16 +47,21 @@ class IndicatorsController {
) { [weak self] _ in
guard let self = self else { return }

self.updateWindowFrames()
self.updateColorByInputSource()
Task { @MainActor in
self.updateWindowFrames()
self.updateColorByInputSource()
}
}

userSettings.objectWillChange.sink { _ in
self.updateWindowFrames()
self.updateColorByInputSource()
Task { @MainActor in
self.updateWindowFrames()
self.updateColorByInputSource()
}
}.store(in: &cancellables)

setupWindows()
updateMenubarOrigins()
updateWindowFrames()
updateColorByInputSource()
}
Expand Down Expand Up @@ -107,6 +121,37 @@ class IndicatorsController {
}
}

private func updateMenubarOrigins() {
print("updateMenubarOrigins")

var newMenubarOrigins: [CGPoint] = []

if let windows = CGWindowListCopyWindowInfo(.optionOnScreenOnly, kCGNullWindowID)
as? [[String: Any]]
{
// We detect full screen spaces by checking if there's a menubar in the window list.
// If not, we assume it's in fullscreen mode.
for dict in windows {
if dict["kCGWindowOwnerName"] as? String == "Window Server",
dict["kCGWindowName"] as? String == "Menubar"
{
if let bounds = dict["kCGWindowBounds"] as? [String: Any],
let x = bounds["X"] as? NSNumber,
let y = bounds["Y"] as? NSNumber
{
newMenubarOrigins.append(
CGPoint(
x: x.doubleValue,
y: y.doubleValue))
}
}
}
}

menubarOrigins = newMenubarOrigins
print("menubarOrigins \(menubarOrigins)")
}

private func updateWindowFrames() {
setupWindows()

Expand All @@ -126,7 +171,7 @@ class IndicatorsController {
let menuOriginX = screenFrame.origin.x
let menuOriginY = firstScreenFrame.size.height - screenFrame.maxY

let isFullScreenSpace = !WorkspaceData.shared.menubarOrigins.contains(
let isFullScreenSpace = !menubarOrigins.contains(
CGPoint(
x: menuOriginX,
y: menuOriginY))
Expand Down
53 changes: 0 additions & 53 deletions src/ShowyEdge/swift/WorkspaceData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ import Foundation

public class WorkspaceData: NSObject, ObservableObject {
public static let shared = WorkspaceData()
public static let fullScreenModeChanged = Notification.Name("fullScreenModeChanged")
public static let currentInputSourceChanged = Notification.Name("currentInputSourceChanged")

@Published var currentInputSourceID: String = ""
@Published var currentInputModeID: String = ""
var menubarOrigins: [CGPoint] = []
var activeSpaceDidChangeObserver: NSObjectProtocol?

public func start() {
stop()
Expand All @@ -24,27 +21,11 @@ public class WorkspaceData: NSObject, ObservableObject {
suspensionBehavior: .deliverImmediately
)

activeSpaceDidChangeObserver = NSWorkspace.shared.notificationCenter.addObserver(
forName: NSWorkspace.activeSpaceDidChangeNotification,
object: nil,
queue: OperationQueue.main,
using: { [weak self] _ in
guard let self = self else { return }

self.updateMenubarOrigins()
}
)

updateMenubarOrigins()
selectedKeyboardInputSourceChanged()
}

public func stop() {
DistributedNotificationCenter.default.removeObserver(self)

if activeSpaceDidChangeObserver != nil {
NSWorkspace.shared.notificationCenter.removeObserver(activeSpaceDidChangeObserver!)
}
}

@objc
Expand All @@ -60,38 +41,4 @@ public class WorkspaceData: NSObject, ObservableObject {
object: nil)
}
}

private func updateMenubarOrigins() {
var menubarOrigins: [CGPoint] = []

if let windows = CGWindowListCopyWindowInfo(.optionOnScreenOnly, kCGNullWindowID)
as? [[String: Any]]
{
// We detect full screen spaces by checking if there's a menubar in the window list.
// If not, we assume it's in fullscreen mode.
for dict in windows {
if dict["kCGWindowOwnerName"] as? String == "Window Server",
dict["kCGWindowName"] as? String == "Menubar"
{
if let bounds = dict["kCGWindowBounds"] as? [String: Any],
let x = bounds["X"] as? NSNumber,
let y = bounds["Y"] as? NSNumber
{
menubarOrigins.append(
CGPoint(
x: x.doubleValue,
y: y.doubleValue))
}
}
}

if self.menubarOrigins != menubarOrigins {
self.menubarOrigins = menubarOrigins

NotificationCenter.default.post(
name: WorkspaceData.fullScreenModeChanged,
object: nil)
}
}
}
}

0 comments on commit d3bfe7e

Please sign in to comment.