From afbe77531b05d6e54a62fbb9714eb372758e65f3 Mon Sep 17 00:00:00 2001 From: phlpsong Date: Tue, 9 Jan 2024 13:57:01 +0800 Subject: [PATCH] feat: optimize tab selection window transition --- Easydict.xcodeproj/project.pbxproj | 4 ++ .../NewApp/View/SettingView/SettingView.swift | 39 ++++++++++++++++--- .../View/SettingView/Tabs/AboutTab.swift | 2 + Easydict/NewApp/View/WindowAccessor.swift | 23 +++++++++++ 4 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 Easydict/NewApp/View/WindowAccessor.swift diff --git a/Easydict.xcodeproj/project.pbxproj b/Easydict.xcodeproj/project.pbxproj index 5f142b1d0..74ad242f6 100644 --- a/Easydict.xcodeproj/project.pbxproj +++ b/Easydict.xcodeproj/project.pbxproj @@ -232,6 +232,7 @@ 0A057D6F2B499A0B0025C51D /* ServiceItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A057D6E2B499A0B0025C51D /* ServiceItemView.swift */; }; 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 */; }; 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 */; }; @@ -697,6 +698,7 @@ 0A057D6E2B499A0B0025C51D /* ServiceItemView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ServiceItemView.swift; sourceTree = ""; }; 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 = ""; }; 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 = ""; }; @@ -2032,6 +2034,7 @@ children = ( 27FE980A2B3DD5D1000AD654 /* MenuItemView.swift */, 0A057D6E2B499A0B0025C51D /* ServiceItemView.swift */, + 0AC11B212B4D16A500F07198 /* WindowAccessor.swift */, 27FE98072B3DD52B000AD654 /* SettingView */, ); path = View; @@ -2593,6 +2596,7 @@ 033B7134293CE2430096E2DF /* EZWebViewTranslator.m in Sources */, 03CF88632B137F650030C199 /* Array+Convenience.swift in Sources */, 03B0231229231FA6001C7E63 /* NSObject+DarkMode.m in Sources */, + 0AC11B222B4D16A500F07198 /* WindowAccessor.swift in Sources */, 03B0233829231FA6001C7E63 /* MMOrderedDictionary.m in Sources */, 278540342B3DE04F004E9488 /* GeneralTab.swift in Sources */, 03BDA7BC2A26DA280079D04F /* XPMArgumentSignature.m in Sources */, diff --git a/Easydict/NewApp/View/SettingView/SettingView.swift b/Easydict/NewApp/View/SettingView/SettingView.swift index de1464158..fb4105d66 100644 --- a/Easydict/NewApp/View/SettingView/SettingView.swift +++ b/Easydict/NewApp/View/SettingView/SettingView.swift @@ -8,23 +8,52 @@ import SwiftUI +enum SettingTab: Int { + case general + case service + case privacy + case about +} + @available(macOS 13, *) struct SettingView: View { + @State private var selection = SettingTab.general.rawValue + @State private var window: NSWindow? + var body: some View { - TabView { + TabView(selection: $selection.didSet(execute: { _ in + resizeWindowFrame() + })) { GeneralTab() .tabItem { Label("setting_general", systemImage: "gear") } - .frame(width: 500, height: 400) + .tag(SettingTab.general.rawValue) + ServiceTab() .tabItem { Label("service", systemImage: "briefcase") } - .frame(width: 360, height: 540) + .tag(SettingTab.service.rawValue) + PrivacyTab() .tabItem { Label("privacy", systemImage: "hand.raised.square") } - .frame(width: 500, height: 400) + .tag(SettingTab.privacy.rawValue) + AboutTab() .tabItem { Label("about", systemImage: "info.bubble") } - .frame(width: 500, height: 400) + .tag(SettingTab.about.rawValue) } + .background(WindowAccessor(window: $window)) + } + + func resizeWindowFrame() { + guard let window else { return } + + let originalFrame = window.frame + let newSize = selection == SettingTab.service.rawValue + ? CGSize(width: 360, height: 520) : CGSize(width: 500, height: 400) + + let newY = originalFrame.origin.y + originalFrame.size.height - newSize.height + let newRect = NSRect(origin: CGPoint(x: originalFrame.origin.x, y: newY), size: newSize) + + window.setFrame(newRect, display: true, animate: true) } } diff --git a/Easydict/NewApp/View/SettingView/Tabs/AboutTab.swift b/Easydict/NewApp/View/SettingView/Tabs/AboutTab.swift index 92de82c24..aa6f6a4e4 100644 --- a/Easydict/NewApp/View/SettingView/Tabs/AboutTab.swift +++ b/Easydict/NewApp/View/SettingView/Tabs/AboutTab.swift @@ -34,7 +34,9 @@ struct AboutTab: View { } .padding(.horizontal, 50) .padding(.vertical, 30) + .frame(maxWidth: .infinity) } + .scrollIndicators(.hidden) .task { let version = await EZMenuItemManager.shared().fetchRepoLatestVersion(EZGithubRepoEasydict) await MainActor.run { diff --git a/Easydict/NewApp/View/WindowAccessor.swift b/Easydict/NewApp/View/WindowAccessor.swift new file mode 100644 index 000000000..b748023cb --- /dev/null +++ b/Easydict/NewApp/View/WindowAccessor.swift @@ -0,0 +1,23 @@ +// +// WindowAccessor.swift +// Easydict +// +// Created by phlpsong on 2024/1/9. +// Copyright © 2024 izual. All rights reserved. +// + +import SwiftUI + +struct WindowAccessor: NSViewRepresentable { + @Binding var window: NSWindow? + + func makeNSView(context _: Context) -> NSView { + let view = NSView() + DispatchQueue.main.async { + window = view.window + } + return view + } + + func updateNSView(_: NSView, context _: Context) {} +}