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

Add PopAllStyleColors #6407

Closed
wants to merge 1 commit into from
Closed

Conversation

anthonyafgx
Copy link

@anthonyafgx anthonyafgx commented May 6, 2023

This PR adds the function PopAllStyleColors, which essentially is a version of PopStyleColor() where you do not have to specify the number styles you want to pop.

This is specially useful when the count of styles can vary and it helps in a way so you don't have to worry about the "count" parameter with extra code. Instead, user can just focus in popping all styles in the color stack.

Example of a code that shows a gray button when the option is off and a default color button when the option is on:

	if (ImGui::TreeNode("Opciones del Builder"))
	{
		auto grayColor = IM_COL32(97, 97, 97, 255);

		static bool gridSnapIsActive = false;
		static std::string gridSnapLabel = "GRID SNAP OFF";
		if (!gridSnapIsActive)
		{
			ImGui::PushStyleColor(ImGuiCol_Button, grayColor);
			ImGui::PushStyleColor(ImGuiCol_ButtonHovered, grayColor);
			ImGui::PushStyleColor(ImGuiCol_ButtonActive, grayColor);
		}

		if (ImGui::Button(gridSnapLabel.c_str()))
		{
			gridSnapIsActive = !gridSnapIsActive;
			gridSnapLabel = (gridSnapIsActive) ? "GRID SNAP ON" : "GRID SNAP OFF";

			mGraphics->GetGraph()->SetSnapToGrid(gridSnapIsActive);
		}
		ImGui::PopAllStyleColors();


		static bool smartGuideIsActive = true;
		static std::string smartGuideLabel = "SMART GUIDES ON";
		if (!smartGuideIsActive)
		{
			ImGui::PushStyleColor(ImGuiCol_Button, grayColor);
			ImGui::PushStyleColor(ImGuiCol_ButtonHovered, grayColor);
			ImGui::PushStyleColor(ImGuiCol_ButtonActive, grayColor);
		}

		if (ImGui::Button(smartGuideLabel.c_str()))
		{
			smartGuideIsActive = !smartGuideIsActive;
			smartGuideLabel = (smartGuideIsActive) ? "SMART GUIDES ON" : "SMART GUIDES OFF";

			mGraphics->GetGraph()->SetSnapToSmartGuides(smartGuideIsActive);
		}
		ImGui::PopAllStyleColors();

		ImGui::TreePop();
    }

image

Alternatively, another option would be that if you don't pass any parameter, all style colors would be deleted by default.

@anthonyafgx anthonyafgx changed the title Added PopAllStyleColors Add PopAllStyleColors May 6, 2023
@ocornut ocornut added the style label May 9, 2023
@ocornut
Copy link
Owner

ocornut commented May 9, 2023

Hello,

Thanks for the PR, but I believe this a dangerous and highly undesirable idea. It creates a problematic dependency between caller code and called code. If you take your example code and call it from another function which itself used PushStyleColor() eve once, your example would break.

If this was desirable, but I don't think the trade-off of complexity is worth it, this could be replaced by a scheme where the current stack location is recorded and then recorded. So more of a "backup current state", "restore current state".

Instead you may be interested in using RAII wrappers (https://github.com/ocornut/imgui/wiki/Useful-Extensions#cness) such as #2096, #2197 or https://github.com/mnesarco/imgui_sugar. I personally think they are not a good idea because they prevent asymetrical uses of stack which are useful, but you can technically combine both styles.

@ocornut ocornut closed this May 9, 2023
@anthonyafgx
Copy link
Author

Hello,

Thanks for the feedback. I'll keep everything you said in mind and maybe sometime soon I'll bring value to this project.

Again, thanks and have a great day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants