Skip to content

Commit

Permalink
show spinner for browser refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
chrxh committed Oct 10, 2024
1 parent 45f83c8 commit 5bf59b7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
32 changes: 32 additions & 0 deletions source/Gui/AlienImGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,38 @@ bool AlienImGui::Button(ButtonParameters const& parameters)
return result;
}

void AlienImGui::Spinner(SpinnerParameters const& parameters)
{
static std::optional<std::chrono::steady_clock::time_point> spinnerRefTimepoint;
static float spinnerAngle = 0;
if (!spinnerRefTimepoint.has_value()) {
spinnerRefTimepoint = std::chrono::steady_clock::now();
}
auto now = std::chrono::steady_clock::now();
auto duration = toFloat(std::chrono::duration_cast<std::chrono::milliseconds>(now - *spinnerRefTimepoint).count());

ImDrawList* drawList = ImGui::GetWindowDrawList();

AlienImGui::RotateStart(drawList);
auto font = StyleRepository::get().getIconFont();
auto text = ICON_FA_SPINNER;
ImVec4 clipRect(-100000.0f, -100000.0f, 100000.0f, 100000.0f);
font->RenderText(
drawList,
scale(30.0f),
{parameters._pos.x - scale(15.0f), parameters._pos.y - scale(80.0f)},
ImColor::HSV(0.5f, 0.1f, 1.0f, std::min(1.0f, duration / 500)),
clipRect,
text,
text + strlen(text),
0.0f,
false);

auto angle = sinf(duration * Const::DegToRad / 10 - Const::Pi / 2) * 4 + 6.0f;
spinnerAngle += angle;
AlienImGui::RotateEnd(spinnerAngle, drawList);
}

void AlienImGui::Tooltip(std::string const& text, bool delay)
{
if (ImGui::IsItemHovered() && (!delay || (delay && GImGui->HoveredIdTimer > HoveredTimer))) {
Expand Down
6 changes: 6 additions & 0 deletions source/Gui/AlienImGui.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,12 @@ class AlienImGui
};
static bool Button(ButtonParameters const& parameters);

struct SpinnerParameters
{
MEMBER_DECLARATION(SpinnerParameters, RealVector2D, pos, RealVector2D());
};
static void Spinner(SpinnerParameters const& parameters);

static void Tooltip(std::string const& text, bool delay = true);
static void Tooltip(std::function<std::string()> const& textFunc, bool delay = true);

Expand Down
6 changes: 3 additions & 3 deletions source/Gui/BrowserWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ void _BrowserWindow::processToolbar()

//refresh button
if (AlienImGui::ToolbarButton(ICON_FA_SYNC)) {
delayedExecution([this] { onRefresh(); });
printOverlayMessage("Refreshing ...");
onRefresh();
}
AlienImGui::Tooltip("Refresh");

Expand Down Expand Up @@ -1173,14 +1172,15 @@ void _BrowserWindow::processRefreshingScreen(RealVector2D const& startPos)
{
if (!_pendingRefreshRequestIds.empty()) {
auto color = ImColor(ImGui::GetStyleColorVec4(ImGuiCol_WindowBg));
color.Value.w = 0.8f;
color.Value.w = 0.5f;
auto size = ImGui::GetItemRectSize();
auto afterTablePos = ImGui::GetCursorScreenPos();

ImGui::SetCursorScreenPos({startPos.x, startPos.y});
if (ImGui::BeginChild("##overlay", {size.x, size.y}, 0, ImGuiWindowFlags_NoScrollbar)) {
ImDrawList* drawList = ImGui::GetWindowDrawList();
drawList->AddRectFilled({startPos.x, startPos.y}, {startPos.x + size.x, startPos.y + size.y}, color);
AlienImGui::Spinner(AlienImGui::SpinnerParameters().pos({startPos.x + size.x / 2, startPos.y + size.y / 2}));
}
ImGui::EndChild();
ImGui::SetCursorScreenPos(afterTablePos);
Expand Down

0 comments on commit 5bf59b7

Please sign in to comment.