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

xwidgets 2.0 #91

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 13 additions & 5 deletions CMakeLists.txt
@@ -1,5 +1,5 @@
# ##################################################################################################
# Copyright (c) 2022, Giulio Girardi
# Copyright (c) 2023, Giulio Girardi
#
# Distributed under the terms of the GNU General Public License v3.
#
Expand Down Expand Up @@ -72,6 +72,8 @@ option(
# ============

find_package(xeus-zmq REQUIRED)
find_package(xwidgets REQUIRED)
find_package(xproperty REQUIRED)
find_package(PNG REQUIRED)
find_package(glad REQUIRED)
find_package(glfw3)
Expand Down Expand Up @@ -112,6 +114,7 @@ set(
include/xeus-octave/plotstream.hpp
include/xeus-octave/tex2html.hpp
include/xeus-octave/display.hpp
include/xeus-octave/xwidgets2.hpp
)

set(
Expand All @@ -122,6 +125,7 @@ set(
src/xinterpreter.cpp
src/input.cpp
src/output.cpp
src/xwidgets2.cpp
)

set(XEUS_OCTAVE_MAIN_SRC src/main.cpp)
Expand Down Expand Up @@ -187,7 +191,7 @@ endmacro()
# Scripts directory for xeus-octave
set(
XEUS_OCTAVE_SCRIPTS_BASEDIR
"share"
${CMAKE_INSTALL_DATADIR}
CACHE STRING "Xeus-octave scripts base directory"
)

Expand Down Expand Up @@ -215,7 +219,8 @@ macro(xeus_octave_create_target target_name linkage output_name)
target_compile_definitions(
${target_name}
PUBLIC "XEUS_OCTAVE_EXPORTS"
PRIVATE XEUS_OCTAVE_OVERRIDE_PATH="${CMAKE_INSTALL_PREFIX}/share/xeus-octave"
PRIVATE
XEUS_OCTAVE_OVERRIDE_PATH="${CMAKE_INSTALL_PREFIX}/${XEUS_OCTAVE_SCRIPTS_BASEDIR}/xeus-octave"
)
target_compile_features(${target_name} PRIVATE cxx_std_17)

Expand All @@ -226,7 +231,7 @@ macro(xeus_octave_create_target target_name linkage output_name)

target_link_libraries(
${target_name}
PUBLIC xtl PkgConfig::octinterp
PUBLIC xtl xwidgets PkgConfig::octinterp
PRIVATE glad::glad glfw PNG::PNG
)
if(XEUS_OCTAVE_USE_SHARED_XEUS)
Expand Down Expand Up @@ -276,6 +281,8 @@ if(XEUS_OCTAVE_BUILD_EXECUTABLE)
xeus_octave_set_kernel_options(xoctave)
endif()

add_subdirectory(share/xeus-octave/+xwidgets)

# Installation
# ============

Expand Down Expand Up @@ -312,7 +319,7 @@ if(XEUS_OCTAVE_BUILD_EXECUTABLE)
# Configuration and data directories for jupyter and xeus-octave
set(
XJUPYTER_DATA_DIR
"share/jupyter"
"${CMAKE_INSTALL_DATADIR}/jupyter"
CACHE STRING "Jupyter data directory"
)

Expand All @@ -330,6 +337,7 @@ if(XEUS_OCTAVE_BUILD_EXECUTABLE)
DIRECTORY ${XOCTAVE_SCRIPTS_DIR}
DESTINATION ${XEUS_OCTAVE_SCRIPTS_BASEDIR}
PATTERN "*.in" EXCLUDE
PATTERN "+xwidgets" EXCLUDE
)

# Extra path for installing Jupyter kernelspec
Expand Down
6 changes: 5 additions & 1 deletion environment-dev.yml
Expand Up @@ -3,6 +3,7 @@ channels:
dependencies:
# Build dependencies
- cxx-compiler
- fortran-compiler
- c-compiler
- cmake
- make
Expand All @@ -11,6 +12,7 @@ dependencies:
- libuuid
- xtl
- xeus-zmq =1.*
- xwidgets =0.27.3
- nlohmann_json
- cppzmq
- octave =7.*
Expand All @@ -36,5 +38,7 @@ dependencies:
- ccache
- cmake-format
- plotly
- ipywidgets
- ipywidgets =8
- widgetsnbextension
- jupyter-dash
- mako
92 changes: 92 additions & 0 deletions include/xeus-octave/xwidgets2.hpp
@@ -0,0 +1,92 @@
/*
* Copyright (C) 2022 Giulio Girardi.
*
* This file is part of xeus-octave.
*
* xeus-octave is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* xeus-octave is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with xeus-octave. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef XEUS_OCTAVE_XWIDGETS2_H
#define XEUS_OCTAVE_XWIDGETS2_H

#include <octave/cdef-object.h>
#include <octave/interpreter.h>
#include <octave/ov-base.h>
#include <octave/ov-classdef.h>
#include <octave/ov.h>
#include <octave/ovl.h>

#include <string>
#include <xwidgets/xcommon.hpp>

namespace xeus_octave::widgets
{

void register_all2(octave::interpreter&);

constexpr inline char const* XWIDGET_CLASS_NAME = "__xwidget_internal__";

class xwidget : public octave::handle_cdef_object, public xw::xcommon
{

public:

void put(std::string const&, octave_value const&) override;
void mark_as_constructed(octave::cdef_class const& cls) override;

private:

xwidget();
~xwidget();

void open();
void close();

void serialize_state(nl::json&, xeus::buffer_sequence&) const;
void apply_patch(nl::json const&, xeus::buffer_sequence const&);
void handle_message(xeus::xmessage const&);
void handle_custom_message(nl::json const&);

/**
* @brief call any observers set in the octave interpreter context for the
* specified property name
*/
void notify_backend(std::string const&);
/**
* @brief send to the frontend a new value for the specified property.
* Octave value is automatically converted to a json value
*/
void notify_frontend(std::string const&, octave_value const&);

private:

static octave_value_list cdef_constructor(octave::interpreter&, octave_value_list const&, int);
static octave_value_list cdef_observe(octave_value_list const&, int);
static octave_value_list cdef_display(octave_value_list const&, int);
static octave_value_list cdef_id(octave_value_list const&, int);
static octave_value_list cdef_on(octave_value_list const&, int);

template <typename T> friend inline void xw::xwidgets_serialize(T const& value, nl::json& j, xeus::buffer_sequence&);
friend void xeus_octave::widgets::register_all2(octave::interpreter&);

private:

std::map<std::string, std::list<octave_value>> m_observerCallbacks;
std::map<std::string, std::list<octave_value>> m_eventCallbacks;
};

