feat: small codebase refactoring (mostly removed unused code and changes in access control)

This commit is contained in:
Serhiy Mytrovtsiy
2024-05-08 17:21:58 +02:00
parent e925e6d131
commit eb882774bb
37 changed files with 160 additions and 583 deletions

View File

@@ -171,7 +171,7 @@ class ApplicationSettings: NSStackView {
NotificationCenter.default.removeObserver(self, name: .fanHelperState, object: nil)
}
public func viewWillAppear() {
internal func viewWillAppear() {
self.startAtLoginBtn?.state = LaunchAtLogin.isEnabled ? .on : .off
self.telemetryBtn?.state = telemetry.isEnabled ? .on : .off
@@ -240,7 +240,7 @@ class ApplicationSettings: NSStackView {
// MARK: - actions
@objc private func updateAction(_ sender: NSObject) {
@objc private func updateAction() {
updater.check(force: true, completion: { result, error in
if error != nil {
debug("error updater.check(): \(error!.localizedDescription)")
@@ -309,7 +309,7 @@ class ApplicationSettings: NSStackView {
NotificationCenter.default.post(name: .combinedModulesPopup, object: nil, userInfo: nil)
}
@objc private func importSettings(_ sender: NSObject) {
@objc private func importSettings() {
let panel = NSOpenPanel()
panel.level = NSWindow.Level(rawValue: Int(CGWindowLevelForKey(.modalPanelWindow)))
panel.begin { (result) in
@@ -320,7 +320,7 @@ class ApplicationSettings: NSStackView {
}
}
@objc private func exportSettings(_ sender: NSObject) {
@objc private func exportSettings() {
let panel = NSSavePanel()
panel.nameFieldStringValue = "Stats.plist"
panel.showsTagField = false
@@ -333,7 +333,7 @@ class ApplicationSettings: NSStackView {
}
}
@objc private func resetSettings(_ sender: NSObject) {
@objc private func resetSettings() {
let alert = NSAlert()
alert.messageText = localizedString("Reset settings")
alert.informativeText = localizedString("Reset settings text")
@@ -482,12 +482,10 @@ private class ModuleSelectorView: NSStackView {
}
}
internal class ModulePreview: NSStackView {
private let id: String
private class ModulePreview: NSStackView {
private let imageView: NSImageView
public init(id: String, icon: NSImage?) {
self.id = id
fileprivate init(id: String, icon: NSImage?) {
self.imageView = NSImageView(frame: NSRect(origin: .zero, size: NSSize(width: Constants.Widget.height, height: Constants.Widget.height)))
let size: CGSize = CGSize(width: Constants.Widget.height + (Constants.Widget.spacing * 2), height: Constants.Widget.height)

View File

@@ -12,7 +12,7 @@
import Cocoa
import Kit
class CombinedView: NSObject, NSGestureRecognizerDelegate {
internal class CombinedView: NSObject, NSGestureRecognizerDelegate {
private var menuBarItem: NSStatusItem? = nil
private var view: NSView = NSView(frame: NSRect(x: 0, y: 0, width: 0, height: Constants.Widget.height))
private var popup: PopupWindow? = nil
@@ -194,7 +194,8 @@ class CombinedView: NSObject, NSGestureRecognizerDelegate {
}
private class Popup: NSStackView, Popup_p {
public var sizeCallback: ((NSSize) -> Void)? = nil
fileprivate var sizeCallback: ((NSSize) -> Void)? = nil
let log = NextLog(writer: .file)
init() {
super.init(frame: NSRect(x: 0, y: 0, width: Constants.Popup.width, height: 0))
@@ -217,16 +218,18 @@ private class Popup: NSStackView, Popup_p {
NotificationCenter.default.removeObserver(self, name: .toggleOneView, object: nil)
}
public func settings() -> NSView? { return nil }
public func appear() {}
public func disappear() {}
fileprivate func settings() -> NSView? { return nil }
fileprivate func appear() {}
fileprivate func disappear() {}
@objc private func reinit() {
self.subviews.forEach({ $0.removeFromSuperview() })
let availableModules = modules.filter({ $0.enabled && $0.portal != nil })
debug("modules: \(availableModules.count)", log: self.log)
availableModules.forEach { (m: Module) in
if let p = m.portal {
debug("adding: \(m.name) portal", log: self.log)
self.addArrangedSubview(p)
}
}

View File

@@ -224,6 +224,7 @@ class Dashboard: NSStackView {
osField.alignment = .center
osField.font = NSFont.systemFont(ofSize: 12, weight: .regular)
osField.stringValue = "macOS \(SystemKit.shared.device.os?.name ?? localizedString("Unknown")) (\(SystemKit.shared.device.os?.version.getFullVersion() ?? ""))"
osField.toolTip = SystemKit.shared.device.os?.build ?? localizedString("Unknown")
osField.isSelectable = true
container.addRow(with: [deviceImageView])

View File

@@ -211,7 +211,7 @@ class SettingsWindow: NSWindow, NSWindowDelegate, NSToolbarDelegate {
}
}
public func setModules() {
internal func setModules() {
self.sidebarView.setModules(modules)
if !self.pauseState && modules.filter({ $0.enabled != false && $0.available != false && !$0.menuBar.widgets.filter({ $0.isActive }).isEmpty }).isEmpty {
self.setIsVisible(true)
@@ -229,7 +229,7 @@ class SettingsWindow: NSWindow, NSWindowDelegate, NSToolbarDelegate {
// MARK: - MainView
private class MainView: NSView {
public let container: NSStackView
fileprivate let container: NSStackView
override init(frame: NSRect) {
self.container = NSStackView(frame: NSRect(x: 0, y: 0, width: frame.width, height: frame.height))
@@ -251,7 +251,7 @@ private class MainView: NSView {
fatalError("init(coder:) has not been implemented")
}
public func setView(_ view: NSView) {
fileprivate func setView(_ view: NSView) {
self.container.subviews.forEach{ $0.removeFromSuperview() }
self.container.addArrangedSubview(view)
@@ -380,7 +380,7 @@ private class SidebarView: NSStackView {
fatalError("init(coder:) has not been implemented")
}
public func openMenu(_ title: String) {
fileprivate func openMenu(_ title: String) {
self.scrollView.stackView.subviews.forEach({ (m: NSView) in
if let menu = m as? MenuItem {
if menu.title == title {
@@ -392,7 +392,7 @@ private class SidebarView: NSStackView {
})
}
public func setModules(_ list: [Module]) {
fileprivate func setModules(_ list: [Module]) {
list.reversed().forEach { (m: Module) in
if !m.available { return }
let menu: NSView = MenuItem(icon: m.config.icon, title: m.config.name)
@@ -460,26 +460,7 @@ private class SidebarView: NSStackView {
return vc
}
private func supportButton(name: String, image: String, action: Selector) -> NSButton {
let button = NSButtonWithPadding()
button.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
button.verticalPadding = 16
button.horizontalPadding = 16
button.title = name
button.toolTip = name
button.bezelStyle = .regularSquare
button.translatesAutoresizingMaskIntoConstraints = false
button.imageScaling = .scaleNone
button.image = Bundle(for: type(of: self)).image(forResource: image)!
button.isBordered = false
button.target = self
button.focusRingType = .none
button.action = action
return button
}
@objc private func reportBug(_ sender: Any) {
@objc private func reportBug() {
NSWorkspace.shared.open(URL(string: "https://github.com/exelban/stats/issues/new")!)
}
@@ -487,11 +468,11 @@ private class SidebarView: NSStackView {
self.supportPopover.show(relativeTo: sender.bounds, of: sender, preferredEdge: NSRectEdge.minY)
}
@objc private func closeApp(_ sender: Any) {
@objc private func closeApp(_ sender: NSButton) {
NSApp.terminate(sender)
}
@objc private func togglePause(_ sender: NSButton) {
@objc private func togglePause() {
self.pauseState = !self.pauseState
self.pauseButton?.toolTip = localizedString(self.pauseState ? "Resume the Stats" : "Pause the Stats")
self.pauseButton?.image = self.pauseState ? self.resumeIcon : self.pauseIcon
@@ -505,9 +486,9 @@ private class SidebarView: NSStackView {
}
private class MenuItem: NSView {
public let title: String
public var active: Bool = false
fileprivate let title: String
private var active: Bool = false
private var imageView: NSImageView? = nil
private var titleView: NSTextField? = nil
@@ -560,7 +541,7 @@ private class MenuItem: NSView {
self.activate()
}
public func activate() {
fileprivate func activate() {
guard !self.active else { return }
self.active = true
@@ -571,7 +552,7 @@ private class MenuItem: NSView {
self.titleView?.textColor = .white
}
public func reset() {
fileprivate func reset() {
self.layer?.backgroundColor = .clear
self.imageView?.contentTintColor = .labelColor
self.titleView?.textColor = .labelColor

View File

@@ -15,11 +15,11 @@ import Kit
private let setupSize: CGSize = CGSize(width: 600, height: 400)
internal class SetupWindow: NSWindow, NSWindowDelegate {
internal var finishHandler: () -> Void = {}
private let view: SetupContainer = SetupContainer()
private let vc: NSViewController = NSViewController(nibName: nil, bundle: nil)
public var finishHandler: () -> Void = {}
init() {
self.vc.view = self.view
@@ -44,12 +44,12 @@ internal class SetupWindow: NSWindow, NSWindowDelegate {
windowController.loadWindow()
}
public func show() {
internal func show() {
self.setIsVisible(true)
self.orderFrontRegardless()
}
public func hide() {
internal func hide() {
self.close()
}
@@ -496,10 +496,10 @@ private class SetupView_end: NSStackView {
}
}
public class SupportButtonView: NSButton {
public var callback: (() -> Void) = {}
internal class SupportButtonView: NSButton {
internal var callback: (() -> Void) = {}
public init(name: String, image: String, action: @escaping () -> Void) {
init(name: String, image: String, action: @escaping () -> Void) {
self.callback = action
super.init(frame: NSRect(x: 0, y: 0, width: 30, height: 30))
@@ -544,7 +544,7 @@ public class SupportButtonView: NSButton {
NSCursor.arrow.set()
}
@objc private func click(_ sender: NSControl) {
@objc private func click() {
self.callback()
}
}

View File

@@ -12,7 +12,7 @@
import Cocoa
import Kit
class UpdateWindow: NSWindow, NSWindowDelegate {
internal class UpdateWindow: NSWindow, NSWindowDelegate {
private let viewController: UpdateViewController = UpdateViewController()
init() {
@@ -39,7 +39,7 @@ class UpdateWindow: NSWindow, NSWindowDelegate {
windowController.loadWindow()
}
public func open(_ v: version_s, settingButton: Bool = false) {
internal func open(_ v: version_s, settingButton: Bool = false) {
if !self.isVisible || settingButton {
self.setIsVisible(true)
self.makeKeyAndOrderFront(nil)
@@ -68,7 +68,7 @@ private class UpdateViewController: NSViewController {
fatalError("init(coder:) has not been implemented")
}
public func open(_ v: version_s) {
internal func open(_ v: version_s) {
self.update.clear()
if v.newest {
@@ -99,7 +99,7 @@ private class UpdateView: NSView {
fatalError("init(coder:) has not been implemented")
}
public func newVersion(_ version: version_s) {
internal func newVersion(_ version: version_s) {
self.version = version
let view: NSStackView = NSStackView(frame: NSRect(
x: Constants.Settings.margin,
@@ -190,7 +190,7 @@ private class UpdateView: NSView {
self.addSubview(view)
}
public func noUpdates() {
internal func noUpdates() {
let view: NSView = NSView(frame: NSRect(x: 10, y: 10, width: self.frame.width - 20, height: self.frame.height - 20))
let title: NSTextField = TextView(frame: NSRect(x: 0, y: ((view.frame.height - 18)/2), width: view.frame.width, height: 34))
@@ -209,11 +209,11 @@ private class UpdateView: NSView {
self.addSubview(view)
}
public func clear() {
internal func clear() {
self.subviews.filter{ !($0 is NSVisualEffectView) }.forEach{ $0.removeFromSuperview() }
}
@objc private func download(_ sender: Any) {
@objc private func download() {
guard let urlString = self.version?.url, let url = URL(string: urlString) else {
return
}
@@ -273,17 +273,17 @@ private class UpdateView: NSView {
self.addSubview(view)
}
@objc private func close(_ sender: Any) {
@objc private func close() {
self.window?.close()
}
@objc private func changelog(_ sender: Any) {
@objc private func changelog() {
if let version = self.version {
NSWorkspace.shared.open(URL(string: "https://github.com/exelban/stats/releases/tag/\(version.latest)")!)
}
}
@objc private func install(_ sender: Any) {
@objc private func install() {
updater.install(path: self.path) { error in
if let error {
showAlert("Error update Stats", error, .critical)