Skip to content

Commit bf33b92

Browse files
committed
Update panel style and mode change logic
1 parent e975775 commit bf33b92

File tree

5 files changed

+49
-16
lines changed

5 files changed

+49
-16
lines changed

Core/Sources/Environment/Environment.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,15 @@ public enum Environment {
138138

139139
try await runAppleScript(appleScript)
140140
}
141+
142+
public static var makeXcodeActive: () async throws -> Void = {
143+
let appleScript = """
144+
tell application "Xcode"
145+
activate
146+
end tell
147+
"""
148+
try await runAppleScript(appleScript)
149+
}
141150
}
142151

143152
@discardableResult

Core/Sources/SuggestionWidget/Styles.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import AppKit
22
import SwiftUI
33

44
enum Style {
5-
static let panelHeight: Double = 300
6-
static let panelWidth: Double = 450
5+
static let panelHeight: Double = 304
6+
static let panelWidth: Double = 454
77
static let widgetHeight: Double = 30
88
static var widgetWidth: Double { widgetHeight }
99
static let widgetPadding: Double = 4

Core/Sources/SuggestionWidget/SuggestionPanelContent/ChatPanel.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ struct ChatPanel: View {
2828
chat.close()
2929
}) {
3030
Image(systemName: "xmark")
31-
.padding(8)
32-
.background(.regularMaterial, in: Circle())
3331
.padding(4)
32+
.background(.regularMaterial, in: Circle())
33+
.padding(2)
3434
.foregroundStyle(.secondary)
3535
}
3636
.buttonStyle(.plain)
@@ -112,6 +112,7 @@ struct ChatPanelMessages: View {
112112
)
113113
.xcodeStyleFrame()
114114
.rotationEffect(Angle(degrees: 180))
115+
115116
}
116117
}
117118
}

Core/Sources/SuggestionWidget/SuggestionPanelView.swift

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Environment
12
import SwiftUI
23

