mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-14 00:04:15 +09:00
feat: added an option to resize the settings window, also the settings window position and size will be autosaved
This commit is contained in:
@@ -312,6 +312,7 @@ private class WidgetSelectorView: NSStackView {
|
|||||||
private var background: NSVisualEffectView = {
|
private var background: NSVisualEffectView = {
|
||||||
let view = NSVisualEffectView(frame: NSRect.zero)
|
let view = NSVisualEffectView(frame: NSRect.zero)
|
||||||
view.blendingMode = .withinWindow
|
view.blendingMode = .withinWindow
|
||||||
|
view.translatesAutoresizingMaskIntoConstraints = false
|
||||||
if #available(macOS 26.0, *) {
|
if #available(macOS 26.0, *) {
|
||||||
view.material = .titlebar
|
view.material = .titlebar
|
||||||
} else {
|
} else {
|
||||||
@@ -386,7 +387,8 @@ private class WidgetSelectorView: NSStackView {
|
|||||||
NSLayoutConstraint.activate([
|
NSLayoutConstraint.activate([
|
||||||
self.heightAnchor.constraint(equalToConstant: Constants.Widget.height + (Constants.Settings.margin*2)),
|
self.heightAnchor.constraint(equalToConstant: Constants.Widget.height + (Constants.Settings.margin*2)),
|
||||||
separator.widthAnchor.constraint(equalToConstant: 1),
|
separator.widthAnchor.constraint(equalToConstant: 1),
|
||||||
separator.heightAnchor.constraint(equalTo: self.heightAnchor, constant: -18)
|
separator.heightAnchor.constraint(equalTo: self.heightAnchor, constant: -18),
|
||||||
|
self.background.widthAnchor.constraint(equalTo: self.widthAnchor)
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,8 @@ public extension NSToolbarItem.Identifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class SettingsWindow: NSWindow, NSWindowDelegate, NSToolbarDelegate {
|
class SettingsWindow: NSWindow, NSWindowDelegate, NSToolbarDelegate {
|
||||||
static let size: CGSize = CGSize(width: 720, height: 480)
|
private static let size: CGSize = CGSize(width: 720, height: 480)
|
||||||
|
private static let frameAutosaveName = "eu.exelban.Stats.Settings.WindowFrame"
|
||||||
|
|
||||||
private let mainView: MainView = MainView(frame: NSRect(x: 0, y: 0, width: 540, height: 480))
|
private let mainView: MainView = MainView(frame: NSRect(x: 0, y: 0, width: 540, height: 480))
|
||||||
private let sidebarView: SidebarView = SidebarView(frame: NSRect(x: 0, y: 0, width: 180, height: 480))
|
private let sidebarView: SidebarView = SidebarView(frame: NSRect(x: 0, y: 0, width: 180, height: 480))
|
||||||
@@ -28,9 +29,7 @@ class SettingsWindow: NSWindow, NSWindowDelegate, NSToolbarDelegate {
|
|||||||
private var toggleButton: NSControl? = nil
|
private var toggleButton: NSControl? = nil
|
||||||
private var activeModuleName: String? = nil
|
private var activeModuleName: String? = nil
|
||||||
|
|
||||||
private var pauseState: Bool {
|
private var pauseState: Bool { Store.shared.bool(key: "pause", defaultValue: false) }
|
||||||
Store.shared.bool(key: "pause", defaultValue: false)
|
|
||||||
}
|
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
super.init(
|
super.init(
|
||||||
@@ -40,7 +39,7 @@ class SettingsWindow: NSWindow, NSWindowDelegate, NSToolbarDelegate {
|
|||||||
width: SettingsWindow.size.width,
|
width: SettingsWindow.size.width,
|
||||||
height: SettingsWindow.size.height
|
height: SettingsWindow.size.height
|
||||||
),
|
),
|
||||||
styleMask: [.closable, .titled, .miniaturizable, .fullSizeContentView],
|
styleMask: [.closable, .titled, .miniaturizable, .fullSizeContentView, .resizable],
|
||||||
backing: .buffered,
|
backing: .buffered,
|
||||||
defer: false
|
defer: false
|
||||||
)
|
)
|
||||||
@@ -61,6 +60,8 @@ class SettingsWindow: NSWindow, NSWindowDelegate, NSToolbarDelegate {
|
|||||||
sidebarViewController.addSplitViewItem(sidebarItem)
|
sidebarViewController.addSplitViewItem(sidebarItem)
|
||||||
sidebarViewController.addSplitViewItem(contentItem)
|
sidebarViewController.addSplitViewItem(contentItem)
|
||||||
|
|
||||||
|
contentItem.minimumThickness = 540
|
||||||
|
|
||||||
let newToolbar = NSToolbar(identifier: "eu.exelban.Stats.Settings.Toolbar")
|
let newToolbar = NSToolbar(identifier: "eu.exelban.Stats.Settings.Toolbar")
|
||||||
newToolbar.allowsUserCustomization = false
|
newToolbar.allowsUserCustomization = false
|
||||||
newToolbar.autosavesConfiguration = true
|
newToolbar.autosavesConfiguration = true
|
||||||
@@ -74,20 +75,18 @@ class SettingsWindow: NSWindow, NSWindowDelegate, NSToolbarDelegate {
|
|||||||
if #unavailable(macOS 26.0) {
|
if #unavailable(macOS 26.0) {
|
||||||
self.backgroundColor = .clear
|
self.backgroundColor = .clear
|
||||||
}
|
}
|
||||||
self.positionCenter()
|
self.isRestorable = true
|
||||||
|
self.setFrameAutosaveName(SettingsWindow.frameAutosaveName)
|
||||||
|
if !self.setFrameUsingName(SettingsWindow.frameAutosaveName) {
|
||||||
|
self.positionCenter()
|
||||||
|
}
|
||||||
self.setIsVisible(false)
|
self.setIsVisible(false)
|
||||||
|
self.minSize = NSSize(width: SettingsWindow.size.width, height: SettingsWindow.size.height-Constants.Popup.headerHeight)
|
||||||
|
|
||||||
let windowController = NSWindowController()
|
let windowController = NSWindowController()
|
||||||
windowController.window = self
|
windowController.window = self
|
||||||
windowController.loadWindow()
|
windowController.loadWindow()
|
||||||
|
|
||||||
NSLayoutConstraint.activate([
|
|
||||||
self.mainView.widthAnchor.constraint(equalToConstant: 540),
|
|
||||||
self.mainView.container.widthAnchor.constraint(equalToConstant: 540),
|
|
||||||
self.mainView.container.topAnchor.constraint(equalTo: (self.contentLayoutGuide as! NSLayoutGuide).topAnchor),
|
|
||||||
self.mainView.container.bottomAnchor.constraint(equalTo: (self.contentLayoutGuide as! NSLayoutGuide).bottomAnchor)
|
|
||||||
])
|
|
||||||
|
|
||||||
NotificationCenter.default.addObserver(self, selector: #selector(menuCallback), name: .openModuleSettings, object: nil)
|
NotificationCenter.default.addObserver(self, selector: #selector(menuCallback), name: .openModuleSettings, object: nil)
|
||||||
NotificationCenter.default.addObserver(self, selector: #selector(toggleSettingsHandler), name: .toggleSettings, object: nil)
|
NotificationCenter.default.addObserver(self, selector: #selector(toggleSettingsHandler), name: .toggleSettings, object: nil)
|
||||||
NotificationCenter.default.addObserver(self, selector: #selector(externalModuleToggle), name: .toggleModule, object: nil)
|
NotificationCenter.default.addObserver(self, selector: #selector(externalModuleToggle), name: .toggleModule, object: nil)
|
||||||
@@ -121,30 +120,16 @@ class SettingsWindow: NSWindow, NSWindowDelegate, NSToolbarDelegate {
|
|||||||
func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
|
func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
|
||||||
switch itemIdentifier {
|
switch itemIdentifier {
|
||||||
case .toggleButton:
|
case .toggleButton:
|
||||||
var toggleBtn: NSControl = NSControl()
|
let switchButton = NSSwitch()
|
||||||
if #available(OSX 10.15, *) {
|
switchButton.state = .on
|
||||||
let switchButton = NSSwitch()
|
switchButton.action = #selector(self.toggleEnable)
|
||||||
switchButton.state = .on
|
switchButton.target = self
|
||||||
switchButton.action = #selector(self.toggleEnable)
|
switchButton.controlSize = .small
|
||||||
switchButton.target = self
|
self.toggleButton = switchButton
|
||||||
switchButton.controlSize = .small
|
|
||||||
toggleBtn = switchButton
|
|
||||||
} else {
|
|
||||||
let button: NSButton = NSButton()
|
|
||||||
button.setButtonType(.switch)
|
|
||||||
button.state = .on
|
|
||||||
button.title = ""
|
|
||||||
button.action = #selector(self.toggleEnable)
|
|
||||||
button.isBordered = false
|
|
||||||
button.isTransparent = false
|
|
||||||
button.target = self
|
|
||||||
toggleBtn = button
|
|
||||||
}
|
|
||||||
self.toggleButton = toggleBtn
|
|
||||||
|
|
||||||
let toolbarItem = NSToolbarItem(itemIdentifier: itemIdentifier)
|
let toolbarItem = NSToolbarItem(itemIdentifier: itemIdentifier)
|
||||||
toolbarItem.toolTip = localizedString("Toggle the module")
|
toolbarItem.toolTip = localizedString("Toggle the module")
|
||||||
toolbarItem.view = toggleBtn
|
toolbarItem.view = switchButton
|
||||||
toolbarItem.isBordered = false
|
toolbarItem.isBordered = false
|
||||||
|
|
||||||
return toolbarItem
|
return toolbarItem
|
||||||
@@ -232,22 +217,21 @@ class SettingsWindow: NSWindow, NSWindowDelegate, NSToolbarDelegate {
|
|||||||
// MARK: - MainView
|
// MARK: - MainView
|
||||||
|
|
||||||
private class MainView: NSView {
|
private class MainView: NSView {
|
||||||
fileprivate let container: NSStackView
|
fileprivate let container: NSStackView = NSStackView()
|
||||||
|
|
||||||
override init(frame: NSRect) {
|
override init(frame: NSRect) {
|
||||||
self.container = NSStackView(frame: NSRect(x: 0, y: 0, width: frame.width, height: frame.height))
|
|
||||||
|
|
||||||
let foreground = NSVisualEffectView(frame: NSRect(x: 0, y: 0, width: frame.width, height: frame.height))
|
|
||||||
foreground.blendingMode = .withinWindow
|
|
||||||
foreground.material = .windowBackground
|
|
||||||
foreground.state = .active
|
|
||||||
|
|
||||||
super.init(frame: NSRect.zero)
|
super.init(frame: NSRect.zero)
|
||||||
|
|
||||||
self.container.translatesAutoresizingMaskIntoConstraints = false
|
self.container.translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
|
||||||
self.addSubview(foreground, positioned: .below, relativeTo: .none)
|
|
||||||
self.addSubview(self.container)
|
self.addSubview(self.container)
|
||||||
|
|
||||||
|
NSLayoutConstraint.activate([
|
||||||
|
self.container.leadingAnchor.constraint(equalTo: leadingAnchor),
|
||||||
|
self.container.trailingAnchor.constraint(equalTo: trailingAnchor),
|
||||||
|
self.container.topAnchor.constraint(equalTo: topAnchor, constant: Constants.Popup.headerHeight*1.4),
|
||||||
|
self.container.bottomAnchor.constraint(equalTo: bottomAnchor)
|
||||||
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
required init?(coder: NSCoder) {
|
required init?(coder: NSCoder) {
|
||||||
|
|||||||
Reference in New Issue
Block a user