Skip to content

Commit

Permalink
fix: onTap conflict with onMove in service tab
Browse files Browse the repository at this point in the history
  • Loading branch information
phlpsong committed Jan 10, 2024
1 parent 50770c8 commit b05f2ba
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
4 changes: 4 additions & 0 deletions Easydict.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@
0A2BA9602B49A989002872A4 /* Binding+DidSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A2BA95F2B49A989002872A4 /* Binding+DidSet.swift */; };
0A2BA9642B4A3CCD002872A4 /* Notification+Name.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A2BA9632B4A3CCD002872A4 /* Notification+Name.swift */; };
0AC11B222B4D16A500F07198 /* WindowAccessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AC11B212B4D16A500F07198 /* WindowAccessor.swift */; };
0AC11B242B4E46B300F07198 /* TapHandlerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AC11B232B4E46B300F07198 /* TapHandlerView.swift */; };
17BCAEF72B0DFF9000A7D372 /* EZNiuTransTranslateResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = 17BCAEF52B0DFF9000A7D372 /* EZNiuTransTranslateResponse.m */; };
17BCAEF82B0DFF9000A7D372 /* EZNiuTransTranslate.m in Sources */ = {isa = PBXBuildFile; fileRef = 17BCAEF62B0DFF9000A7D372 /* EZNiuTransTranslate.m */; };
2721E4D02AFE920700A059AC /* Alamofire in Frameworks */ = {isa = PBXBuildFile; productRef = 2721E4CF2AFE920700A059AC /* Alamofire */; };
Expand Down Expand Up @@ -699,6 +700,7 @@
0A2BA95F2B49A989002872A4 /* Binding+DidSet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Binding+DidSet.swift"; sourceTree = "<group>"; };
0A2BA9632B4A3CCD002872A4 /* Notification+Name.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Notification+Name.swift"; sourceTree = "<group>"; };
0AC11B212B4D16A500F07198 /* WindowAccessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowAccessor.swift; sourceTree = "<group>"; };
0AC11B232B4E46B300F07198 /* TapHandlerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TapHandlerView.swift; sourceTree = "<group>"; };
17BCAEF32B0DFF9000A7D372 /* EZNiuTransTranslateResponse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EZNiuTransTranslateResponse.h; sourceTree = "<group>"; };
17BCAEF42B0DFF9000A7D372 /* EZNiuTransTranslate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EZNiuTransTranslate.h; sourceTree = "<group>"; };
17BCAEF52B0DFF9000A7D372 /* EZNiuTransTranslateResponse.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EZNiuTransTranslateResponse.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2035,6 +2037,7 @@
27FE980A2B3DD5D1000AD654 /* MenuItemView.swift */,
0A057D6E2B499A0B0025C51D /* ServiceItemView.swift */,
0AC11B212B4D16A500F07198 /* WindowAccessor.swift */,
0AC11B232B4E46B300F07198 /* TapHandlerView.swift */,
27FE98072B3DD52B000AD654 /* SettingView */,
);
path = View;
Expand Down Expand Up @@ -2585,6 +2588,7 @@
27FE95272B3DC55F000AD654 /* EasydictApp.swift in Sources */,
03882F9129D95044005B5A52 /* CTCommon.m in Sources */,
276742082B3DC230002A2C75 /* PrivacyTab.swift in Sources */,
0AC11B242B4E46B300F07198 /* TapHandlerView.swift in Sources */,
03882F8F29D95044005B5A52 /* CTScreen.m in Sources */,
27FE980B2B3DD5D1000AD654 /* MenuItemView.swift in Sources */,
03DC7C6A2A3CA852000BF7C9 /* EZAppCell.m in Sources */,
Expand Down
1 change: 1 addition & 0 deletions Easydict/NewApp/View/SettingView/SettingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ struct SettingView: View {
.tag(SettingTab.about.rawValue)
}
.background(WindowAccessor(window: $window.didSet(execute: { _ in
// reset frame when first launch
resizeWindowFrame()
})))
}
Expand Down
26 changes: 16 additions & 10 deletions Easydict/NewApp/View/SettingView/Tabs/ServiceTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ struct ServiceTab: View {
@State private var serviceTypes: [ServiceType] = []
@State private var services: [QueryService] = []
@State private var selectedIndex: Int?
// workaround for tap gesture conflict with onMove
@State private var isNeedTapHandler = true

var segmentCtrl: some View {
Picker("", selection: $windowTypeValue) {
Expand All @@ -35,25 +37,29 @@ struct ServiceTab: View {
}

var serviceList: some View {
List(selection: $selectedIndex) {
List {
ForEach(Array(zip(serviceTypes.indices, serviceTypes)), id: \.0) { index, _ in
ServiceItemView(
service: $services[index]
) { isEnable in
serviceToggled(index: index, isEnable: isEnable)
selectedIndex = nil
isNeedTapHandler = false
}
.frame(height: 30)
.tag(index)
.contentShape(Rectangle())
// .onTapGesture {
// if selectedIndex == nil || selectedIndex != index {
// selectedIndex = index
// } else {
// selectedIndex = nil
// }
// }
// .listRowBackground(selectedIndex == index ? Color("service_cell_highlight") : Color.clear)
.listRowBackground(selectedIndex == index ? Color("service_cell_highlight") : Color.clear)
.overlay(TapHandler(tapAction: {
if !isNeedTapHandler {
isNeedTapHandler.toggle()
return
}
if selectedIndex == nil || selectedIndex != index {
selectedIndex = index
} else {
selectedIndex = nil
}
}))
}
.onMove(perform: { indices, newOffset in
onServiceItemMove(fromOffsets: indices, toOffset: newOffset)
Expand Down
43 changes: 43 additions & 0 deletions Easydict/NewApp/View/TapHandlerView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// TapHandlerView.swift
// Easydict
//
// Created by phlpsong on 2024/1/10.
// Copyright © 2024 izual. All rights reserved.
//

import SwiftUI

// Ref: https://stackoverflow.com/a/64194868/8378840
// Fix conflicts between onTap and onMove modifier
class TapHandlerView: NSView {
var tapAction: () -> Void

init(_ block: @escaping () -> Void) {
tapAction = block
super.init(frame: .zero)
}

@available(*, unavailable)
required init?(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func mouseDown(with event: NSEvent) {
print("event: \(event)")
super.mouseDown(with: event)
tapAction()
}
}

struct TapHandler: NSViewRepresentable {
let tapAction: () -> Void

func makeNSView(context _: Context) -> TapHandlerView {
TapHandlerView(tapAction)
}

func updateNSView(_ nsView: TapHandlerView, context _: Context) {
nsView.tapAction = tapAction
}
}

0 comments on commit b05f2ba

Please sign in to comment.