Skip to content

Commit

Permalink
Change SwiftUI ScrollView to NSScrollView and UIScrollView (#5)
Browse files Browse the repository at this point in the history
* Apply NSScrollView, NSClipView

* Apply UIScrollView

* Add supplemental config on NSHwpView

* Remove Trailing Whitespace
  • Loading branch information
sboh1214 authored Aug 18, 2021
1 parent c3c0a4f commit 87b8181
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 214 deletions.
44 changes: 30 additions & 14 deletions Like Hangul.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
3625A06925B6D35300C5C3FE /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3625A03B25B6D35300C5C3FE /* Assets.xcassets */; };
3636F15C25B8916300ECE0D8 /* SummaryView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3636F15B25B8916300ECE0D8 /* SummaryView.swift */; };
3636F15D25B8916300ECE0D8 /* SummaryView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3636F15B25B8916300ECE0D8 /* SummaryView.swift */; };
3636F16425B898B400ECE0D8 /* UndoRedo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3636F16325B898B400ECE0D8 /* UndoRedo.swift */; };
3636F16525B898B400ECE0D8 /* UndoRedo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3636F16325B898B400ECE0D8 /* UndoRedo.swift */; };
366394D725CC02300035EA1A /* Dictionary+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 366394D625CC02300035EA1A /* Dictionary+Extension.swift */; };
366394D825CC02300035EA1A /* Dictionary+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 366394D625CC02300035EA1A /* Dictionary+Extension.swift */; };
3683739D25B7BE2200401C48 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 3683739F25B7BE2200401C48 /* InfoPlist.strings */; };
368373BD25B7C00100401C48 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 3683739F25B7BE2200401C48 /* InfoPlist.strings */; };
369D184225B7B78B001C487E /* GeneralInfoView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 369D184125B7B78B001C487E /* GeneralInfoView.swift */; };
Expand Down Expand Up @@ -53,6 +53,8 @@
36D48DF125B6D9FB000434B4 /* GeneralView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36D48DEF25B6D9FB000434B4 /* GeneralView.swift */; };
36D48DF725B6DA30000434B4 /* AdvancedView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36D48DF625B6DA30000434B4 /* AdvancedView.swift */; };
36D48DF825B6DA30000434B4 /* AdvancedView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36D48DF625B6DA30000434B4 /* AdvancedView.swift */; };
36E024E125CED0C600047375 /* TextViewManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36E024E025CED0C600047375 /* TextViewManager.swift */; };
36E024E225CED0C600047375 /* TextViewManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36E024E025CED0C600047375 /* TextViewManager.swift */; };
36F71B8E25C9DA1400CC7754 /* HANBatang.ttf in Resources */ = {isa = PBXBuildFile; fileRef = 36F71B8D25C9DA1400CC7754 /* HANBatang.ttf */; };
36F71B8F25C9DA1400CC7754 /* HANBatang.ttf in Resources */ = {isa = PBXBuildFile; fileRef = 36F71B8D25C9DA1400CC7754 /* HANBatang.ttf */; };
36F71B9525C9DA1F00CC7754 /* HANBatangB.ttf in Resources */ = {isa = PBXBuildFile; fileRef = 36F71B9425C9DA1F00CC7754 /* HANBatangB.ttf */; };
Expand All @@ -61,8 +63,6 @@
36F71B9D25C9DA2800CC7754 /* HANDotum.ttf in Resources */ = {isa = PBXBuildFile; fileRef = 36F71B9B25C9DA2800CC7754 /* HANDotum.ttf */; };
36F71BA325C9DA3600CC7754 /* HANDotumB.ttf in Resources */ = {isa = PBXBuildFile; fileRef = 36F71BA225C9DA3600CC7754 /* HANDotumB.ttf */; };
36F71BA425C9DA3600CC7754 /* HANDotumB.ttf in Resources */ = {isa = PBXBuildFile; fileRef = 36F71BA225C9DA3600CC7754 /* HANDotumB.ttf */; };
36F756F625C9C34800CCD636 /* HwpColor+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36F756F525C9C34800CCD636 /* HwpColor+Extension.swift */; };
36F756F725C9C34800CCD636 /* HwpColor+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36F756F525C9C34800CCD636 /* HwpColor+Extension.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -114,7 +114,7 @@
3625A05F25B6D35300C5C3FE /* LaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LaunchTests.swift; sourceTree = "<group>"; };
3625A06125B6D35300C5C3FE /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3636F15B25B8916300ECE0D8 /* SummaryView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SummaryView.swift; sourceTree = "<group>"; };
3636F16325B898B400ECE0D8 /* UndoRedo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UndoRedo.swift; sourceTree = "<group>"; };
366394D625CC02300035EA1A /* Dictionary+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Dictionary+Extension.swift"; sourceTree = "<group>"; };
3683739E25B7BE2200401C48 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
368373A025B7BE2500401C48 /* ko */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ko; path = ko.lproj/InfoPlist.strings; sourceTree = "<group>"; };
369D184125B7B78B001C487E /* GeneralInfoView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeneralInfoView.swift; sourceTree = "<group>"; };
Expand All @@ -132,11 +132,11 @@
36D48DE825B6D9E1000434B4 /* PreferencesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesView.swift; sourceTree = "<group>"; };
36D48DEF25B6D9FB000434B4 /* GeneralView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeneralView.swift; sourceTree = "<group>"; };
36D48DF625B6DA30000434B4 /* AdvancedView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdvancedView.swift; sourceTree = "<group>"; };
36E024E025CED0C600047375 /* TextViewManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextViewManager.swift; sourceTree = "<group>"; };
36F71B8D25C9DA1400CC7754 /* HANBatang.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = HANBatang.ttf; sourceTree = "<group>"; };
36F71B9425C9DA1F00CC7754 /* HANBatangB.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = HANBatangB.ttf; sourceTree = "<group>"; };
36F71B9B25C9DA2800CC7754 /* HANDotum.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = HANDotum.ttf; sourceTree = "<group>"; };
36F71BA225C9DA3600CC7754 /* HANDotumB.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = HANDotumB.ttf; sourceTree = "<group>"; };
36F756F525C9C34800CCD636 /* HwpColor+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "HwpColor+Extension.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -182,6 +182,7 @@
3625A05325B6D35300C5C3FE /* Tests iOS */,
3625A05E25B6D35300C5C3FE /* Tests macOS */,
3625A04125B6D35300C5C3FE /* Products */,
366394E425CC9F2D0035EA1A /* Frameworks */,
);
sourceTree = "<group>";
};
Expand Down Expand Up @@ -255,12 +256,18 @@
isa = PBXGroup;
children = (
36BBC7DF25B857670045BF6B /* String+Extension.swift */,
3636F16325B898B400ECE0D8 /* UndoRedo.swift */,
36F756F525C9C34800CCD636 /* HwpColor+Extension.swift */,
366394D625CC02300035EA1A /* Dictionary+Extension.swift */,
);
path = Utils;
sourceTree = "<group>";
};
366394E425CC9F2D0035EA1A /* Frameworks */ = {
isa = PBXGroup;
children = (
);
name = Frameworks;
sourceTree = "<group>";
};
36D48DA225B6D79A000434B4 /* FileInfo */ = {
isa = PBXGroup;
children = (
Expand All @@ -284,10 +291,10 @@
36D48DC825B6D85A000434B4 /* Content */ = {
isa = PBXGroup;
children = (
36E024DF25CED0A600047375 /* Shared */,
36D48DC925B6D869000434B4 /* ContentView.swift */,
36D48DD425B6D8AC000434B4 /* UIHwpView.swift */,
36D48DDA25B6D8D1000434B4 /* NSHwpView.swift */,
360ADF2825C9D094007799B7 /* Shared.swift */,
);
path = Content;
sourceTree = "<group>";
Expand All @@ -302,6 +309,15 @@
path = Preferences;
sourceTree = "<group>";
};
36E024DF25CED0A600047375 /* Shared */ = {
isa = PBXGroup;
children = (
360ADF2825C9D094007799B7 /* Shared.swift */,
36E024E025CED0C600047375 /* TextViewManager.swift */,
);
path = Shared;
sourceTree = "<group>";
};
36F71B8C25C9D9C300CC7754 /* Fonts */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -533,21 +549,21 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
366394D725CC02300035EA1A /* Dictionary+Extension.swift in Sources */,
36D48DA425B6D7A9000434B4 /* FileInfoView.swift in Sources */,
36D48DC225B6D843000434B4 /* TOCView.swift in Sources */,
36D48DCA25B6D869000434B4 /* ContentView.swift in Sources */,
36D48DBB25B6D819000434B4 /* PageView.swift in Sources */,
36D48DE125B6D9B9000434B4 /* AboutView.swift in Sources */,
360ADF2925C9D094007799B7 /* Shared.swift in Sources */,
3636F15C25B8916300ECE0D8 /* SummaryView.swift in Sources */,
3636F16425B898B400ECE0D8 /* UndoRedo.swift in Sources */,
3625A06425B6D35300C5C3FE /* LikeHangulDocument.swift in Sources */,
36D48DD525B6D8AD000434B4 /* UIHwpView.swift in Sources */,
36F756F625C9C34800CCD636 /* HwpColor+Extension.swift in Sources */,
369D184225B7B78B001C487E /* GeneralInfoView.swift in Sources */,
36D48DB025B6D7F7000434B4 /* SidebarView.swift in Sources */,
3625A06225B6D35300C5C3FE /* LikeHangulApp.swift in Sources */,
36D48DF025B6D9FB000434B4 /* GeneralView.swift in Sources */,
36E024E125CED0C600047375 /* TextViewManager.swift in Sources */,
36D48DF725B6DA30000434B4 /* AdvancedView.swift in Sources */,
3625A06625B6D35300C5C3FE /* MainView.swift in Sources */,
36BBC7E025B857670045BF6B /* String+Extension.swift in Sources */,
Expand All @@ -559,21 +575,21 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
366394D825CC02300035EA1A /* Dictionary+Extension.swift in Sources */,
36D48DA525B6D7A9000434B4 /* FileInfoView.swift in Sources */,
36D48DC325B6D843000434B4 /* TOCView.swift in Sources */,
36D48DCB25B6D869000434B4 /* ContentView.swift in Sources */,
36D48DBC25B6D819000434B4 /* PageView.swift in Sources */,
36D48DE225B6D9B9000434B4 /* AboutView.swift in Sources */,
360ADF2A25C9D094007799B7 /* Shared.swift in Sources */,
3636F15D25B8916300ECE0D8 /* SummaryView.swift in Sources */,
3636F16525B898B400ECE0D8 /* UndoRedo.swift in Sources */,
3625A06525B6D35300C5C3FE /* LikeHangulDocument.swift in Sources */,
36D48DDB25B6D8D1000434B4 /* NSHwpView.swift in Sources */,
36F756F725C9C34800CCD636 /* HwpColor+Extension.swift in Sources */,
369D184325B7B78B001C487E /* GeneralInfoView.swift in Sources */,
36D48DB125B6D7F7000434B4 /* SidebarView.swift in Sources */,
3625A06325B6D35300C5C3FE /* LikeHangulApp.swift in Sources */,
36D48DF125B6D9FB000434B4 /* GeneralView.swift in Sources */,
36E024E225CED0C600047375 /* TextViewManager.swift in Sources */,
36D48DF825B6DA30000434B4 /* AdvancedView.swift in Sources */,
3625A06725B6D35300C5C3FE /* MainView.swift in Sources */,
36BBC7E125B857670045BF6B /* String+Extension.swift in Sources */,
Expand Down Expand Up @@ -1007,7 +1023,7 @@
repositoryURL = "https://github.com/sboh1214/Hwp-Swift.git";
requirement = {
kind = exactVersion;
version = 0.12.0;
version = 0.14.1;
};
};
/* End XCRemoteSwiftPackageReference section */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"repositoryURL": "https://github.com/sboh1214/Hwp-Swift.git",
"state": {
"branch": null,
"revision": "b2bde1d31a0496d380fe4f628bc1783d96526aa7",
"version": "0.12.0"
"revision": "64482d3c8f2dcf3762ddb4405fc606bf81b9b579",
"version": "0.14.1"
}
},
{
Expand All @@ -33,8 +33,8 @@
"repositoryURL": "https://github.com/tsolomko/SWCompression.git",
"state": {
"branch": null,
"revision": "4fa4adcf5bbf263388edcbf2631987039a84b389",
"version": "4.5.7"
"revision": "48ecebcadeecb81a41d66af547a5d7fcc1d8d5cb",
"version": "4.5.8"
}
}
]
Expand Down
14 changes: 5 additions & 9 deletions Shared/Content/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ import SwiftUI

