Skip to content

Commit

Permalink
Merge pull request #22 from jasonjmcghee/rem-21-disable-cmd-scroll-ti…
Browse files Browse the repository at this point in the history
…meline

[rem-21]: Add option to disable cmd-scroll
  • Loading branch information
jasonjmcghee committed Dec 30, 2023
2 parents 58f7aee + ea5dbb5 commit 75dc246
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
5 changes: 4 additions & 1 deletion rem/SettingsManager.swift
Expand Up @@ -11,6 +11,7 @@ import SwiftUI
// The settings structure
struct AppSettings: Codable {
var saveEverythingCopiedToClipboard: Bool
var enableCmdScrollShortcut: Bool
}

// The settings manager handles saving and loading the settings
Expand All @@ -26,7 +27,7 @@ class SettingsManager: ObservableObject {
self.settings = decodedSettings
} else {
// Default settings
self.settings = AppSettings(saveEverythingCopiedToClipboard: false)
self.settings = AppSettings(saveEverythingCopiedToClipboard: false, enableCmdScrollShortcut: true)
}
}

Expand All @@ -48,6 +49,8 @@ struct SettingsView: View {
Form {
Toggle("Remember everything copied to clipboard", isOn: $settingsManager.settings.saveEverythingCopiedToClipboard)
.onChange(of: settingsManager.settings.saveEverythingCopiedToClipboard) { settingsManager.saveSettings() }
Toggle("Allow opening / closing timeline with CMD + Scroll", isOn: $settingsManager.settings.enableCmdScrollShortcut)
.onChange(of: settingsManager.settings.enableCmdScrollShortcut) { settingsManager.saveSettings() }
}
}
.padding()
Expand Down
13 changes: 11 additions & 2 deletions rem/TimelineView.swift
Expand Up @@ -14,12 +14,14 @@ struct TimelineView: View {
let overlayView = ImageAnalysisOverlayView()
private let imageAnalyzer = ImageAnalyzer()

var settingsManager: SettingsManager
var onClose: () -> Void // Closure to handle thumbnail click

private var fps: Int32 = 25

init(viewModel: TimelineViewModel, onClose: @escaping () -> Void) {
init(viewModel: TimelineViewModel, settingsManager: SettingsManager, onClose: @escaping () -> Void) {
self.viewModel = viewModel
self.settingsManager = settingsManager
self.onClose = onClose
_frame = State(initialValue: NSScreen.main?.visibleFrame ?? NSRect.zero)
_customHostingView = State(initialValue: nil)
Expand All @@ -30,7 +32,7 @@ struct TimelineView: View {
let index = viewModel.currentFrameIndex
if let image = DatabaseManager.shared.getImage(index: index) {
let nsImage = NSImage(cgImage: image, size: NSSize(width: image.width, height: image.width))
CustomHostingControllerRepresentable(onClose: onClose, image: nsImage, analysis: $imageAnalysis, frame: frame)
CustomHostingControllerRepresentable(settingsManager: settingsManager, onClose: onClose, image: nsImage, analysis: $imageAnalysis, frame: frame)
.frame(width: frame.width, height: frame.height)
.ignoresSafeArea(.all)
.onChange(of: viewModel.currentFrameIndex) {
Expand Down Expand Up @@ -169,6 +171,7 @@ class CustomHostingView: NSHostingView<AnyView> {
}

struct CustomHostingControllerRepresentable: NSViewControllerRepresentable {
var settingsManager: SettingsManager
var onClose: () -> Void // Closure to handle thumbnail click
var image: NSImage
@Binding var analysis: ImageAnalysis?
Expand All @@ -177,6 +180,7 @@ struct CustomHostingControllerRepresentable: NSViewControllerRepresentable {
func makeNSViewController(context: Context) -> CustomHostingViewController {
let viewController = CustomHostingViewController()
viewController.onClose = onClose
viewController.settingsManager = settingsManager
viewController.updateImage(image, frame: frame)
return viewController
}
Expand All @@ -185,10 +189,12 @@ struct CustomHostingControllerRepresentable: NSViewControllerRepresentable {
nsViewController.updateImage(image, frame: frame)
nsViewController.updateAnalysis(analysis)
nsViewController.onClose = onClose
nsViewController.settingsManager = settingsManager
}
}

class CustomHostingViewController: NSViewController {
var settingsManager: SettingsManager?
var onClose: (() -> Void)? // Closure to handle thumbnail click
var customHostingView: CustomHostingView?
var interceptingView: CustomInterceptingView?
Expand All @@ -200,6 +206,7 @@ class CustomHostingViewController: NSViewController {
override func loadView() {
let _interceptingView = CustomInterceptingView()
_interceptingView.onClose = onClose
_interceptingView.settingsManager = settingsManager
self.view = _interceptingView // Basic NSView as a container
if customHostingView == nil {
customHostingView = CustomHostingView(image: NSImage(), frame: self.view.bounds)
Expand Down Expand Up @@ -227,6 +234,7 @@ class CustomHostingViewController: NSViewController {
}

class CustomInterceptingView: NSView {
var settingsManager: SettingsManager?
var onClose: (() -> Void)? // Closure to handle thumbnail click
weak var customHostingView: CustomHostingView?

Expand Down Expand Up @@ -258,6 +266,7 @@ class CustomInterceptingView: NSView {
}

override func scrollWheel(with event: NSEvent) {
guard settingsManager?.settings.enableCmdScrollShortcut ?? false else { return }
if event.modifierFlags.contains(.command) && event.scrollingDeltaY > 0 {
self.exit()
}
Expand Down
3 changes: 2 additions & 1 deletion rem/remApp.swift
Expand Up @@ -239,6 +239,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

private func handleGlobalScrollEvent(_ event: NSEvent) {
guard settingsManager.settings.enableCmdScrollShortcut else { return}
guard event.modifierFlags.contains(.command) else { return }

if event.scrollingDeltaY < 0 && !self.isTimelineOpen() { // Check if scroll up
Expand Down Expand Up @@ -579,7 +580,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {

timelineViewWindow?.collectionBehavior = [.fullScreenAuxiliary, .canJoinAllSpaces, .participatesInCycle]
timelineViewWindow?.ignoresMouseEvents = false
timelineView = TimelineView(viewModel: TimelineViewModel(), onClose: {
timelineView = TimelineView(viewModel: TimelineViewModel(), settingsManager: settingsManager, onClose: {
DispatchQueue.main.async { [weak self] in
self?.closeTimelineView()
}
Expand Down

0 comments on commit 75dc246

Please sign in to comment.