Skip to content

Commit

Permalink
Merge pull request #74 from gao-sun/v1.4.3
Browse files Browse the repository at this point in the history
v1.4.3
  • Loading branch information
gao-sun authored Nov 11, 2020
2 parents a4bc63d + 85f913a commit adef125
Show file tree
Hide file tree
Showing 26 changed files with 204 additions and 73 deletions.
1 change: 1 addition & 0 deletions Resource/de.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
"language.de" = "Deutsch";
"language.ru" = "Русский";
"language.es" = "Español";
"language.pt" = "Português";

// MARK: General UI
"ui.app" = "App";
Expand Down
1 change: 1 addition & 0 deletions Resource/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
"language.de" = "Deutsch";
"language.ru" = "Русский";
"language.es" = "Español";
"language.pt" = "Português";

// MARK: General UI
"ui.app" = "App";
Expand Down
1 change: 1 addition & 0 deletions Resource/es.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
"language.de" = "Deutsch";
"language.ru" = "Русский";
"language.es" = "Español";
"language.pt" = "Português";

// MARK: General UI
"ui.app" = "Aplicación";
Expand Down
4 changes: 2 additions & 2 deletions Resource/pt.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"cpu.temperature" = "Temp";
"gpu.temperature" = "Temp GPU";
"cpu.info" = "Info";
"cpu.physical_cores" = Núcleos Físicos";
"cpu.physical_cores" = "Núcleos Físicos";
"cpu.logical_cores" = "Núcleos Lógicos";
"cpu.up_time" = "Tempo de Atividade";
"cpu.thermal_level" = "Nvl. Térmico";
Expand Down Expand Up @@ -81,7 +81,7 @@
"temp.kelvin" = "Kelvin";

// MARK: Text Display
"text_display" = Estilo de Texto";
"text_display" = "Estilo de Texto";
"text_display.compact" = "compacto";
"text_display.single_line" = "uma linha";

Expand Down
1 change: 1 addition & 0 deletions Resource/ru.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
"language.de" = "Deutsch";
"language.ru" = "Русский";
"language.es" = "Español";
"language.pt" = "Português";

// MARK: General UI
"ui.app" = "Приложение";
Expand Down
1 change: 1 addition & 0 deletions Resource/zh-Hans.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
"language.de" = "Deutsch";
"language.ru" = "Русский";
"language.es" = "Español";
"language.pt" = "Português";

// MARK: General UI
"ui.app" = "App";
Expand Down
20 changes: 20 additions & 0 deletions SharedLibrary/Extension/Int.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,24 @@ public extension Int {
}
return count
}

