Skip to content

Commit

Permalink
Merge pull request #28 from gao-sun/v1.3
Browse files Browse the repository at this point in the history
v1.3
  • Loading branch information
gao-sun authored Oct 25, 2020
2 parents af5afb1 + 546696c commit 25ba49a
Show file tree
Hide file tree
Showing 20 changed files with 515 additions and 122 deletions.
24 changes: 18 additions & 6 deletions eul.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
6CB260B0250DF1A900328DAA /* BatteryIconView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CB260AF250DF1A900328DAA /* BatteryIconView.swift */; };
6CB26F7924E8310900E809FE /* SectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CB26F7824E8310900E809FE /* SectionView.swift */; };
6CC0798C250CD793000D7DAC /* PreferenceGeneralView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CC0798B250CD793000D7DAC /* PreferenceGeneralView.swift */; };
6CDBB4C1254462B100D01EA0 /* PreferenceSectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CDBB4C0254462B100D01EA0 /* PreferenceSectionView.swift */; };
6CDBB4C325447B7500D01EA0 /* EulMenuComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CDBB4C225447B7500D01EA0 /* EulMenuComponent.swift */; };
6CDBB4C525447D8B00D01EA0 /* ComponentsStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CDBB4C425447D8B00D01EA0 /* ComponentsStore.swift */; };
6CDE6C7224F0007D00463114 /* StatusBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CDE6C7124F0007D00463114 /* StatusBarItem.swift */; };
6CDE6C7424F0047600463114 /* SizeChangeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CDE6C7324F0047600463114 /* SizeChangeView.swift */; };
6CDE6C7624F10C8000463114 /* StatusBarManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CDE6C7524F10C8000463114 /* StatusBarManager.swift */; };
Expand Down Expand Up @@ -165,6 +168,9 @@
6CB260AF250DF1A900328DAA /* BatteryIconView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BatteryIconView.swift; sourceTree = "<group>"; };
6CB26F7824E8310900E809FE /* SectionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SectionView.swift; sourceTree = "<group>"; };
6CC0798B250CD793000D7DAC /* PreferenceGeneralView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferenceGeneralView.swift; sourceTree = "<group>"; };
6CDBB4C0254462B100D01EA0 /* PreferenceSectionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferenceSectionView.swift; sourceTree = "<group>"; };
6CDBB4C225447B7500D01EA0 /* EulMenuComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EulMenuComponent.swift; sourceTree = "<group>"; };
6CDBB4C425447D8B00D01EA0 /* ComponentsStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComponentsStore.swift; sourceTree = "<group>"; };
6CDE6C7124F0007D00463114 /* StatusBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusBarItem.swift; sourceTree = "<group>"; };
6CDE6C7324F0047600463114 /* SizeChangeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SizeChangeView.swift; sourceTree = "<group>"; };
6CDE6C7524F10C8000463114 /* StatusBarManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusBarManager.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -210,6 +216,7 @@
isa = PBXGroup;
children = (
6CDE6C7724F112D700463114 /* EulComponent.swift */,
6CDBB4C225447B7500D01EA0 /* EulMenuComponent.swift */,
6C7DB6E524E82C5A00133B06 /* Preference.swift */,
6C1FA40D24AA162800CA7F71 /* Refreshable.swift */,
6CDE6C7324F0047600463114 /* SizeChangeView.swift */,
Expand Down Expand Up @@ -288,6 +295,7 @@
6CC0798B250CD793000D7DAC /* PreferenceGeneralView.swift */,
6CE33C7E25260E3D00831857 /* PreferenceRefreshRateView.swift */,
6C5FBD4D253C7A060016F3A2 /* PreferenceMenuViewView.swift */,
6CDBB4C0254462B100D01EA0 /* PreferenceSectionView.swift */,
);
path = Preference;
sourceTree = "<group>";
Expand Down Expand Up @@ -321,6 +329,7 @@
6C387DC6253B08F100CBB81B /* NetworkTopStore.swift */,
6C7DB6DB24E7FFE300133B06 /* PreferenceStore.swift */,
6C457391253AC69E00BB2584 /* UIStore.swift */,
6CDBB4C425447D8B00D01EA0 /* ComponentsStore.swift */,
);
path = Store;
sourceTree = "<group>";
Expand Down Expand Up @@ -356,12 +365,12 @@
6CDE6C7924F2AA3200463114 /* Menu */ = {
isa = PBXGroup;
children = (
6C33CEC82517634400345977 /* StatusMenuView.swift */,
6C993B8F2517920600586C6B /* BatteryMenuBlockView.swift */,
6C33CECC25177CE300345977 /* CpuMenuBlockView.swift */,
6C993B892517868B00586C6B /* FanMenuBlockView.swift */,
6C993B8B25178D3B00586C6B /* MemoryMenuBlockView.swift */,
6C993B8F2517920600586C6B /* BatteryMenuBlockView.swift */,
6C387DC8253B1A8000CBB81B /* NetworkMenuBlockMenuView.swift */,
6C33CEC82517634400345977 /* StatusMenuView.swift */,
);
path = Menu;
sourceTree = "<group>";
Expand Down Expand Up @@ -538,6 +547,8 @@
6C1FA40B24AA139300CA7F71 /* NSNotification.swift in Sources */,
6C79AD382539F9030033845C /* MenuActionButtonView.swift in Sources */,
6C7DB6D524E7A75E00133B06 /* NetworkView.swift in Sources */,
6CDBB4C325447B7500D01EA0 /* EulMenuComponent.swift in Sources */,
6CDBB4C1254462B100D01EA0 /* PreferenceSectionView.swift in Sources */,
6C7DB6E824E82C7700133B06 /* StringEnum.swift in Sources */,
6CC0798C250CD793000D7DAC /* PreferenceGeneralView.swift in Sources */,
6C457394253AC91500BB2584 /* TooltipView.swift in Sources */,
Expand All @@ -556,6 +567,7 @@
6C05F2C825120517004A0F72 /* Double.swift in Sources */,
6C993B902517920600586C6B /* BatteryMenuBlockView.swift in Sources */,
6C79AD362539EBE60033845C /* ProcessUsage.swift in Sources */,
6CDBB4C525447D8B00D01EA0 /* ComponentsStore.swift in Sources */,
6CE33C7D252601BB00831857 /* Int.swift in Sources */,
6C7DB6D124E0300E00133B06 /* NetworkStore.swift in Sources */,
6C33CEC92517634400345977 /* StatusMenuView.swift in Sources */,
Expand Down Expand Up @@ -712,7 +724,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 13;
CURRENT_PROJECT_VERSION = 14;
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = M8G2RFZVFV;
ENABLE_HARDENED_RUNTIME = YES;
Expand All @@ -724,7 +736,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 1.2.3;
MARKETING_VERSION = 1.3;
PRODUCT_BUNDLE_IDENTIFIER = com.gaosun.eul;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "";
Expand All @@ -742,7 +754,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 13;
CURRENT_PROJECT_VERSION = 14;
DEVELOPMENT_ASSET_PATHS = "";
DEVELOPMENT_TEAM = M8G2RFZVFV;
ENABLE_HARDENED_RUNTIME = YES;
Expand All @@ -754,7 +766,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = 1.2.3;
MARKETING_VERSION = 1.3;
PRODUCT_BUNDLE_IDENTIFIER = com.gaosun.eul;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "";
Expand Down
10 changes: 8 additions & 2 deletions eul/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,18 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
)
window.center()
window.setFrameAutosaveName("Eul Preferences")
window.contentView = NSHostingView(rootView: contentView)
window.contentView = NSHostingView(rootView: contentView.withGlobalEnvironmentObjects())
window.isReleasedWhenClosed = false
window.titlebarAppearsTransparent = true
window.standardWindowButton(.zoomButton)?.isHidden = true
window.standardWindowButton(.miniaturizeButton)?.isHidden = true
window.makeKeyAndOrderFront(nil)
window.delegate = self

// comment out for not showing window at login. no proper solution currently, tracking:
// https://github.com/sindresorhus/LaunchAtLogin/issues/33
window.makeKeyAndOrderFront(nil)
NSApp.activate(ignoringOtherApps: true)

SmcControl.shared.start()
refreshSMCRepeatedly()
refreshNetworkRepeatedly()
Expand Down
2 changes: 2 additions & 0 deletions eul/Extension/View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,7 @@ extension View {
.environmentObject(NetworkStore.shared)
.environmentObject(NetworkTopStore.shared)
.environmentObject(PreferenceStore.shared)
.environmentObject(sharedComponentsStore)
.environmentObject(sharedMenuComponentsStore)
}
}
46 changes: 46 additions & 0 deletions eul/Schema/EulMenuComponent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// EulMenuComponent.swift
// eul
//
// Created by Gao Sun on 2020/10/24.
// Copyright © 2020 Gao Sun. All rights reserved.
//

import Foundation
import SwiftUI

enum EulMenuComponent: String, CaseIterable, Identifiable {
var id: String {
rawValue
}

var localizedDescription: String {
"component.\(rawValue.lowercased())".localized()
}

func getView() -> AnyView {
switch self {
case .Battery:
return AnyView(BatteryMenuBlockView())
case .CPU:
return AnyView(CpuMenuBlockView())
case .Fan:
return AnyView(FanMenuBlockView())
case .Memory:
return AnyView(MemoryMenuBlockView())
case .Network:
return AnyView(NetworkMenuBlockMenuView())
}
}

case CPU
case Fan
case Memory
case Battery
case Network

static var allCases: [EulMenuComponent] {
let components: [EulMenuComponent] = [.CPU, .Fan, .Memory, .Network]
return BatteryStore.shared.isValid ? components + [.Battery] : components
}
}
5 changes: 3 additions & 2 deletions eul/StatusBar/StatusBarManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import SwiftUI

