Skip to content

Commit

Permalink
feat: support of device orientation
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Nov 28, 2022
1 parent 659798f commit d11680c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
27 changes: 27 additions & 0 deletions PlantUML/PlantUMLContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,33 @@ struct PlantUMLContentView: View {
}
}
}
.onRotate(perform: { orientation in
switch( orientation ) {
case .portrait, .portraitUpsideDown:
print( "portrait: \(orientation)")
if !isEditorVisible {
isEditorVisible.toggle()
}
break
case .landscapeLeft, .landscapeRight:
print( "landscape")
if !isDiagramVisible {
isEditorVisible.toggle()
}
break
case .faceDown:
print( "faceDown")
break
case .faceUp:
print( "faceUp")
break
case .unknown:
print( "unknown")
break
@unknown default:
print( "default")
}
})
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItemGroup(placement: .navigationBarLeading) {
Expand Down
30 changes: 30 additions & 0 deletions PlantUML/SwiftUI+Rotate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// SwiftUIView.swift
//
//
// Created by Bartolomeo Sorrentino on 28/11/22.
//
// Inspired by https://www.hackingwithswift.com/quick-start/swiftui/how-to-detect-device-rotation

import SwiftUI

// Our custom view modifier to track rotation and
// call our action
struct DeviceRotationViewModifier: ViewModifier {
let action: (UIDeviceOrientation) -> Void

func body(content: Content) -> some View {
content
.onAppear()
.onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in
action(UIDevice.current.orientation)
}
}
}

// A View wrapper to make the modifier easier to use
extension View {
func onRotate(perform action: @escaping (UIDeviceOrientation) -> Void) -> some View {
self.modifier(DeviceRotationViewModifier(action: action))
}
}
10 changes: 8 additions & 2 deletions PlantUML4iPad.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
A0D3C65E28984A10000838D7 /* PlantUMLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0D3C65D28984A10000838D7 /* PlantUMLTests.swift */; };
A0D3C66828984A11000838D7 /* PlantUMLUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0D3C66728984A11000838D7 /* PlantUMLUITests.swift */; };
A0D3C66A28984A11000838D7 /* PlantUMLUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0D3C66928984A11000838D7 /* PlantUMLUITestsLaunchTests.swift */; };
A0F2B14229353C2D00A44481 /* SwiftUI+Rotate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0F2B14129353C2D00A44481 /* SwiftUI+Rotate.swift */; };
A0F2B14329353C2D00A44481 /* SwiftUI+Rotate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A0F2B14129353C2D00A44481 /* SwiftUI+Rotate.swift */; };
A0F329202902AC5E00E0C47C /* LineEditor in Frameworks */ = {isa = PBXBuildFile; productRef = A0F3291F2902AC5E00E0C47C /* LineEditor */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -61,6 +63,7 @@
A0D3C66728984A11000838D7 /* PlantUMLUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlantUMLUITests.swift; sourceTree = "<group>"; };
A0D3C66928984A11000838D7 /* PlantUMLUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlantUMLUITestsLaunchTests.swift; sourceTree = "<group>"; };
A0EF7AF128C40A6300660F09 /* PlantUMLKeyboard */ = {isa = PBXFileReference; lastKnownFileType = wrapper; path = PlantUMLKeyboard; sourceTree = "<group>"; };
A0F2B14129353C2D00A44481 /* SwiftUI+Rotate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SwiftUI+Rotate.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -124,6 +127,7 @@
A0D3C64828984A0E000838D7 /* PlantUML */ = {
isa = PBXGroup;
children = (
A0F2B14129353C2D00A44481 /* SwiftUI+Rotate.swift */,
A0D3C64928984A0E000838D7 /* PlantUMLApp.swift */,
A0D3C64B28984A0E000838D7 /* PlantUMLDocument.swift */,
A0D3C64D28984A0E000838D7 /* PlantUMLContentView.swift */,
Expand Down Expand Up @@ -306,6 +310,7 @@
A0D3C64A28984A0E000838D7 /* PlantUMLApp.swift in Sources */,
A0D3C64C28984A0E000838D7 /* PlantUMLDocument.swift in Sources */,
A0D3C64E28984A0E000838D7 /* PlantUMLContentView.swift in Sources */,
A0F2B14229353C2D00A44481 /* SwiftUI+Rotate.swift in Sources */,
A0A42A76289ABC2D00E929EB /* PlantUMLDiagramView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -325,6 +330,7 @@
A0D3C66A28984A11000838D7 /* PlantUMLUITestsLaunchTests.swift in Sources */,
A0A42A7A289AC37C00E929EB /* PlantUMLDiagramObject.swift in Sources */,
A0A42A77289ABC2D00E929EB /* PlantUMLDiagramView.swift in Sources */,
A0F2B14329353C2D00A44481 /* SwiftUI+Rotate.swift in Sources */,
A0D3C66828984A11000838D7 /* PlantUMLUITests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -476,7 +482,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 5;
CURRENT_PROJECT_VERSION = 6;
DEVELOPMENT_ASSET_PATHS = "\"PlantUML/Preview Content\"";
DEVELOPMENT_TEAM = 48J595L9BX;
ENABLE_PREVIEWS = YES;
Expand Down Expand Up @@ -509,7 +515,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 5;
CURRENT_PROJECT_VERSION = 6;
DEVELOPMENT_ASSET_PATHS = "\"PlantUML/Preview Content\"";
DEVELOPMENT_TEAM = 48J595L9BX;
ENABLE_PREVIEWS = YES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"location" : "https://github.com/bsorrentino/PlantUML4iPad.git",
"state" : {
"branch" : "line_editor",
"revision" : "3286a2542bc414ce9d938b2d227adaa4768d7211"
"revision" : "223b6f403621940322b3bbc475d67f49e4cf56b1"
}
}
],
Expand Down

0 comments on commit d11680c

Please sign in to comment.