Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3442516

Browse files
1 parentf33afbd commit3442516

File tree

4 files changed

+83
-18
lines changed

4 files changed

+83
-18
lines changed

‎Coder Desktop/Coder Desktop/Coder_DesktopApp.swift

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import FluidMenuBarExtra
2+
import NetworkExtension
23
import SwiftUI
34

45
@main
@@ -26,7 +27,7 @@ struct DesktopApp: App {
2627

2728
@MainActor
2829
classAppDelegate:NSObject,NSApplicationDelegate{
29-
privatevarmenuBarExtra:FluidMenuBarExtra?
30+
privatevarmenuBar:MenuBarController?
3031
letvpn:CoderVPNService
3132
letstate:AppState
3233

@@ -36,11 +37,18 @@ class AppDelegate: NSObject, NSApplicationDelegate {
3637
}
3738

3839
func applicationDidFinishLaunching(_:Notification){
39-
menuBarExtra=FluidMenuBarExtra(title:"Coder Desktop", image:"MenuBarIcon"){
40+
menuBar=.init(menuBarExtra:FluidMenuBarExtra(title:"Coder Desktop", image:"MenuBarIcon"){
4041
VPNMenu<CoderVPNService>().frame(width:256)
4142
.environmentObject(self.vpn)
4243
.environmentObject(self.state)
43-
}
44+
})
45+
// Subscribe to system VPN updates
46+
NotificationCenter.default.addObserver(
47+
self,
48+
selector: #selector(vpnDidUpdate(_:)),
49+
name:.NEVPNStatusDidChange,
50+
object:nil
51+
)
4452
}
4553

4654
// This function MUST eventually call `NSApp.reply(toApplicationShouldTerminate: true)`
@@ -59,6 +67,16 @@ class AppDelegate: NSObject, NSApplicationDelegate {
5967
}
6068
}
6169

70+
extensionAppDelegate{
71+
@objcprivatefunc vpnDidUpdate(_ notification:Notification){
72+
guardlet connection= notification.objectas?NETunnelProviderSessionelse{
73+
return
74+
}
75+
vpn.vpnDidUpdate(connection)
76+
menuBar?.vpnDidUpdate(connection)
77+
}
78+
}
79+
6280
@MainActor
6381
func appActivate(){
6482
NSApp.activate()
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import FluidMenuBarExtra
2+
import NetworkExtension
3+
import SwiftUI
4+
5+
@MainActor
6+
classMenuBarController{
7+
letmenuBarExtra:FluidMenuBarExtra
8+
privateletonImage=NSImage(named:"MenuBarIcon")!
9+
privateletoffOpacity=CGFloat(0.3)
10+
privateletonOpacity=CGFloat(1.0)
11+
12+
privatevaranimationTask:Task<Void,Never>?
13+
14+
init(menuBarExtra:FluidMenuBarExtra){
15+
self.menuBarExtra= menuBarExtra
16+
}
17+
18+
func vpnDidUpdate(_ connection:NETunnelProviderSession){
19+
switch connection.status{
20+
case.connected:
21+
stopAnimation()
22+
menuBarExtra.setOpacity(onOpacity)
23+
case.connecting,.reasserting,.disconnecting:
24+
startAnimation()
25+
case.invalid,.disconnected:
26+
stopAnimation()
27+
menuBarExtra.setOpacity(offOpacity)
28+
@unknowndefault:
29+
stopAnimation()
30+
menuBarExtra.setOpacity(offOpacity)
31+
}
32+
}
33+
34+
func startAnimation(){
35+
if animationTask!=nil{return}
36+
animationTask=Task{
37+
defer{ animationTask=nil}
38+
lettotalFrames=60
39+
letcycleDurationMs:UInt64=2000
40+
letframeDurationMs= cycleDurationMs/ UInt64(totalFrames-1)
41+
repeat{
42+
forframein0..< totalFrames{
43+
ifTask.isCancelled{break}
44+
letprogress=Double(frame)/ Double(totalFrames-1)
45+
letalpha=0.3+0.7*(0.5-0.5* cos(2* Double.pi* progress))
46+
menuBarExtra.setOpacity(CGFloat(alpha))
47+
try?awaitTask.sleep(for:.milliseconds(frameDurationMs))
48+
}
49+
}while !Task.isCancelled
50+
}
51+
}
52+
53+
func stopAnimation(){
54+
animationTask?.cancel()
55+
animationTask=nil
56+
}
57+
}

‎Coder Desktop/Coder Desktop/VPNService.swift

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,6 @@ final class CoderVPNService: NSObject, VPNService {
7070
Task{
7171
awaitloadNetworkExtensionConfig()
7272
}
73-
NotificationCenter.default.addObserver(
74-
self,
75-
selector: #selector(vpnDidUpdate(_:)),
76-
name:.NEVPNStatusDidChange,
77-
object:nil
78-
)
7973
}
8074

8175
deinit{
@@ -159,13 +153,7 @@ final class CoderVPNService: NSObject, VPNService {
159153
}
160154

161155
extensionCoderVPNService{
162-
// The number of NETunnelProviderSession states makes the excessive branching
163-
// necessary.
164-
// swiftlint:disable:next cyclomatic_complexity
165-
@objcprivatefunc vpnDidUpdate(_ notification:Notification){
166-
guardlet connection= notification.objectas?NETunnelProviderSessionelse{
167-
return
168-
}
156+
publicfunc vpnDidUpdate(_ connection:NETunnelProviderSession){
169157
switch(tunnelState, connection.status){
170158
// Any -> Disconnected: Update UI w/ error if present
171159
case(_,.disconnected):

‎Coder Desktop/project.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ packages:
8989
url:https://github.com/SimplyDanny/SwiftLintPlugins
9090
from:0.57.1
9191
FluidMenuBarExtra:
92-
url:https://github.com/lfroms/fluid-menu-bar-extra
93-
from:1.1.0
92+
# Forked so we can dynamically update the menu bar icon.
93+
# The upstream repo has a purposefully limited API
94+
url:https://github.com/coder/fluid-menu-bar-extra
95+
revision:020be37
9496
KeychainAccess:
9597
url:https://github.com/kishikawakatsumi/KeychainAccess
9698
branch:e0c7eebc5a4465a3c4680764f26b7a61f567cdaf

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp