Skip to content

Commit

Permalink
feat: optimize tab selection window transition
Browse files Browse the repository at this point in the history
  • Loading branch information
phlpsong committed Jan 9, 2024
1 parent e72ef0d commit afbe775
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Easydict.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -697,6 +698,7 @@
0A057D6E2B499A0B0025C51D /* ServiceItemView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ServiceItemView.swift; sourceTree = "<group>"; };
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>"; };
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 @@ -2032,6 +2034,7 @@
children = (
27FE980A2B3DD5D1000AD654 /* MenuItemView.swift */,
0A057D6E2B499A0B0025C51D /* ServiceItemView.swift */,
0AC11B212B4D16A500F07198 /* WindowAccessor.swift */,
27FE98072B3DD52B000AD654 /* SettingView */,
);
path = View;
Expand Down Expand Up @@ -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 */,
Expand Down
39 changes: 34 additions & 5 deletions Easydict/NewApp/View/SettingView/SettingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
2 changes: 2 additions & 0 deletions Easydict/NewApp/View/SettingView/Tabs/AboutTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 23 additions & 0 deletions Easydict/NewApp/View/WindowAccessor.swift
Original file line number Diff line number Diff line change
@@ -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) {}
}

0 comments on commit afbe775

Please sign in to comment.