Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #96: Swiping on Hint view should dismiss it. #97

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion macos/Onit/UI/OS/OnitPromptView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ import SwiftUI

struct OnitPromptView: View {
@Environment(\.model) var model
@State private var offset: CGSize = .zero
@State private var isDragging = false

var shortcut: KeyboardShortcut? {
KeyboardShortcuts.getShortcut(for: .launch)?.native
}

var body: some View {
if model.panel == nil {
// Reset offset when view appears
let _ = DispatchQueue.main.async {
offset = .zero
isDragging = false
}
HStack(spacing: 3) {
Image(.smirk)
.resizable()
Expand All @@ -33,8 +40,39 @@ struct OnitPromptView: View {
.fill(.thickMaterial)
}
.padding(.vertical, 5)
.offset(x: offset.width, y: offset.height)
.gesture(
DragGesture()
.onChanged { value in
isDragging = true
offset = value.translation
}
.onEnded { value in
let distance = sqrt(pow(value.translation.width, 2) + pow(value.translation.height, 2))
if distance > 50 {
let screenSize = NSScreen.main?.frame.size ?? .zero
let angle = atan2(value.translation.height, value.translation.width)
let finalX = cos(angle) * screenSize.width * 2
let finalY = sin(angle) * screenSize.height * 2

withAnimation(.easeOut) {
offset = CGSize(width: finalX, height: finalY)
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
model.dismissPanel()
}
} else {
withAnimation(.easeOut) {
offset = .zero
isDragging = false
}
}
}
)
.onTapGesture {
model.launchPanel()
if !isDragging {
model.launchPanel()
}
}
}
}
Expand Down
36 changes: 35 additions & 1 deletion macos/Onit/UI/OS/StaticPromptView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ import SwiftUI

struct StaticPromptView: View {
@Environment(\.model) var model
@State private var offset: CGFloat = 0
@State private var isDragging = false

var shortcut: KeyboardShortcut? {
KeyboardShortcuts.getShortcut(for: .launch)?.native
}

var body: some View {
if model.panel == nil {
// Reset offset when view appears
let _ = DispatchQueue.main.async {
offset = 0
isDragging = false
}
VStack(spacing: 3) {
Image(.smirk)
.resizable()
Expand Down Expand Up @@ -46,8 +53,35 @@ struct StaticPromptView: View {
topTrailingRadius: 0
)
)
.offset(x: offset)
.gesture(
DragGesture()
.onChanged { value in
if value.translation.width > 0 {
isDragging = true
offset = value.translation.width
}
}
.onEnded { value in
if value.translation.width > 50 {
withAnimation(.easeOut) {
offset = NSScreen.main?.frame.width ?? 1000
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
model.dismissPanel()
}
} else {
withAnimation(.easeOut) {
offset = 0
isDragging = false
}
}
}
)
.onTapGesture {
model.launchPanel()
if !isDragging {
model.launchPanel()
}
}
}
}
Expand Down