34
@MainActor
@@ -40,15 +41,8 @@ final class SuggestionPanelViewModel: ObservableObject {
4041

4142
@Published var activeTab: ActiveTab {
4243
didSet {
43-
#warning("""
44-
TODO: There should be a better way for that
45-
Currently, we have to make the app an accessory so that we can type things in the chat mode.
46-
But in other modes, we want to keep it prohibited so the helper app won't take over the focus.
47-
""")
48-
if case .chat = activeTab {
49-
NSApp.setActivationPolicy(.accessory)
50-
} else {
51-
NSApp.setActivationPolicy(.prohibited)
44+
if activeTab != oldValue {
45+
onActiveTabChanged?(activeTab)
5246
}
5347
}
5448
}
@@ -61,6 +55,7 @@ final class SuggestionPanelViewModel: ObservableObject {
6155
var onRejectButtonTapped: (() -> Void)?
6256
var onPreviousButtonTapped: (() -> Void)?
6357
var onNextButtonTapped: (() -> Void)?
58+
var onActiveTabChanged: ((ActiveTab) -> Void)?
6459

6560
public init(
6661
content: Content? = nil,
@@ -71,7 +66,8 @@ final class SuggestionPanelViewModel: ObservableObject {
7166
onAcceptButtonTapped: (() -> Void)? = nil,
7267
onRejectButtonTapped: (() -> Void)? = nil,
7368
onPreviousButtonTapped: (() -> Void)? = nil,
74-
onNextButtonTapped: (() -> Void)? = nil
69+
onNextButtonTapped: (() -> Void)? = nil,
70+
onActiveTabChanged: ((ActiveTab) -> Void)? = nil
7571
) {
7672
self.content = content
7773
self.chat = chat
@@ -82,6 +78,7 @@ final class SuggestionPanelViewModel: ObservableObject {
8278
self.onRejectButtonTapped = onRejectButtonTapped
8379
self.onPreviousButtonTapped = onPreviousButtonTapped
8480
self.onNextButtonTapped = onNextButtonTapped
81+
self.onActiveTabChanged = onActiveTabChanged
8582
}
8683

8784
func adjustActiveTabAndShowHideIfNeeded(tab: ActiveTab) {
@@ -90,11 +87,13 @@ final class SuggestionPanelViewModel: ObservableObject {
9087
if content != nil {
9188
activeTab = .suggestion
9289
isPanelDisplayed = true
90+
return
9391
}
9492
case .chat:
9593
if chat != nil {
9694
activeTab = .chat
9795
isPanelDisplayed = true
96+
return
9897
}
9998
}
10099

@@ -158,6 +157,7 @@ struct SuggestionPanelView: View {
158157
Color.userChatContentBackground,
159158
in: RoundedRectangle(cornerRadius: 8, style: .continuous)
160159
)
160+
.fixedSize(horizontal: false, vertical: true)
161161
})
162162
.buttonStyle(.plain)
163163
.xcodeStyleFrame()
@@ -187,6 +187,7 @@ struct SuggestionPanelView: View {
187187
Color.userChatContentBackground,
188188
in: RoundedRectangle(cornerRadius: 8, style: .continuous)
189189
)
190+
.fixedSize(horizontal: false, vertical: true)
190191
})
191192
.buttonStyle(.plain)
192193
.xcodeStyleFrame()
@@ -211,6 +212,9 @@ struct SuggestionPanelView: View {
211212
.animation(.easeInOut(duration: 0.2), value: viewModel.chat)
212213
.animation(.easeInOut(duration: 0.2), value: viewModel.activeTab)
213214
.animation(.easeInOut(duration: 0.2), value: viewModel.isPanelDisplayed)
215+
.shadow(color: .black.opacity(0.1), radius: 2)
216+
.padding(2)
217+
.frame(maxWidth: Style.panelWidth, maxHeight: Style.panelHeight)
214218
}
215219
}
216220

@@ -221,7 +225,7 @@ struct CommandButtonStyle: ButtonStyle {
221225
configuration.label
222226
.padding(.vertical, 4)
223227
.padding(.horizontal, 8)
224-
.foregroundColor(.white)
228+
.foregroundColor(.white)
225229
.background(
226230
RoundedRectangle(cornerRadius: 4, style: .continuous)
227231
.fill(color.opacity(configuration.isPressed ? 0.8 : 1))

Core/Sources/SuggestionWidget/SuggestionWidgetController.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,24 @@ public final class SuggestionWidgetController {
197197
context: nil
198198
)
199199
}
200+
201+
Task { @MainActor in
202+
suggestionPanelViewModel.onActiveTabChanged = { [weak self] activeTab in
203+
#warning("""
204+
TODO: There should be a better way for that
205+
Currently, we have to make the app an accessory so that we can type things in the chat mode.
206+
But in other modes, we want to keep it prohibited so the helper app won't take over the focus.
207+
""")
208+
if case .chat = activeTab {
209+
NSApp.setActivationPolicy(.accessory)
210+
} else {
211+
Task {
212+
try await Environment.makeXcodeActive()
213+
NSApp.setActivationPolicy(.prohibited)
214+
}
215+
}
216+
}
217+
}
200218
}
201219
}
202220
@@ -384,7 +402,8 @@ extension SuggestionWidgetController {
384402
activeScreen: firstScreen
385403
)
386404
widgetWindow.setFrame(result.widgetFrame, display: false, animate: animated)
387-
panelWindow.setFrame(result.panelFrame, display: false, animate: animated)
405+
panelWindow.setFrame(result.panelFrame, display: true, animate: animated)
406+
388407
suggestionPanelViewModel.alignTopToAnchor = result.alignPanelTopToAnchor
389408
case .alignToTextCursor:
390409
let result = UpdateLocationStrategy.AlignToTextCursor().framesForWindows(

0 commit comments

Comments
 (0)