var readableTimeInMin: String {
if self <= 0 {
return "0 min"
}

var result = [String]()
let hour = self / 60
let minute = self % 60

if hour > 0 {
result.append("\(hour) hr")
}

if minute > 0 {
result.append("\(minute) min")
}

return result.joined(separator: " ")
}
}
2 changes: 1 addition & 1 deletion SharedLibrary/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSHumanReadableCopyright</key>
Expand Down
2 changes: 1 addition & 1 deletion SharedLibrary/Utilities/ByteUnit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public struct ByteUnit {
public var readable: String {
switch bytes {
case 0..<(kilo * kilo):
return "\(String(format: "%.\(kilobytes >= 100 ? 0 : 1)f", kilobytes)) KB"
return "\(String(format: "%.\(0)f", kilobytes)) KB"
case kilo..<(kilo * kilo * kilo):
return "\(String(format: "%.\(megabytes >= 100 ? 0 : 1)f", megabytes)) MB"
case (kilo * kilo * kilo)...UInt64.max:
Expand Down
73 changes: 43 additions & 30 deletions eul.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion eul/Extension/View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ extension View {
.environmentObject(PreferenceStore.shared)
.environmentObject(sharedComponentsStore)
.environmentObject(sharedMenuComponentsStore)
.environmentObject(StatusComponentConfigStore.shared)
.environmentObject(ComponentConfigStore.shared)
}
}
2 changes: 1 addition & 1 deletion eul/Schema/EulComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ enum EulComponent: String, CaseIterable, Identifiable, Codable {
"component.\(rawValue.lowercased())".localized()
}

var graphAvailable: Bool {
var isGraphAvailable: Bool {
guard self == .CPU || self == .Memory else {
return false
}
Expand Down
24 changes: 24 additions & 0 deletions eul/Schema/FramePreferenceKey.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// FramePreferenceKey.swift
// eul
//
// Created by Gao Sun on 2020/11/11.
// Copyright © 2020 Gao Sun. All rights reserved.
//

import SwiftUI

struct FramePreferenceData: Equatable {
let index: Int
let frame: CGRect
}

struct FramePreferenceKey: PreferenceKey {
typealias Value = [FramePreferenceData]

static var defaultValue: [FramePreferenceData] = []

static func reduce(value: inout [FramePreferenceData], nextValue: () -> [FramePreferenceData]) {
value += nextValue()
}
}
14 changes: 8 additions & 6 deletions eul/Schema/SizeChangeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ typealias SizeChangeViewBuilder = (SizeChange) -> AnyView

protocol SizeChangeView: View {
var onSizeChange: SizeChange { get }
func reportSize(_ geometry: GeometryProxy) -> Color
func reportSize(_ geometry: GeometryProxy) -> AnyView
}

extension SizeChangeView {
func reportSize(_ geometry: GeometryProxy) -> Color {
DispatchQueue.main.async {
self.onSizeChange?(geometry.size)
}
return Color.clear
func reportSize(_ geometry: GeometryProxy) -> AnyView {
AnyView(
Color.clear.preference(
key: SizePreferenceKey.self,
value: [geometry.size]
)
)
}
}
19 changes: 19 additions & 0 deletions eul/Schema/SizePreferenceKey.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// SizePreferenceKey.swift
// eul
//
// Created by Gao Sun on 2020/11/11.
// Copyright © 2020 Gao Sun. All rights reserved.
//

import SwiftUI

struct SizePreferenceKey: PreferenceKey {
typealias Value = [CGSize]

static var defaultValue: [CGSize] = []

static func reduce(value: inout [CGSize], nextValue: () -> [CGSize]) {
value += nextValue()
}
}
7 changes: 5 additions & 2 deletions eul/StatusBar/StatusBarItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ class StatusBarItem: NSObject, NSMenuDelegate {
private var statusView: NSHostingView<AnyView>?
private var menuView: NSHostingView<AnyView>?
private var shouldCloseObserver: NSObjectProtocol?
private var visibilityTimer: Timer?

var isVisible: Bool {
get { item.isVisible }
set {
item.isVisible = newValue
if newValue {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
visibilityTimer?.invalidate()
visibilityTimer = Timer.scheduledTimer(withTimeInterval: 1.5, repeats: false, block: { _ in
self.checkStatusItemVisibility()
}
})
}
}
}
Expand Down Expand Up @@ -90,6 +92,7 @@ class StatusBarItem: NSObject, NSMenuDelegate {
let customItem = NSMenuItem()
menuView = StatusBarMenuHostingView(rootView: menuBuilder(onMenuSizeChange))
menuView?.translatesAutoresizingMaskIntoConstraints = false
menuView?.setFrameSize(NSSize(width: 1, height: 1))
customItem.view = menuView
statusBarMenu.addItem(customItem)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// StatusComponentConfigStore.swift
// ComponentConfigStore.swift
// eul
//
// Created by Gao Sun on 2020/11/8.
Expand All @@ -10,9 +10,9 @@ import Combine
import Foundation
import SwiftyJSON

class StatusComponentConfigStore: ObservableObject {
static let shared = StatusComponentConfigStore()
private let userDefaultsKey = "statusComponentConfig"
class ComponentConfigStore: ObservableObject {
static let shared = ComponentConfigStore()
private let userDefaultsKey = "componentConfig"
private var cancellable: AnyCancellable?

@Published var configDict: [EulComponent: EulComponentConfig] = [:]
Expand Down
2 changes: 1 addition & 1 deletion eul/Store/CpuStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class CpuStore: ObservableObject, Refreshable {
private func getUsage() {
let usage = Info.system.usageCPU()
usageCPU = usage
usageString = String(format: "%.1f%%", usage.system + usage.user)
usageString = String(format: "%.0f%%", usage.system + usage.user)
usageHistory = (usageHistory + [usage.system + usage.user]).suffix(10)
}

Expand Down
39 changes: 32 additions & 7 deletions eul/ViewModifier/StableWidth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,43 @@
import SwiftUI

extension View {
func stableWidth(_ factor: CGFloat = 10) -> some View {
modifier(StableWidth(factor: factor))
func stableWidth(_ factor: CGFloat = 8, minWidth: CGFloat? = nil) -> some View {
modifier(StableWidth(factor: factor, minWidth: minWidth))
}
}

private struct CGFloatPreferenceKey: PreferenceKey {
typealias Value = [CGFloat]

static var defaultValue: [CGFloat] = []

static func reduce(value: inout [CGFloat], nextValue: () -> [CGFloat]) {
value += nextValue()
}
}

struct StableWidth: ViewModifier {
@State private var idealWidth: CGFloat?

var factor: CGFloat
var minWidth: CGFloat?

func getSize(_ proxy: GeometryProxy) -> some View {
DispatchQueue.main.async { [self] in
idealWidth = factor * ceil(proxy.size.width / factor)
var computedIdealWidth: CGFloat? {
if let idealWidth = idealWidth {
if let minWidth = minWidth {
return max(idealWidth, minWidth)
}
return idealWidth
}
return Color.clear

return nil
}

func getSize(_ proxy: GeometryProxy) -> some View {
Color.clear.preference(
key: CGFloatPreferenceKey.self,
value: [factor * ceil(proxy.size.width / factor)]
)
}

func body(content: Content) -> some View {
Expand All @@ -33,7 +55,10 @@ struct StableWidth: ViewModifier {
.fixedSize()
.background(GeometryReader { getSize($0) })
}
.frame(idealWidth: idealWidth)
.frame(idealWidth: computedIdealWidth)
.fixedSize()
.onPreferenceChange(CGFloatPreferenceKey.self, perform: { value in
idealWidth = value.first
})
}
}
4 changes: 2 additions & 2 deletions eul/Views/Menu/BatteryMenuBlockView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ struct BatteryMenuBlockView: View {
.menuSection()
Spacer()
if io.isCharging && io.timeToFullCharge >= 0 {
Text("\(io.timeToFullCharge) min")
Text(io.timeToFullCharge.readableTimeInMin)
.displayText()
Text("battery.to_full_charge".localized())
.miniSection()
.padding(.trailing, 4)
}
if !io.isCharging && !io.isCharged && io.timeToEmpty >= 0 {
Text("\(io.timeToEmpty) min")
Text(io.timeToEmpty.readableTimeInMin)
.displayText()
Text("battery.to_empty".localized())
.miniSection()
Expand Down
5 changes: 5 additions & 0 deletions eul/Views/Menu/StatusMenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,10 @@ struct StatusMenuView: SizeChangeView {
.fixedSize()
.animation(.none)
.background(GeometryReader { self.reportSize($0) })
.onPreferenceChange(SizePreferenceKey.self, perform: { value in
if let size = value.first {
onSizeChange?(size)
}
})
}
}
17 changes: 11 additions & 6 deletions eul/Views/Preference/PreferenceComponentsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ extension Preference {
@GestureState var offsetWidth: CGFloat = 0

func updateFrame(geometry: GeometryProxy, index: Int) -> some View {
if !componentsStore.isActiveComponentToggling {
DispatchQueue.main.async {
self.frames[index] = geometry.frame(in: CoordinateSpace.named(coordinateSpace))
}
}
return Color.clear
Color.clear.preference(
key: FramePreferenceKey.self,
value: componentsStore.isActiveComponentToggling
? []
: [FramePreferenceData(index: index, frame: geometry.frame(in: CoordinateSpace.named(coordinateSpace)))]
)
}

var body: some View {
Expand Down Expand Up @@ -177,6 +177,11 @@ extension Preference {
}
}
}
.onPreferenceChange(FramePreferenceKey.self, perform: { value in
for data in value {
self.frames[data.index] = data.frame
}
})
}
}
}
17 changes: 11 additions & 6 deletions eul/Views/Preference/PreferenceMenuViewView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ extension Preference {
@GestureState var offsetHeight: CGFloat = 0

func updateFrame(geometry: GeometryProxy, index: Int) -> some View {
if !componentsStore.isActiveComponentToggling {
DispatchQueue.main.async {
self.frames[index] = geometry.frame(in: CoordinateSpace.named(coordinateSpace))
}
}
return Color.clear
Color.clear.preference(
key: FramePreferenceKey.self,
value: componentsStore.isActiveComponentToggling
? []
: [FramePreferenceData(index: index, frame: geometry.frame(in: CoordinateSpace.named(coordinateSpace)))]
)
}

var body: some View {
Expand Down Expand Up @@ -187,6 +187,11 @@ extension Preference {
}
}
.padding(.vertical, 8)
.onPreferenceChange(FramePreferenceKey.self, perform: { value in
for data in value {
self.frames[data.index] = data.frame
}
})
}
}
}
Loading

0 comments on commit adef125

Please sign in to comment.