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

Improve MenuBar #99

Merged
merged 6 commits into from
Aug 17, 2020
Merged
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
1 change: 1 addition & 0 deletions SOURCECONF.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ SET( ${TARGETLIB}_HEADERS
NCi18n.h
tnode.h
position.h
CyclicContainer.h
YNCursesUI.h
NCtoY2Event.h
NCApplication.h
Expand Down
2 changes: 1 addition & 1 deletion VERSION.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
SET( VERSION_MAJOR "2" )
SET( VERSION_MINOR "56" )
SET( VERSION_PATCH "0" )
SET( VERSION_PATCH "1" )
SET( VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}${GIT_SHA1_VERSION}" )

##### This is needed for the libyui core ONLY.
Expand Down
2 changes: 1 addition & 1 deletion package/libyui-ncurses-doc.spec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
%define so_version 13

Name: %{parent}-doc
Version: 2.56.0
Version: 2.56.1
Release: 0
Source: %{parent}-%{version}.tar.bz2

Expand Down
7 changes: 7 additions & 0 deletions package/libyui-ncurses.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
-------------------------------------------------------------------
Wed Aug 12 15:46:18 UTC 2020 - José Iván López González <jlopez@suse.com>

- Handle hot-keys for top level menu options.
- Related to bsc#1175115.
- 2.56.1

-------------------------------------------------------------------
Tue Aug 11 22:15:17 UTC 2020 - José Iván López González <jlopez@suse.com>

Expand Down
2 changes: 1 addition & 1 deletion package/libyui-ncurses.spec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


Name: libyui-ncurses
Version: 2.56.0
Version: 2.56.1
Release: 0
Source: %{name}-%{version}.tar.bz2

Expand Down
146 changes: 146 additions & 0 deletions src/CyclicContainer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
Copyright (C) 2020 SUSE LLC
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) version 3.0 of the License. This library
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 Lesser General Public
License for more details. You should have received a copy of the GNU
Lesser General Public License along with this library; if not, write
to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
Floor, Boston, MA 02110-1301 USA
*/


/*-/

File: CyclicContainer.h

Author: Jose Iván López <jlopez@suse.de>

/-*/


#ifndef CyclicContainer_h
#define CyclicContainer_h

#include <algorithm>
#include <iterator>
#include <vector>

/** Container class that allows cyclic navigation between its elements by moving to the next/previous
* element.
*
* @note This class holds pointers, but it does not own the pointers.
**/
template <class T>
class CyclicContainer
Copy link
Contributor

Choose a reason for hiding this comment

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

So this is a container with wrap-around behaviour for next() and previous(), right Please add some lines of documentation to describe this.

Does it also have something like selectFirst() and selectLast() ?

Copy link
Member

Choose a reason for hiding this comment

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

Please run make docs and make sure that the HTML docs make sense for the new code. A one line description for a new class is a bare minimum!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Selecting first and last is not explicitly needed. Do you think we need to add it?

{
public:

using Iterator = typename std::vector<T *>::iterator;
using ReverseIterator = std::reverse_iterator<Iterator>;

Iterator begin() { return _elements.begin(); }
Iterator end() { return _elements.end(); }

CyclicContainer() : _elements(), _current( nullptr )
{}


~CyclicContainer()
{
clear();
}


void clear()
{
_elements.clear();
_current = nullptr;
}


void add( T * element )
Copy link
Member

Choose a reason for hiding this comment

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

NO! NO! NO!

This is a "naked pointer". Nobody knows who owns (deletes) the data pointed to.

Use smart pointers instead. If you must use a dumb pointer you must document the ownership.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd vote to document who owns the object. That is indeed missing here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Since this class is ultimately using YMenuItems, ownership will remain with the YSelectionWidget, as usual; but that knowledge is implicit here. Somebody reading this code will probably not know that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you are right. I will add documentation.

{
_elements.push_back(element);
}


void setCurrent(Iterator element)
{
if ( element != _elements.end() )
_current = *element;
}


Iterator current()
{
return std::find( _elements.begin(), _elements.end(), _current );
}


Iterator next()
{
Iterator current = this->current();

if ( current == _elements.end() )
return findNext( _elements.begin() );

Iterator next = findNext( std::next( current, 1 ) );

if ( next == _elements.end() )
return findNext( _elements.begin() );

return next;
}


Iterator previous()
{
Iterator current = this->current();

ReverseIterator rbegin;

if ( current == _elements.end() )
rbegin = _elements.rbegin();
else
rbegin = ReverseIterator( current );

ReverseIterator previous = findPrevious( rbegin );

if ( previous == _elements.rend() && rbegin != _elements.rbegin() )
previous = findPrevious( _elements.rbegin() );

if ( previous == _elements.rend() )
return _elements.end();

return find( _elements.begin(), _elements.end(), *previous );
}

private:

Iterator findNext( Iterator begin )
{
return find_if( begin, _elements.end(), [](const T * element) {
return element->isSelectable();
});
}


ReverseIterator findPrevious( ReverseIterator rbegin )
{
return find_if( rbegin, _elements.rend(), [](const T * element) {
return element->isSelectable();
});
}


std::vector<T *> _elements;

T * _current;
};

#endif // CyclicContainer_h