Skip to content

Commit

Permalink
integrate AlertToast for notification
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj committed Nov 4, 2024
1 parent 77ec5b9 commit 9a603d3
Show file tree
Hide file tree
Showing 18 changed files with 172 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
[submodule "data/rime-stroke"]
path = data/rime-stroke
url = https://github.com/rime/rime-stroke
[submodule "deps/AlertToast"]
path = deps/AlertToast
url = https://github.com/fcitx-contrib/AlertToast
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ find_package(LibIntl)
set(fmt_DIR "${PREBUILDER_LIB_DIR}/cmake/fmt")
find_package(fmt)

add_subdirectory(deps)

option(ENABLE_TEST "" OFF)
option(ENABLE_COVERAGE "" OFF)
option(ENABLE_ENCHANT "" OFF)
Expand All @@ -63,6 +65,7 @@ add_subdirectory(fcitx5)

add_subdirectory(protocol)
add_subdirectory(iosfrontend)
add_subdirectory(iosnotifications)
add_subdirectory(uipanel)
add_subdirectory(ipc)

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ You can also use `Cmd+Shift+B` in VSCode to execute a task.
```sh
xcrun simctl list devices
xcrun simctl boot UUID
open /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app
xcrun simctl install booted build/src/Debug-iphonesimulator/Fcitx5.app
```
After the first time you execute `xcrun simctl install`,
Expand Down
3 changes: 3 additions & 0 deletions common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
add_library(FcitxCommon common.cpp)
target_include_directories(FcitxCommon PRIVATE
"${PROJECT_SOURCE_DIR}/fcitx5/src/modules/notifications"
)
target_link_libraries(FcitxCommon Fcitx5::Core)
16 changes: 10 additions & 6 deletions common/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "../engines/fcitx5-rime/src/rimeengine.h"
#include "../fcitx5/src/modules/spell/spell.h"
#include "../iosfrontend/iosfrontend.h"
#include "../iosnotifications/iosnotifications.h"
#include "../uipanel/uipanel.h"
#include "nativestreambuf.h"
#include <filesystem>
Expand All @@ -19,16 +20,19 @@ fcitx::RimeEngineFactory RimeFactory;

namespace fs = std::filesystem;

fcitx::IosFrontendFactory IosFrontendFactory;
fcitx::UIPanelFactory UIPanelFactory;
static fcitx::IosFrontendFactory IosFrontendFactory;
static fcitx::IosNotificationsFactory IosNotificationsFactory;
static fcitx::UIPanelFactory UIPanelFactory;

