Hacktoberfest #2 - PrimedKeys, ImGui Bugfix
Published: 2023-10-24 19:08:23

### Links Mentioned
The project I contributed to: https://github.com/GudBoiNero/PrimedKeys
The Issue I worked on: https://github.com/GudBoiNero/PrimedKeys/issues/22
My Pull Request: https://github.com/GudBoiNero/PrimedKeys/pull/41

## Blog Update

After a 2-week hiatus, I am proud to give an update on my Hacktoberfest 2023 progress. Due to school commitments I've had to delay my blog. However, the past 2 weeks were not spent in vain, as I have been working on a couple Pull Requests to Open Source projects, one of which I am happy to share today.

### Pull Request

The Pull Request I would like to share is my contribution to [PrimedKeys](https://github.com/GudBoiNero/PrimedKeys) by [GudBoiNero](https://github.com/GudBoiNero). PrimedKeys is a GUI application for point-and-click macros. PrimedKeys is a project created so the author (and other artists) can use hotkeys on a touchscreen laptop without configuring a [stylus pen](https://en.wikipedia.org/wiki/Stylus_(computing)). PrimedKeys is built in C++, and also uses Dear ImGui for the user interface. 

Overall I think the project is great. The author identified a problem they had, found that existing solutions did not suit their needs and decided to instead build their own solution for others to use. Despite my limited Open Source experience, I think PrimedKeys is a quintessential embodiment of Open Source.

### My Goals

I chose to contribute to this Project for two reasons:
- because I wanted to fix a bug on a C++ project, and
- because I wanted more experience working with Dear ImGui projects.

Since contributing to Skyrim Community Shaders, I've learned about Dear ImGui, and have noticed it's use in many projects.
In my C++ courses I've only made desktop applications that served the UI on the terminal/command line, but I've always wanted to make
C++ applications with a Graphical User Interface (GUI). Due to school commitments, I couldn't make the time to try creating my own project using `Dear ImGui`. However, I figured contributing to more projects built using `Dear ImGui` would be a good way to become familiar with the library and how others have used it to create their applications.

### The Issue - ImGui Window Exit

Previously, my last Pull Request was a code enhancement. For my next Pull Request, I wanted to look into a bug fix. PrimedKeys uses Dear ImGui to render a Macro Menu. The [Issue](https://github.com/GudBoiNero/PrimedKeys/issues/22) I worked on involved the Close Button not closing the Macro Menu window or stopping the application.

#### Locating the Bug

After forking the repository, I tried running the program to observe the problem myself, and to try and pinpoint the bug location. The project has many header and implementation files, which was initially intimidating. However, by running the program I was able to determine the window at least had the partial string "MacroMe" as the name:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wca1v8uji7wwcg517niv.png)

#### Git Grep

This information turned out to be enough for my to locate where the bug could be, as I was able to use [git grep](https://git-scm.com/book/en/v2/Git-Tools-Searching) to determine the files I should look at. I've found `git grep` to be a very useful tool for my PR's since learning about it in class. After doing this, I found the bug to be related to an ImGui method called `ImGui::Begin()`.

#### Researching Libraries

Working on this issue required me to research how Dear ImGui is used to render windows and menus. Part of my research involved watching a couple YouTube tutorials on Dear ImGui. However, an important resource in solving this issue was actually the [Dear ImGui project repository](https://github.com/ocornut/imgui), which contains [example code](https://github.com/ocornut/imgui/tree/master/examples) for various graphics API and rendering platforms. The Dear ImGui library also provides documentation for each method, visible through Visual Studio.

The following is the commented documentation for `ImGui::Begin()`:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ik0vupcequurb1cynmzo.png)

### My Changes
Based on the above, I learned that `ImGui::Begin()` can take the reference of a boolean to make the window closeable. After studying some examples in the `Dear ImGui` codebase, I noticed part of the bug was that in `PrimedKeys`, the call to `ImGui::Begin()` was inside a method, `gui::ShowMacroMenu()`, that had a `boolean` as its formal parameter instead of a `boolean pointer`. Because of this, any boolean sent to `gui::ShowMacroMenu()` would be passed by value (copied) instead of being passed by reference, meaning closing the window would not affect the original boolean. So I modified `gui::ShowMacroMenu()` to take a boolean pointer type so that the actual variable would be passed by reference.

However, this would not be enough to solve the issue. This change alone would allow the MacroMenu window to be closed, but in the main loop the MacroMenu window would be re-rendered until the application was closed. By examining one of the [Dear ImGui examples](https://github.com/ocornut/imgui/blob/313676d200f093e2694b5cfca574f72a2b116c85/examples/example_win32_opengl3/main.cpp#L144-L152)
I found an example of how to ensure the MacroMenu would stay closed and how to end the program.

### Conclusion

Until now I've only contributed to new features or code enhancements. For Hacktoberfest I wanted to try contributing to a bug fix. For this Pull Request, I worked on a C++ project because it's the language I'm most comfortable with, but I'll admit I might be hiding in my comfort zone. In the future I'm going to try contributing to projects written in other languages, like JavaScript. This is the first bugfix I've helped solve on an Open Source C++ project though. That's something I'm proud of.

Working on this project was a fun experience. I got to work on a cool project and learn more about `Dear ImGui`. I think once the semester is over I'll try making my own C++ apps using `Dear ImGui`.