# radial — the radial caret menu.
#
# `radial` (the static lib) is the pure model + menu widget + config loader,
# unit tested headless. `deft_radial` is the Kate plugin ([deft:util] radial);
# it also carries the optional Wayland pointer-warp glue used to follow the
# keyboard focus between split panes after a pane-focus radial action.
add_library(radial STATIC
    radialmodel.cpp
    radialmodel.h
    radialmenu.cpp
    radialmenu.h
    radialconfig.cpp
    radialconfig.h
)
target_link_libraries(radial PUBLIC Qt6::Core Qt6::Gui Qt6::Widgets)
target_include_directories(radial PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

if(Qt6Test_FOUND)
    add_executable(test_radialmodel test_radialmodel.cpp)
    target_link_libraries(test_radialmodel PRIVATE radial Qt6::Test Qt6::Widgets)
    add_test(NAME radialmodel COMMAND test_radialmodel)

    add_executable(test_radialconfig test_radialconfig.cpp)
    target_link_libraries(test_radialconfig PRIVATE radial Qt6::Test Qt6::Widgets)
    add_test(NAME radialconfig COMMAND test_radialconfig)
endif()

# --- [deft:util] radial — the Kate plugin ----------------------------------
if(NOT KF6TextEditor_FOUND)
    return()
endif()

# --- optional Wayland pointer-warp support ---------------------------------
# Warping the pointer on Wayland needs the pointer-constraints protocol glue
# (generated with wayland-scanner via ECM) plus Qt's Wayland native interface.
# If any piece is missing we build without it; WaylandCursorWarp::warp() then
# simply returns false and the X11 path (QCursor::setPos) is used instead.
set(DEFT_WAYLAND OFF)
find_package(PkgConfig QUIET)
find_package(WaylandScanner QUIET)
find_package(Qt6 QUIET COMPONENTS GuiPrivate)
if(PkgConfig_FOUND)
    pkg_check_modules(WAYLAND_CLIENT QUIET wayland-client)
    pkg_check_modules(WAYLAND_PROTOCOLS QUIET wayland-protocols)
endif()

set(RADIAL_PLUGIN_SOURCES radialplugin.cpp radialplugin.h waylandcursorwarp.h)
set(DEFT_WAYLAND_PROTO_SRCS "")

if(WaylandScanner_FOUND AND WAYLAND_CLIENT_FOUND AND Qt6GuiPrivate_FOUND)
    if(WAYLAND_PROTOCOLS_FOUND)
        pkg_get_variable(WAYLAND_PROTOCOLS_DIR wayland-protocols pkgdatadir)
    endif()
    set(PC_XML
        "${WAYLAND_PROTOCOLS_DIR}/unstable/pointer-constraints/pointer-constraints-unstable-v1.xml")
    if(EXISTS "${PC_XML}")
        # ECM wires the scanner output (basename-client-protocol.h + .c) into the
        # given source-list variable with correct generated-source handling.
        ecm_add_wayland_client_protocol(DEFT_WAYLAND_PROTO_SRCS
            PROTOCOL "${PC_XML}"
            BASENAME pointer-constraints-unstable-v1)
        # kcoreaddons_add_plugin only compiles C++ sources — a generated .c in
        # its SOURCES is silently dropped. Build the C glue as its own static
        # lib and link it in.
        add_library(deft_wayland_proto STATIC ${DEFT_WAYLAND_PROTO_SRCS})
        target_include_directories(deft_wayland_proto PUBLIC
            "${CMAKE_CURRENT_BINARY_DIR}" ${WAYLAND_CLIENT_INCLUDE_DIRS})
        set_target_properties(deft_wayland_proto PROPERTIES
            POSITION_INDEPENDENT_CODE ON
            LINKER_LANGUAGE C
            AUTOMOC OFF AUTOUIC OFF AUTORCC OFF)
        list(APPEND RADIAL_PLUGIN_SOURCES waylandcursorwarp.cpp)
        set(DEFT_WAYLAND ON)
    endif()
endif()

if(NOT DEFT_WAYLAND)
    message(STATUS "Wayland pointer-warp support disabled (missing scanner / "
                   "wayland-client / Qt6 GuiPrivate / protocol xml)")
    list(APPEND RADIAL_PLUGIN_SOURCES waylandcursorwarp_stub.cpp)
endif()

kcoreaddons_add_plugin(deft_radial
    SOURCES ${RADIAL_PLUGIN_SOURCES}
    INSTALL_NAMESPACE "kf6/ktexteditor"
)
target_include_directories(deft_radial PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
target_link_libraries(deft_radial PRIVATE
    radial
    KF6::TextEditor
    KF6::XmlGui
    KF6::CoreAddons
    KF6::I18n
    Qt6::Widgets
)

if(DEFT_WAYLAND)
    target_compile_definitions(deft_radial PRIVATE OLLIE_WAYLAND=1)
    target_include_directories(deft_radial PRIVATE ${WAYLAND_CLIENT_INCLUDE_DIRS})
    target_link_libraries(deft_radial PRIVATE
        deft_wayland_proto
        Qt6::GuiPrivate
        ${WAYLAND_CLIENT_LIBRARIES})
endif()
