From b05f2baf7996c15cd14cca213aefbe2490d6730b Mon Sep 17 00:00:00 2001 From: phlpsong Date: Wed, 10 Jan 2024 14:15:53 +0800 Subject: [PATCH] fix: onTap conflict with onMove in service tab --- Easydict.xcodeproj/project.pbxproj | 4 ++ .../NewApp/View/SettingView/SettingView.swift | 1 + .../View/SettingView/Tabs/ServiceTab.swift | 26 ++++++----- Easydict/NewApp/View/TapHandlerView.swift | 43 +++++++++++++++++++ 4 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 Easydict/NewApp/View/TapHandlerView.swift diff --git a/Easydict.xcodeproj/project.pbxproj b/Easydict.xcodeproj/project.pbxproj index 74ad242f6..0d7029cbb 100644 --- a/Easydict.xcodeproj/project.pbxproj +++ b/Easydict.xcodeproj/project.pbxproj @@ -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 */; }; @@ -699,6 +700,7 @@ 0A2BA95F2B49A989002872A4 /* Binding+DidSet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Binding+DidSet.swift"; sourceTree = ""; }; 0A2BA9632B4A3CCD002872A4 /* Notification+Name.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Notification+Name.swift"; sourceTree = ""; }; 0AC11B212B4D16A500F07198 /* WindowAccessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowAccessor.swift; sourceTree = ""; }; + 0AC11B232B4E46B300F07198 /* TapHandlerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TapHandlerView.swift; sourceTree = ""; }; 17BCAEF32B0DFF9000A7D372 /* EZNiuTransTranslateResponse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EZNiuTransTranslateResponse.h; sourceTree = ""; }; 17BCAEF42B0DFF9000A7D372 /* EZNiuTransTranslate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EZNiuTransTranslate.h; sourceTree = ""; }; 17BCAEF52B0DFF9000A7D372 /* EZNiuTransTranslateResponse.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EZNiuTransTranslateResponse.m; sourceTree = ""; }; @@ -2035,6 +2037,7 @@ 27FE980A2B3DD5D1000AD654 /* MenuItemView.swift */, 0A057D6E2B499A0B0025C51D /* ServiceItemView.swift */, 0AC11B212B4D16A500F07198 /* WindowAccessor.swift */, + 0AC11B232B4E46B300F07198 /* TapHandlerView.swift */, 27FE98072B3DD52B000AD654 /* SettingView */, ); path = View; @@ -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 */, diff --git a/Easydict/NewApp/View/SettingView/SettingView.swift b/Easydict/NewApp/View/SettingView/SettingView.swift index 4e8f605b3..8c4fec008 100644 --- a/Easydict/NewApp/View/SettingView/SettingView.swift +++ b/Easydict/NewApp/View/SettingView/SettingView.swift @@ -41,6 +41,7 @@ struct SettingView: View { .tag(SettingTab.about.rawValue) } .background(WindowAccessor(window: $window.didSet(execute: { _ in + // reset frame when first launch resizeWindowFrame() }))) } diff --git a/Easydict/NewApp/View/SettingView/Tabs/ServiceTab.swift b/Easydict/NewApp/View/SettingView/Tabs/ServiceTab.swift index 59ffe5db1..3da7ca423 100644 --- a/Easydict/NewApp/View/SettingView/Tabs/ServiceTab.swift +++ b/Easydict/NewApp/View/SettingView/Tabs/ServiceTab.swift @@ -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) { @@ -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) diff --git a/Easydict/NewApp/View/TapHandlerView.swift b/Easydict/NewApp/View/TapHandlerView.swift new file mode 100644 index 000000000..5f273e37e --- /dev/null +++ b/Easydict/NewApp/View/TapHandlerView.swift @@ -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 + } +}