PCM: Select the preferred version first in version grid

This commit is contained in:
Mikolaj Wielgus 2021-11-13 18:55:41 +01:00 committed by Jeff Young
parent 0e4e9206ff
commit 0b59246279
3 changed files with 56 additions and 21 deletions

View File

@ -111,30 +111,14 @@ void PANEL_PACKAGE::SetState( PCM_PACKAGE_STATE aState )
void PANEL_PACKAGE::OnButtonClicked( wxCommandEvent& event )
{
// Versions are already presorted in descending order
if( m_data.state == PPS_AVAILABLE )
{
// Find last stable compatible version
auto ver_it = std::find_if( m_data.package.versions.begin(), m_data.package.versions.end(),
[]( const PACKAGE_VERSION& ver )
{
return ver.compatible && ver.status == PVS_STABLE;
} );
wxString version = GetPreferredVersion();
// If not found then find any compatible version
if( ver_it == m_data.package.versions.end() )
{
ver_it = std::find_if( m_data.package.versions.begin(), m_data.package.versions.end(),
[]( const PACKAGE_VERSION& ver )
{
return ver.compatible;
} );
}
if( version.IsEmpty() )
return;
if( ver_it == m_data.package.versions.end() )
return; // Shouldn't happen
m_actionCallback( m_data, PPA_INSTALL, ver_it->version );
m_actionCallback( m_data, PPA_INSTALL, version );
}
else
{
@ -177,3 +161,31 @@ void PANEL_PACKAGE::SetSelected( bool aSelected )
m_selected = aSelected;
Refresh();
}
wxString PANEL_PACKAGE::GetPreferredVersion() const
{
// Versions are already presorted in descending order
// Find last stable compatible version
auto ver_it = std::find_if( m_data.package.versions.begin(), m_data.package.versions.end(),
[]( const PACKAGE_VERSION& ver )
{
return ver.compatible && ver.status == PVS_STABLE;
} );
// If not found then find any compatible version
if( ver_it == m_data.package.versions.end() )
{
ver_it = std::find_if( m_data.package.versions.begin(), m_data.package.versions.end(),
[]( const PACKAGE_VERSION& ver )
{
return ver.compatible;
} );
}
if( ver_it == m_data.package.versions.end() )
return wxEmptyString; // Shouldn't happen
return ver_it->version;
}

View File

@ -75,6 +75,9 @@ public:
void OnSize( wxSizeEvent& event ) override;
///< Get preferred version. If criteria are not met, return wxEmptyString
wxString GetPreferredVersion() const;
const PACKAGE_VIEW_DATA& GetPackageData() const { return m_data; };
private:

View File

@ -318,7 +318,27 @@ void PANEL_PACKAGES_VIEW::setPackageDetails( const PACKAGE_VIEW_DATA& aPackageDa
}
if( m_gridVersions->GetNumberRows() >= 1 )
m_gridVersions->SelectRow( 0 );
{
wxString version = m_currentSelected->GetPreferredVersion();
if( !version.IsEmpty() )
{
for( int i = 0; i < m_gridVersions->GetNumberRows(); i++ )
{
if( m_gridVersions->GetCellValue( i, COL_VERSION ) == version )
{
std::cout << "auto select row: " << i << std::endl;
m_gridVersions->SelectRow( i );
break;
}
}
}
else
{
// Fall back to first row.
m_gridVersions->SelectRow( 0 );
}
}
m_gridVersions->Thaw();