fix(panel): drag the window manually and make the whole header a drag bar
performDrag did nothing while the app was inactive (verified: mouseDown reached the drag view, performDrag returned, the frame never changed). Move the window from the screen-space mouse delta in mouseDown/mouseDragged instead. Make the header a full-width 56 pt bar flush with the panel top, with the drag view behind it and the language/gear buttons above, so the whole top area moves the panel. Verified: make verify green; synthetic drags from the title, top edge, empty header and grip moved the panel by the full delta while Finder was frontmost; body drag does not move it.
This commit is contained in:
1 parent
f8064ac430
commit
d126124f97
4 files changed
+75
-44
No files matched your search
+48
-40
@@ -12,17 +12,21 @@ struct PanelView: View {
|
||||
@State private var showSource = true
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 10) {
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
header
|
||||
if model.configuration.showSourcePreview, let source = model.source {
|
||||
sourcePreview(source)
|
||||
VStack(alignment: .leading, spacing: 10) {
|
||||
if model.configuration.showSourcePreview, let source = model.source {
|
||||
sourcePreview(source)
|
||||
}
|
||||
translationArea
|
||||
warningArea
|
||||
errorArea
|
||||
footer
|
||||
}
|
||||
translationArea
|
||||
warningArea
|
||||
errorArea
|
||||
footer
|
||||
.padding(.horizontal, 16)
|
||||
.padding(.top, 10)
|
||||
.padding(.bottom, 16)
|
||||
}
|
||||
.padding(16)
|
||||
.frame(width: 540)
|
||||
.background(.regularMaterial, in: RoundedRectangle(cornerRadius: 14, style: .continuous))
|
||||
.clipShape(RoundedRectangle(cornerRadius: 14, style: .continuous))
|
||||
@@ -30,6 +34,7 @@ struct PanelView: View {
|
||||
RoundedRectangle(cornerRadius: 14, style: .continuous)
|
||||
.strokeBorder(.separator, lineWidth: 1)
|
||||
)
|
||||
.ignoresSafeArea(.container, edges: .top)
|
||||
.onAppear { showSource = model.configuration.showSourcePreview }
|
||||
.onChange(of: model.configuration.showSourcePreview) { _, newValue in
|
||||
showSource = newValue
|
||||
@@ -40,9 +45,10 @@ struct PanelView: View {
|
||||
}
|
||||
|
||||
private var header: some View {
|
||||
HStack(spacing: 10) {
|
||||
ZStack(alignment: .leading) {
|
||||
WindowDragArea()
|
||||
ZStack {
|
||||
WindowDragArea()
|
||||
Color.primary.opacity(0.04).allowsHitTesting(false)
|
||||
HStack(spacing: 10) {
|
||||
HStack(spacing: 10) {
|
||||
Image(systemName: "line.3.horizontal")
|
||||
.font(.system(size: 12, weight: .semibold))
|
||||
@@ -61,38 +67,40 @@ struct PanelView: View {
|
||||
}
|
||||
}
|
||||
.allowsHitTesting(false)
|
||||
}
|
||||
.frame(height: 44)
|
||||
.contentShape(Rectangle())
|
||||
.help(String(localized: "Drag to move panel"))
|
||||
.accessibilityLabel(String(localized: "Move panel"))
|
||||
.accessibilityElement(children: .combine)
|
||||
.accessibilityLabel(String(localized: "Move panel"))
|
||||
|
||||
Spacer()
|
||||
Button {
|
||||
showLanguagePicker.toggle()
|
||||
} label: {
|
||||
Label(model.configuration.target.localizedDisplayName, systemImage: "globe")
|
||||
.labelStyle(.titleAndIcon)
|
||||
Spacer().allowsHitTesting(false)
|
||||
Button {
|
||||
showLanguagePicker.toggle()
|
||||
} label: {
|
||||
Label(model.configuration.target.localizedDisplayName, systemImage: "globe")
|
||||
.labelStyle(.titleAndIcon)
|
||||
}
|
||||
.accessibilityLabel(String(localized: "Target language"))
|
||||
.popover(isPresented: $showLanguagePicker, arrowEdge: .bottom) {
|
||||
LanguagePickerView(
|
||||
selection: Binding(
|
||||
get: { model.configuration.target },
|
||||
set: { language in
|
||||
Task { await model.setTarget(language) }
|
||||
}
|
||||
),
|
||||
recent: model.configuration.recentTargets
|
||||
)
|
||||
}
|
||||
Button {
|
||||
openSettings()
|
||||
} label: {
|
||||
Image(systemName: "gearshape")
|
||||
}
|
||||
.accessibilityLabel(String(localized: "Settings"))
|
||||
}
|
||||
.accessibilityLabel(String(localized: "Target language"))
|
||||
.popover(isPresented: $showLanguagePicker, arrowEdge: .bottom) {
|
||||
LanguagePickerView(
|
||||
selection: Binding(
|
||||
get: { model.configuration.target },
|
||||
set: { language in
|
||||
Task { await model.setTarget(language) }
|
||||
}
|
||||
),
|
||||
recent: model.configuration.recentTargets
|
||||
)
|
||||
}
|
||||
Button {
|
||||
openSettings()
|
||||
} label: {
|
||||
Image(systemName: "gearshape")
|
||||
}
|
||||
.accessibilityLabel(String(localized: "Settings"))
|
||||
.padding(.horizontal, 16)
|
||||
}
|
||||
.frame(height: 56)
|
||||
.frame(maxWidth: .infinity)
|
||||
.help(String(localized: "Drag to move panel"))
|
||||
}
|
||||
|
||||
private var sourceLanguageLabel: String {
|
||||
|
||||
@@ -2,7 +2,8 @@ import AppKit
|
||||
import SwiftUI
|
||||
|
||||
/// An AppKit drag surface for the non-activating panel. SwiftUI's hosting view consumes mouse
|
||||
/// events, so this view starts a native window drag on the first click.
|
||||
/// events, and `performDrag` is unreliable while the app is inactive, so the view moves the
|
||||
/// window itself from the screen-space mouse delta.
|
||||
struct WindowDragArea: NSViewRepresentable {
|
||||
func makeNSView(context: Context) -> NSView {
|
||||
DragView()
|
||||
@@ -12,8 +13,11 @@ struct WindowDragArea: NSViewRepresentable {
|
||||
}
|
||||
|
||||
private final class DragView: NSView {
|
||||
private var startMouse = NSPoint.zero
|
||||
private var startOrigin = NSPoint.zero
|
||||
|
||||
override var mouseDownCanMoveWindow: Bool {
|
||||
true
|
||||
false
|
||||
}
|
||||
|
||||
override func acceptsFirstMouse(for event: NSEvent?) -> Bool {
|
||||
@@ -21,7 +25,25 @@ private final class DragView: NSView {
|
||||
}
|
||||
|
||||
override func mouseDown(with event: NSEvent) {
|
||||
window?.performDrag(with: event)
|
||||
guard let window else { return }
|
||||
startMouse = NSEvent.mouseLocation
|
||||
startOrigin = window.frame.origin
|
||||
NSCursor.closedHand.push()
|
||||
}
|
||||
|
||||
override func mouseDragged(with event: NSEvent) {
|
||||
guard let window else { return }
|
||||
let mouse = NSEvent.mouseLocation
|
||||
window.setFrameOrigin(
|
||||
NSPoint(
|
||||
x: startOrigin.x + mouse.x - startMouse.x,
|
||||
y: startOrigin.y + mouse.y - startMouse.y
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
override func mouseUp(with event: NSEvent) {
|
||||
NSCursor.pop()
|
||||
}
|
||||
|
||||
override func resetCursorRects() {
|
||||
|
||||
Reference in new issue
Block a user