Skip to content

Commit

Permalink
Merge pull request #8 from lvsti/simulate-overflow
Browse files Browse the repository at this point in the history
Simulate overflow
  • Loading branch information
lvsti authored Oct 21, 2018
2 parents 0d3483c + 4aa99fe commit 2acf412
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 17 deletions.
10 changes: 10 additions & 0 deletions Ilion/Ilion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ extension Ilion: BrowserWindowControllerDelegate {
toolsPanelController = ToolsPanelController()
toolsPanelController?.shouldInsertStartEndMarkers = StringsManager.defaultManager.insertsStartEndMarkers
toolsPanelController?.shouldTransformCharacters = StringsManager.defaultManager.transformsCharacters
if let factor = StringsManager.defaultManager.expansionFactor {
toolsPanelController?.shouldSimulateExpansion = true
toolsPanelController?.expansionFactor = factor
}
else {
toolsPanelController?.shouldSimulateExpansion = false
}
toolsPanelController?.delegate = self

browserWindowController?.window?.beginSheet(toolsPanelController!.window!)
Expand Down Expand Up @@ -156,12 +163,15 @@ extension Ilion: ToolsPanelControllerDelegate {
func toolsPanelControllerDidClose(_ sender: ToolsPanelController) {
let markersFlag = toolsPanelController!.shouldInsertStartEndMarkers
let transformFlag = toolsPanelController!.shouldTransformCharacters
let expansionFlag = toolsPanelController!.shouldSimulateExpansion
let factor = toolsPanelController!.expansionFactor

browserWindowController?.window?.endSheet(sender.window!)
toolsPanelController = nil

StringsManager.defaultManager.insertsStartEndMarkers = markersFlag
StringsManager.defaultManager.transformsCharacters = transformFlag
StringsManager.defaultManager.expansionFactor = expansionFlag ? factor : nil
}

}
21 changes: 19 additions & 2 deletions Ilion/StringsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ typealias StringsDB = [BundleURI: [ResourceURI: [LocKey: StringsEntry]]]
private let storedOverridesKey = "Ilion.TranslationOverrides"
private let markersKey = "Ilion.InsertsStartEndMarkers"
private let transformKey = "Ilion.TransformsCharacters"

private let expansionFactorKey = "Ilion.SimulatedExpansionFactor"

private let userDefaults: UserDefaults
private let stringsFileParser: StringsFileParser

Expand All @@ -54,6 +55,17 @@ typealias StringsDB = [BundleURI: [ResourceURI: [LocKey: StringsEntry]]]
}
}

var expansionFactor: Double? = nil {
didSet {
if let factor = expansionFactor {
userDefaults.setValue(factor, forKey: expansionFactorKey)
}
else {
userDefaults.removeObject(forKey: expansionFactorKey)
}
}
}

@objc static let defaultManager = StringsManager(userDefaults: .standard, stringsFileParser: StringsFileParser())

private init(userDefaults: UserDefaults, stringsFileParser: StringsFileParser) {
Expand All @@ -65,7 +77,10 @@ typealias StringsDB = [BundleURI: [ResourceURI: [LocKey: StringsEntry]]]
overriddenKeyPaths = []
insertsStartEndMarkers = userDefaults.value(forKey: markersKey) as? Bool ?? false
transformsCharacters = userDefaults.value(forKey: transformKey) as? Bool ?? false

if let factor = userDefaults.value(forKey: expansionFactorKey) as? Double {
expansionFactor = min(max(factor, 1), 2)
}

super.init()

loadStringsFilesInBundle(Bundle.main)
Expand Down Expand Up @@ -145,13 +160,15 @@ typealias StringsDB = [BundleURI: [ResourceURI: [LocKey: StringsEntry]]]
let baseCopy = (value?.isEmpty ?? true) ? key : value!
return [Translation.static(baseCopy)]
.map { transformsCharacters ? $0.applyingPseudoLocalization() : $0 }
.map { expansionFactor != nil ? $0.simulatingExpansion(by: expansionFactor!) : $0 }
.map { insertsStartEndMarkers ? $0.addingStartEndMarkers() : $0 }
.first!
.toString()
}

