2023-03-01 11:43:48 +08:00
|
|
|
//
|
|
|
|
// NewStatusMenuView.swift
|
|
|
|
// ClashX Pro
|
|
|
|
//
|
|
|
|
// Created by yicheng on 2023/3/1.
|
|
|
|
// Copyright © 2023 west2online. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import AppKit
|
|
|
|
import SwiftUI
|
|
|
|
@available(macOS 10.15, *)
|
2023-04-26 14:14:19 +08:00
|
|
|
class NewStatusMenuView: NSHostingView<SwiftUIView>, StatusItemViewProtocol {
|
2023-03-01 11:43:48 +08:00
|
|
|
private var viewModel: StatusMenuViewModel!
|
|
|
|
|
|
|
|
static func create(on button: NSView) -> NewStatusMenuView {
|
|
|
|
let model = StatusMenuViewModel()
|
|
|
|
let view = NewStatusMenuView(rootView: SwiftUIView(viewModel: model))
|
|
|
|
view.viewModel = model
|
|
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
button.addSubview(view)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
view.topAnchor.constraint(equalTo: button.topAnchor),
|
|
|
|
view.leadingAnchor.constraint(equalTo: button.leadingAnchor),
|
|
|
|
view.trailingAnchor.constraint(equalTo: button.trailingAnchor),
|
|
|
|
view.bottomAnchor.constraint(equalTo: button.bottomAnchor)
|
|
|
|
])
|
|
|
|
return view
|
|
|
|
}
|
|
|
|
func updateViewStatus(enableProxy: Bool) {
|
|
|
|
viewModel.image = StatusItemTool.getMenuImage(enableProxy: enableProxy)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateSpeedLabel(up: Int, down: Int) {
|
|
|
|
let upSpeed = StatusItemTool.getSpeedString(for: up)
|
|
|
|
let downSpeed = StatusItemTool.getSpeedString(for: down)
|
|
|
|
if upSpeed != viewModel.upSpeed {viewModel.upSpeed = upSpeed}
|
|
|
|
if downSpeed != viewModel.downSpeed {viewModel.downSpeed = downSpeed}
|
|
|
|
}
|
|
|
|
|
|
|
|
func showSpeedContainer(show: Bool) {
|
|
|
|
viewModel.showSpeed = show
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateSize(width: CGFloat) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
@available(macOS 10.15, *)
|
2023-04-26 14:14:19 +08:00
|
|
|
class StatusMenuViewModel: ObservableObject {
|
2023-03-01 11:43:48 +08:00
|
|
|
@Published var image = StatusItemTool.getMenuImage(enableProxy: false)
|
|
|
|
@Published var upSpeed = "0KB/s"
|
|
|
|
@Published var downSpeed = "0KB/s"
|
|
|
|
@Published var showSpeed = true
|
|
|
|
}
|
|
|
|
|
|
|
|
@available(macOS 10.15, *)
|
|
|
|
struct SwiftUIView: View {
|
|
|
|
@ObservedObject var viewModel: StatusMenuViewModel
|
|
|
|
var body: some View {
|
2023-04-26 14:14:19 +08:00
|
|
|
HStack(alignment: .center) {
|
2023-03-01 11:43:48 +08:00
|
|
|
Image(nsImage: $viewModel.image.wrappedValue).renderingMode(.template)
|
|
|
|
if $viewModel.showSpeed.wrappedValue {
|
|
|
|
Spacer(minLength: 0)
|
|
|
|
VStack(alignment: .trailing) {
|
|
|
|
Text($viewModel.upSpeed.wrappedValue)
|
|
|
|
Text($viewModel.downSpeed.wrappedValue)
|
|
|
|
}.font(Font(StatusItemTool.font))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.frame(width: $viewModel.showSpeed.wrappedValue ? statusItemLengthWithSpeed - 6 : 25)
|
|
|
|
.frame(minHeight: 22)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@available(macOS 10.15, *)
|
|
|
|
struct SwiftUIView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
SwiftUIView(viewModel: StatusMenuViewModel())
|
|
|
|
}
|
|
|
|
}
|