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

@@ -8,18 +8,6 @@
import Cocoa
public protocol Module_p {
var available: Bool { get }
var enabled: Bool { get }
var settings: Settings_p? { get }
func mount()
func unmount()
func terminate()
}
public struct module_c {
public var name: String = ""
public var icon: NSImage?
@@ -75,7 +63,7 @@ public struct module_c {
}
}
open class Module: Module_p {
open class Module {
public var config: module_c
public var available: Bool = false
@@ -155,7 +143,6 @@ open class Module: Module_p {
self.settings = Settings(
config: &self.config,
widgets: &self.menuBar.widgets,
enabled: self.enabled,
moduleSettings: self.settingsView,
popupSettings: self.popupView,
notificationsSettings: self.notificationsView
@@ -232,13 +219,6 @@ open class Module: Module_p {
self.readers = list.filter({ $0 != nil }).map({ $0! as Reader_p })
}
// replace a popup view
public func replacePopup(_ view: Popup_p) {
self.popup?.setIsVisible(false)
self.popupView = view
self.popup = PopupWindow(title: self.config.name, view: self.popupView, visibilityCallback: self.visibilityCallback)
}
// determine if module is available (can be overrided in module)
open func isAvailable() -> Bool { return true }
@@ -257,9 +237,6 @@ open class Module: Module_p {
}
}
// call after widget set up
open func widgetDidSet(_ type: widget_t) {}
// call when popup appear/disappear
private func visibilityCallback(_ state: Bool) {
self.readers.filter{ $0.popup }.forEach { (reader: Reader_p) in
@@ -339,7 +316,7 @@ open class Module: Module_p {
}
}
@objc private func listenForMouseDownInSettings(_ notification: Notification) {
@objc private func listenForMouseDownInSettings() {
if let popup = self.popup, popup.isVisible && !popup.locked {
self.popup?.setIsVisible(false)
}

View File

@@ -77,7 +77,7 @@ public class PopupWindow: NSWindow, NSWindowDelegate {
}
internal class PopupViewController: NSViewController {
public var visibilityCallback: (_ state: Bool) -> Void = {_ in }
fileprivate var visibilityCallback: (_ state: Bool) -> Void = {_ in }
private var popup: PopupView
public init() {
@@ -112,19 +112,18 @@ internal class PopupViewController: NSViewController {
self.visibilityCallback(false)
}
public func setup(title: String, view: Popup_p?) {
fileprivate func setup(title: String, view: Popup_p?) {
self.title = title
self.popup.setTitle(title)
self.popup.setView(view)
}
public func setCloseButton(_ state: Bool) {
fileprivate func setCloseButton(_ state: Bool) {
self.popup.setCloseButton(state)
}
}
internal class PopupView: NSView {
private var title: String? = nil
private var view: Popup_p? = nil
private var foreground: NSVisualEffectView
@@ -190,7 +189,7 @@ internal class PopupView: NSView {
self.background.layer?.backgroundColor = self.isDarkMode ? .clear : NSColor.white.cgColor
}
public func setView(_ view: Popup_p?) {
fileprivate func setView(_ view: Popup_p?) {
self.view = view
var isScrollVisible: Bool = false
@@ -226,12 +225,11 @@ internal class PopupView: NSView {
}
}
public func setTitle(_ newTitle: String) {
self.title = newTitle
fileprivate func setTitle(_ newTitle: String) {
self.header.setTitle(newTitle)
}
public func setCloseButton(_ state: Bool) {
fileprivate func setCloseButton(_ state: Bool) {
self.header.setCloseButton(state)
}
@@ -299,7 +297,6 @@ internal class PopupView: NSView {
internal class HeaderView: NSStackView {
private var titleView: NSTextField? = nil
private var activityButton: NSButton?
private var settingsButton: NSButton?
private var title: String = ""
private var isCloseAction: Bool = false
@@ -355,7 +352,6 @@ internal class HeaderView: NSStackView {
settings.target = self
settings.toolTip = localizedString("Open module settings")
settings.focusRingType = .none
self.settingsButton = settings
self.addArrangedSubview(activity)
self.addArrangedSubview(title)
@@ -372,7 +368,7 @@ internal class HeaderView: NSStackView {
fatalError("init(coder:) has not been implemented")
}
public func setTitle(_ newTitle: String) {
fileprivate func setTitle(_ newTitle: String) {
self.title = newTitle
self.titleView?.stringValue = localizedString(newTitle)
}
@@ -388,12 +384,12 @@ internal class HeaderView: NSStackView {
line.stroke()
}
@objc func openActivityMonitor(_ sender: Any) {
@objc func openActivityMonitor() {
guard let app = self.app else { return }
NSWorkspace.shared.open([], withApplicationAt: app, configuration: NSWorkspace.OpenConfiguration())
}
@objc func openSettings(_ sender: Any) {
@objc func openSettings() {
NotificationCenter.default.post(name: .toggleSettings, object: nil, userInfo: ["module": self.title])
}
@@ -403,7 +399,7 @@ internal class HeaderView: NSStackView {
return
}
public func setCloseButton(_ state: Bool) {
fileprivate func setCloseButton(_ state: Bool) {
if state && !self.isCloseAction {
self.activityButton?.image = Bundle(for: type(of: self)).image(forResource: "close")!
self.activityButton?.toolTip = localizedString("Close popup")

View File

@@ -17,7 +17,6 @@ public protocol Portal_p: NSView {
open class PortalWrapper: NSStackView, Portal_p {
public var name: String
private let header: PortalHeader
public init(_ type: ModuleType, height: CGFloat = Constants.Popup.portalHeight) {
@@ -102,7 +101,7 @@ public class PortalHeader: NSStackView {
fatalError("init(coder:) has not been implemented")
}
@objc private func openSettings(_ sender: Any) {
@objc private func openSettings() {
self.window?.setIsVisible(false)
NotificationCenter.default.post(name: .toggleSettings, object: nil, userInfo: ["module": self.name])
}

View File

@@ -11,10 +11,6 @@
import Cocoa
public protocol value_t {
var widgetValue: Double { get }
}
public protocol Reader_p {
var optional: Bool { get }
var popup: Bool { get }
@@ -60,7 +56,6 @@ open class Reader<T: Codable>: NSObject, ReaderInternal_p {
private let module: ModuleType
private var history: Bool
private var repeatTask: Repeater?
private var nilCallbackCounter: Int = 0
private var locked: Bool = true
private var initlizalized: Bool = false

View File

@@ -16,7 +16,6 @@ public protocol Settings_p: NSView {
}
public protocol Settings_v: NSView {
var callback: (() -> Void) { get set }
func load(widgets: [widget_t])
}
@@ -58,7 +57,7 @@ open class Settings: NSStackView, Settings_p {
private var isPopupSettingsAvailable: Bool
private var isNotificationsSettingsAvailable: Bool
init(config: UnsafePointer<module_c>, widgets: UnsafeMutablePointer<[Widget]>, enabled: Bool, moduleSettings: Settings_v?, popupSettings: Popup_p?, notificationsSettings: NotificationsWrapper?) {
init(config: UnsafePointer<module_c>, widgets: UnsafeMutablePointer<[Widget]>, moduleSettings: Settings_v?, popupSettings: Popup_p?, notificationsSettings: NotificationsWrapper?) {
self.config = config
self.widgets = widgets.pointee
self.moduleSettings = moduleSettings
@@ -271,7 +270,7 @@ open class Settings: NSStackView, Settings_p {
}
}
class WidgetSelectorView: NSStackView {
private class WidgetSelectorView: NSStackView {
private var module: String
private var stateCallback: () -> Void = {}
private var moved: Bool = false
@@ -286,7 +285,7 @@ class WidgetSelectorView: NSStackView {
return view
}()
public init(module: String, widgets: [Widget], stateCallback: @escaping () -> Void) {
fileprivate init(module: String, widgets: [Widget], stateCallback: @escaping () -> Void) {
self.module = module
self.stateCallback = stateCallback
@@ -465,7 +464,7 @@ class WidgetSelectorView: NSStackView {
}
}
internal class WidgetPreview: NSStackView {
private class WidgetPreview: NSStackView {
private var stateCallback: (_ status: Bool) -> Void = {_ in }
private let rgbImage: NSImage
@@ -475,12 +474,12 @@ internal class WidgetPreview: NSStackView {
private var state: Bool
private let id: String
public var position: Int {
fileprivate var position: Int {
get { Store.shared.int(key: "\(self.id)_position", defaultValue: 0) }
set { Store.shared.set(key: "\(self.id)_position", value: newValue) }
}
public init(id: String, type: widget_t, image: NSImage, isActive: Bool, _ callback: @escaping (_ status: Bool) -> Void) {
fileprivate init(id: String, type: widget_t, image: NSImage, isActive: Bool, _ callback: @escaping (_ status: Bool) -> Void) {
self.id = id
self.stateCallback = callback
self.rgbImage = image
@@ -531,7 +530,7 @@ internal class WidgetPreview: NSStackView {
fatalError("init(coder:) has not been implemented")
}
public func status(_ newState: Bool) {
fileprivate func status(_ newState: Bool) {
self.state = newState
self.stateCallback(newState)
self.imageView.image = newState ? self.rgbImage : self.grayImage
@@ -555,8 +554,8 @@ internal class WidgetPreview: NSStackView {
}
}
internal class WidgetSettings: NSStackView {
public init(title: String, image: NSImage, settingsView: NSView) {
private class WidgetSettings: NSStackView {
fileprivate init(title: String, image: NSImage, settingsView: NSView) {
super.init(frame: NSRect.zero)
self.translatesAutoresizingMaskIntoConstraints = false

View File

@@ -54,11 +54,11 @@ public enum widget_t: String {
preview = SpeedWidget(title: module, config: widgetConfig, preview: true)
item = SpeedWidget(title: module, config: widgetConfig, preview: false)
case .battery:
preview = BatteryWidget(title: module, config: widgetConfig, preview: true)
item = BatteryWidget(title: module, config: widgetConfig, preview: false)
preview = BatteryWidget(title: module, preview: true)
item = BatteryWidget(title: module, preview: false)
case .batteryDetails:
preview = BatteryDetailsWidget(title: module, config: widgetConfig, preview: true)
item = BatteryDetailsWidget(title: module, config: widgetConfig, preview: false)
preview = BatteryDetailsWidget(title: module, preview: true)
item = BatteryDetailsWidget(title: module, preview: false)
case .stack:
preview = StackWidget(title: module, config: widgetConfig, preview: true)
item = StackWidget(title: module, config: widgetConfig, preview: false)
@@ -66,11 +66,11 @@ public enum widget_t: String {
preview = MemoryWidget(title: module, config: widgetConfig, preview: true)
item = MemoryWidget(title: module, config: widgetConfig, preview: false)
case .label:
preview = Label(title: module, config: widgetConfig, preview: true)
item = Label(title: module, config: widgetConfig, preview: false)
preview = Label(title: module, config: widgetConfig)
item = Label(title: module, config: widgetConfig)
case .tachometer:
preview = Tachometer(title: module, config: widgetConfig, preview: true)
item = Tachometer(title: module, config: widgetConfig, preview: false)
preview = Tachometer(title: module, preview: true)
item = Tachometer(title: module, preview: false)
case .state:
preview = StateWidget(title: module, config: widgetConfig, preview: true)
item = StateWidget(title: module, config: widgetConfig, preview: false)
@@ -144,21 +144,15 @@ public enum widget_t: String {
extension widget_t: CaseIterable {}
public protocol widget_p: NSView {
var type: widget_t { get }
var title: String { get }
var position: Int { get set }
var widthHandler: (() -> Void)? { get set }
var onClick: (() -> Void)? { get set }
func setValues(_ values: [value_t])
func settings() -> NSView
}
open class WidgetWrapper: NSView, widget_p {
public var type: widget_t
public var title: String
public var position: Int = -1
public var widthHandler: (() -> Void)? = nil
public var onClick: (() -> Void)? = nil
public var shadowSize: CGSize
@@ -214,10 +208,7 @@ open class WidgetWrapper: NSView, widget_p {
return width
}
// MARK: - stubs
open func settings() -> NSView { return NSView() }
open func setValues(_ values: [value_t]) {}
open override func mouseDown(with event: NSEvent) {
if let f = self.onClick {
@@ -273,7 +264,6 @@ public class Widget {
}
}
private var config: NSDictionary = NSDictionary()
private var menuBarItem: NSStatusItem? = nil
private var originX: CGFloat
@@ -363,7 +353,7 @@ public class Widget {
}
}
@objc private func togglePopup(_ sender: Any) {
@objc private func togglePopup() {
if let item = self.menuBarItem, let window = item.button?.window {
NotificationCenter.default.post(name: .togglePopup, object: nil, userInfo: [
"module": self.module,
@@ -517,7 +507,7 @@ public class MenuBar {
self.callback?()
}
@objc private func togglePopup(_ sender: NSEvent) {
@objc private func togglePopup() {
if let item = self.menuBarItem, let window = item.button?.window {
NotificationCenter.default.post(name: .togglePopup, object: nil, userInfo: [
"module": self.moduleName,