KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_git_switch.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20
21#include "dialog_git_switch.h"
22
23#include <wx/button.h>
24#include <wx/listctrl.h>
25#include <wx/event.h>
26#include <wx/sizer.h>
27#include <wx/stattext.h>
28#include <wx/textctrl.h>
29
30#include <algorithm>
31#include <utility>
32
33
34DIALOG_GIT_SWITCH::DIALOG_GIT_SWITCH( wxWindow* aParent, std::vector<KIGIT_BRANCH_INFO> aBranches ) :
35 DIALOG_SHIM( aParent, wxID_ANY, _( "Git Branch Switch" ), wxDefaultPosition, wxDefaultSize,
36 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER ),
38 [this]()
39 {
41 },
42 500 ),
43 m_branches( std::move( aBranches ) )
44{
45 // Only local branches can be checked out. Keep the picker alphabetical.
46 std::erase_if( m_branches,
47 []( const KIGIT_BRANCH_INFO& aBranch )
48 {
49 return aBranch.isRemote;
50 } );
51 std::sort( m_branches.begin(), m_branches.end(),
52 []( const KIGIT_BRANCH_INFO& aFirst, const KIGIT_BRANCH_INFO& aSecond )
53 {
54 return aFirst.name < aSecond.name;
55 } );
56
57 wxBoxSizer* sizer = new wxBoxSizer( wxVERTICAL );
58
59 // Add explanation text
60 wxStaticText* explanationText =
61 new wxStaticText( this, wxID_ANY, _( "Select or enter a branch name:" ) );
62 sizer->Add( explanationText, 0, wxALL, 10 );
63
64 // Add branch list with three columns
65 m_branchList = new wxListView( this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
66 wxLC_REPORT | wxLC_SINGLE_SEL );
67 m_branchList->InsertColumn( 0, _( "Branch" ) );
68 m_branchList->InsertColumn( 1, _( "Last Commit" ) );
69 m_branchList->InsertColumn( 2, _( "Last Updated" ) );
70 sizer->Add( m_branchList, 1, wxALL | wxEXPAND, 10 );
71
72 // Add branch name text box
73 m_branchNameText = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition,
74 wxDefaultSize, wxTE_PROCESS_ENTER );
75 sizer->Add( m_branchNameText, 0, wxALL | wxEXPAND, 10 );
76
77 // Add buttons
78 wxStdDialogButtonSizer* buttonSizer = new wxStdDialogButtonSizer();
79 m_switchButton = new wxButton( this, wxID_OK, _( "Switch" ) );
80 buttonSizer->AddButton( m_switchButton );
81 m_switchButton->Disable();
82 wxButton* cancelButton = new wxButton( this, wxID_CANCEL, _( "Cancel" ) );
83 buttonSizer->AddButton( cancelButton );
84 buttonSizer->Realize();
85 sizer->Add( buttonSizer, 0, wxALIGN_RIGHT | wxALL, 10 );
86
87 // Bind events
88 Bind( wxEVT_LIST_ITEM_SELECTED, &DIALOG_GIT_SWITCH::OnBranchListSelection, this, m_branchList->GetId() );
89 Bind( wxEVT_LIST_ITEM_ACTIVATED, &DIALOG_GIT_SWITCH::OnBranchListDClick, this, m_branchList->GetId() );
90 Bind( wxEVT_BUTTON, &DIALOG_GIT_SWITCH::OnSwitchButton, this, m_switchButton->GetId() );
91 Bind( wxEVT_BUTTON, &DIALOG_GIT_SWITCH::OnCancelButton, this, cancelButton->GetId() );
92 Bind( wxEVT_TEXT, &DIALOG_GIT_SWITCH::OnTextChanged, this, m_branchNameText->GetId() );
93
94 // Populate branch list
95 PopulateBranchList();
96
97 // Set sizer for the dialog
98 SetSizerAndFit( sizer );
99
100 finishDialogSettings();
101}
102
103
108
109
111{
112 m_branchList->DeleteAllItems();
113 m_currentBranch.clear();
114
115 long currentIndex = -1;
116
117 // Populate the list
118 for( const KIGIT_BRANCH_INFO& branch : m_branches )
119 {
120 wxString displayName = ( branch.isCurrent ? wxS( "* " ) : wxS( " " ) ) + branch.name;
121
122 long itemIndex = m_branchList->InsertItem( m_branchList->GetItemCount(), displayName );
123 m_branchList->SetItem( itemIndex, 1, branch.message );
124 m_branchList->SetItem( itemIndex, 2, branch.when.Format() );
125
126 if( branch.isCurrent )
127 {
128 m_currentBranch = branch.name;
129 currentIndex = itemIndex;
130 }
131 }
132
133 m_branchList->SetColumnWidth( 0, wxLIST_AUTOSIZE );
134 m_branchList->SetColumnWidth( 1, wxLIST_AUTOSIZE );
135 m_branchList->SetColumnWidth( 2, wxLIST_AUTOSIZE );
136
137 if( currentIndex >= 0 )
138 {
139 m_branchList->Select( currentIndex );
140 m_branchList->EnsureVisible( currentIndex );
142 }
143}
144
145
146void DIALOG_GIT_SWITCH::OnBranchListDClick( wxListEvent& aEvent )
147{
148 int selection = aEvent.GetIndex();
149
150 if( selection >= 0 && static_cast<size_t>( selection ) < m_branches.size() )
151 {
152 const wxString& branchName = m_branches[selection].name;
153 m_branchNameText->SetValue( branchName );
154
155 if( branchName != m_currentBranch )
156 EndModal( wxID_OK );
157 }
158}
159
160
162{
163 int selection = aEvent.GetIndex();
164
165 if( selection >= 0 && static_cast<size_t>( selection ) < m_branches.size() )
166 {
167 const wxString& branchName = m_branches[selection].name;
168 m_branchNameText->SetValue( branchName );
169 m_switchButton->SetLabel( _( "Switch" ) );
170 m_switchButton->Enable( branchName != m_currentBranch );
171 }
172 else
173 {
174 // Deselect all elements in the list
175 for( int ii = 0; ii < m_branchList->GetItemCount(); ++ii )
176 m_branchList->SetItemState( ii, 0, 0 );
177 }
178}
179
180
181void DIALOG_GIT_SWITCH::OnSwitchButton( wxCommandEvent& aEvent )
182{
183 EndModal( branchExists( m_branchNameText->GetValue() ) ? wxID_OK : wxID_ADD );
184}
185
186
187void DIALOG_GIT_SWITCH::OnCancelButton(wxCommandEvent& aEvent)
188{
189 EndModal(wxID_CANCEL); // Return Cancel code
190}
191
192
194{
195 return m_branchNameText->GetValue();
196}
197
198
200{
201 wxString branchName = m_branchNameText->GetValue();
202
203 if( branchName == m_lastEnteredText )
204 return;
205
206 m_lastEnteredText = branchName;
207
208 if( branchExists( branchName ) )
209 {
210 m_switchButton->SetLabel( _( "Switch" ) );
211 m_switchButton->Enable( branchName != m_currentBranch );
212 }
213 else
214 {
215 m_switchButton->SetLabel( _( "Add" ) );
216 m_switchButton->Enable();
217 }
218}
219
220
221void DIALOG_GIT_SWITCH::OnTextChanged( wxCommandEvent& aEvt )
222{
223 m_branchNameDebounce.Restart();
224}
225
226
227bool DIALOG_GIT_SWITCH::branchExists( const wxString& aName ) const
228{
229 return std::any_of( m_branches.begin(), m_branches.end(),
230 [&aName]( const KIGIT_BRANCH_INFO& aBranch )
231 {
232 return aBranch.name == aName;
233 } );
234}
std::vector< KIGIT_BRANCH_INFO > m_branches
bool branchExists(const wxString &aName) const
wxListView * m_branchList
WX_DEBOUNCED_ACTION m_branchNameDebounce
wxString GetBranchName() const
void OnTextChanged(wxCommandEvent &event)
void OnCancelButton(wxCommandEvent &event)
DIALOG_GIT_SWITCH(wxWindow *aParent, std::vector< KIGIT_BRANCH_INFO > aBranches)
wxTextCtrl * m_branchNameText
void OnBranchListSelection(wxListEvent &event)
void OnBranchListDClick(wxListEvent &event)
void OnSwitchButton(wxCommandEvent &event)
DIALOG_SHIM(wxWindow *aParent, wxWindowID id, const wxString &title, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxDEFAULT_FRAME_STYLE|wxRESIZE_BORDER, const wxString &name=wxDialogNameStr)
static wxString displayName(const DATABASE_LIB_TABLE &aTable)
#define _(s)