Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clipboard #165

Merged
merged 10 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions assets/po/zh_CN.po
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,26 @@ msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: macosfrontend/macosfrontend.h:42
#: macosfrontend/macosfrontend.h:43
msgid "App default IM"
msgstr "应用默认输入法"

#: macosfrontend/macosfrontend.h:44
#: macosfrontend/macosfrontend.h:45
msgid "Simulate key release"
msgstr "模拟按键释放"

#: macosfrontend/macosfrontend.h:47
#: macosfrontend/macosfrontend.h:48
msgid "Delay of simulated key release in milliseconds"
msgstr "模拟按键释放延迟毫秒数"

#: macosfrontend/macosfrontend.h:51
msgid "Monitor Pasteboard"
msgstr "监视剪贴板"

#: macosfrontend/macosfrontend.h:53
msgid "Poll Pasteboard interval (s)"
msgstr "轮询剪贴板的间隔(秒)"

#: macosnotifications/macosnotifications.h:20
msgid "Hidden Notifications"
msgstr "隐藏通知"
Expand Down
2 changes: 1 addition & 1 deletion fcitx5-webview
30 changes: 30 additions & 0 deletions macosfrontend/macosfrontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <fcitx/inputcontext.h>
#include <fcitx/inputpanel.h>

#include "../fcitx5/src/modules/clipboard/clipboard_public.h"

namespace fcitx {

MacosFrontend::MacosFrontend(Instance *instance)
Expand All @@ -24,10 +26,38 @@ MacosFrontend::MacosFrontend(Instance *instance)
reloadConfig();
}

// Runs on the fcitx thread.
void MacosFrontend::pollPasteboard() {
monitorPasteboardEvent_ = instance_->eventLoop().addTimeEvent(
CLOCK_MONOTONIC,
now(CLOCK_MONOTONIC) + *config_.pollPasteboardInterval * 1000000,
100000, [this](EventSourceTime *time, uint64_t) {
if (!*config_.monitorPasteboard) {
return true;
}
if (auto clipboard =
instance_->addonManager().addon("clipboard", true)) {
bool isPassword = false;
const char *p = SwiftFrontend::getPasteboardString(&isPassword);
if (p) {
std::string str = p;
clipboard->call<IClipboard::setClipboardV2>("", str,
isPassword);
FCITX_DEBUG() << "Add to clipboard: " << str;
}
}
time->setNextInterval(*config_.pollPasteboardInterval * 1000000);
time->setOneShot();
return true;
});
monitorPasteboardEvent_->setOneShot();
}

void MacosFrontend::updateConfig() {
simulateKeyRelease_ = config_.simulateKeyRelease.value();
simulateKeyReleaseDelay_ =
static_cast<long>(config_.simulateKeyReleaseDelay.value()) * 1000L;
pollPasteboard();
}

void MacosFrontend::reloadConfig() {
Expand Down
10 changes: 9 additions & 1 deletion macosfrontend/macosfrontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <fcitx-config/configuration.h>
#include <fcitx-config/iniparser.h>
#include <fcitx-utils/event.h>
#include <fcitx-utils/i18n.h>
#include <fcitx/addonfactory.h>
#include <fcitx/addoninstance.h>
Expand Down Expand Up @@ -45,7 +46,12 @@ FCITX_CONFIGURATION(
Option<int, IntConstrain> simulateKeyReleaseDelay{
this, "SimulateKeyReleaseDelay",
_("Delay of simulated key release in milliseconds"), 100,
IntConstrain(10, 1500)};);
IntConstrain(10, 1500)};
Option<bool> monitorPasteboard{this, "MonitorPasteboard",
_("Monitor Pasteboard"), false};
Option<int, IntConstrain> pollPasteboardInterval{
this, "PollPasteboardInterval", _("Poll Pasteboard interval (s)"), 2,
IntConstrain(1, 60)};);

class MacosFrontend : public AddonInstance {
public:
Expand Down Expand Up @@ -75,6 +81,8 @@ class MacosFrontend : public AddonInstance {
MacosFrontendConfig config_;
bool simulateKeyRelease_;
long simulateKeyReleaseDelay_;
std::unique_ptr<EventSourceTime> monitorPasteboardEvent_;
void pollPasteboard();

static const inline std::string ConfPath = "conf/macosfrontend.conf";

Expand Down
20 changes: 20 additions & 0 deletions macosfrontend/macosfrontend.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import AppKit
import InputMethodKit

private var u16pos = 0
Expand Down Expand Up @@ -81,3 +82,22 @@ 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<Bool>) -> UnsafePointer<CChar>? {
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
}
7 changes: 4 additions & 3 deletions webpanel/webpanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,12 @@ void WebPanel::update(UserInterfaceComponent component,
scrollState_ = candidate_window::scroll_state_t::none;
}
window_->set_paging_buttons(pageable, hasPrev, hasNext);
window_->set_layout(layout);
window_->set_writing_mode(writingMode);
// Must be called after set_layout and set_writing_mode so that proper
// states are read after set.
window_->set_candidates(candidates, highlighted, scrollState_, false,
false);
window_->set_layout(layout);
updatePanelShowFlags(!candidates.empty(), PanelShowFlag::HasCandidates);
updateClient(inputContext);
showAsync(panelShow_);
Expand Down Expand Up @@ -358,8 +360,7 @@ void WebPanel::updateInputPanel(const Text &preedit, const Text &auxUp,

void WebPanel::updateClient(InputContext *ic) {
if (auto macosIC = dynamic_cast<MacosInputContext *>(ic)) {
macosIC->setDummyPreedit((panelShow_ & PanelShowFlag::HasPreedit) |
(panelShow_ & PanelShowFlag::HasCandidates));
macosIC->setDummyPreedit(bool(panelShow_));
if (!macosIC->isSyncEvent) {
macosIC->commitAndSetPreeditAsync();
}
Expand Down