From ea81cd72aac8739d927e1399fbe7ac607b756fa5 Mon Sep 17 00:00:00 2001 From: taekchef Date: Tue, 14 Jul 2026 23:39:59 +0800 Subject: [PATCH] fix: refine empty workspace composition # Conflicts: # Sources/WeiBeiSelfCheck/main.swift --- .../Views/EmptyWorkspaceLauncherView.swift | 184 ++++++++++-------- Sources/WeiBeiSelfCheck/main.swift | 38 +++- 2 files changed, 139 insertions(+), 83 deletions(-) diff --git a/Sources/WeiBei/Views/EmptyWorkspaceLauncherView.swift b/Sources/WeiBei/Views/EmptyWorkspaceLauncherView.swift index d607d3919..6a8fa58c2 100644 --- a/Sources/WeiBei/Views/EmptyWorkspaceLauncherView.swift +++ b/Sources/WeiBei/Views/EmptyWorkspaceLauncherView.swift @@ -2,6 +2,17 @@ import AppKit import SwiftUI import WeiBeiCore +private enum EmptyWorkspaceLayoutMetrics { + static let compactWidthThreshold: CGFloat = 1140 + static let compactHeightThreshold: CGFloat = 680 + static let entryCenterRatio: CGFloat = 0.402 + static let inspirationCenterRatio: CGFloat = 0.66 + static let contentMaxWidth: CGFloat = 760 + static let inspirationMaxWidth: CGFloat = 660 + static let inspirationSlotHeight: CGFloat = 210 + static let compactInspirationSlotHeight: CGFloat = 176 +} + struct EmptyWorkspaceLauncherView: View { @EnvironmentObject private var store: WorkspaceStore @Environment(\.accessibilityReduceMotion) private var reduceMotion @@ -11,10 +22,13 @@ struct EmptyWorkspaceLauncherView: View { var body: some View { TimelineView(.periodic(from: .now, by: 60)) { timeline in GeometryReader { geometry in - let compact = geometry.size.width < 760 || geometry.size.height < 620 + let compact = geometry.size.width < EmptyWorkspaceLayoutMetrics.compactWidthThreshold + || geometry.size.height < EmptyWorkspaceLayoutMetrics.compactHeightThreshold let horizontalPadding: CGFloat = compact ? 24 : 52 let entryWidth = min(116, max(76, (geometry.size.width - (horizontalPadding * 2) - 2) / 3)) - let inspirationSlotHeight: CGFloat = compact ? 176 : 210 + let inspirationSlotHeight = compact + ? EmptyWorkspaceLayoutMetrics.compactInspirationSlotHeight + : EmptyWorkspaceLayoutMetrics.inspirationSlotHeight let currentInspiration = inspiration(at: timeline.date) ZStack { @@ -23,6 +37,7 @@ struct EmptyWorkspaceLauncherView: View { workspaceContent( at: timeline.date, inspiration: currentInspiration, + availableSize: geometry.size, compact: compact, horizontalPadding: horizontalPadding, entryWidth: entryWidth, @@ -39,23 +54,49 @@ struct EmptyWorkspaceLauncherView: View { private func workspaceContent( at date: Date, inspiration: EmptyWorkspaceInspiration, + availableSize: CGSize, compact: Bool, horizontalPadding: CGFloat, entryWidth: CGFloat, inspirationSlotHeight: CGFloat ) -> some View { - if store.showDailyInspiration { - VStack(spacing: 0) { - Spacer(minLength: compact ? 18 : 38) - - entryCluster( - at: date, - spacing: compact ? 14 : 19, - entryWidth: entryWidth - ) + let contentWidth = max( + 1, + min(EmptyWorkspaceLayoutMetrics.contentMaxWidth, availableSize.width - horizontalPadding * 2) + ) + let entryHeight: CGFloat = compact ? 84 : 98 + let entryCenterRatio: CGFloat = store.showDailyInspiration ? EmptyWorkspaceLayoutMetrics.entryCenterRatio : 0.5 + let entryCenterY = clampedCenterY( + ratio: entryCenterRatio, + elementHeight: entryHeight, + availableHeight: availableSize.height, + edgeInset: compact ? 14 : 20 + ) + let minimumInspirationCenterY = max( + clampedCenterY( + ratio: EmptyWorkspaceLayoutMetrics.inspirationCenterRatio, + elementHeight: inspirationSlotHeight, + availableHeight: availableSize.height, + edgeInset: compact ? 12 : 18 + ), + entryCenterY + entryHeight / 2 + (compact ? 12 : 20) + inspirationSlotHeight / 2 + ) + let inspirationCenterY = min( + minimumInspirationCenterY, + availableSize.height - (compact ? 12 : 18) - inspirationSlotHeight / 2 + ) - Spacer(minLength: compact ? 18 : 34) + ZStack { + entryCluster( + at: date, + compact: compact, + spacing: store.showDailyInspiration ? (compact ? 18 : 26) : (compact ? 16 : 29), + entryWidth: entryWidth + ) + .frame(width: contentWidth) + .position(x: availableSize.width / 2, y: entryCenterY) + if store.showDailyInspiration { ZStack { EmptyWorkspaceInspirationView( inspiration: inspiration, @@ -65,58 +106,44 @@ struct EmptyWorkspaceLauncherView: View { .id(inspiration.id) .transition(.opacity) } - .frame(height: inspirationSlotHeight) - - Spacer(minLength: compact ? 20 : 42) - } - .frame(maxWidth: 760) - .padding(.horizontal, horizontalPadding) - .padding(.vertical, compact ? 4 : 10) - } else { - VStack(spacing: 0) { - Spacer(minLength: compact ? 20 : 40) - entryCluster( - at: date, - spacing: compact ? 16 : 29, - entryWidth: entryWidth + .frame( + width: min(EmptyWorkspaceLayoutMetrics.inspirationMaxWidth, contentWidth), + height: inspirationSlotHeight ) - .offset(y: compact ? 0 : 5) - Spacer(minLength: compact ? 20 : 40) + .position(x: availableSize.width / 2, y: inspirationCenterY) } - .frame(maxWidth: 760, maxHeight: .infinity) - .padding(.horizontal, horizontalPadding) - .padding(.vertical, compact ? 4 : 10) } + .frame(maxWidth: .infinity, maxHeight: .infinity) } - private func entryCluster(at date: Date, spacing: CGFloat, entryWidth: CGFloat) -> some View { - VStack(spacing: spacing) { - greeting(at: date) - EmptyWorkspaceEntryRow(entryWidth: entryWidth) - } + private func clampedCenterY( + ratio: CGFloat, + elementHeight: CGFloat, + availableHeight: CGFloat, + edgeInset: CGFloat + ) -> CGFloat { + let halfHeight = elementHeight / 2 + return min( + max(availableHeight * ratio, edgeInset + halfHeight), + availableHeight - edgeInset - halfHeight + ) } - private func greeting(at date: Date) -> some View { - HStack(spacing: 12) { - greetingRule - - Text(EmptyWorkspaceDayPeriod.current(at: date).greeting(language: store.interfaceLanguage)) - .font(WeiBeiTypography.brandFont(language: store.interfaceLanguage, size: 14.5, weight: .regular)) - .tracking(store.interfaceLanguage == .chinese ? 0.8 : 0.35) - .foregroundStyle(WeiBeiTheme.secondaryInk.opacity(0.92)) - .multilineTextAlignment(.center) - .fixedSize(horizontal: false, vertical: true) - .accessibilityIdentifier("empty-workspace-greeting") - - greetingRule + private func entryCluster(at date: Date, compact: Bool, spacing: CGFloat, entryWidth: CGFloat) -> some View { + VStack(spacing: spacing) { + greeting(at: date, compact: compact) + EmptyWorkspaceEntryRow(entryWidth: entryWidth) } } - private var greetingRule: some View { - Rectangle() - .fill(WeiBeiTheme.hairline.opacity(0.76)) - .frame(width: 18, height: 1) - .accessibilityHidden(true) + private func greeting(at date: Date, compact: Bool) -> some View { + Text(EmptyWorkspaceDayPeriod.current(at: date).greeting(language: store.interfaceLanguage)) + .font(WeiBeiTypography.brandFont(language: store.interfaceLanguage, size: compact ? 14.5 : 16, weight: .regular)) + .tracking(store.interfaceLanguage == .chinese ? 0.8 : 0.35) + .foregroundStyle(WeiBeiTheme.secondaryInk.opacity(0.92)) + .multilineTextAlignment(.center) + .fixedSize(horizontal: false, vertical: true) + .accessibilityIdentifier("empty-workspace-greeting") } private func inspiration(at date: Date) -> EmptyWorkspaceInspiration { @@ -186,18 +213,11 @@ private struct EmptyWorkspacePaperField: View { endPoint: .bottomTrailing ) - VStack(spacing: 0) { - Rectangle() - .fill(WeiBeiTheme.hairline.opacity(0.30)) - .frame(width: 1, height: compact ? 24 : 48) - - Spacer() - - Rectangle() - .fill(WeiBeiTheme.hairline.opacity(0.22)) - .frame(width: 1, height: compact ? 28 : 58) - } - .padding(.vertical, compact ? 14 : 24) + Rectangle() + .fill(WeiBeiTheme.hairline.opacity(0.30)) + .frame(width: 1, height: compact ? 24 : 48) + .frame(maxHeight: .infinity, alignment: .top) + .padding(.top, compact ? 14 : 24) } .allowsHitTesting(false) .accessibilityHidden(true) @@ -268,7 +288,7 @@ private struct EmptyWorkspaceEntryButton: View { Button(action: action) { VStack(spacing: 2) { Text(title) - .font(WeiBeiTypography.englishBrandFont(size: 20, weight: .semibold)) + .font(WeiBeiTypography.englishBrandFont(size: 22, weight: .semibold)) .tracking(active ? 3.5 : 2.2) .foregroundStyle(active ? WeiBeiTheme.ink : WeiBeiTheme.secondaryInk.opacity(0.88)) .offset(y: active && !reduceMotion ? -2.5 : 0) @@ -365,14 +385,14 @@ private struct EmptyWorkspaceInspirationView: View { @FocusState private var focused: Bool var body: some View { - VStack(spacing: compact ? 7 : 9) { + VStack(spacing: compact ? 7 : 8) { Button(action: onAdvance) { - VStack(spacing: compact ? 8 : 11) { + VStack(spacing: compact ? 8 : 9) { inspirationContent Text(inspiration.credit) - .font(.system(size: compact ? 10.5 : 11.5, weight: .medium, design: .serif)) - .foregroundStyle(WeiBeiTheme.secondaryInk) + .font(.system(size: compact ? 11 : 12, weight: .medium, design: .serif)) + .foregroundStyle(WeiBeiTheme.secondaryInk.opacity(0.86)) .multilineTextAlignment(.center) .lineLimit(2) @@ -404,10 +424,10 @@ private struct EmptyWorkspaceInspirationView: View { Text("→") .offset(x: hovering && !reduceMotion ? 2 : 0) } - .font(.system(size: compact ? 8.5 : 9, weight: .medium, design: .serif)) + .font(.system(size: compact ? 9.5 : 10.5, weight: .medium, design: .serif)) .tracking(store.interfaceLanguage == .chinese ? 0.7 : 1.2) - .foregroundStyle(WeiBeiTheme.tertiaryInk) - .opacity(hovering || focused ? 0.94 : 0.56) + .foregroundStyle(WeiBeiTheme.secondaryInk.opacity(0.82)) + .opacity(hovering || focused ? 1 : 0.78) .accessibilityHidden(true) } @@ -421,22 +441,22 @@ private struct EmptyWorkspaceInspirationView: View { .resizable() .scaledToFit() .foregroundStyle(WeiBeiTheme.ink.opacity(0.88)) - .frame(maxWidth: compact ? 390 : 500, maxHeight: compact ? 54 : 72) + .frame(maxWidth: compact ? 390 : 470, maxHeight: compact ? 54 : 64) .padding(.vertical, compact ? 1 : 2) .accessibilityLabel(Text(inspiration.text)) } else { - inspirationText(size: compact ? 24 : 30) + inspirationText(size: compact ? 21 : 24) } case .quotation: - inspirationText(size: compact ? 21 : 26) + inspirationText(size: compact ? 20 : 23.5) case .formula: - formulaContent(size: compact ? 24 : 30) + formulaContent(size: compact ? 21 : 24) } } private func formulaContent(size: CGFloat) -> some View { formulaText(size: size) - .foregroundStyle(WeiBeiTheme.ink.opacity(0.90)) + .foregroundStyle(WeiBeiTheme.ink.opacity(0.78)) .multilineTextAlignment(.center) .lineLimit(1) .minimumScaleFactor(0.78) @@ -471,7 +491,7 @@ private struct EmptyWorkspaceInspirationView: View { private func inspirationText(size: CGFloat) -> some View { Text(inspiration.text) .font(.system(size: size, weight: .regular, design: .serif)) - .foregroundStyle(WeiBeiTheme.ink.opacity(0.90)) + .foregroundStyle(WeiBeiTheme.ink.opacity(0.78)) .multilineTextAlignment(.center) .lineLimit(compact ? 3 : 2) .minimumScaleFactor(compact ? 0.72 : 0.78) @@ -492,8 +512,8 @@ private struct EmptyWorkspaceInspirationView: View { rightsLink } } - .font(.system(size: compact ? 9 : 9.5, weight: .regular)) - .foregroundStyle(WeiBeiTheme.tertiaryInk) + .font(.system(size: compact ? 9.5 : 10.5, weight: .regular)) + .foregroundStyle(WeiBeiTheme.secondaryInk.opacity(0.78)) .multilineTextAlignment(.center) .lineLimit(2) } diff --git a/Sources/WeiBeiSelfCheck/main.swift b/Sources/WeiBeiSelfCheck/main.swift index 52ff14a0b..c4c586c41 100644 --- a/Sources/WeiBeiSelfCheck/main.swift +++ b/Sources/WeiBeiSelfCheck/main.swift @@ -142,6 +142,14 @@ CTFontManagerRegisterFontsForURL(displayFontURL as CFURL, .process, nil) CTFontManagerRegisterFontsForURL(monoFontURL as CFURL, .process, nil) expect(NSFont(name: "WeiBeiStele-Regular", size: 18) != nil && NSFont(name: "WeiBeiSteleMono-Regular", size: 13) != nil, "bundled WeiBei English fonts register under their PostScript names") +let emptyWorkspaceEntryFont = CTFontCreateWithName("WeiBeiStele-Regular" as CFString, 22, nil) +let emptyWorkspaceEntryCharacters = Array("DOCCHATNOTES".utf16) +var emptyWorkspaceEntryGlyphs = Array(repeating: CGGlyph(), count: emptyWorkspaceEntryCharacters.count) +expect(emptyWorkspaceEntryCharacters.withUnsafeBufferPointer { characters in + emptyWorkspaceEntryGlyphs.withUnsafeMutableBufferPointer { glyphs in + CTFontGetGlyphsForCharacters(emptyWorkspaceEntryFont, characters.baseAddress!, glyphs.baseAddress!, characters.count) + } +} && emptyWorkspaceEntryGlyphs.allSatisfy { $0 != 0 }, "WeiBeiStele keeps every glyph required by the DOC, CHAT, and NOTES work entries") func inspectCalligraphyAsset(_ url: URL) throws -> (width: Int, height: Int, visible: Int, transparent: Int, opaqueWhite: Int, outerInk: Int) { let data = try Data(contentsOf: url) @@ -1333,9 +1341,37 @@ expect(emptyWorkspaceSource.contains("WeiBeiTypography.englishBrandFont") && !emptyWorkspaceSource.contains("stroke(WeiBeiTheme.cinnabar"), "empty workspace entry uses WeiBei English lettering, hairlines, and whitespace without cards, pills, or red outlines") expect(emptyWorkspaceSource.contains("TimelineView(.periodic(from: .now, by: 60))") && emptyWorkspaceSource.contains("EmptyWorkspaceDayPeriod.current(at:") - && emptyWorkspaceSource.contains("geometry.size.width < 760 || geometry.size.height < 620") + && emptyWorkspaceSource.contains("static let compactWidthThreshold: CGFloat = 1140") + && emptyWorkspaceSource.contains("static let compactHeightThreshold: CGFloat = 680") + && emptyWorkspaceSource.contains("geometry.size.width < EmptyWorkspaceLayoutMetrics.compactWidthThreshold") && emptyWorkspaceSource.contains("min(116, max(76") && emptyWorkspaceSource.contains("minimumScaleFactor(0.78)"), "empty workspace greeting updates with time and its entry and inspiration typography adapt to compact windows") +expect(emptyWorkspaceSource.contains("selectedInspirationID") + && emptyWorkspaceSource.contains("randomItem(excludingID: currentID") + && emptyWorkspaceSource.contains("static let entryCenterRatio: CGFloat = 0.402") + && emptyWorkspaceSource.contains("static let inspirationCenterRatio: CGFloat = 0.66") + && emptyWorkspaceSource.contains("let entryCenterY = clampedCenterY(") + && emptyWorkspaceSource.contains("let minimumInspirationCenterY = max(") + && emptyWorkspaceSource.contains("let inspirationCenterY = min(") + && emptyWorkspaceSource.contains(".position(x: availableSize.width / 2, y: entryCenterY)") + && emptyWorkspaceSource.contains(".position(x: availableSize.width / 2, y: inspirationCenterY)") + && emptyWorkspaceSource.contains("let inspirationSlotHeight = compact") + && emptyWorkspaceSource.contains("height: inspirationSlotHeight") + && emptyWorkspaceSource.contains("private func workspaceContent(") + && emptyWorkspaceSource.contains("if store.showDailyInspiration") + && emptyWorkspaceSource.contains(".frame(maxWidth: .infinity, maxHeight: .infinity)") + && emptyWorkspaceSource.contains("private func entryCluster(") + && emptyWorkspaceSource.contains("let entryCenterRatio: CGFloat = store.showDailyInspiration ? EmptyWorkspaceLayoutMetrics.entryCenterRatio : 0.5") + && emptyWorkspaceSource.contains(".id(inspiration.id)") + && emptyWorkspaceSource.contains(".transition(.opacity)") + && emptyWorkspaceSource.contains("随机换一则") + && emptyWorkspaceSource.contains("WeiBeiTypography.englishBrandFont(size: 22, weight: .semibold)") + && emptyWorkspaceSource.contains("foregroundStyle(WeiBeiTheme.ink.opacity(0.78))") + && emptyWorkspaceSource.contains("foregroundStyle(WeiBeiTheme.secondaryInk.opacity(0.86))") + && !emptyWorkspaceSource.contains("greetingRule") + && !emptyWorkspaceSource.contains("inspirationCounter") + && !emptyWorkspaceSource.contains("%02d / %02d") + && !emptyWorkspaceSource.contains("inspirationOffset"), "empty workspace inspiration switches randomly without repeating the current item or showing catalog counters") expect(emptyWorkspaceSource.contains("if store.showDailyInspiration") && emptyWorkspaceSource.contains("EmptyWorkspaceInspirationCatalog.item") && emptyWorkspaceSource.contains("case \"empty-workspace-calligraphy-light\":")