From 49590dbd8f6686d90ce553edabf8b1280ee2ce88 Mon Sep 17 00:00:00 2001 From: Qijia Liu Date: Fri, 8 Nov 2024 23:12:04 -0500 Subject: [PATCH] initialize profile --- CMakeLists.txt | 12 ++++++++++++ common/util.swift | 10 ++++++++++ keyboard/CMakeLists.txt | 11 +++++++++++ keyboard/KeyboardViewController.swift | 1 + scripts/configure.py | 25 +++++++++++++++++++++++++ src/AppDelegate.swift | 1 + src/CMakeLists.txt | 21 ++++++++++----------- 7 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 scripts/configure.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 69ad300..bb42e76 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -74,11 +74,13 @@ option(HALLELUJAH "" OFF) option(RIME "" OFF) set(ADDONS) +set(DEFAULT_INPUT_METHODS) if (HALLELUJAH) add_definitions(-DHALLELUJAH) add_subdirectory(engines/fcitx5-hallelujah) list(APPEND ADDONS hallelujah) + list(APPEND DEFAULT_INPUT_METHODS hallelujah) endif() if (RIME) # RIME_DATA_DIR is not actually used but must exist. @@ -88,8 +90,18 @@ if (RIME) add_definitions(-DRIME) add_subdirectory(engines/fcitx5-rime) list(APPEND ADDONS rime) + list(APPEND DEFAULT_INPUT_METHODS rime) endif() +add_custom_command( + OUTPUT "${PROJECT_BINARY_DIR}/profile" + COMMAND python scripts/configure.py ${DEFAULT_INPUT_METHODS} + DEPENDS "${PROJECT_SOURCE_DIR}/default" + WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" + COMMENT "Generating profile" +) +add_custom_target(gen_profile DEPENDS "${PROJECT_BINARY_DIR}/profile") + if (HALLELUJAH) list(APPEND ADDONS spell) endif() diff --git a/common/util.swift b/common/util.swift index f085ea1..4aefe86 100644 --- a/common/util.swift +++ b/common/util.swift @@ -7,6 +7,7 @@ public let logger = Logger(subsystem: "org.fcitx.Fcitx5", category: "FcitxLog") public let documents = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! public let appGroup = FileManager.default.containerURL( forSecurityApplicationGroupIdentifier: "org.fcitx.Fcitx5")! +public let appGroupConfig = appGroup.appendingPathComponent("config") public let appGroupTmp = appGroup.appendingPathComponent("tmp") public let appGroupData = appGroup.appendingPathComponent("data") @@ -90,3 +91,12 @@ public func removeFile(_ file: URL) -> Bool { return false } } + +// Call on both app and keyboard to initialize input method list after install. +public func initProfile() { + mkdirP(appGroupConfig.path) + let profileURL = appGroupConfig.appendingPathComponent("profile") + if !profileURL.exists() { + try? FileManager.default.copyItem(at: Bundle.main.bundleURL.appendingPathComponent("profile"), to: profileURL) + } +} diff --git a/keyboard/CMakeLists.txt b/keyboard/CMakeLists.txt index 9244ea3..13b3aa8 100644 --- a/keyboard/CMakeLists.txt +++ b/keyboard/CMakeLists.txt @@ -32,3 +32,14 @@ target_link_libraries(keyboard PRIVATE SwiftUtil ${ADDONS} ) + +add_dependencies(keyboard gen_profile) +add_custom_command( + TARGET keyboard + POST_BUILD COMMAND /bin/sh -c + \" + ${CMAKE_COMMAND} -E copy + ${PROJECT_BINARY_DIR}/profile + ${CMAKE_CURRENT_BINARY_DIR}/$${CMAKE_XCODE_EFFECTIVE_PLATFORMS}/keyboard.appex/profile + \" +) diff --git a/keyboard/KeyboardViewController.swift b/keyboard/KeyboardViewController.swift index 92b1c3b..b77aa4a 100644 --- a/keyboard/KeyboardViewController.swift +++ b/keyboard/KeyboardViewController.swift @@ -33,6 +33,7 @@ class KeyboardViewController: UIInputViewController, FcitxProtocol { mainStackView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0), ]) + initProfile() startFcitx(Bundle.main.bundlePath, appGroup.path) // Perform custom UI setup here diff --git a/scripts/configure.py b/scripts/configure.py new file mode 100644 index 0000000..0608d65 --- /dev/null +++ b/scripts/configure.py @@ -0,0 +1,25 @@ +import sys + +PROFILE_HEADER = ''' +[Groups/0] +Name=Default +Default Layout=us +DefaultIM={0} +''' + +PROFILE_ITEM = ''' +[Groups/0/Items/{0}] +Name={1} +Layout= +''' + +PROFILE_TAIL = ''' +[GroupOrder] +0=Default +''' + +with open('build/profile', 'w') as f: + f.write(PROFILE_HEADER.format(sys.argv[1])) + for i in range(len(sys.argv) - 1): + f.write(PROFILE_ITEM.format(i, sys.argv[i + 1])) + f.write(PROFILE_TAIL) diff --git a/src/AppDelegate.swift b/src/AppDelegate.swift index e7dcc64..258d189 100644 --- a/src/AppDelegate.swift +++ b/src/AppDelegate.swift @@ -7,6 +7,7 @@ class AppDelegate: NSObject, UIApplicationDelegate { _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { + initProfile() startFcitx(Bundle.main.bundlePath, appGroup.path) return true } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b2a2855..5278308 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -31,16 +31,6 @@ target_link_libraries(${BUNDLE_NAME} PRIVATE ${ADDONS} ) -add_custom_command( - TARGET ${BUNDLE_NAME} - POST_BUILD COMMAND /bin/sh -c - \" - ${CMAKE_COMMAND} -E copy - ${PROJECT_SOURCE_DIR}/assets/${ICON_FILE} - ${PROJECT_BINARY_DIR}/src/$${CMAKE_XCODE_EFFECTIVE_PLATFORMS}/${BUNDLE_NAME}.app - \" -) - add_dependencies(${BUNDLE_NAME} keyboard) set_target_properties(${BUNDLE_NAME} PROPERTIES @@ -59,7 +49,8 @@ add_custom_command( \" ) -# Embed keyboard.appex in app and copy share directory. +# Embed keyboard.appex in app. +# Copy share directory, icon and profile. add_custom_command( TARGET ${BUNDLE_NAME} POST_BUILD COMMAND /bin/sh -c @@ -71,5 +62,13 @@ add_custom_command( ${CMAKE_COMMAND} -E copy_directory ${PROJECT_BINARY_DIR}/keyboard/$${CMAKE_XCODE_EFFECTIVE_PLATFORMS}/keyboard.appex/share ${PROJECT_BINARY_DIR}/src/$${CMAKE_XCODE_EFFECTIVE_PLATFORMS}/${BUNDLE_NAME}.app/share + \; + ${CMAKE_COMMAND} -E copy + ${PROJECT_SOURCE_DIR}/assets/${ICON_FILE} + ${PROJECT_BINARY_DIR}/src/$${CMAKE_XCODE_EFFECTIVE_PLATFORMS}/${BUNDLE_NAME}.app + \; + ${CMAKE_COMMAND} -E copy + ${PROJECT_BINARY_DIR}/profile + ${PROJECT_BINARY_DIR}/src/$${CMAKE_XCODE_EFFECTIVE_PLATFORMS}/${BUNDLE_NAME}.app/profile \" )