Added initial menu bar to macOS
This commit is contained in:
parent
923bc9511d
commit
5438542284
5 changed files with 149 additions and 31 deletions
|
|
@ -4,14 +4,50 @@ import SwiftUI
|
|||
@main
|
||||
@MainActor
|
||||
struct BurrowApp: App {
|
||||
//To connect to the App Delegate
|
||||
@NSApplicationDelegateAdaptor(AppDelegate.self) var delegate
|
||||
|
||||
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
TunnelView()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@MainActor
|
||||
class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
|
||||
static let tunnel = Tunnel { manager, proto in
|
||||
proto.serverAddress = "hackclub.com"
|
||||
manager.localizedDescription = "Burrow"
|
||||
}
|
||||
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
TunnelView(tunnel: Self.tunnel)
|
||||
var statusItem: NSStatusItem?
|
||||
var popOver = NSPopover()
|
||||
func applicationDidFinishLaunching(_ notification: Notification) {
|
||||
let menuView = MenuView(tunnel: AppDelegate.tunnel)
|
||||
// Creating apopOver
|
||||
popOver.behavior = .transient
|
||||
popOver.animates = true
|
||||
popOver.contentViewController = NSViewController()
|
||||
popOver.contentViewController?.view = NSHostingView(rootView: menuView)
|
||||
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
|
||||
// Safe Check if status Button is Available or not...
|
||||
if let menuButton = statusItem?.button {
|
||||
let icon = "network.badge.shield.half.filled"
|
||||
menuButton.image = NSImage(systemSymbolName: icon, accessibilityDescription: nil)
|
||||
menuButton.action = #selector(menuButtonToggle)
|
||||
}
|
||||
}
|
||||
@objc func menuButtonToggle() {
|
||||
|
||||
|
||||
if let menuButton = statusItem?.button {
|
||||
|
||||
self.popOver.show(relativeTo: menuButton.bounds, of: menuButton, preferredEdge: NSRectEdge.minY)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
69
Apple/App/Menu/MenuView.swift
Normal file
69
Apple/App/Menu/MenuView.swift
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
//
|
||||
// MenuView.swift
|
||||
// App
|
||||
//
|
||||
// Created by Thomas Stubblefield on 5/13/23.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct MenuView: View {
|
||||
@State private var isToggled = false
|
||||
@ObservedObject var tunnel: Tunnel
|
||||
|
||||
private func start() {
|
||||
|
||||
do {
|
||||
try tunnel.start()
|
||||
} catch {
|
||||
print(error)
|
||||
}
|
||||
}
|
||||
|
||||
private func stop() {
|
||||
tunnel.stop()
|
||||
}
|
||||
|
||||
private func configure() {
|
||||
Task { try await tunnel.configure() }
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
HStack {
|
||||
Text("Burrow")
|
||||
.fontWeight(.bold)
|
||||
|
||||
Spacer()
|
||||
Toggle("", isOn: $isToggled)
|
||||
.toggleStyle(SwitchToggleStyle(tint: .blue))
|
||||
.onChange(of: isToggled) { value in
|
||||
if value {
|
||||
start()
|
||||
} else {
|
||||
stop()
|
||||
}
|
||||
print("Toggle value: \(value)")
|
||||
}
|
||||
}
|
||||
Divider()
|
||||
switch tunnel.status {
|
||||
case .permissionRequired:
|
||||
VStack(alignment: .leading) {
|
||||
Text("Burrow requires additional permissions to function optimally on your machine. Please grant the necessary permissions to ensure smooth operation.")
|
||||
.font(.caption)
|
||||
.truncationMode(.tail)
|
||||
|
||||
Button("Grant Permissions", action: configure)
|
||||
}
|
||||
default:
|
||||
|
||||
Text("Burrow is equipped with the necessary permissions to operate seamlessly on your device.")
|
||||
.font(.caption)
|
||||
}
|
||||
}
|
||||
.frame(width: 250)
|
||||
.padding(16)
|
||||
.task { await tunnel.update() }
|
||||
}
|
||||
}
|
||||
|
|
@ -92,7 +92,7 @@ class Tunnel: ObservableObject {
|
|||
let proto = NETunnelProviderProtocol()
|
||||
proto.providerBundleIdentifier = bundleIdentifier
|
||||
configure(manager, proto)
|
||||
|
||||
|
||||
manager.protocolConfiguration = proto
|
||||
try await manager.save()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,35 +1,36 @@
|
|||
import SwiftUI
|
||||
|
||||
struct TunnelView: View {
|
||||
@ObservedObject var tunnel: Tunnel
|
||||
// @ObservedObject var tunnel: Tunnel
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
Text(verbatim: tunnel.status.description)
|
||||
switch tunnel.status {
|
||||
case .connected:
|
||||
Button("Disconnect", action: stop)
|
||||
case .permissionRequired:
|
||||
Button("Allow", action: configure)
|
||||
case .disconnected:
|
||||
Button("Start", action: start)
|
||||
default:
|
||||
EmptyView()
|
||||
}
|
||||
}
|
||||
.task { await tunnel.update() }
|
||||
.padding()
|
||||
EmptyView()
|
||||
// VStack {
|
||||
// Text(verbatim: tunnel.status.description)
|
||||
// switch tunnel.status {
|
||||
// case .connected:
|
||||
// Button("Disconnect", action: stop)
|
||||
// case .permissionRequired:
|
||||
// Button("Allow", action: configure)
|
||||
// case .disconnected:
|
||||
// Button("Start", action: start)
|
||||
// default:
|
||||
// EmptyView()
|
||||
// }
|
||||
// }
|
||||
// .task { await tunnel.update() }
|
||||
// .padding()
|
||||
}
|
||||
|
||||
private func start() {
|
||||
try? tunnel.start()
|
||||
}
|
||||
|
||||
private func stop() {
|
||||
tunnel.stop()
|
||||
}
|
||||
|
||||
private func configure() {
|
||||
Task { try await tunnel.configure() }
|
||||
}
|
||||
// private func start() {
|
||||
// try? tunnel.start()
|
||||
// }
|
||||
//
|
||||
// private func stop() {
|
||||
// tunnel.stop()
|
||||
// }
|
||||
//
|
||||
// private func configure() {
|
||||
// Task { try await tunnel.configure() }
|
||||
// }
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue