Skip to content

Commit 0dd8835

Browse files
committed
fix: fixed invisible elements bug when enabling spacer between widgets (#3362)
1 parent 68b7e5f commit 0dd8835

4 files changed

Lines changed: 55 additions & 38 deletions

File tree

Kit/extensions.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ public class KeyboardShartcutView: NSStackView {
709709
private var keyCodes: [UInt16] = []
710710
private var value: [UInt16] = []
711711
private var interaction: Bool = false
712+
private var monitor: Any? = nil
712713

713714
public init(callback: @escaping (_ value: [UInt16]) -> Void, value: [UInt16]) {
714715
self.callback = callback
@@ -733,7 +734,7 @@ public class KeyboardShartcutView: NSStackView {
733734
self.startButton = startButton
734735
self.stopButton = stopButton
735736

736-
NSEvent.addLocalMonitorForEvents(matching: [.keyDown, .flagsChanged]) { [weak self] event in
737+
self.monitor = NSEvent.addLocalMonitorForEvents(matching: [.keyDown, .flagsChanged]) { [weak self] event in
737738
self?.handleKeyEvent(event)
738739
return event
739740
}
@@ -743,6 +744,12 @@ public class KeyboardShartcutView: NSStackView {
743744
fatalError("init(coder:) has not been implemented")
744745
}
745746

747+
deinit {
748+
if let monitor = self.monitor {
749+
NSEvent.removeMonitor(monitor)
750+
}
751+
}
752+
746753
@objc private func startListening() {
747754
guard AXIsProcessTrustedWithOptions([kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String: true] as CFDictionary) else { return }
748755
if let btn = self.stopButton {

Kit/module/module.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ open class Module {
158158
}
159159

160160
self.window = Window(
161-
config: &self.config,
162-
widgets: &self.menuBar.widgets,
161+
config: self.config,
162+
widgets: self.menuBar.widgets,
163163
modulePreview: self.previewView,
164164
moduleSettings: self.settingsView,
165165
popupSettings: self.popupView,
@@ -315,9 +315,14 @@ open class Module {
315315
var x = buttonOrigin.x - windowCenter + buttonCenter
316316
let y = buttonOrigin.y - popup.contentView!.intrinsicContentSize.height - 3
317317

318-
let maxWidth = NSScreen.screens.map{ $0.frame.width }.reduce(0, +)
319-
if x + popup.contentView!.intrinsicContentSize.width > maxWidth {
320-
x = maxWidth - popup.contentView!.intrinsicContentSize.width - 3
318+
let buttonPoint = NSPoint(x: buttonOrigin.x + buttonCenter, y: buttonOrigin.y)
319+
if let screen = NSScreen.screens.first(where: { $0.frame.contains(buttonPoint) }) ?? NSScreen.main {
320+
if x + popup.contentView!.intrinsicContentSize.width > screen.frame.maxX {
321+
x = screen.frame.maxX - popup.contentView!.intrinsicContentSize.width - 3
322+
}
323+
if x < screen.frame.minX {
324+
x = screen.frame.minX + 3
325+
}
321326
}
322327

323328
popup.setFrameOrigin(NSPoint(x: x, y: y))

Kit/module/window.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ open class PreviewWrapper: NSStackView {
3434
}
3535

3636
open class Window: NSStackView {
37-
private var config: UnsafePointer<module_c>
37+
private var config: module_c
3838
private var widgets: [SWidget]
3939

4040
private var widgetSelector: NSView?
@@ -62,8 +62,8 @@ open class Window: NSStackView {
6262
Store.shared.bool(key: "OneView", defaultValue: false)
6363
}
6464
private var oneViewState: Bool {
65-
get { Store.shared.bool(key: "\(self.config.pointee.name)_oneView", defaultValue: false) }
66-
set { Store.shared.set(key: "\(self.config.pointee.name)_oneView", value: newValue) }
65+
get { Store.shared.bool(key: "\(self.config.name)_oneView", defaultValue: false) }
66+
set { Store.shared.set(key: "\(self.config.name)_oneView", value: newValue) }
6767
}
6868

6969
private var isPreviewAvailable: Bool
@@ -74,24 +74,24 @@ open class Window: NSStackView {
7474
private var settingsView: NSView? = nil
7575

7676
init(
77-
config: UnsafePointer<module_c>,
78-
widgets: UnsafeMutablePointer<[SWidget]>,
77+
config: module_c,
78+
widgets: [SWidget],
7979
modulePreview: PreviewWrapper?,
8080
moduleSettings: Settings_v?,
8181
popupSettings: Popup_p?,
8282
notificationsSettings: NotificationsWrapper?
8383
) {
8484
self.config = config
85-
self.widgets = widgets.pointee
85+
self.widgets = widgets
8686
self.modulePreview = modulePreview
8787
self.moduleSettings = moduleSettings
8888
self.popupSettings = popupSettings
8989
self.notificationsSettings = notificationsSettings
9090

91-
self.isPreviewAvailable = config.pointee.previewConfig["available"] as? Bool ?? false
91+
self.isPreviewAvailable = config.previewConfig["available"] as? Bool ?? false
9292

93-
self.isPopupSettingsAvailable = config.pointee.settingsConfig["popup"] as? Bool ?? false
94-
self.isNotificationsSettingsAvailable = config.pointee.settingsConfig["notifications"] as? Bool ?? false
93+
self.isPopupSettingsAvailable = config.settingsConfig["popup"] as? Bool ?? false
94+
self.isNotificationsSettingsAvailable = config.settingsConfig["notifications"] as? Bool ?? false
9595

9696
super.init(frame: NSRect.zero)
9797

@@ -157,8 +157,8 @@ open class Window: NSStackView {
157157
right: Constants.Settings.margin
158158
)
159159

160-
if self.config.pointee.name == "Remote" {
161-
let widgetSelector = WidgetSelectorView(module: self.config.pointee.name, widgets: self.widgets, stateCallback: self.loadWidget)
160+
if self.config.name == "Remote" {
161+
let widgetSelector = WidgetSelectorView(module: self.config.name, widgets: self.widgets, stateCallback: self.loadWidget)
162162
self.widgetSelector = widgetSelector
163163

164164
view.addArrangedSubview(widgetSelector)
@@ -245,7 +245,7 @@ open class Window: NSStackView {
245245
tabView.addTabViewItem(notificationsTab)
246246
}
247247

248-
let widgetSelector = WidgetSelectorView(module: self.config.pointee.name, widgets: self.widgets, stateCallback: self.loadWidget)
248+
let widgetSelector = WidgetSelectorView(module: self.config.name, widgets: self.widgets, stateCallback: self.loadWidget)
249249

250250
view.addArrangedSubview(widgetSelector)
251251
view.addArrangedSubview(segmentedControl)
@@ -325,7 +325,7 @@ open class Window: NSStackView {
325325
@objc private func toggleOneView(_ sender: NSControl) {
326326
guard !self.globalOneView else { return }
327327
self.oneViewState = controlState(sender)
328-
NotificationCenter.default.post(name: .toggleOneView, object: nil, userInfo: ["module": self.config.pointee.name])
328+
NotificationCenter.default.post(name: .toggleOneView, object: nil, userInfo: ["module": self.config.name])
329329
}
330330

331331
@objc private func listenForOneView(_ notification: Notification) {
@@ -337,7 +337,7 @@ open class Window: NSStackView {
337337
}
338338

339339
@objc private func listenForToggleView(_ notification: Notification) {
340-
guard let moduleName = notification.userInfo?["module"], self.config.pointee.name == moduleName as? String else { return }
340+
guard let moduleName = notification.userInfo?["module"], self.config.name == moduleName as? String else { return }
341341
self.toggleView()
342342
}
343343

Stats/Views/CombinedView.swift

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -117,23 +117,23 @@ internal class CombinedView: NSObject, NSGestureRecognizerDelegate {
117117

118118
private func recalculate() {
119119
self.view.subviews.forEach({ $0.removeFromSuperview() })
120-
120+
121+
let visibleModules = self.activeModules.filter({ !$0.menuBar.activeWidgets.isEmpty })
121122
var w: CGFloat = 0
122-
var i: Int = 0
123-
self.activeModules.forEach { (m: Module) in
124-
self.view.addSubview(m.menuBar.view)
125-
self.view.subviews[i].setFrameOrigin(NSPoint(x: w, y: 0))
126-
w += m.menuBar.view.frame.width + self.spacing
127-
i += 1
128-
129-
if self.separator && i < 2 * self.activeModules.count - 1 {
130-
let separator = NSView(frame: NSRect(x: w, y: 3, width: 1, height: Constants.Widget.height-6))
131-
separator.wantsLayer = true
132-
separator.layer?.backgroundColor = (separator.isDarkMode ? NSColor.white : NSColor.black).cgColor
133-
self.view.addSubview(separator)
134-
w += 3 + self.spacing
135-
i += 1
123+
visibleModules.enumerated().forEach { (i, m) in
124+
if i != 0 {
125+
w += self.spacing
126+
if self.separator {
127+
let separator = NSView(frame: NSRect(x: w, y: 3, width: 1, height: Constants.Widget.height-6))
128+
separator.wantsLayer = true
129+
separator.layer?.backgroundColor = (separator.isDarkMode ? NSColor.white : NSColor.black).cgColor
130+
self.view.addSubview(separator)
131+
w += 3 + self.spacing
132+
}
136133
}
134+
self.view.addSubview(m.menuBar.view)
135+
m.menuBar.view.setFrameOrigin(NSPoint(x: w, y: 0))
136+
w += m.menuBar.view.frame.width
137137
}
138138
self.view.setFrameSize(NSSize(width: w, height: self.view.frame.height))
139139
self.menuBarItem?.length = w
@@ -156,9 +156,14 @@ internal class CombinedView: NSObject, NSGestureRecognizerDelegate {
156156
var x = window.frame.origin.x - windowCenter + window.frame.width/2
157157
let y = window.frame.origin.y - popup.contentView!.intrinsicContentSize.height - 3
158158

159-
let maxWidth = NSScreen.screens.map{ $0.frame.width }.reduce(0, +)
160-
if x + popup.contentView!.intrinsicContentSize.width > maxWidth {
161-
x = maxWidth - popup.contentView!.intrinsicContentSize.width - 3
159+
let buttonPoint = NSPoint(x: window.frame.midX, y: window.frame.midY)
160+
if let screen = NSScreen.screens.first(where: { $0.frame.contains(buttonPoint) }) ?? NSScreen.main {
161+
if x + popup.contentView!.intrinsicContentSize.width > screen.frame.maxX {
162+
x = screen.frame.maxX - popup.contentView!.intrinsicContentSize.width - 3
163+
}
164+
if x < screen.frame.minX {
165+
x = screen.frame.minX + 3
166+
}
162167
}
163168

164169
popup.setFrameOrigin(NSPoint(x: x, y: y))

0 commit comments

Comments
 (0)