KiCad PCB EDA Suite
Loading...
Searching...
No Matches
git_ui_utils.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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
21
22#include <algorithm>
23
24#include <wx/button.h>
25#include <wx/datetime.h>
26#include <wx/intl.h>
27#include <wx/listctrl.h>
28#include <wx/sizer.h>
29#include <wx/string.h>
30#include <wx/splitter.h>
31#include <wx/utils.h>
32
33#include <confirm.h>
36#include <kiplatform/ui.h>
38
39
40void KIGIT::ShowBusyMessage( wxWindow* aParent )
41{
42 DisplayInfoMessage( aParent, BusyMessage() );
43}
44
45
46std::unique_lock<std::mutex> KIGIT::TryLockAction( KIGIT_COMMON& aCommon, wxWindow* aParent )
47{
48 std::unique_lock<std::mutex> lock = aCommon.TryLockAction();
49
50 if( !lock.owns_lock() )
51 ShowBusyMessage( aParent );
52
53 return lock;
54}
55
56
57std::unique_lock<std::mutex> KIGIT::WaitForActionLock( KIGIT_COMMON& aCommon, wxWindow* aParent )
58{
59 std::unique_lock<std::mutex> lock;
60
61 {
62 wxBusyCursor busy;
64 }
65
66 if( !lock.owns_lock() )
67 ShowBusyMessage( aParent );
68
69 return lock;
70}
71
72
73void KIGIT::SortBranchesForDisplay( std::vector<KIGIT_BRANCH_INFO>& aBranches )
74{
75 std::sort( aBranches.begin(), aBranches.end(),
76 []( const KIGIT_BRANCH_INFO& aLeft, const KIGIT_BRANCH_INFO& aRight )
77 {
78 if( aLeft.isCurrent != aRight.isCurrent )
79 return aLeft.isCurrent;
80
81 if( aLeft.isRemote != aRight.isRemote )
82 return !aLeft.isRemote;
83
84 if( aLeft.when.IsValid() != aRight.when.IsValid() )
85 return aLeft.when.IsValid();
86
87 if( aLeft.when.IsValid() && aLeft.when != aRight.when )
88 return aLeft.when > aRight.when;
89
90 return aLeft.name < aRight.name;
91 } );
92}
93
94
95wxString KIGIT::RelativeDate( const wxDateTime& aWhen )
96{
97 return RelativeDate( aWhen, wxDateTime::Now() );
98}
99
100
101wxString KIGIT::RelativeDate( const wxDateTime& aWhen, const wxDateTime& aNow )
102{
103 if( !aWhen.IsValid() )
104 return wxEmptyString;
105
106 wxTimeSpan diff = aNow - aWhen;
107
108 if( diff.GetSeconds() < 60 )
109 return _( "just now" );
110
111 if( int minutes = diff.GetMinutes(); minutes < 60 )
112 return wxString::Format( wxPLURAL( "%d minute ago", "%d minutes ago", minutes ), minutes );
113
114 if( int hours = diff.GetHours(); hours < 24 )
115 return wxString::Format( wxPLURAL( "%d hour ago", "%d hours ago", hours ), hours );
116
117 if( int days = diff.GetDays(); days < 30 )
118 return wxString::Format( wxPLURAL( "%d day ago", "%d days ago", days ), days );
119
120 return aWhen.FormatISODate();
121}
122
123
124void KIGIT::UnsplitKeepingSash( wxSplitterWindow* aSplitter, wxWindow* aPane, int& aSashPos )
125{
126 if( !aSplitter->IsSplit() )
127 return;
128
129 aSashPos = aSplitter->GetSashPosition();
130 aSplitter->Unsplit( aPane );
131}
132
133
135{
136 wxWindow* detail = aSplitter->GetWindow2();
137 int detailMin = detail->GetSizer()->CalcMin().x;
138 int frame = aSplitter->GetSashSize() + 2 * aSplitter->GetBorderSize();
139
140 detail->SetMinSize( wxSize( detailMin, -1 ) );
141 aSplitter->SetMinSize( wxSize( aSplitter->GetMinimumPaneSize() + frame + detailMin, -1 ) );
142 aSplitter->SetPaneMinimums( aSplitter->GetMinimumPaneSize(), frame + detailMin );
143}
144
145
146// Markup is what reaches the native button title on every platform, where a foreground colour
147// does not.
148void KIGIT::MarkDestructive( wxButton* aButton )
149{
151 wxColour colour = theme.removed.ToColour();
152
153 aButton->SetLabelMarkup( wxString::Format( wxS( "<span foreground='%s'>%s</span>" ),
154 colour.GetAsString( wxC2S_HTML_SYNTAX ), aButton->GetLabel() ) );
155}
156
157
158// Git records the address without checking it, so this only catches a typo before every
159// commit carries it. A local part, one at sign, and a domain with a dot inside it.
160static bool looksLikeAnEmailAddress( const wxString& aEmail )
161{
162 const size_t at = aEmail.find( '@' );
163
164 if( at == wxString::npos || at == 0 || aEmail.find( '@', at + 1 ) != wxString::npos )
165 return false;
166
167 const wxString domain = aEmail.Mid( at + 1 );
168 const size_t dot = domain.find( '.' );
169
170 if( dot == wxString::npos || dot == 0 || dot + 1 == domain.length() )
171 return false;
172
173 for( const wxUniChar& c : aEmail )
174 {
175 if( wxIsspace( c ) )
176 return false;
177 }
178
179 return true;
180}
181
182
183bool KIGIT::ValidateIdentity( const wxString& aName, const wxString& aEmail, wxString& aError )
184{
185 wxString name = aName;
186
187 if( name.Trim( true ).Trim( false ).IsEmpty() )
188 {
189 aError = _( "Enter your name. It is recorded as the author of your commits." );
190 return false;
191 }
192
193 if( !looksLikeAnEmailAddress( aEmail ) )
194 {
195 aError = _( "Enter a valid email address. It is recorded as the author of your commits." );
196 return false;
197 }
198
199 return true;
200}
201
202
203wxString KIGIT::SelectedText( wxListCtrl* aList, int aColumn )
204{
205 long sel = aList->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
206
207 if( sel == -1 )
208 return wxEmptyString;
209
210 return aList->GetItemText( sel, aColumn );
211}
const char * name
wxColour ToColour() const
Definition color4d.cpp:221
The project's git session.
std::unique_lock< std::mutex > TryLockActionFor(std::chrono::milliseconds aWait)
TryLockAction that keeps trying for aWait before giving up.
static constexpr std::chrono::milliseconds ACTION_LOCK_WAIT
Long enough for a status read to finish, short enough that a window does not look hung.
std::unique_lock< std::mutex > TryLockAction()
void SetPaneMinimums(int aFirst, int aSecond)
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
Definition confirm.cpp:245
This file is part of the common library.
#define _(s)
static bool looksLikeAnEmailAddress(const wxString &aEmail)
void SortBranchesForDisplay(std::vector< KIGIT_BRANCH_INFO > &aBranches)
Put the current branch first, then local and remote branches with the newest tips first.
wxString BusyMessage()
What every caller says when it finds the session already busy.
std::unique_lock< std::mutex > WaitForActionLock(KIGIT_COMMON &aCommon, wxWindow *aParent)
TryLockAction that first waits a moment for a status read or fetch to let go.
void UnsplitKeepingSash(wxSplitterWindow *aSplitter, wxWindow *aPane, int &aSashPos)
Close a splitter's second pane, keeping the sash where the user left it for next time.
void ShowBusyMessage(wxWindow *aParent)
Show a busy message for a user action. UI thread only.
std::unique_lock< std::mutex > TryLockAction(KIGIT_COMMON &aCommon, wxWindow *aParent)
Try the action lock and show a busy message if it is unavailable.
bool ValidateIdentity(const wxString &aName, const wxString &aEmail, wxString &aError)
Check a name and email for recording commits. False leaves the reason in aError.
wxString SelectedText(wxListCtrl *aList, int aColumn)
The text of the selected row in a report list, or empty when nothing is selected.
wxString RelativeDate(const wxDateTime &aWhen)
"just now", "3 hours ago", or the ISO date once it is more than a month old.
void KeepDetailWidth(WX_SPLITTER_WINDOW *aSplitter)
Keep a split detail pane at the width its controls need, against both a narrow window and a dragged s...
void MarkDestructive(wxButton *aButton)
Paint a button's text the diff's red, for one that throws something away.
bool IsDarkTheme()
Determine if the desktop interface is currently using a dark theme or a light theme.
Definition wxgtk/ui.cpp:57
Diff colors with defaults for light backgrounds.
Definition diff_scene.h:278
static DIFF_COLOR_THEME ForText(bool aDarkTheme)
Resolve change colors for UI text. Pass the desktop theme captured on the UI thread.