-
Notifications
You must be signed in to change notification settings - Fork 0
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
Use forked pyimgui
for imgui.input_text_multiline()
widget which supports paragraph indentation
#1
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In previous revision, loaded `pyimgui` module is not the one forked by us, it is the one which is already installed in site-packages instead. The reason why this happened is that those `PathFinder`s in `sys.meta_path` search the site-packages first, and it returns result right after a module spec is found. This issue can be solved by making a custom `PathFinder`, but things can be solved in a easier way. We can register the original module name into `sys.modules` with our desired module spec before calling `spec.loader.exec_module()`, e.g. ```python spec = importlib.util.spec_from_file_location('foobar', fn) mod = importlib.util.module_from_spec(spec) sys.modules['foobar'] = mod # <- register name here spec.loader.exec_module(mod) ``` And this trick can make the loader get our desired module when it is executing `_find_and_load_unlocked()`.
Major updates: - Modify flags and add configs to `imgui.input_text_multiline()` to make it able to convert TAB key with spaces. Note that this feature requires installing `pyimgui` of the version forked by us instead of the original one (`swistakm/pyimgui`) since those callback facilities are not supported in it yet. However, in our implementation, callback are not implemented by user. Those limited callback features can just be enabled by passing a `imgui.core.InputTextCallbackConfig` object into the widget `imgui.input_text_multiline()`. See also the following link for details: NaleRaphael/pyimgui@96d65c7 Minor updates/revisions: - Missing `imgui.pop_item_width()` calls in `CodeNodeCreatorWindow.render()` and `CodeNodeViewer.finalize_canvas()` are now added. - Add input textbox for setting `url` in `CodeNodeCreatorWindow`. - Arguments `path` and `url` are now passed into `Snippet.__init__()`.
In Unix-like system, name of shared objects would be: `[FILENAME].[PYTHON_VERSION_AND_PLATFORM].so` So that the proper pattern for searching shared objects in forked `pyimgui/imgui` repository should be `dir_imgui.rglob('core.*.so')`. Besides, we should raise `ImportError` with different message to indicate the actual cause of execution failure.
In order to make this package runable by command `run codememo` while using `gdb python`, we have to change the import statement from ```python from .app import Application ``` to: ```python from codememo.app import Application ``` Otherwise, we will get `ModuleNotFoundError: '__main__' is not a package`.
Facilities for text indentation are implemented in that forked repository, see also the link attached below. NaleRaphael/pyimgui@7c6aaff
User now can use the following command to install this package with forked pyimgui which currently supports callbacks for text indentation in `imgui.input_text_multiline()` widget. ```bash $ pip install -v --global-option="--use-forked-pyimgui" ./ ```
`setuptools.command.install` one In order to make those built files of `pyimgui` able to be installed to the site-package folder, we should use a custom command class inheriting from `setuptools.command.install` instead. Besides, we also have to generate a destination-source pairs as the argument `data_files` for `setup()` command. See also `prepare_data_files()` for details. And the reason why `sys.argv` based approach implemented in previous revision does not work for this case is that operations written in `if ... in sys.argv: ...` are not actually executed before the installation of this package itself.
`--install-option` is for `InstallationCommand` instead.
…yimgui` There is a critical problem in previous implementation: value of `data_files` are determined before running operations defined in pre-intallation stage. Hence that `prepare_data_files()` does not return desired result. (the reason why it worked previously is that forked `pyimgui` repository was pulled already) So that we fallback to `sys.argv` based approach. But we execute those operations for pre-installation right before creating the dictionary `metadata`.
Update it for solving the issue of indentation. See also the following link for details: NaleRaphael/pyimgui@af56524
NaleRaphael
added a commit
to NaleRaphael/pyimgui-colortextedit
that referenced
this pull request
Oct 3, 2020
Note that we've added an function `get_current_context_ptr()` to pass pointer of `ImGuiContext` since we cannot access the actual memory address of it by the value returned by a Cython-wrapped `get_current_context()` function. (just like the reason mentioned in [1], a C object will be wrapped by a Python object, so that we cannot access its real address even if we have defined a `_ptr` field in the Cython extension class) Besides, those functions for setting/getting context should also be implemented in `pyimgui` (i.e. `get_current_context_ptr()` and `set_current_context_ptr()`, but at least the former one). And note that this might not be a good solution since we have to expose the real address of internal object, so that these functions are classified as workarounds and we should figure out a better ones in the future. [1]: NaleRaphael/codememo#1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, paragraph indentation is not supported in the original version of
pyimgui
(swistakm/pyimgui
). In order to implement this facility, we have to fork that repository and extend it by ourselves.The ideal approach to extend
pyimgui
is writing actypes
wrapper forImGuiInputTextCallbackData
, so that users can access those data in it or manipulate them with the help of those member functions (e.g.DeleteChars
,InsertChars
).However, we had tried this approach for days and with no luck. Because those data are actually new Python objects created by the
ctypes
wrapper instead of the real data used by that widget (literally, their memory addresses are different), and it seems there is no chance to access the memory address of real data from these Python objects either.Though we had also tried to manipulate those data by helper functions, application always crashed with segmentation fault (access violation) when we were trying to invoke them. With the help of
gdb
, we've known that it was resulted by invoking a function of which the address is actually pointing to an unknown location (part of message generated by backtrace is shown as below).Hence that we tried another approach: writing an Cython extension which includes
cimgui.pxd
,*.pxd
made byswistakm/pyimgui
as dependencies. The benefit of this approach is that we don't have to modify code inswistakm/pyimgui
directly.Though we actually had made an extension, application always terminated by the following exception:
After an investigation, we found that it's resulted by the failure of acquiring context (
ImGuiContext
) while invokingour_extension.input_text_multline()
. Since we can make sure thatimgui.new_frame()
has been called, the best guess is that we have to share the context created bypyimgui
to our extension. But currently we haven't figured out how to achieve it.The last approach, which is also the one we adopted, is extending code in a forked
pyimgui
repository directly.With the issue of manipulating data from Python side (metioned in the paragraph of implementing
ctypes
wrapper above), we have figure out how to provide a cython wrapper which takes a Python callback function and can be used inimgui
. Therefore, we provide a configuration class for user to pass desired settings for callback instead. Though this approach is less flexible, it works fine and more efficiently then implementing the same feature in pure Python.More details are recorded in commits for this PR.