class StatusBarManager {
@ObservedObject var preferenceStore = PreferenceStore.shared
@ObservedObject var componentsStore = sharedComponentsStore
private var activeCancellable: AnyCancellable?
private var displayCancellable: AnyCancellable?
private var showComponentsCancellable: AnyCancellable?
Expand All @@ -27,13 +28,13 @@ class StatusBarManager {

func subscribe() {
// TO-DO: refactor
activeCancellable = preferenceStore.$activeComponents.sink {
activeCancellable = sharedComponentsStore.$activeComponents.sink {
self.render(components: $0)
}
displayCancellable = preferenceStore.$textDisplay.sink { _ in
self.refresh()
}
showComponentsCancellable = preferenceStore.$showComponents.sink { _ in
showComponentsCancellable = sharedComponentsStore.$showComponents.sink { _ in
self.refresh()
}
showIconCancellable = preferenceStore.$showIcon.sink { _ in
Expand Down
102 changes: 102 additions & 0 deletions eul/Store/ComponentsStore.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//
// ComponentsStore.swift
// eul
//
// Created by Gao Sun on 2020/10/24.
// Copyright © 2020 Gao Sun. All rights reserved.
//

import Combine
import Foundation
import SwiftyJSON

class ComponentsStore<Component: CaseIterable & RawRepresentable & Equatable>: ObservableObject where Component.RawValue == String {
@Published var showComponents = true
@Published var isActiveComponentToggling = false
@Published var activeComponents = Array(Component.allCases)
@Published var availableComponents: [Component] = []

private let userDefaultsKey: String
private var cancellable: AnyCancellable?

var json: JSON {
JSON([
"showComponents": showComponents,
"activeComponents": activeComponents.map { $0.rawValue },
"availableComponents": availableComponents.map { $0.rawValue },
])
}

init(key: String) {
userDefaultsKey = key
loadFromDefaults()
cancellable = objectWillChange.sink {
DispatchQueue.main.async {
self.saveToDefaults()
}
}
}

func toggleActiveComponent(at index: Int) {
isActiveComponentToggling = true
availableComponents.append(activeComponents[index])
activeComponents.remove(at: index)

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { // wait for rendering, will crash w/o delay
self.isActiveComponentToggling = false
}
}

func toggleAvailableComponent(at index: Int) {
activeComponents.append(availableComponents[index])
availableComponents.remove(at: index)
}

func loadFromDefaults() {
if let raw = UserDefaults.standard.data(forKey: userDefaultsKey) {
do {
let data = try JSON(data: raw)

print("⚙️ loaded data from user defaults", userDefaultsKey, data)

if let value = data["showComponents"].bool {
showComponents = value
}

if let array = data["activeComponents"].array {
activeComponents = array.compactMap {
if let string = $0.string {
return Component(rawValue: string)
}
return nil
}
}
if let array = data["availableComponents"].array {
availableComponents = array.compactMap {
if let string = $0.string {
return Component(rawValue: string)
}
return nil
}
availableComponents += Array(Component.allCases).filter {
!activeComponents.contains($0) && !availableComponents.contains($0)
}
}
} catch {
print("Unable to get preference data from user defaults")
}
}
}

func saveToDefaults() {
do {
let data = try json.rawData()
UserDefaults.standard.set(data, forKey: userDefaultsKey)
} catch {
print("Unable to get preference data")
}
}
}

let sharedComponentsStore = ComponentsStore<EulComponent>(key: "components")
let sharedMenuComponentsStore = ComponentsStore<EulMenuComponent>(key: "menuComponents")
12 changes: 8 additions & 4 deletions eul/Store/CpuTopStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ class CpuTopStore: ObservableObject {
}

init() {
update(shouldStart: preferenceStore.showTopActivities)
activeCancellable = preferenceStore.$showTopActivities.sink { [self] in
update(shouldStart: $0)
}
activeCancellable = Publishers
.CombineLatest(preferenceStore.$showCPUTopActivities, sharedMenuComponentsStore.$activeComponents)
.map {
$0 && $1.contains(.CPU)
}
.sink { [self] in
update(shouldStart: $0)
}
}
}
12 changes: 8 additions & 4 deletions eul/Store/NetworkTopStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,13 @@ class NetworkTopStore: ObservableObject {
}

init() {
update(shouldStart: preferenceStore.showTopActivities)
activeCancellable = preferenceStore.$showTopActivities.sink { [self] in
update(shouldStart: $0)
}
activeCancellable = Publishers
.CombineLatest(preferenceStore.$showNetworkTopActivities, sharedMenuComponentsStore.$activeComponents)
.map {
$0 && $1.contains(.Network)
}
.sink { [self] in
update(shouldStart: $0)
}
}
}
Loading

0 comments on commit 25ba49a

Please sign in to comment.