Skip to content

Commit

Permalink
feat: Add command bar for macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeWeidmann committed Feb 7, 2024
1 parent 631926f commit 9209669
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 0 deletions.
75 changes: 75 additions & 0 deletions Mail/Commands/CustomCommands.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Infomaniak Mail - iOS App
Copyright (C) 2024 Infomaniak Network SA
This program 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
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/

import InfomaniakCore
import InfomaniakCoreUI
import InfomaniakDI
import MailCore
import MailResources
import SwiftUI

struct CustomCommands: Commands {
@LazyInjectService private var matomo: MatomoUtils

@ObservedObject var rootViewState: RootViewState

var mainViewState: MainViewState? {
if case .mainView(let mainViewState) = rootViewState.state {
return mainViewState
} else {
return nil
}
}

var body: some Commands {
CommandGroup(replacing: .newItem) {
if #available(iOS 16.0, *) {
NewMessageCommand(mailboxManager: mainViewState?.mailboxManager)
}
}

CommandGroup(after: .newItem) {
Button(MailResourcesStrings.Localizable.shortcutRefreshAction) {
guard let mainViewState else { return }
refresh(mailboxManager: mainViewState.mailboxManager, currentFolder: mainViewState.selectedFolder)
}
.keyboardShortcut("n", modifiers: [.shift, .command])
.disabled(mainViewState == nil)
}

CommandGroup(replacing: .printItem) {
if let mainViewState {
PrintMessageCommand(mainViewState: mainViewState)
}
}

CommandGroup(replacing: .appSettings) {
if #available(iOS 16.0, *) {
OpenSettingsCommand()
.disabled(mainViewState == nil)
}
}
}

func refresh(mailboxManager: MailboxManager, currentFolder: Folder) {
matomo.track(eventWithCategory: .menuAction, name: "refresh")
Task {
await mailboxManager.refreshFolderContent(currentFolder)
}
}
}
50 changes: 50 additions & 0 deletions Mail/Commands/NewMessageCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Infomaniak Mail - iOS App
Copyright (C) 2024 Infomaniak Network SA
This program 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
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/

import InfomaniakCore
import InfomaniakCoreUI
import InfomaniakDI
import MailCore
import MailResources
import SwiftUI

@available(iOS 16.0, *)
struct NewMessageCommand: View {
@LazyInjectService private var matomo: MatomoUtils

@Environment(\.openWindow) private var openWindow

let mailboxManager: MailboxManager?

var body: some View {
Button(MailResourcesStrings.Localizable.buttonNewMessage) {
guard let mailboxManager else { return }
newMessage(mailboxManager: mailboxManager)
}
.keyboardShortcut("n")
.disabled(mailboxManager == nil)
}

func newMessage(mailboxManager: MailboxManager) {
matomo.track(eventWithCategory: .menuAction, name: "newMessage")
openWindow(
id: DesktopWindowIdentifier.composeWindowIdentifier,
value: ComposeMessageIntent.new(originMailboxManager: mailboxManager)
)
}
}
44 changes: 44 additions & 0 deletions Mail/Commands/OpenSettingsCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Infomaniak Mail - iOS App
Copyright (C) 2024 Infomaniak Network SA
This program 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
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/

import InfomaniakCoreUI
import InfomaniakDI
import MailCore
import MailResources
import SwiftUI

@available(iOS 16.0, *)
struct OpenSettingsCommand: View {
@LazyInjectService private var matomo: MatomoUtils

@Environment(\.openWindow) private var openWindow

var body: some View {
Button(MailResourcesStrings.Localizable.settingsTitle) {
openSettings()
}
}

func openSettings() {
matomo.track(eventWithCategory: .menuAction, name: "settings")
openWindow(
id: DesktopWindowIdentifier.settingsWindowIdentifier,
value: SettingsViewConfig(baseNavigationPath: [])
)
}
}
45 changes: 45 additions & 0 deletions Mail/Commands/PrintMessageCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Infomaniak Mail - iOS App
Copyright (C) 2024 Infomaniak Network SA
This program 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
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/

import InfomaniakCore
import InfomaniakCoreUI
import InfomaniakDI
import MailCore
import MailResources
import SwiftUI

struct PrintMessageCommand: View {
@LazyInjectService private var matomo: MatomoUtils

@ObservedObject var mainViewState: MainViewState

var body: some View {
Button(MailResourcesStrings.Localizable.actionPrint) {
printCurrentMessage(mainViewState: mainViewState)
}
.keyboardShortcut("p")
.disabled(mainViewState.selectedThread?.lastMessageFromFolder == nil)
}

func printCurrentMessage(mainViewState: MainViewState) {
matomo.track(eventWithCategory: .menuAction, name: "print")

guard let message = mainViewState.selectedThread?.lastMessageFromFolder else { return }
NotificationCenter.default.post(name: Notification.Name.printNotification, object: message)
}
}
3 changes: 3 additions & 0 deletions Mail/UserAccountScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ struct UserAccountScene: Scene {
refreshCacheData()
}
}
.commands {
CustomCommands(rootViewState: rootViewState)
}
.defaultAppStorage(.shared)

if #available(iOS 16.0, *) {
Expand Down
1 change: 1 addition & 0 deletions MailCore/Utils/Matomo+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public extension MatomoUtils.EventCategory {
static let notificationActions = MatomoUtils.EventCategory(displayName: "notificationActions")
static let threadActions = MatomoUtils.EventCategory(displayName: "threadActions")
static let swipeActions = MatomoUtils.EventCategory(displayName: "swipeActions")
static let menuAction = MatomoUtils.EventCategory(displayName: "menuAction")

// Settings

Expand Down
2 changes: 2 additions & 0 deletions MailResources/Mail.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
<true/>
<key>com.apple.security.network.client</key>
<true/>
<key>com.apple.security.print</key>
<true/>
<key>com.apple.security.personal-information.addressbook</key>
<true/>
<key>com.apple.security.personal-information.photos-library</key>
Expand Down

0 comments on commit 9209669

Please sign in to comment.