Skip to content

Commit

Permalink
refactor: preview
Browse files Browse the repository at this point in the history
  • Loading branch information
ksqsf committed Jul 8, 2024
1 parent aefd6df commit 3684417
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 39 deletions.
16 changes: 5 additions & 11 deletions preview/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
add_executable(preview preview.cpp)
target_link_libraries(preview WebviewCandidateWindow)
target_include_directories(preview PRIVATE "${PROJECT_SOURCE_DIR}/include")

if(APPLE)
add_executable(preview
MACOSX_BUNDLE
preview.mm
)
target_link_libraries(preview WebviewCandidateWindow "-framework WebKit" "-framework Cocoa")
target_compile_options(preview PRIVATE "-Wno-auto-var-id")
elseif(LINUX)
add_executable(preview preview.cpp)
target_link_libraries(preview WebviewCandidateWindow)
target_compile_options(preview PRIVATE "-Wno-auto-var-id" "-ObjC++")
endif()

target_include_directories(preview PRIVATE "${PROJECT_SOURCE_DIR}/include")

add_dependencies(preview GenerateHTML)
55 changes: 29 additions & 26 deletions preview/preview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,42 @@
#include <iostream>
#include <thread>

std::unique_ptr<candidate_window::CandidateWindow> candidateWindow;

void doPreview() {
candidateWindow = std::make_unique<candidate_window::WebviewCandidateWindow>();
candidateWindow->set_select_callback(
[](int index) { std::cout << "selected " << index << std::endl; });
candidateWindow->set_page_callback([](bool next) {
std::cout << (next ? "next" : "prev") << " page" << std::endl;
});
candidateWindow->set_action_callback([](int index, int id) {
std::cout << "action " << id << " on " << index << std::endl;
});
candidateWindow->set_init_callback([=]() {
std::cout << "Window loaded" << std::endl;
candidateWindow->set_layout(candidate_window::layout_t::vertical);
candidateWindow->set_paging_buttons(true, false, true);
candidateWindow->set_candidates(
{{"<h1>防注入</h1>", "1", "注释", {{0, "<h1>防注入</h1>"}}},
{"候选词", "2", "", {{1, "删词"}, {2, "置顶"}}},
{"\t\t\n多 空 格", "2", ""}},
0, candidate_window::scroll_state_t::none, false, false);
candidateWindow->set_theme(candidate_window::theme_t::light);
candidateWindow->show(100, 200);
});
}

int main(int argc, char *argv[]) {
#ifdef __APPLE__
@autoreleasepool {
NSApplication *application = [NSApplication sharedApplication];
#endif
std::unique_ptr<candidate_window::CandidateWindow> candidateWindow =
std::make_unique<candidate_window::WebviewCandidateWindow>();
candidateWindow->set_select_callback(
[](int index) { std::cout << "selected " << index << std::endl; });
candidateWindow->set_init_callback(
[]() { std::cout << "Window loaded" << std::endl; });
candidateWindow->set_page_callback([](bool next) {
std::cout << (next ? "next" : "prev") << " page" << std::endl;
});
candidateWindow->set_action_callback([](int index, int id) {
std::cout << "action " << id << " on " << index << std::endl;
});
auto t = std::thread([&] {
std::this_thread::sleep_for(std::chrono::seconds(1));
candidateWindow->set_layout(candidate_window::layout_t::vertical);
candidateWindow->set_paging_buttons(true, false, true);
candidateWindow->set_candidates(
{{"<h1>防注入</h1>", "1", "注释", {{0, "<h1>防注入</h1>"}}},
{"候选词", "2", "", {{1, "删词"}, {2, "置顶"}}},
{"\t\t\n多 空 格", "2", ""}},
0, candidate_window::scroll_state_t::none, false, false);
candidateWindow->set_theme(candidate_window::theme_t::light);
candidateWindow->show(100, 200);
});
#ifdef __APPLE__
doPreview();
[application run];
}
#elif defined(__linux__)
gtk_init(&argc, &argv);
doPreview()
gtk_main();
#endif
return 0;
Expand Down
1 change: 0 additions & 1 deletion preview/preview.mm

This file was deleted.

2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ add_library(WebviewCandidateWindow ${SOURCES})
target_include_directories(WebviewCandidateWindow PUBLIC "${PROJECT_SOURCE_DIR}/include" "${PROJECT_SOURCE_DIR}/webview")

if(APPLE)
target_link_libraries(WebviewCandidateWindow "-framework WebKit" PkgConfig::NlohmannJson)
target_link_libraries(WebviewCandidateWindow PUBLIC "-framework Cocoa" "-framework WebKit" PkgConfig::NlohmannJson)
target_compile_options(WebviewCandidateWindow PRIVATE "-Wno-auto-var-id")
elseif(LINUX)
target_link_libraries(WebviewCandidateWindow PkgConfig::webkit2gtk PkgConfig::NlohmannJson)
Expand Down

0 comments on commit 3684417

Please sign in to comment.