Skip to content

Commit

Permalink
変換候補ビューの表示回りのバグ修正
Browse files Browse the repository at this point in the history
インライン注釈
- 背景が透過されていたのを修正
- 表示位置が左にずれてしまうのを修正
リスト表示
- 注釈の高さをなるべく内容に合わせて縮める
  • Loading branch information
mtgto committed Jan 31, 2024
1 parent 0c0218c commit 5d1c5a8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
13 changes: 8 additions & 5 deletions macSKK/View/CandidatesPanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,23 @@ final class CandidatesPanel: NSPanel {
print("preferredContentSize = \(viewController.preferredContentSize)")
print("sizeThatFits = \(viewController.sizeThatFits(in: CGSize(width: 10000, height: 10000)))")
#endif
let width = viewModel.showAnnotationPopover ? viewModel.minWidth + CandidatesView.annotationPopupWidth : viewModel.minWidth
var origin = cursorPosition.origin
let width: CGFloat
let height: CGFloat
if case let .panel(words, _, _) = viewModel.candidates {
width = viewModel.showAnnotationPopover ? viewModel.minWidth + CandidatesView.annotationPopupWidth : viewModel.minWidth
height = CGFloat(words.count) * CandidatesView.lineHeight + CandidatesView.footerHeight
if viewModel.displayPopoverInLeft {
origin.x = origin.x - CandidatesView.annotationPopupWidth - CandidatesView.annotationMargin
}
} else {
// FIXME: 短い文のときにはそれに合わせて高さを縮める
width = viewModel.minWidth
height = 200
}
setContentSize(NSSize(width: width, height: height))
var origin = cursorPosition.origin
if viewModel.displayPopoverInLeft {
origin.x = origin.x - CandidatesView.annotationPopupWidth - CandidatesView.annotationMargin
}
if let mainScreen = NSScreen.main {
// スクリーン右にはみ出す場合はスクリーン右端に接するように表示する
if origin.x + width > mainScreen.visibleFrame.size.width {
origin.x = mainScreen.frame.size.width - width
}
Expand Down
21 changes: 18 additions & 3 deletions macSKK/View/CandidatesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct CandidatesView: View {
AnnotationView(annotations: $candidates.selectedAnnotations, systemAnnotation: $candidates.selectedSystemAnnotation)
.padding(EdgeInsets(top: 4, leading: 4, bottom: 4, trailing: 4))
.frame(width: 300, height: 200)
.background()
case let .panel(words, currentPage, totalPageCount):
HStack(alignment: .top, spacing: Self.annotationMargin) {
if candidates.displayPopoverInLeft {
Expand All @@ -30,9 +31,10 @@ struct CandidatesView: View {
annotations: $candidates.selectedAnnotations,
systemAnnotation: $candidates.selectedSystemAnnotation
)
.padding(EdgeInsets(top: 16, leading: 12, bottom: 16, trailing: 16))
.padding(EdgeInsets(top: 16, leading: 12, bottom: 16, trailing: 8))
.frame(width: Self.annotationPopupWidth, alignment: .topLeading)
.fixedSize(horizontal: false, vertical: true)
.frame(maxHeight: 200)
.fixedSize(horizontal: false, vertical: /*@START_MENU_TOKEN@*/true/*@END_MENU_TOKEN@*/)
.background(.regularMaterial, in: RoundedRectangle(cornerRadius: 10))
.opacity(0.9)
} else {
Expand Down Expand Up @@ -77,7 +79,8 @@ struct CandidatesView: View {
)
.padding(EdgeInsets(top: 16, leading: 28, bottom: 16, trailing: 4))
.frame(width: Self.annotationPopupWidth, alignment: .topLeading)
.fixedSize(horizontal: false, vertical: true)
.frame(maxHeight: 200)
.fixedSize(horizontal: false, vertical: /*@START_MENU_TOKEN@*/true/*@END_MENU_TOKEN@*/)
.background(.regularMaterial, in: RoundedRectangle(cornerRadius: 10))
.opacity(0.9)
}
Expand All @@ -98,6 +101,15 @@ struct CandidatesView_Previews: PreviewProvider {
let viewModel = CandidatesViewModel(candidates: words, currentPage: 0, totalPageCount: 3, showAnnotationPopover: true)
viewModel.selected = words.first
viewModel.systemAnnotations = [words.first!.word: String(repeating: "これはシステム辞書の注釈です。", count: 20)]
viewModel.maxWidth = 1000
return viewModel
}

private static func pageViewModelLeftPopover() -> CandidatesViewModel {
let viewModel = CandidatesViewModel(candidates: words, currentPage: 0, totalPageCount: 3, showAnnotationPopover: true)
viewModel.selected = words.first
viewModel.systemAnnotations = [words.first!.word: String(repeating: "これはシステム辞書の注釈です。", count: 20)]
viewModel.maxWidth = 1
return viewModel
}

Expand All @@ -120,6 +132,9 @@ struct CandidatesView_Previews: PreviewProvider {
CandidatesView(candidates: pageViewModel())
.background(Color.cyan)
.previewDisplayName("パネル表示")
CandidatesView(candidates: pageViewModelLeftPopover())
.background(Color.cyan)
.previewDisplayName("パネル表示 (注釈左)")
CandidatesView(candidates: pageWithoutPopoverViewModel())
.previewDisplayName("パネル表示 (注釈なし)")
CandidatesView(candidates: inlineViewModel())
Expand Down
6 changes: 5 additions & 1 deletion macSKK/View/CandidatesViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class CandidatesViewModel: ObservableObject {
@Published var showAnnotationPopover: Bool
/// 表示座標から右方向に取れる最大の幅。負数のときは不明なとき
@Published var maxWidth: CGFloat = -1
/// 最長のテキストを表示するために必要なビューの横幅
/// 最長のテキストを表示するために必要なビューの横幅。パネル表示のときは注釈部分は除いたリスト部分の幅。
@Published var minWidth: CGFloat = 0
/// パネル表示時の注釈を左側に表示するかどうか
@Published var displayPopoverInLeft: Bool = false
Expand Down Expand Up @@ -85,6 +85,10 @@ final class CandidatesViewModel: ObservableObject {
.assign(to: &$minWidth)

$maxWidth.combineLatest($minWidth, $showAnnotationPopover)
.filter { maxWidth, _, _ in
// maxWidthが0未満のときはまだスクリーンサイズがわかっていない
maxWidth >= 0
}
.map { maxWidth, minWidth, showAnnotationPopover in
showAnnotationPopover &&
minWidth + CandidatesView.annotationPopupWidth + CandidatesView.annotationMargin >= maxWidth
Expand Down

0 comments on commit 5d1c5a8

Please sign in to comment.