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

gh-802: Macro API to report VMenu alignment state. Part 1: Backend implementation. #895

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

MKadaner
Copy link
Contributor

@MKadaner MKadaner commented Jan 5, 2025

Summary

Implemented vmenu_horizontal_tracker which knows whether menu items are currently aligned.

Surfacing the state maintained by vmenu_horizontal_tracker is coming soon.

References

Checklist

  • I have followed the contributing guidelines.
  • I have discussed this with project maintainers:

    If not checked, I accept that this work might be rejected in favor of a different great big ineffable plan.

…ckend implementation.

Implemented `vmenu_horizontal_tracker` which knows whether menu items are currently aligned.

Surfacing the state maintained by `vmenu_horizontal_tracker` is coming soon.
Copy link

sonarqubecloud bot commented Jan 5, 2025


return m_HorizontalTracker.start_bulk_update(
vmenu_horizontal_tracker::alignment::Right, TextAreaWidth + NewHPos + 1, TextAreaWidth, Policy);
}() };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIFE looks somewhat complicated here, maybe ternary operator?

	const auto Cookie = NewHPos >= 0?
		m_HorizontalTracker.start_bulk_update(vmenu_horizontal_tracker::alignment::Left, ...) :
		m_HorizontalTracker.start_bulk_update(vmenu_horizontal_tracker::alignment::Right, ...);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, it requires copy constructor:

1>D:\DEV\FarManager\far\vmenu.cpp(2188,19): error C2280: 'vmenu_horizontal_tracker::cookie::cookie(const vmenu_horizontal_tracker::cookie &)': attempting to reference a deleted function
1>	const auto Cookie{
1>	                 ^ (compiling source file '/vmenu.cpp')
1>    D:\DEV\FarManager\far\vmenu.hpp(169,3):
1>    see declaration of 'vmenu_horizontal_tracker::cookie::cookie'
1>    		NONCOPYABLE(cookie);
1>    		^
1>    D:\DEV\FarManager\far\vmenu.hpp(169,3):
1>    'vmenu_horizontal_tracker::cookie::cookie(const vmenu_horizontal_tracker::cookie &)': function was explicitly deleted
1>    		NONCOPYABLE(cookie);
1>    		^

What do you think of this?

	const auto Cookie{
		m_HorizontalTracker.start_bulk_update(
			NewHPos >= 0 ? vmenu_horizontal_tracker::alignment::Left : vmenu_horizontal_tracker::alignment::Right,
			NewHPos >= 0 ? NewHPos : TextAreaWidth + NewHPos + 1,
			TextAreaWidth,
			Policy) };

And it reminded me that the cookie should be either non-movable or should properly implement move operations.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, it requires copy constructor

Looks like it's a compiler bug 😆
Please ignore my previous comment or use whatever compiles.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Everything is relative to menu_layout::TextArea::first (Left edge).
class vmenu_horizontal_tracker
{
struct cookie
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that "cookie" conveys the intended meaning.

Perhaps saying what it does ("scoped bulk update") oh what it actually does ("hold stray items") could be better?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the name we use throughout in our codebase at work. I think the stress is on it being small and opaque to the caller.

What do you think of bulk_update_scope_guard?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like caller needs to understand its meaning after all, i.e. "keeping the thing alive enforces some specific behavior". bulk_update_scope_guard sounds good, or maybe just bulk_update.

// Everything is relative to menu_layout::TextArea::first (Left edge).
class vmenu_horizontal_tracker
{
struct cookie
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be possible to forward-declare this struct and define it in the cpp.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is returned by value from start_bulk_update. The question is moot if we remove vmenu_horizontal_tracker from the header. Let me know please.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will work if the type definition precedes start_bulk_update definition in the cpp.
Indeed, it's irrelevant if we move the whole thing, I've mentioned just in case we don't for any reason.

// Keeps track of the horizontal state of all items.
// The tracking is best effort. See comments below.
// Everything is relative to menu_layout::TextArea::first (Left edge).
class vmenu_horizontal_tracker
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need all these implementation details exposed in the header?
Maybe forward-declare it and have a unique_ptr to it in the menu?
Yes, it's overhead, but one per menu is nothing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping it by std::unique_ptr means an extra allocation. If it's fine with you, I can do that.

Copy link
Contributor Author

@MKadaner MKadaner Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ouch! Moved the definition of vmenu_horizontal_tracker to vmenu.cpp and changed the type of m_HorizontalTracker to std::unique_ptr<vmenu_horizontal_tracker>. Got a bunch of "can't delete an incomplete type" errors in many other files: https://github.com/MKadaner/FarManager/tree/mzk/gh-802/retrieve-vmenu-alignment-api-WIP

Never mind. Silly me. Working on it.

namespace
{
MenuItemEx far_list_to_menu_item(const FarListItem& FItem)
const FarListItem string_to_far_list_item(const wchar_t* NewStrItem)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove const from the return value please.

{
NONCOPYABLE(cookie);
explicit cookie(vmenu_horizontal_tracker* Tracker) noexcept: m_Tracker{ Tracker } {}
~cookie() noexcept { m_Tracker->m_IsBulkUpdate = false; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dtors are noexcept by default, do we need to state it explicitly?

NONCOPYABLE(cookie);
explicit cookie(vmenu_horizontal_tracker* Tracker) noexcept: m_Tracker{ Tracker } {}
~cookie() noexcept { m_Tracker->m_IsBulkUpdate = false; }
vmenu_horizontal_tracker* m_Tracker{};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's always initialized in the ctor anyway.

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

Successfully merging this pull request may close these issues.

2 participants