Skip to content

Commit

Permalink
support open fcitx:// scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj committed Nov 1, 2024
1 parent 91418ea commit f7eb56b
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ jobs:
git apply --directory=engines/fcitx5-hallelujah patches/hallelujah.patch
git apply --directory=engines/fcitx5-rime patches/rime.patch
cmake -B build -G Xcode \
-DURL_SCHEME=fcitx \
-DHALLELUJAH=ON \
-DRIME=ON \
-DCMAKE_TOOLCHAIN_FILE=cmake/ios.cmake \
Expand Down
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{
"type": "shell",
"label": "Configure",
"command": "cmake -B build -G Xcode -DHALLELUJAH=ON -DRIME=ON -DCMAKE_TOOLCHAIN_FILE=cmake/ios.cmake -DIOS_PLATFORM=SIMULATOR",
"command": "cmake -B build -G Xcode -DHALLELUJAH=ON -DURL_SCHEME=fcitx -DRIME=ON -DCMAKE_TOOLCHAIN_FILE=cmake/ios.cmake -DIOS_PLATFORM=SIMULATOR",
"group": {
"kind": "build"
}
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ else()
set(F5I_TARGET "${CMAKE_OSX_ARCHITECTURES}-apple-ios${CMAKE_OSX_DEPLOYMENT_TARGET}")
endif()

if (NOT URL_SCHEME)
message(FATAL_ERROR "Must provide URL_SCHEME")
endif()

include(InitializeSwift)
include(AddSwift)

Expand Down
30 changes: 30 additions & 0 deletions cmake/MacOSXBundleInfo.plist.in
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,36 @@
<string>@BUNDLE_NAME@</string>
<key>CFBundleIconFile</key>
<string>@ICON_FILE@</string>
<!-- Register fcitx:// scheme. -->
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>@BUNDLE_IDENTIFIER@</string>
<key>CFBundleURLSchemes</key>
<array>
<string>@URL_SCHEME@</string>
</array>
</dict>
</array>
<!-- Recognize fcitx:// scheme. -->
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
<false/>
<key>UISceneConfigurations</key>
<dict>
<key>UIWindowSceneSessionRoleApplication</key>
<array>
<dict>
<key>UISceneConfigurationName</key>
<string>Default Configuration</string>
<key>UISceneDelegateClassName</key>
<string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
</dict>
</array>
</dict>
</dict>
<!-- A random string makes it fullscreen. -->
<key>UILaunchScreen</key>
<string>foo</string>
Expand Down
8 changes: 7 additions & 1 deletion src/App.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import OSLog
import SwiftUI

let logger = Logger(subsystem: "org.fcitx.Fcitx5", category: "FcitxLog")

@main
struct Fcitx5App: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

// Dummy. Actual Scene in managed by SceneDelegate.
var body: some Scene {
WindowGroup {
ContentView()
EmptyView()
}
}
}
23 changes: 23 additions & 0 deletions src/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import UIKit

class AppDelegate: NSObject, UIApplicationDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
return true
}

func application(
_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession,
options: UIScene.ConnectionOptions
) -> UISceneConfiguration {
return UISceneConfiguration(
name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

func application(
_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>
) {
}
}
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ add_executable(
ContentView.swift
App.swift
util.swift
AppDelegate.swift
SceneDelegate.swift
)

add_custom_command(
Expand Down
13 changes: 12 additions & 1 deletion src/ContentView.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import SwiftUI

private class ViewModel: ObservableObject {
@Published var url: URL?
}

struct ContentView: View {
@Environment(\.scenePhase) private var scenePhase
@ObservedObject private var viewModel = ViewModel()

func handleURL(_ url: URL) {
viewModel.url = url
}

var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
if let url = viewModel.url {
Text("\(url)")
}
}
.padding()
.onAppear {
Expand Down
25 changes: 25 additions & 0 deletions src/SceneDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import SwiftUI

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
var contentView: ContentView?

func scene(
_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions
) {
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
contentView = ContentView()
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}

func scene(_ scene: UIScene, openURLContexts urlContexts: Set<UIOpenURLContext>) {
guard let url = urlContexts.first?.url else { return }
contentView?.handleURL(url)
}
}

0 comments on commit f7eb56b

Please sign in to comment.