xwidget* get_widget(octave_classdef const*);

} // namespace xeus_octave::widgets
#endif
36 changes: 36 additions & 0 deletions share/xeus-octave/+xwidgets/CMakeLists.txt
@@ -0,0 +1,36 @@
# ##################################################################################################
# Copyright (c) 2023, Giulio Girardi
#
# Distributed under the terms of the GNU General Public License v3.
#
# The full license is in the file LICENSE, distributed with this software.
# ##################################################################################################

execute_process(
COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/xwidgets_generate.py
OUTPUT_VARIABLE XEUS_OCTAVE_WIDGETS
)

set(XEUS_OCTAVE_WIDGETS_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})

add_custom_command(
OUTPUT ${XEUS_OCTAVE_WIDGETS}
DEPENDS ${XEUS_OCTAVE_WIDGETS_SOURCE_DIR}/Widget.m
COMMAND
python ${CMAKE_CURRENT_SOURCE_DIR}/xwidgets_generate.py
${XEUS_OCTAVE_WIDGETS_SOURCE_DIR}/Widget.m
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
VERBATIM
)

list(
TRANSFORM XEUS_OCTAVE_WIDGETS
PREPEND "${CMAKE_CURRENT_BINARY_DIR}/" OUTPUT_VARIABLE XEUS_OCTAVE_GENERATED_WIDGETS
)

install(
FILES ${XEUS_OCTAVE_GENERATED_WIDGETS}
DESTINATION ${XEUS_OCTAVE_SCRIPTS_BASEDIR}/xeus-octave/+xwidgets
)

add_custom_target(widgets ALL DEPENDS ${XEUS_OCTAVE_WIDGETS})