Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyboard navigation improvements for macOS #1084

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 50 additions & 15 deletions Views/SearchResults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct SearchResults: View {
@Environment(\.managedObjectContext) private var managedObjectContext
@EnvironmentObject private var viewModel: SearchViewModel
@EnvironmentObject private var navigation: NavigationViewModel
@FocusState private var focusedSearchItem: URL? // macOS only
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \ZimFile.size, ascending: false)],
predicate: ZimFile.Predicate.isDownloaded,
Expand Down Expand Up @@ -75,22 +76,55 @@ struct SearchResults: View {
} else if viewModel.results.isEmpty {
Message(text: "search_result.zimfile.no_result.message".localized)
} else {
ScrollView {
LazyVGrid(columns: [GridItem(.flexible(minimum: 300, maximum: 700), alignment: .center)]) {
ForEach(viewModel.results) { result in
Button {
recentSearchTexts = {
var searchTexts = Defaults[.recentSearchTexts]
searchTexts.removeAll(where: { $0 == viewModel.searchText })
searchTexts.insert(viewModel.searchText, at: 0)
return searchTexts
}()
NotificationCenter.openURL(result.url)
} label: {
ArticleCell(result: result, zimFile: viewModel.zimFiles[result.zimFileID])
}.buttonStyle(.plain)
ScrollViewReader { scrollReader in
ScrollView {
LazyVGrid(columns: [GridItem(.flexible(minimum: 300, maximum: 700), alignment: .center)]) {
ForEach(viewModel.results) { result in
Button {
recentSearchTexts = {
var searchTexts = Defaults[.recentSearchTexts]
searchTexts.removeAll(where: { $0 == viewModel.searchText })
searchTexts.insert(viewModel.searchText, at: 0)
return searchTexts
}()
NotificationCenter.openURL(result.url)
} label: {
ArticleCell(result: result, zimFile: viewModel.zimFiles[result.zimFileID])
}
.buttonStyle(.plain)
.modifier(
Focusable( // macOS only
$focusedSearchItem,
equals: result.url,
onReturn: {
NotificationCenter.openURL(result.url)
},
onDismiss: {
$focusedSearchItem.wrappedValue = nil
dismissSearch()
})
)
}
}.padding()
}
.onReceive(self.focusedSearchItem.publisher) { focusedURL in
scrollReader.scrollTo(focusedURL, anchor: .center)
}
.modifier(MoveCommand(perform: { direction in
// macOS only
if let focusedSearchItem,
let index = viewModel.results.firstIndex(where: { $0.url == focusedSearchItem }) {
let nextIndex: Int
switch direction {
case .up: nextIndex = viewModel.results.index(before: index)
case .down: nextIndex = viewModel.results.index(after: index)
default: nextIndex = viewModel.results.startIndex
}
if (viewModel.results.startIndex..<viewModel.results.endIndex).contains(nextIndex) {
$focusedSearchItem.wrappedValue = viewModel.results[nextIndex].url
}
}
}.padding()
}))
}
}
}
Expand Down Expand Up @@ -129,6 +163,7 @@ struct SearchResults: View {
} header: { searchFilterHeader }
}
}
.modifier(NotFocusable()) // macOS only
}

private var recentSearchHeader: some View {
Expand Down
65 changes: 65 additions & 0 deletions Views/ViewModifiers/Focusable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// This file is part of Kiwix for iOS & macOS.
//
// Kiwix is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// any later version.
//
// Kiwix is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kiwix; If not, see https://www.gnu.org/licenses/.

import SwiftUI

struct NotFocusable: ViewModifier {

func body(content: Content) -> some View {
#if os(macOS)
content.focusable(false)
#else
content
#endif
}
}

struct Focusable<Value: Hashable>: ViewModifier {

private let value: Value
private let focusState: FocusState<Value>.Binding
private let onReturn: () -> Void
private let onDissmiss: () -> Void

init(
_ binding: FocusState<Value>.Binding,
equals value: Value,
onReturn: @escaping () -> Void,
onDismiss: @escaping () -> Void
) {
self.focusState = binding
self.value = value
self.onReturn = onReturn
self.onDissmiss = onDismiss
}

func body(content: Content) -> some View {
#if os(macOS)
content
.id(value)
.focusable()
.focused(focusState, equals: value)
.modifier(KeyPressHandler(key: .return, action: {
onReturn()
}))
.modifier(KeyPressHandler(key: .escape, action: {
onDissmiss()
}))
#else
content
#endif

}
}
44 changes: 44 additions & 0 deletions Views/ViewModifiers/KeyPressHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// This file is part of Kiwix for iOS & macOS.
//
// Kiwix is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// any later version.
//
// Kiwix is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kiwix; If not, see https://www.gnu.org/licenses/.

import SwiftUI

struct KeyPressHandler: ViewModifier {

let key: KeyEquivalent
let action: () -> Void

func body(content: Content) -> some View {
#if os(macOS)
if #available(macOS 14.0, *) {
newApi(content: content)
} else {
content
}
#else
content
#endif
}

@available(macOS 14.0, iOS 17.0, *)
private func newApi(content: Content) -> some View {
content.onKeyPress(key, action: {
Task { await MainActor.run {
action()
}}
return .handled
})
}
}
58 changes: 58 additions & 0 deletions Views/ViewModifiers/MoveCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// This file is part of Kiwix for iOS & macOS.
//
// Kiwix is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// any later version.
//
// Kiwix is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kiwix; If not, see https://www.gnu.org/licenses/.

import SwiftUI

enum MoveDirection: Sendable {

// swiftlint:disable:next identifier_name
case up
case down
case left
case right

#if os(macOS)
init?(from direction: MoveCommandDirection) {
switch direction {
case .up: self = .up
case .down: self = .down
case .left: self = .left
case .right: self = .right
@unknown default: return nil
}
}
#endif
}

struct MoveCommand: ViewModifier {

private let action: ((MoveDirection) -> Void)?

init(perform action: ((MoveDirection) -> Void)?) {
self.action = action
}

func body(content: Content) -> some View {
#if os(macOS)
content.onMoveCommand { (direction: MoveCommandDirection) in
if let mappedDirection = MoveDirection(from: direction) {
action?(mappedDirection)
}
}
#else
content
#endif
}
}
Loading