Skip to content

Commit 45190c1

Browse files
committed
Changed the window style to more consistent. Handling keyboard navigation to fix bug when using plain window style. Focusing list by default so it is highlighted. Made welcome action symbols a bit smaller
1 parent a6efad2 commit 45190c1

File tree

5 files changed

+127
-22
lines changed

5 files changed

+127
-22
lines changed

CodeEdit/Features/Welcome/Views/RecentProjectsListView.swift

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ import SwiftUI
99
import CoreSpotlight
1010

1111
struct RecentProjectsListView: View {
12+
@Environment(\.colorScheme)
13+
var colorScheme
14+
15+
@FocusState private var isFocused: Bool
1216

1317
@State private var selection: Set<URL>
14-
@State var recentProjects: [URL]
18+
@State private var recentProjects: [URL]
19+
@State private var eventMonitor: Any?
1520

1621
private let openDocument: (URL?, @escaping () -> Void) -> Void
1722
private let dismissWindow: () -> Void
@@ -37,6 +42,7 @@ struct RecentProjectsListView: View {
3742
List(recentProjects, id: \.self, selection: $selection) { project in
3843
RecentProjectListItem(projectPath: project)
3944
}
45+
.focused($isFocused)
4046
.listStyle(.sidebar)
4147
.contextMenu(forSelectionType: URL.self) { items in
4248
switch items.count {
@@ -66,7 +72,15 @@ struct RecentProjectsListView: View {
6672
.onDeleteCommand {
6773
removeRecentProjects()
6874
}
69-
.background(EffectView(.underWindowBackground, blendingMode: .behindWindow))
75+
.background {
76+
if self.colorScheme == .dark {
77+
Color(.black).opacity(0.075)
78+
.background(.thickMaterial)
79+
} else {
80+
Color(.white).opacity(0.6)
81+
.background(.regularMaterial)
82+
}
83+
}
7084
.background {
7185
Button("") {
7286
selection.forEach { openDocument($0, dismissWindow) }
@@ -84,6 +98,22 @@ struct RecentProjectsListView: View {
8498
.onReceive(NotificationCenter.default.publisher(for: RecentProjectsStore.didUpdateNotification)) { _ in
8599
updateRecentProjects()
86100
}
101+
.onAppear {
102+
isFocused = true
103+
// NOTE: workaround for FB16112506
104+
self.eventMonitor = NSEvent.addLocalMonitorForEvents(matching: [.keyDown]) { event in
105+
switch event.keyCode {
106+
case 126: // Up Arrow
107+
return self.handleArrowUpKeyPressed() == .handled ? nil : event
108+
case 125: // Down Arrow
109+
return self.handleArrowDownKeyPressed() == .handled ? nil : event
110+
case 76, 36: // Enter and Return Arrow
111+
return self.handleReturnKeyPressed() == .handled ? nil : event
112+
default:
113+
return event
114+
}
115+
}
116+
}
87117
}
88118

89119
func removeRecentProjects() {
@@ -93,4 +123,49 @@ struct RecentProjectsListView: View {
93123
func updateRecentProjects() {
94124
recentProjects = RecentProjectsStore.recentProjectURLs()
95125
}
126+
127+
// MARK: - Key Handling
128+
129+
enum KeyHandlingResult {
130+
case handled
131+
case notHandled
132+
}
133+
134+
@discardableResult
135+
private func handleArrowUpKeyPressed() -> KeyHandlingResult {
136+
guard let current = currentSelectedIndex() else {
137+
selection = Set(recentProjects.suffix(1)) // select last if none selected
138+
return .handled
139+
}
140+
if current > 0 {
141+
selection = [recentProjects[current - 1]]
142+
return .handled
143+
}
144+
return .handled
145+
}
146+
147+
@discardableResult
148+
private func handleArrowDownKeyPressed() -> KeyHandlingResult {
149+
guard let current = currentSelectedIndex() else {
150+
selection = Set(recentProjects.prefix(1)) // select first if none selected
151+
return .handled
152+
}
153+
if current < recentProjects.count - 1 {
154+
selection = [recentProjects[current + 1]]
155+
return .handled
156+
}
157+
return .handled
158+
}
159+
160+
@discardableResult
161+
private func handleReturnKeyPressed() -> KeyHandlingResult {
162+
guard let selected = selection.first else { return .notHandled }
163+
openDocument(selected, dismissWindow)
164+
return .handled
165+
}
166+
167+
private func currentSelectedIndex() -> Int? {
168+
guard let selected = selection.first else { return nil }
169+
return recentProjects.firstIndex(of: selected)
170+
}
96171
}

CodeEdit/Features/Welcome/Views/WelcomeActionView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct WelcomeActionView: View {
2424
Image(systemName: iconName)
2525
.aspectRatio(contentMode: .fit)
2626
.foregroundColor(.secondary)
27-
.font(.system(size: 20))
27+
.font(.system(size: 17, weight: .medium))
2828
.frame(width: 24)
2929
Text(title)
3030
.font(.system(size: 13, weight: .semibold))

CodeEdit/Features/Welcome/Views/WelcomeView.swift

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ struct WelcomeView: View {
173173
copyInformation()
174174
}
175175
.help("Copy System Information to Clipboard")
176-
177176
Spacer().frame(height: 40)
178177
HStack {
179178
VStack(alignment: .leading, spacing: 8) {
@@ -207,12 +206,16 @@ struct WelcomeView: View {
207206
.padding(.horizontal, 56)
208207
.padding(.bottom, 16)
209208
.frame(width: 460)
210-
.background(
211-
colorScheme == .dark
212-
? Color(.black).opacity(0.2)
213-
: Color(.white).opacity(controlActiveState == .inactive ? 1.0 : 0.5)
214-
)
215-
.background(EffectView(.underWindowBackground, blendingMode: .behindWindow))
209+
.frame(maxHeight: .infinity)
210+
.background {
211+
if self.colorScheme == .dark {
212+
Color(.black).opacity(0.275)
213+
.background(.ultraThickMaterial)
214+
} else {
215+
Color(.white)
216+
.background(.regularMaterial)
217+
}
218+
}
216219
}
217220

218221
private var dismissButton: some View {

CodeEdit/Features/Welcome/Views/WelcomeWindow.swift

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,46 @@
88
import SwiftUI
99

1010
struct WelcomeWindow: Scene {
11-
1211
@ObservedObject var settings = Settings.shared
1312

13+
var windowContent: some View {
14+
ContentView()
15+
.task {
16+
if let window = NSApp.findWindow(.welcome) {
17+
window.standardWindowButton(.closeButton)?.isHidden = true
18+
window.standardWindowButton(.miniaturizeButton)?.isHidden = true
19+
window.standardWindowButton(.zoomButton)?.isHidden = true
20+
window.isMovableByWindowBackground = true
21+
}
22+
}
23+
}
24+
1425
var body: some Scene {
15-
Window("Welcome To CodeEdit", id: SceneID.welcome.rawValue) {
16-
ContentView()
26+
#if swift(>=5.9) // Needed to safely use availability in Scene builder
27+
if #available(macOS 15, *) {
28+
return Window("Welcome To CodeEdit", id: SceneID.welcome.rawValue) {
29+
windowContent
30+
.frame(width: 740, height: 460)
31+
}
32+
.windowStyle(.plain)
33+
.windowResizability(.contentSize)
34+
.defaultLaunchBehavior(.presented)
35+
} else {
36+
return Window("Welcome To CodeEdit", id: SceneID.welcome.rawValue) {
37+
windowContent
38+
.frame(width: 740, height: 432)
39+
}
40+
.windowStyle(.hiddenTitleBar)
41+
.windowResizability(.contentSize)
42+
}
43+
#else
44+
return Window("Welcome To CodeEdit", id: SceneID.welcome.rawValue) {
45+
windowContent
1746
.frame(width: 740, height: 432)
18-
.task {
19-
if let window = NSApp.findWindow(.welcome) {
20-
window.standardWindowButton(.closeButton)?.isHidden = true
21-
window.standardWindowButton(.miniaturizeButton)?.isHidden = true
22-
window.standardWindowButton(.zoomButton)?.isHidden = true
23-
window.isMovableByWindowBackground = true
24-
}
25-
}
2647
}
2748
.windowStyle(.hiddenTitleBar)
2849
.windowResizability(.contentSize)
50+
#endif
2951
}
3052

3153
struct ContentView: View {

CodeEdit/Features/Welcome/Views/WelcomeWindowView.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ struct WelcomeWindowView: View {
3030
dismissWindow: dismissWindow
3131
)
3232
RecentProjectsListView(openDocument: openDocument, dismissWindow: dismissWindow)
33-
.frame(width: 280)
3433
}
34+
.clipShape(.rect(cornerRadius: 8))
35+
.onAppear {
36+
NSApplication.shared.windows.first?.isMovableByWindowBackground = true
37+
NSApplication.shared.windows.first?.hasShadow = true
38+
}
39+
.cursor(.current)
3540
.edgesIgnoringSafeArea(.top)
3641
.onDrop(of: [.fileURL], isTargeted: .constant(true)) { providers in
3742
NSApp.activate(ignoringOtherApps: true)

0 commit comments

Comments
 (0)