Skip to content

Commit

Permalink
display(iOS): support locking zoom
Browse files Browse the repository at this point in the history
  • Loading branch information
osy committed Aug 3, 2022
1 parent 23209fe commit 4355723
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Platform/iOS/Display/VMDisplayMetalViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ - (instancetype)initWithDisplay:(CSDisplay *)display input:(CSInput *)input {
if (self = [super initWithNibName:nil bundle:nil]) {
self.vmDisplay = display;
self.vmInput = input;
[self addObserver:self forKeyPath:@"vmDisplay.displaySize" options:NSKeyValueObservingOptionNew context:nil];
}
return self;
}
Expand Down Expand Up @@ -200,4 +201,10 @@ - (void)setDisplayScaling:(CGFloat)scaling origin:(CGPoint)origin {
}
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"vmDisplay.displaySize"]) {
[self.delegate display:self.vmDisplay didResizeTo:self.vmDisplay.displaySize];
}
}

@end
1 change: 1 addition & 0 deletions Platform/iOS/Display/VMDisplayViewControllerDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import Foundation

func displayDidAssertUserInteraction()
func displayDidAppear()
func display(_ display: CSDisplay, didResizeTo size: CGSize)
func serialDidError(_ error: String)
func requestInputTablet(_ tablet: Bool)
}
6 changes: 6 additions & 0 deletions Platform/iOS/VMDisplayHostedView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ struct VMDisplayHostedView: UIViewControllerRepresentable {
}
}

func display(_ display: CSDisplay, didResizeTo size: CGSize) {
if state.isDisplayZoomLocked {
state.resizeDisplayToFit(display, size: size)
}
}

func serialDidError(_ error: String) {
state.alert = .nonfatalError(error)
}
Expand Down
9 changes: 7 additions & 2 deletions Platform/iOS/VMWindowState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ struct VMWindowState: Identifiable {

var displayViewSize: CGSize = .zero

var isDisplayZoomLocked: Bool = false

var isKeyboardRequested: Bool = false

var isKeyboardShown: Bool = false
Expand Down Expand Up @@ -114,9 +116,9 @@ extension VMWindowState {
"stty cols $COLS rows $ROWS\\n"
}

private mutating func resizeDisplayToFit(_ display: CSDisplay) {
mutating func resizeDisplayToFit(_ display: CSDisplay, size: CGSize = .zero) {
let viewSize = displayViewSize
let displaySize = display.displaySize
let displaySize = size == .zero ? display.displaySize : size
let scaled = CGSize(width: viewSize.width / displaySize.width, height: viewSize.height / displaySize.height)
let viewportScale = min(scaled.width, scaled.height)
display.viewportScale = viewportScale
Expand Down Expand Up @@ -148,13 +150,16 @@ extension VMWindowState {
mutating func toggleDisplayResize(command: String? = nil) {
if case let .display(display, _) = device {
if isViewportChanged {
isDisplayZoomLocked = false
resetDisplay(display)
} else {
isDisplayZoomLocked = true
resizeDisplayToFit(display)
}
} else if case let .serial(serial, _) = device {
resetConsole(serial)
isViewportChanged = false
isDisplayZoomLocked = false
}
}
}

0 comments on commit 4355723

Please sign in to comment.