struct ContentView: View {
var body: some View {
// ScrollView([.horizontal, .vertical]) {
GeometryReader { geometry in
#if os(iOS)
UIHwpView(geometry: geometry)
#elseif os(macOS)
NSHwpView(geometry: geometry)
#endif
}
// }
#if os(iOS)
UIHwpView()
#elseif os(macOS)
NSHwpView()
#endif
}
}

Expand Down
44 changes: 26 additions & 18 deletions Shared/Content/NSHwpView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,41 @@ import SwiftUI
import CoreHwp

struct NSHwpView: NSViewRepresentable {
typealias NSViewType = NSTextView
typealias NSViewType = NSScrollView

@EnvironmentObject var file: FileObject
var geometry: GeometryProxy

@State var textViewManager: TextViewManager!

func makeNSView(context: Context) -> NSViewType {
// context.environment.colorScheme == .light

let scrollView = NSScrollView()
let contentSize = scrollView.contentSize
scrollView.borderType = .noBorder
scrollView.hasVerticalScroller = true
scrollView.hasHorizontalScroller = true
scrollView.hasHorizontalRuler = true

let hwp = file.document.wrappedValue.hwp
let textViewManager = TextViewManager(from: hwp)

let textStorage = NSTextStorage(attributedString: makeStringForStorage(from: hwp))
let layoutManager = NSLayoutManager()
textStorage.addLayoutManager(layoutManager)
let textView = NSTextView(frame: NSRect(x: 0, y: 0, width: 1000, height: 2000), textContainer: textViewManager.textContainer)
textView.isEditable = false
textView.isRichText = true
textView.isSelectable = true
textView.breakUndoCoalescing()

let containerSize = CGSize(width: geometry.size.width, height: .greatestFiniteMagnitude)
let container = NSTextContainer(size: containerSize)
container.widthTracksTextView = true
layoutManager.addTextContainer(container)
textStorage.addLayoutManager(layoutManager)
textView.minSize = CGSize(width: 0, height: 0)
textView.maxSize = CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
textView.isVerticallyResizable = true
textView.isHorizontallyResizable = true
textView.frame = CGRect(x: scrollView.frame.origin.x, y: scrollView.frame.origin.y, width: contentSize.width, height: contentSize.height)
textView.autoresizingMask = [.width, .height]

let view = NSTextView(frame: geometry.frame(in: .local), textContainer: container)
view.isEditable = false
view.isRichText = true
view.isSelectable = true
scrollView.documentView = textView

return view
return scrollView
}

func updateNSView(_ nsView: NSViewType, context: Context) {
Expand All @@ -36,8 +46,6 @@ struct NSHwpView: NSViewRepresentable {

struct NSHwpViewPreviews: PreviewProvider {
static var previews: some View {
GeometryReader { geometry in
NSHwpView(geometry: geometry)
}
NSHwpView()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,3 @@ func makeStringForStorage(from hwp: HwpFile) -> NSMutableAttributedString {
}
return allString
}

extension Dictionary where Key == NSAttributedString.Key, Value == Any {
mutating func append(anotherDict: [NSAttributedString.Key: Any]) {
for (key, value) in anotherDict {
self.updateValue(value, forKey: key)
}
}
}
24 changes: 24 additions & 0 deletions Shared/Content/Shared/TextViewManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#if os(iOS)
import UIKit
#elseif os(macOS)
import AppKit
#endif
import CoreHwp

struct TextViewManager {
let textStorage: NSTextStorage
let layoutManager: NSLayoutManager
let textContainer: NSTextContainer

init(from hwp: HwpFile) {
textStorage = NSTextStorage(attributedString: makeStringForStorage(from: hwp))
layoutManager = NSLayoutManager()
textStorage.addLayoutManager(layoutManager)

let containerSize = CGSize(width: 1000, height: 2000)
textContainer = NSTextContainer(size: containerSize)
textContainer.widthTracksTextView = false
layoutManager.addTextContainer(textContainer)
textStorage.addLayoutManager(layoutManager)
}
}
46 changes: 25 additions & 21 deletions Shared/Content/UIHwpView.swift
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
import SwiftUI
import CoreHwp

struct UIHwpView: UIViewRepresentable {
typealias UIViewType = UITextView
typealias UIViewType = UIScrollView

@EnvironmentObject var file: FileObject
var geometry: GeometryProxy

func makeUIView(context: Context) -> UIViewType {
let hwp = file.document.wrappedValue.hwp

let textStorage = NSTextStorage(attributedString: makeStringForStorage(from: hwp))
let layoutManager = NSLayoutManager()
textStorage.addLayoutManager(layoutManager)

let containerSize = CGSize(width: geometry.size.width, height: .greatestFiniteMagnitude)
let container = NSTextContainer(size: containerSize)
container.widthTracksTextView = true
layoutManager.addTextContainer(container)
textStorage.addLayoutManager(layoutManager)

let view = UITextView(frame: geometry.frame(in: .local), textContainer: container)
view.isEditable = false
view.isSelectable = true

return view
let textViewManager = TextViewManager(from: hwp)

let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 1000, height: 2000), textContainer: textViewManager.textContainer)
textView.isEditable = false
textView.isSelectable = true
textView.isScrollEnabled = false

let scrollView = UIScrollView()
scrollView.isScrollEnabled = true
scrollView.showsHorizontalScrollIndicator = true
scrollView.showsVerticalScrollIndicator = true
scrollView.addSubview(textView)

let constraints = [
textView.leadingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leadingAnchor),
textView.trailingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor),
textView.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor),
textView.bottomAnchor.constraint(equalTo: scrollView.contentLayoutGuide.bottomAnchor)
]
scrollView.addConstraints(constraints)

return scrollView
}

func updateUIView(_ uiView: UIViewType, context: Context) {
Expand All @@ -33,8 +39,6 @@ struct UIHwpView: UIViewRepresentable {

struct UIHwpViewPreviews: PreviewProvider {
static var previews: some View {
GeometryReader { geometry in
UIHwpView(geometry: geometry)
}
UIHwpView()
}
}
9 changes: 9 additions & 0 deletions Shared/Utils/Dictionary+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Foundation

extension Dictionary where Key == NSAttributedString.Key, Value == Any {
mutating func append(anotherDict: [NSAttributedString.Key: Any]) {
for (key, value) in anotherDict {
self.updateValue(value, forKey: key)
}
}
}
Loading

0 comments on commit 87b8181

Please sign in to comment.