Skip to content

Commit

Permalink
submenus
Browse files Browse the repository at this point in the history
  • Loading branch information
FaiChou committed Dec 1, 2022
1 parent 22683a3 commit 138fa60
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 16 deletions.
1 change: 1 addition & 0 deletions en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@
"Open System Preferences" = "Open System Preferences";
"Image" = "Image";
"File" = "File";
"More" = "More";
4 changes: 4 additions & 0 deletions uCopy.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
4D6125042934A19E0044E63C /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 4D6125062934A19E0044E63C /* Localizable.strings */; };
4D61250A2934AAB10044E63C /* KeyboardShortcuts in Frameworks */ = {isa = PBXBuildFile; productRef = 4D6125092934AAB10044E63C /* KeyboardShortcuts */; };
4D805D8829321A25009F4F52 /* SnippetSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D805D8729321A25009F4F52 /* SnippetSettingsView.swift */; };
4D8263952938DDA7008A858F /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4D8263942938DDA7008A858F /* Constants.swift */; };
4DCB3410291D233B00E3B3CA /* uCopy.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 4DCB340E291D233B00E3B3CA /* uCopy.xcdatamodeld */; };
4DCB3412291D23F300E3B3CA /* DataController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DCB3411291D23F300E3B3CA /* DataController.swift */; };
4DCB3437291E345C00E3B3CA /* HistoryView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4DCB3436291E345C00E3B3CA /* HistoryView.swift */; };
Expand All @@ -41,6 +42,7 @@
4D6125072934A1A20044E63C /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
4D61250B2934BAD10044E63C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
4D805D8729321A25009F4F52 /* SnippetSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SnippetSettingsView.swift; sourceTree = "<group>"; };
4D8263942938DDA7008A858F /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
4DCB340F291D233B00E3B3CA /* uCopy.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = uCopy.xcdatamodel; sourceTree = "<group>"; };
4DCB3411291D23F300E3B3CA /* DataController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataController.swift; sourceTree = "<group>"; };
4DCB3436291E345C00E3B3CA /* HistoryView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HistoryView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -101,6 +103,7 @@
4DCB343C2924B4BA00E3B3CA /* GeneralSettingsView.swift */,
4DCB3436291E345C00E3B3CA /* HistoryView.swift */,
4DCB344A292E211500E3B3CA /* CoreDataHelper.swift */,
4D8263942938DDA7008A858F /* Constants.swift */,
4DCB3446292E0F1000E3B3CA /* AccessibilityService.swift */,
4DCB3442292B757C00E3B3CA /* UtilityExtensions.swift */,
4DCB3411291D23F300E3B3CA /* DataController.swift */,
Expand Down Expand Up @@ -202,6 +205,7 @@
buildActionMask = 2147483647;
files = (
4DCB344D292F1EFE00E3B3CA /* MenuManager.swift in Sources */,
4D8263952938DDA7008A858F /* Constants.swift in Sources */,
4DCB343B292228FF00E3B3CA /* PasteboardMonitor.swift in Sources */,
4DE9C4A62915FD0E002D6A34 /* Main.swift in Sources */,
4D805D8829321A25009F4F52 /* SnippetSettingsView.swift in Sources */,
Expand Down
11 changes: 11 additions & 0 deletions uCopy/Constants.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// Constants.swift
// uCopy
//
// Created by 周辉 on 2022/12/1.
//

import Foundation

let DEFAULT_MENU_BAR_PAGE_SIZE = 20
let DEFAULT_MENU_PAGE_SIZE = 40
2 changes: 1 addition & 1 deletion uCopy/HistoryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import SwiftUI
import CoreData

struct HistoryView: View {
@FetchRequest(fetchRequest: CoreDataHelper.historyFetchRequestWithLimit(size: 20)) var history: FetchedResults<History>
@FetchRequest(fetchRequest: CoreDataHelper.historyFetchRequestWithLimit(size: DEFAULT_MENU_BAR_PAGE_SIZE)) var history: FetchedResults<History>
@Environment(\.managedObjectContext) var context
@State private var refreshID = UUID()

Expand Down
69 changes: 54 additions & 15 deletions uCopy/MenuManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,60 @@ class MenuManager {
labelItem.isEnabled = false
historyMenu!.addItem(labelItem)
do {
historyResults = try moc?.fetch(CoreDataHelper.historyFetchRequestWithLimit(size: 20))
for (index, item) in historyResults!.enumerated() {
let string = item.title ?? ""
let tooltipString = "\(string)\n\n" + "\(item.source!) \(item.createDate!)"
let title = "\(index+1). \(string.trimingLeadingSpaces().trunc(length: 30))"
let key = index < 9 ? String(index+1) : ""
let menuItem = NSMenuItem(
title: title,
action: #selector(paste),
keyEquivalent: key
)
menuItem.target = MenuManager.self
menuItem.representedObject = item
menuItem.toolTip = tooltipString // we should use the original formateed string
historyMenu!.addItem(menuItem)
historyResults = try moc?.fetch(CoreDataHelper.historyFetchRequestWithLimit(size: DEFAULT_MENU_PAGE_SIZE))
if historyResults!.count > DEFAULT_MENU_BAR_PAGE_SIZE {
// we need split them to submenu
let prefixResults = historyResults![0..<DEFAULT_MENU_BAR_PAGE_SIZE]
let suffixResults = historyResults![DEFAULT_MENU_BAR_PAGE_SIZE...]
for (index, item) in prefixResults.enumerated() {
let string = item.title ?? ""
let tooltipString = "\(string)\n\n" + "\(item.source!) \(item.createDate!)"
let title = "\(index+1). \(string.trimingLeadingSpaces().trunc(length: 30))"
let key = index < 9 ? String(index+1) : ""
let menuItem = NSMenuItem(
title: title,
action: #selector(paste),
keyEquivalent: key
)
menuItem.target = MenuManager.self
menuItem.representedObject = item
menuItem.toolTip = tooltipString // we should use the original formateed string
historyMenu!.addItem(menuItem)
}
let subMenuItem = NSMenuItem(title: "More", action: nil, keyEquivalent: "")
historyMenu!.addItem(subMenuItem)
let subMenu = NSMenu(title: "Submenu")
subMenuItem.submenu = subMenu
for (index, item) in suffixResults.enumerated() {
let string = item.title ?? ""
let tooltipString = "\(string)\n\n" + "\(item.source!) \(item.createDate!)"
let title = "\(index+1). \(string.trimingLeadingSpaces().trunc(length: 30))"
let menuItem = NSMenuItem(
title: title,
action: #selector(paste),
keyEquivalent: ""
)
menuItem.target = MenuManager.self
menuItem.representedObject = item
menuItem.toolTip = tooltipString // we should use the original formateed string
subMenu.addItem(menuItem)
}
} else {
for (index, item) in historyResults!.enumerated() {
let string = item.title ?? ""
let tooltipString = "\(string)\n\n" + "\(item.source!) \(item.createDate!)"
let title = "\(index+1). \(string.trimingLeadingSpaces().trunc(length: 30))"
let key = index < 9 ? String(index+1) : ""
let menuItem = NSMenuItem(
title: title,
action: #selector(paste),
keyEquivalent: key
)
menuItem.target = MenuManager.self
menuItem.representedObject = item
menuItem.toolTip = tooltipString // we should use the original formateed string
historyMenu!.addItem(menuItem)
}
}
} catch {
print("Something wrong!")
Expand Down
1 change: 1 addition & 0 deletions zh-Hans.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@
"Open System Preferences" = "打开设置";
"Image" = "图片";
"File" = "文件";
"More" = "更多";

0 comments on commit 138fa60

Please sign in to comment.