Merge pull request #26350 from jjd-uk/estuary_media_align
[xbmc.git] / xbmc / dialogs / GUIDialogExtendedProgressBar.cpp
blob9fc1c024ea11ad6cf3639f7e513ffb2d8f8e331a
1 /*
2 * Copyright (C) 2012-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "GUIDialogExtendedProgressBar.h"
11 #include "guilib/GUIMessage.h"
12 #include "guilib/GUIProgressControl.h"
13 #include "utils/TimeUtils.h"
15 #include <cmath>
16 #include <mutex>
18 #define CONTROL_LABELHEADER 30
19 #define CONTROL_LABELTITLE 31
20 #define CONTROL_PROGRESS 32
22 #define ITEM_SWITCH_TIME_MS 2000
24 std::string CGUIDialogProgressBarHandle::Text(void) const
26 std::unique_lock<CCriticalSection> lock(m_critSection);
27 std::string retVal(m_strText);
28 return retVal;
31 void CGUIDialogProgressBarHandle::SetText(const std::string &strText)
33 std::unique_lock<CCriticalSection> lock(m_critSection);
34 m_strText = strText;
37 void CGUIDialogProgressBarHandle::SetTitle(const std::string &strTitle)
39 std::unique_lock<CCriticalSection> lock(m_critSection);
40 m_strTitle = strTitle;
43 void CGUIDialogProgressBarHandle::SetProgress(int currentItem, int itemCount)
45 float fPercentage = (currentItem*100.0f)/itemCount;
46 if (!std::isnan(fPercentage))
47 m_fPercentage = std::min(100.0f, fPercentage);
50 CGUIDialogExtendedProgressBar::CGUIDialogExtendedProgressBar(void)
51 : CGUIDialog(WINDOW_DIALOG_EXT_PROGRESS, "DialogExtendedProgressBar.xml", DialogModalityType::MODELESS)
53 m_loadType = LOAD_ON_GUI_INIT;
54 m_iLastSwitchTime = 0;
55 m_iCurrentItem = 0;
58 CGUIDialogProgressBarHandle *CGUIDialogExtendedProgressBar::GetHandle(const std::string &strTitle)
60 CGUIDialogProgressBarHandle *handle = new CGUIDialogProgressBarHandle(strTitle);
62 std::unique_lock<CCriticalSection> lock(m_critSection);
63 m_handles.push_back(handle);
66 Open();
68 return handle;
71 bool CGUIDialogExtendedProgressBar::OnMessage(CGUIMessage& message)
73 switch (message.GetMessage())
75 case GUI_MSG_WINDOW_INIT:
77 m_iLastSwitchTime = CTimeUtils::GetFrameTime();
78 m_iCurrentItem = 0;
79 CGUIDialog::OnMessage(message);
81 UpdateState(0);
82 return true;
84 break;
87 return CGUIDialog::OnMessage(message);
90 void CGUIDialogExtendedProgressBar::Process(unsigned int currentTime, CDirtyRegionList &dirtyregions)
92 if (m_active)
93 UpdateState(currentTime);
95 CGUIDialog::Process(currentTime, dirtyregions);
98 void CGUIDialogExtendedProgressBar::UpdateState(unsigned int currentTime)
100 std::string strHeader;
101 std::string strTitle;
102 float fProgress(-1.0f);
105 std::unique_lock<CCriticalSection> lock(m_critSection);
107 // delete finished items
108 for (int iPtr = m_handles.size() - 1; iPtr >= 0; iPtr--)
110 if (m_handles.at(iPtr)->IsFinished())
112 delete m_handles.at(iPtr);
113 m_handles.erase(m_handles.begin() + iPtr);
117 if (!m_handles.size())
119 Close(false, 0, true, false);
120 return;
123 // ensure the current item is in our range
124 if (m_iCurrentItem >= m_handles.size())
125 m_iCurrentItem = m_handles.size() - 1;
127 // update the current item ptr
128 if (currentTime > m_iLastSwitchTime &&
129 currentTime - m_iLastSwitchTime >= ITEM_SWITCH_TIME_MS)
131 m_iLastSwitchTime = currentTime;
133 // select next item
134 if (++m_iCurrentItem > m_handles.size() - 1)
135 m_iCurrentItem = 0;
138 CGUIDialogProgressBarHandle *handle = m_handles.at(m_iCurrentItem);
139 if (handle)
141 strTitle = handle->Text();
142 strHeader = handle->Title();
143 fProgress = handle->Percentage();
147 SET_CONTROL_LABEL(CONTROL_LABELHEADER, strHeader);
148 SET_CONTROL_LABEL(CONTROL_LABELTITLE, strTitle);
150 if (fProgress > -1.0f)
152 SET_CONTROL_VISIBLE(CONTROL_PROGRESS);
153 CONTROL_SELECT_ITEM(CONTROL_PROGRESS, (unsigned int)fProgress);