return [entry.override ?? entry.translation]
.map { transformsCharacters ? $0.applyingPseudoLocalization() : $0 }
.map { expansionFactor != nil ? $0.simulatingExpansion(by: expansionFactor!) : $0 }
.map { insertsStartEndMarkers ? $0.addingStartEndMarkers() : $0 }
.first!
.toString()
Expand Down
78 changes: 72 additions & 6 deletions Ilion/ToolsPanel.xib
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="ToolsPanelController" customModule="Ilion" customModuleProvider="target">
<connections>
<outlet property="expansionCheckbox" destination="pb5-I0-Z8m" id="Naa-FJ-4IH"/>
<outlet property="expansionFactorSlider" destination="DAY-2m-sow" id="13k-KV-hs4"/>
<outlet property="markersCheckbox" destination="VXA-57-yvZ" id="8Xl-q5-j4f"/>
<outlet property="transformCheckbox" destination="59J-LL-Tct" id="uMc-TC-obV"/>
<outlet property="window" destination="QvC-M9-y7g" id="Ewr-HP-uli"/>
Expand All @@ -18,14 +20,14 @@
<window title="Window" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" restorable="NO" releasedWhenClosed="NO" visibleAtLaunch="NO" animationBehavior="default" id="QvC-M9-y7g" customClass="NSPanel">
<windowStyleMask key="styleMask" titled="YES" closable="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="196" y="240" width="389" height="115"/>
<rect key="contentRect" x="196" y="240" width="415" height="184"/>
<rect key="screenRect" x="0.0" y="0.0" width="1280" height="777"/>
<view key="contentView" wantsLayer="YES" id="EiT-Mj-1SZ">
<rect key="frame" x="0.0" y="0.0" width="389" height="115"/>
<rect key="frame" x="0.0" y="0.0" width="415" height="184"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="VXA-57-yvZ">
<rect key="frame" x="18" y="79" width="169" height="18"/>
<rect key="frame" x="18" y="148" width="169" height="18"/>
<buttonCell key="cell" type="check" title="Insert start/end markers" bezelStyle="regularSquare" imagePosition="left" inset="2" id="zoO-2q-V83">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
Expand All @@ -35,7 +37,7 @@
</connections>
</button>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="rpe-tP-MWG">
<rect key="frame" x="303" y="13" width="72" height="32"/>
<rect key="frame" x="329" y="13" width="72" height="32"/>
<buttonCell key="cell" type="push" title="Done" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="rL5-sD-4mP">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
Expand All @@ -48,7 +50,7 @@ DQ
</connections>
</button>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="59J-LL-Tct">
<rect key="frame" x="18" y="57" width="222" height="18"/>
<rect key="frame" x="18" y="126" width="222" height="18"/>
<buttonCell key="cell" type="check" title="Fuzzy transform Latin characters" bezelStyle="regularSquare" imagePosition="left" inset="2" id="X66-EC-Nnl">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
Expand All @@ -57,17 +59,81 @@ DQ
<action selector="checkboxToggled:" target="-2" id="SHd-7x-XlM"/>
</connections>
</button>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="pb5-I0-Z8m">
<rect key="frame" x="18" y="104" width="207" height="18"/>
<buttonCell key="cell" type="check" title="Simulate translation expansion" bezelStyle="regularSquare" imagePosition="left" inset="2" id="9Ke-Mu-hOh">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="checkboxToggled:" target="-2" id="Ne8-Zl-vnL"/>
</connections>
</button>
<slider verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="DAY-2m-sow">
<rect key="frame" x="42" y="76" width="204" height="24"/>
<constraints>
<constraint firstAttribute="width" constant="200" id="khE-bj-Obb"/>
</constraints>
<sliderCell key="cell" state="on" alignment="left" minValue="1" maxValue="2" doubleValue="1" tickMarkPosition="below" numberOfTickMarks="11" allowsTickMarkValuesOnly="YES" sliderType="linear" id="yqn-61-2jS"/>
<connections>
<action selector="sliderScrubbed:" target="-2" id="5MJ-Ab-oPJ"/>
</connections>
</slider>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Oad-5t-5s4">
<rect key="frame" x="37" y="59" width="33" height="14"/>
<textFieldCell key="cell" lineBreakMode="clipping" title="100%" id="i9e-Fb-E54">
<font key="font" metaFont="smallSystem"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="TH2-8V-xMI">
<rect key="frame" x="219" y="59" width="34" height="14"/>
<textFieldCell key="cell" lineBreakMode="clipping" title="200%" id="sfd-Wa-2Yv">
<font key="font" metaFont="smallSystem"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="pfq-fP-8dg">
<rect key="frame" x="250" y="83" width="130" height="17"/>
<textFieldCell key="cell" lineBreakMode="clipping" title="of the original length" id="b1E-Rf-gqZ">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="b2R-TD-Pv2">
<rect key="frame" x="128" y="59" width="33" height="14"/>
<textFieldCell key="cell" lineBreakMode="clipping" title="150%" id="oT9-v8-g9Q">
<font key="font" metaFont="smallSystem"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
</subviews>
<constraints>
<constraint firstItem="59J-LL-Tct" firstAttribute="top" secondItem="VXA-57-yvZ" secondAttribute="bottom" constant="8" id="Aku-Mn-Lou"/>
<constraint firstAttribute="bottom" secondItem="rpe-tP-MWG" secondAttribute="bottom" constant="20" id="DUF-Fn-F55"/>
<constraint firstItem="pfq-fP-8dg" firstAttribute="centerY" secondItem="DAY-2m-sow" secondAttribute="centerY" constant="-4" id="NHl-6Y-Lkh"/>
<constraint firstItem="pb5-I0-Z8m" firstAttribute="leading" secondItem="EiT-Mj-1SZ" secondAttribute="leading" constant="20" id="Pec-hc-Mo0"/>
<constraint firstItem="b2R-TD-Pv2" firstAttribute="top" secondItem="DAY-2m-sow" secondAttribute="bottom" constant="4" id="U6q-5I-xbB"/>
<constraint firstItem="Oad-5t-5s4" firstAttribute="top" secondItem="DAY-2m-sow" secondAttribute="bottom" constant="4" id="ZWB-Wb-Kdp"/>
<constraint firstItem="59J-LL-Tct" firstAttribute="leading" secondItem="EiT-Mj-1SZ" secondAttribute="leading" constant="20" id="bed-zX-iBd"/>
<constraint firstItem="VXA-57-yvZ" firstAttribute="top" secondItem="EiT-Mj-1SZ" secondAttribute="top" constant="20" id="bjX-ZG-m5M"/>
<constraint firstItem="DAY-2m-sow" firstAttribute="leading" secondItem="EiT-Mj-1SZ" secondAttribute="leading" constant="44" id="btl-JW-u9B"/>
<constraint firstItem="pb5-I0-Z8m" firstAttribute="top" secondItem="59J-LL-Tct" secondAttribute="bottom" constant="8" id="cWq-LR-DQ9"/>
<constraint firstItem="pfq-fP-8dg" firstAttribute="leading" secondItem="DAY-2m-sow" secondAttribute="trailing" constant="8" id="clQ-tL-rei"/>
<constraint firstItem="b2R-TD-Pv2" firstAttribute="centerX" secondItem="DAY-2m-sow" secondAttribute="centerX" id="dVs-g0-eJl"/>
<constraint firstItem="DAY-2m-sow" firstAttribute="trailing" secondItem="TH2-8V-xMI" secondAttribute="trailing" constant="-7" id="e54-Sg-neF"/>
<constraint firstItem="DAY-2m-sow" firstAttribute="top" secondItem="pb5-I0-Z8m" secondAttribute="bottom" constant="8" id="efx-TG-7NH"/>
<constraint firstAttribute="trailing" secondItem="rpe-tP-MWG" secondAttribute="trailing" constant="20" id="j3q-ni-fpr"/>
<constraint firstItem="VXA-57-yvZ" firstAttribute="leading" secondItem="EiT-Mj-1SZ" secondAttribute="leading" constant="20" id="oGp-39-McN"/>
<constraint firstItem="TH2-8V-xMI" firstAttribute="top" secondItem="DAY-2m-sow" secondAttribute="bottom" constant="4" id="pdq-Og-Orf"/>
<constraint firstItem="Oad-5t-5s4" firstAttribute="leading" secondItem="DAY-2m-sow" secondAttribute="leading" constant="-5" id="ps3-WP-KPf"/>
</constraints>
</view>
<point key="canvasLocation" x="93.5" y="69.5"/>
<point key="canvasLocation" x="106.5" y="105"/>
</window>
</objects>
</document>
50 changes: 41 additions & 9 deletions Ilion/ToolsPanelController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,32 @@ protocol ToolsPanelControllerDelegate: class {
final class ToolsPanelController: NSWindowController {
@IBOutlet private weak var markersCheckbox: NSButton!
@IBOutlet private weak var transformCheckbox: NSButton!

@IBOutlet private weak var expansionCheckbox: NSButton!
@IBOutlet private weak var expansionFactorSlider: NSSlider!

weak var delegate: ToolsPanelControllerDelegate?

var shouldInsertStartEndMarkers: Bool = false {
didSet {
guard isWindowLoaded else { return }
markersCheckbox?.state = shouldInsertStartEndMarkers ? .on : .off
updateUI()
}
}

var shouldTransformCharacters: Bool = false {
didSet {
guard isWindowLoaded else { return }
transformCheckbox?.state = shouldTransformCharacters ? .on : .off
updateUI()
}
}

var shouldSimulateExpansion: Bool = false {
didSet {
updateUI()
}
}

var expansionFactor: Double = 1.0 {
didSet {
updateUI()
}
}

Expand All @@ -38,15 +50,35 @@ final class ToolsPanelController: NSWindowController {

override func windowDidLoad() {
super.windowDidLoad()
updateUI()
}

private func updateUI() {
guard isWindowLoaded else { return }

markersCheckbox.state = shouldInsertStartEndMarkers ? .on : .off
transformCheckbox.state = shouldTransformCharacters ? .on : .off
expansionCheckbox.state = shouldSimulateExpansion ? .on : .off
expansionFactorSlider.doubleValue = expansionFactor
}

@IBAction private func checkboxToggled(_ sender: Any) {
shouldInsertStartEndMarkers = markersCheckbox.state == .on
shouldTransformCharacters = transformCheckbox.state == .on
@IBAction private func checkboxToggled(_ sender: NSButton) {
switch sender {
case markersCheckbox:
shouldInsertStartEndMarkers = markersCheckbox.state == .on
case transformCheckbox:
shouldTransformCharacters = transformCheckbox.state == .on
case expansionCheckbox:
shouldSimulateExpansion = expansionCheckbox.state == .on
default:
return
}
}


@IBAction private func sliderScrubbed(_ sender: Any) {
expansionFactor = expansionFactorSlider.doubleValue
}

@IBAction private func doneClicked(_ sender: Any) {
delegate?.toolsPanelControllerDidClose(self)
}
Expand Down
27 changes: 27 additions & 0 deletions Ilion/Translation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,31 @@ extension Translation {
return .dynamic(transformedFormat)
}
}

func simulatingExpansion(by factor: Double) -> Translation {
func paddingText(forLength length: Int) -> String {
let growth = Int(floor(Double(length) * factor)) - length
let pattern = "lorem ipsum dolor sit amet consectetur adipiscing elit "
let paddingSource = String(repeating: pattern, count: (growth / pattern.count) + 1)
let padding = paddingSource[paddingSource.startIndex ..< paddingSource.index(paddingSource.startIndex, offsetBy: growth)]
return "{\(padding)}"
}

switch self {
case .static(let text):
let padding = paddingText(forLength: text.count)
return .static(text.appending(padding))

case .dynamic(let format):
let transformedFormat = format.applyingTransform { slices in
let charCount = slices.map { $0.count }.reduce(0, +)
let padding = paddingText(forLength: charCount)

var slices = slices
slices[slices.count - 1] = slices.last!.appending(padding)
return slices
}
return .dynamic(transformedFormat)
}
}
}
Loading

0 comments on commit 2acf412

Please sign in to comment.