fcitx::StaticAddonRegistry addons = {
static fcitx::StaticAddonRegistry addons = {
#ifdef HALLELUJAH
std::make_pair<std::string, fcitx::AddonFactory *>("spell",
&SpellModuleFactory),
#endif
std::make_pair<std::string, fcitx::AddonFactory *>("iosfrontend",
&IosFrontendFactory),
std::make_pair<std::string, fcitx::AddonFactory *>(
"notifications", &IosNotificationsFactory),
std::make_pair<std::string, fcitx::AddonFactory *>("uipanel",
&UIPanelFactory),
#ifdef HALLELUJAH
Expand All @@ -43,10 +47,10 @@ fcitx::StaticAddonRegistry addons = {
std::unique_ptr<fcitx::Instance> instance;
std::unique_ptr<fcitx::EventDispatcher> dispatcher;

native_streambuf log_streambuf;
std::ostream stream(&log_streambuf);
static native_streambuf log_streambuf;
static std::ostream stream(&log_streambuf);

std::thread fcitx_thread;
static std::thread fcitx_thread;

void setupLog() {
fcitx::Log::setLogStream(stream);
Expand Down
1 change: 1 addition & 0 deletions deps/AlertToast
Submodule AlertToast added at 6e902e
1 change: 1 addition & 0 deletions deps/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(AlertToast)
30 changes: 30 additions & 0 deletions iosnotifications/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
add_library(NotifySwift STATIC notify.swift toast.swift)
set_target_properties(NotifySwift PROPERTIES Swift_MODULE_NAME NotifySwift)
target_compile_options(NotifySwift PUBLIC "$<$<COMPILE_LANGUAGE:Swift>:-cxx-interoperability-mode=default>")

_swift_generate_cxx_header(
NotifySwift
"${CMAKE_CURRENT_BINARY_DIR}/include/notify-swift.h"
SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/notify.swift"
SEARCH_PATHS "${CMAKE_CURRENT_SOURCE_DIR}"
)

add_library(iosnotifications iosnotifications.cpp)
add_dependencies(iosnotifications NotifySwift)
target_include_directories(iosnotifications PUBLIC
"${CMAKE_CURRENT_BINARY_DIR}/include"
)
target_link_libraries(iosnotifications Fcitx5::Core Fcitx5::Module::Notifications)

configure_file(notifications.conf.in.in notifications.conf.in @ONLY)
fcitx5_translate_desktop_file(${CMAKE_CURRENT_BINARY_DIR}/notifications.conf.in notifications.conf)

add_custom_command(
TARGET iosnotifications
POST_BUILD COMMAND /bin/sh -c
\"
${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_BINARY_DIR}/notifications.conf
${PROJECT_BINARY_DIR}/keyboard/$<CONFIG>${CMAKE_XCODE_EFFECTIVE_PLATFORMS}/keyboard.appex/share/fcitx5/addon/notifications.conf
\"
)
29 changes: 29 additions & 0 deletions iosnotifications/iosnotifications.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "iosnotifications.h"
#include "notify-swift.h"

namespace fcitx {
Notifications::Notifications(Instance *instance) {}

uint32_t Notifications::sendNotification(
const std::string &appName, uint32_t replaceId, const std::string &appIcon,
const std::string &summary, const std::string &body,
const std::vector<std::string> &actions, int32_t timeout,
NotificationActionCallback actionCallback,
NotificationClosedCallback closedCallback) {

FCITX_INFO() << "sendNotification " << body;
return 0;
}

void Notifications::showTip(const std::string &tipId,
const std::string &appName,
const std::string &appIcon,
const std::string &summary, const std::string &body,
int32_t timeout) {
NotifySwift::showTip(body, timeout);
}

void Notifications::closeNotification(uint64_t internalId) {
FCITX_INFO() << "closeNotification " << internalId;
}
} // namespace fcitx
39 changes: 39 additions & 0 deletions iosnotifications/iosnotifications.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <fcitx/addonfactory.h>
#include <fcitx/addoninstance.h>
#include <fcitx/addonmanager.h>
#include <fcitx/instance.h>
#include <notifications_public.h>

namespace fcitx {
class Notifications final : public AddonInstance {
public:
Notifications(Instance *instance);
~Notifications() = default;
uint32_t sendNotification(const std::string &appName, uint32_t replaceId,
const std::string &appIcon,
const std::string &summary,
const std::string &body,
const std::vector<std::string> &actions,
int32_t timeout,
NotificationActionCallback actionCallback,
NotificationClosedCallback closedCallback);
void showTip(const std::string &tipId, const std::string &appName,
const std::string &appIcon, const std::string &summary,
const std::string &body, int32_t timeout);

void closeNotification(uint64_t internalId);

private:
FCITX_ADDON_EXPORT_FUNCTION(Notifications, sendNotification);
FCITX_ADDON_EXPORT_FUNCTION(Notifications, showTip);
FCITX_ADDON_EXPORT_FUNCTION(Notifications, closeNotification);
};

class IosNotificationsFactory : public AddonFactory {
AddonInstance *create(AddonManager *manager) override {
return new Notifications(manager->instance());
}
};
} // namespace fcitx
7 changes: 7 additions & 0 deletions iosnotifications/notifications.conf.in.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[Addon]
Name=iOS Notification
Type=StaticLibrary
Library=libiosnotifications
Category=Module
Version=@PROJECT_VERSION@
Configurable=True
6 changes: 6 additions & 0 deletions iosnotifications/notify.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public func showTip(_ body: String, _ timeout: Int32) {
guard let showToastCallback = showToastCallback else {
return
}
showToastCallback(body, timeout)
}
5 changes: 5 additions & 0 deletions iosnotifications/toast.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var showToastCallback: ((String, Int32) -> Void)?

public func setShowToastCallback(_ callback: @escaping (String, Int32) -> Void) {
showToastCallback = callback
}
2 changes: 2 additions & 0 deletions keyboard/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ target_link_libraries(keyboard PRIVATE
SwiftFrontend
KeyboardUI
uipanel
iosnotifications
NotifySwift
FcitxIpc
FcitxCommon
${ADDONS}
Expand Down
4 changes: 2 additions & 2 deletions scripts/format.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
find common src keyboard iosfrontend uipanel -name '*.cpp' -o -name '*.h' | xargs clang-format -i
swift-format format --in-place $(find src keyboard iosfrontend uipanel protocol ipc -name '*.swift')
find common src keyboard iosfrontend iosnotifications uipanel -name '*.cpp' -o -name '*.h' | xargs clang-format -i
swift-format format --in-place $(find src keyboard iosfrontend iosnotifications uipanel protocol ipc -name '*.swift')
4 changes: 2 additions & 2 deletions scripts/lint.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set -e

find common src keyboard iosfrontend uipanel -name '*.cpp' -o -name '*.h' | xargs clang-format -Werror --dry-run
swift-format lint -rs src keyboard iosfrontend uipanel protocol ipc
find common src keyboard iosfrontend iosnotifications uipanel -name '*.cpp' -o -name '*.h' | xargs clang-format -Werror --dry-run
swift-format lint -rs src keyboard iosfrontend iosnotifications uipanel protocol ipc
9 changes: 8 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@ add_executable(
fcitx.cpp
)

target_include_directories(${BUNDLE_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
target_include_directories(${BUNDLE_NAME} PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}"
"${PROJECT_BINARY_DIR}/iosnotifications/$<CONFIG>${CMAKE_XCODE_EFFECTIVE_PLATFORMS}"
"${PROJECT_BINARY_DIR}/deps/AlertToast/$<CONFIG>${CMAKE_XCODE_EFFECTIVE_PLATFORMS}"
)

target_link_libraries(${BUNDLE_NAME} PRIVATE
AlertToast
iosfrontend
SwiftFrontend
KeyboardUI
uipanel
iosnotifications
NotifySwift
FcitxIpc
FcitxCommon
${ADDONS}
Expand Down
20 changes: 20 additions & 0 deletions src/ContentView.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import AlertToast
import Fcitx
import NotifySwift
import SwiftUI

private class ViewModel: ObservableObject {
Expand All @@ -8,6 +10,9 @@ private class ViewModel: ObservableObject {
struct ContentView: View {
@Environment(\.scenePhase) private var scenePhase
@ObservedObject private var viewModel = ViewModel()
@State private var showToast = false
@State private var duration = 3.0
@State private var message = ""

func handleURL(_ url: URL) {
viewModel.url = url
Expand All @@ -24,10 +29,25 @@ struct ContentView: View {
}
}
.padding()
.toast(isPresenting: $showToast, duration: duration) {
AlertToast(
displayMode: .alert, type: .regular,
subTitle: message,
style: AlertToast.AlertStyle.style(
subTitleFont: Font.system(size: 20)
))
}
.onAppear {
// The stupid iOS doesn't show empty directory in Files.app.
try? "".write(
to: documents.appendingPathComponent("placeholder"), atomically: true, encoding: .utf8)
setShowToastCallback({ message, duration in
DispatchQueue.main.async {
self.duration = Double(duration) / 1000.0
self.message = message
showToast = true
}
})
}
.onChange(of: scenePhase) { newPhase in
if newPhase == .active {
Expand Down

0 comments on commit 9a603d3

Please sign in to comment.