diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2b1cdeb..716ba89 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,6 +21,7 @@ jobs: - name: Lint run: | find macosfrontend macosnotifications webpanel src tests -name '*.cpp' -o -name '*.h' | xargs clang-format -Werror --dry-run -style=file:fcitx5/.clang-format + clang-format -Werror --dry-run macosfrontend/pasteboard.mm swift-format lint --configuration .swift-format.json -rs macosfrontend macosnotifications src assets ./check-code-style.sh file assets/zh-Hans.lproj/Localizable.strings | grep UTF-16 diff --git a/macosfrontend/CMakeLists.txt b/macosfrontend/CMakeLists.txt index d9dd638..37abda0 100644 --- a/macosfrontend/CMakeLists.txt +++ b/macosfrontend/CMakeLists.txt @@ -10,7 +10,7 @@ _swift_generate_cxx_header( SEARCH_PATHS "${CMAKE_CURRENT_SOURCE_DIR}" ) -add_library(macosfrontend STATIC macosfrontend.cpp keycode.cpp) +add_library(macosfrontend STATIC macosfrontend.cpp pasteboard.mm keycode.cpp) add_dependencies(macosfrontend SwiftFrontend) target_link_libraries(macosfrontend Fcitx5::Core) target_include_directories(macosfrontend PUBLIC diff --git a/macosfrontend/macosfrontend.cpp b/macosfrontend/macosfrontend.cpp index ba68354..556561c 100644 --- a/macosfrontend/macosfrontend.cpp +++ b/macosfrontend/macosfrontend.cpp @@ -38,9 +38,8 @@ void MacosFrontend::pollPasteboard() { if (auto clipboard = instance_->addonManager().addon("clipboard", true)) { bool isPassword = false; - const char *p = SwiftFrontend::getPasteboardString(&isPassword); - if (p) { - std::string str = p; + std::string str = getPasteboardString(&isPassword); + if (!str.empty()) { clipboard->call("", str, isPassword); FCITX_DEBUG() << "Add to clipboard: " << str; diff --git a/macosfrontend/macosfrontend.h b/macosfrontend/macosfrontend.h index 2b7c243..54f3014 100644 --- a/macosfrontend/macosfrontend.h +++ b/macosfrontend/macosfrontend.h @@ -145,6 +145,8 @@ class MacosFrontendFactory : public AddonFactory { } }; +std::string getPasteboardString(bool *isPassword); + } // namespace fcitx #endif diff --git a/macosfrontend/macosfrontend.swift b/macosfrontend/macosfrontend.swift index dc9ad25..aa6368b 100644 --- a/macosfrontend/macosfrontend.swift +++ b/macosfrontend/macosfrontend.swift @@ -82,22 +82,3 @@ public func getCursorCoordinates( } return false } - -private var lastChangeCount = 0 -private var data: Data? -private let passwordType = NSPasteboard.PasteboardType("org.nspasteboard.ConcealedType") - -// Retrieve the current string data from the default pasteboard. -public func getPasteboardString(_ isPassword: UnsafeMutablePointer) -> UnsafePointer? { - let pasteboard = NSPasteboard.general - if pasteboard.changeCount == lastChangeCount { - return nil - } - lastChangeCount = pasteboard.changeCount - data = pasteboard.data(forType: .string) - if let data = data { - isPassword.pointee = pasteboard.string(forType: passwordType) != nil - return data.withUnsafeBytes { $0.baseAddress?.assumingMemoryBound(to: CChar.self) } - } - return nil -} diff --git a/macosfrontend/pasteboard.mm b/macosfrontend/pasteboard.mm new file mode 100644 index 0000000..3d3902a --- /dev/null +++ b/macosfrontend/pasteboard.mm @@ -0,0 +1,21 @@ +#include "macosfrontend.h" +#import + +static int lastChangeCount = 0; +static NSPasteboardType passwordType = @"org.nspasteboard.ConcealedType"; + +namespace fcitx { +std::string getPasteboardString(bool *isPassword) { + NSPasteboard *pasteboard = [NSPasteboard generalPasteboard]; + if (pasteboard.changeCount == lastChangeCount) { + return ""; + } + lastChangeCount = (int)pasteboard.changeCount; + NSString *stringData = [pasteboard stringForType:NSPasteboardTypeString]; + if (stringData) { + *isPassword = ([pasteboard stringForType:passwordType] != nil); + return std::string([stringData UTF8String]); + } + return ""; +} +} // namespace fcitx