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/string.h>
29#include <wx/splitter.h>
30
31#include <confirm.h>
34#include <kiplatform/ui.h>
35
36
37void KIGIT::ShowBusyMessage( wxWindow* aParent )
38{
39 DisplayInfoMessage( aParent, BusyMessage() );
40}
41
42
43std::unique_lock<std::mutex> KIGIT::TryLockAction( KIGIT_COMMON& aCommon, wxWindow* aParent )
44{
45 std::unique_lock<std::mutex> lock = aCommon.TryLockAction();
46
47 if( !lock.owns_lock() )
48 ShowBusyMessage( aParent );
49
50 return lock;
51}
52
53
54void KIGIT::SortBranchesForDisplay( std::vector<KIGIT_BRANCH_INFO>& aBranches )
55{
56 std::sort( aBranches.begin(), aBranches.end(),
57 []( const KIGIT_BRANCH_INFO& aLeft, const KIGIT_BRANCH_INFO& aRight )
58 {
59 if( aLeft.isCurrent != aRight.isCurrent )
60 return aLeft.isCurrent;
61
62 if( aLeft.isRemote != aRight.isRemote )
63 return !aLeft.isRemote;
64
65 if( aLeft.when.IsValid() != aRight.when.IsValid() )
66 return aLeft.when.IsValid();
67
68 if( aLeft.when.IsValid() && aLeft.when != aRight.when )
69 return aLeft.when > aRight.when;
70
71 return aLeft.name < aRight.name;
72 } );
73}
74
75
76wxString KIGIT::RelativeDate( const wxDateTime& aWhen )
77{
78 if( !aWhen.IsValid() )
79 return wxEmptyString;
80
81 wxTimeSpan diff = wxDateTime::Now() - aWhen;
82
83 if( diff.GetSeconds() < 60 )
84 return _( "just now" );
85
86 if( diff.GetMinutes() < 60 )
87 return wxString::Format( _( "%d minutes ago" ), static_cast<int>( diff.GetMinutes() ) );
88
89 if( diff.GetHours() < 24 )
90 return wxString::Format( _( "%d hours ago" ), static_cast<int>( diff.GetHours() ) );
91
92 if( diff.GetDays() < 30 )
93 return wxString::Format( _( "%d days ago" ), static_cast<int>( diff.GetDays() ) );
94
95 return aWhen.FormatISODate();
96}
97
98
99void KIGIT::UnsplitKeepingSash( wxSplitterWindow* aSplitter, wxWindow* aPane, int& aSashPos )
100{
101 if( !aSplitter->IsSplit() )
102 return;
103
104 aSashPos = aSplitter->GetSashPosition();
105 aSplitter->Unsplit( aPane );
106}
107
108
109// Markup is what reaches the native button title on every platform, where a foreground colour
110// does not.
111void KIGIT::MarkDestructive( wxButton* aButton )
112{
114 wxColour colour = theme.removed.ToColour();
115
116 aButton->SetLabelMarkup( wxString::Format( wxS( "<span foreground='%s'>%s</span>" ),
117 colour.GetAsString( wxC2S_HTML_SYNTAX ), aButton->GetLabel() ) );
118}
119
120
121// Git records the address without checking it, so this only catches a typo before every
122// commit carries it. A local part, one at sign, and a domain with a dot inside it.
123static bool looksLikeAnEmailAddress( const wxString& aEmail )
124{
125 const size_t at = aEmail.find( '@' );
126
127 if( at == wxString::npos || at == 0 || aEmail.find( '@', at + 1 ) != wxString::npos )
128 return false;
129
130 const wxString domain = aEmail.Mid( at + 1 );
131 const size_t dot = domain.find( '.' );
132
133 if( dot == wxString::npos || dot == 0 || dot + 1 == domain.length() )
134 return false;
135
136 for( const wxUniChar& c : aEmail )
137 {
138 if( wxIsspace( c ) )
139 return false;
140 }
141
142 return true;
143}
144
145
146bool KIGIT::ValidateIdentity( const wxString& aName, const wxString& aEmail, wxString& aError )
147{
148 wxString name = aName;
149
150 if( name.Trim( true ).Trim( false ).IsEmpty() )
151 {
152 aError = _( "Enter your name. It is recorded as the author of your commits." );
153 return false;
154 }
155
156 if( !looksLikeAnEmailAddress( aEmail ) )
157 {
158 aError = _( "Enter a valid email address. It is recorded as the author of your commits." );
159 return false;
160 }
161
162 return true;
163}
164
165
166wxString KIGIT::SelectedText( wxListCtrl* aList, int aColumn )
167{
168 long sel = aList->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
169
170 if( sel == -1 )
171 return wxEmptyString;
172
173 return aList->GetItemText( sel, aColumn );
174}
const char * name
wxColour ToColour() const
Definition color4d.cpp:221
The project's git session.
std::unique_lock< std::mutex > TryLockAction()
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.
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 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:54
Diff colors with defaults for light backgrounds.
Definition diff_scene.h:275
static DIFF_COLOR_THEME ForText(bool aDarkTheme)
Resolve change colors for UI text. Pass the desktop theme captured on the UI thread.