Skip to content

Commit

Permalink
feat: open history window via keybind
Browse files Browse the repository at this point in the history
  • Loading branch information
castdrian committed Dec 30, 2023
1 parent e6496c4 commit cf13b20
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
8 changes: 6 additions & 2 deletions ishare/Util/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ import KeyboardShortcuts

final class AppState: ObservableObject {
@Default(.showMainMenu) var showMainMenu
@Default(.uploadHistory) var uploadHistory

init() {
KeyboardShortcuts.onKeyUp(for: .toggleMainMenu) {
self.showMainMenu = true
KeyboardShortcuts.onKeyUp(for: .toggleMainMenu) { [self] in
showMainMenu = true
}
KeyboardShortcuts.onKeyUp(for: .openHistoryWindow) { [self] in
openHistoryWindow(uploadHistory: uploadHistory)
}
KeyboardShortcuts.onKeyUp(for: .captureRegion) {
captureScreen(type: .REGION)
Expand Down
1 change: 1 addition & 0 deletions ishare/Util/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extension KeyboardShortcuts.Name {
static let captureScreen = Self("captureScreen", default: .init(.x, modifiers: [.option, .command]))
static let recordScreen = Self("recordScreen", default: .init(.z, modifiers: [.control, .option]))
static let recordGif = Self("recordGif", default: .init(.g, modifiers: [.control, .option]))
static let openHistoryWindow = Self("openHistoryWindow", default: .init(.k, modifiers: [.command, .option]))
}

extension Defaults.Keys {
Expand Down
38 changes: 19 additions & 19 deletions ishare/Views/MainMenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ class WindowHolder {
var historyWindowController: HistoryWindowController?
}

func openHistoryWindow(uploadHistory: [HistoryItem]) {
if WindowHolder.shared.historyWindowController == nil {
let historyView = HistoryGridView(uploadHistory: uploadHistory)
let hostingController = NSHostingController(rootView: historyView)
let windowController = HistoryWindowController(contentView: hostingController.view)
windowController.window?.title = "History"

windowController.showWindow(nil)
NSApp.activate(ignoringOtherApps: true)

WindowHolder.shared.historyWindowController = windowController
} else {
WindowHolder.shared.historyWindowController?.window?.makeKeyAndOrderFront(nil)
NSApp.activate(ignoringOtherApps: true)
}
}

struct MainMenuView: View {
@Default(.copyToClipboard) var copyToClipboard
@Default(.openInFinder) var openInFinder
Expand All @@ -52,23 +69,6 @@ struct MainMenuView: View {

@StateObject private var availableContentProvider = AvailableContentProvider()

private func openHistoryWindow() {
if WindowHolder.shared.historyWindowController == nil {
let historyView = HistoryGridView(uploadHistory: uploadHistory)
let hostingController = NSHostingController(rootView: historyView)
let windowController = HistoryWindowController(contentView: hostingController.view)
windowController.window?.title = "History"

windowController.showWindow(nil)
NSApp.activate(ignoringOtherApps: true)

WindowHolder.shared.historyWindowController = windowController
} else {
WindowHolder.shared.historyWindowController?.window?.makeKeyAndOrderFront(nil)
NSApp.activate(ignoringOtherApps: true)
}
}

var body: some View {
VStack {
Menu {
Expand Down Expand Up @@ -250,11 +250,11 @@ struct MainMenuView: View {
if !uploadHistory.isEmpty {
Menu {
Button {
openHistoryWindow()
openHistoryWindow(uploadHistory: uploadHistory)
} label: {
Image(systemName: "clock.arrow.circlepath")
Label("Open History Window", image: String())
}
}.keyboardShortcut(.openHistoryWindow)

Divider()

Expand Down
3 changes: 2 additions & 1 deletion ishare/Views/SettingsMenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,15 @@ struct KeybindSettingsView: View {
VStack {
Form {
KeyboardShortcuts.Recorder("Open Main Menu:", name: .toggleMainMenu)
KeyboardShortcuts.Recorder("Open History Window:", name: .openHistoryWindow)
KeyboardShortcuts.Recorder("Capture Region:", name: .captureRegion)
KeyboardShortcuts.Recorder("Capture Window:", name: .captureWindow)
KeyboardShortcuts.Recorder("Capture Screen:", name: .captureScreen)
KeyboardShortcuts.Recorder("Record Screen:", name: .recordScreen)
KeyboardShortcuts.Recorder("Record GIF:", name: .recordGif)
}
Button("Reset") {
KeyboardShortcuts.reset([.toggleMainMenu, .captureRegion, .captureWindow, .captureScreen, .recordScreen, .recordGif])
KeyboardShortcuts.reset([.toggleMainMenu,.openHistoryWindow, .captureRegion, .captureWindow, .captureScreen, .recordScreen, .recordGif])
BezelNotification.show(messageText: "Reset keybinds", icon: ToastIcon)
}
}
Expand Down

0 comments on commit cf13b20

Please sign in to comment.