duckstation

duckstation, but archived from the revision just before upstream changed it to a proprietary software project, this version is the libre one
git clone https://git.neptards.moe/u3shit/duckstation.git
Log | Files | Refs | README | LICENSE

progress_callback.h (3133B)


      1 // SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
      2 // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
      3 
      4 #pragma once
      5 
      6 #include "small_string.h"
      7 #include "types.h"
      8 
      9 #include "fmt/core.h"
     10 
     11 #include <string>
     12 
     13 class ProgressCallback
     14 {
     15 public:
     16   virtual ~ProgressCallback();
     17 
     18   virtual void PushState();
     19   virtual void PopState();
     20 
     21   virtual bool IsCancelled() const;
     22   virtual bool IsCancellable() const;
     23 
     24   virtual void SetCancellable(bool cancellable);
     25 
     26   virtual void SetTitle(const std::string_view title);
     27   virtual void SetStatusText(const std::string_view text);
     28   virtual void SetProgressRange(u32 range);
     29   virtual void SetProgressValue(u32 value);
     30   virtual void IncrementProgressValue();
     31 
     32   virtual void DisplayError(const std::string_view message);
     33   virtual void DisplayWarning(const std::string_view message);
     34   virtual void DisplayInformation(const std::string_view message);
     35   virtual void DisplayDebugMessage(const std::string_view message);
     36 
     37   virtual void ModalError(const std::string_view message);
     38   virtual bool ModalConfirmation(const std::string_view message);
     39   virtual void ModalInformation(const std::string_view message);
     40 
     41 #define MAKE_PROGRESS_CALLBACK_FORWARDER(from, to)                                                                     \
     42   template<typename... T>                                                                                              \
     43   void from(fmt::format_string<T...> fmt, T&&... args)                                                                 \
     44   {                                                                                                                    \
     45     TinyString str;                                                                                                    \
     46     fmt::vformat_to(std::back_inserter(str), fmt, fmt::make_format_args(args...));                                     \
     47     to(str.view());                                                                                                    \
     48   }
     49 
     50   MAKE_PROGRESS_CALLBACK_FORWARDER(FormatStatusText, SetStatusText);
     51   MAKE_PROGRESS_CALLBACK_FORWARDER(FormatError, DisplayError);
     52   MAKE_PROGRESS_CALLBACK_FORWARDER(FormatWarning, DisplayWarning);
     53   MAKE_PROGRESS_CALLBACK_FORWARDER(FormatInformation, DisplayInformation);
     54   MAKE_PROGRESS_CALLBACK_FORWARDER(FormatDebugMessage, DisplayDebugMessage);
     55   MAKE_PROGRESS_CALLBACK_FORWARDER(FormatModalError, ModalError);
     56   MAKE_PROGRESS_CALLBACK_FORWARDER(FormatModalConfirmation, ModalConfirmation);
     57   MAKE_PROGRESS_CALLBACK_FORWARDER(FormatModalInformation, ModalInformation);
     58 
     59 #undef MAKE_PROGRESS_CALLBACK_FORWARDER
     60 
     61 protected:
     62   struct State
     63   {
     64     std::unique_ptr<State> next_saved_state;
     65     std::string status_text;
     66     u32 progress_range;
     67     u32 progress_value;
     68     u32 base_progress_value;
     69     bool cancellable;
     70   };
     71 
     72   bool m_cancellable = false;
     73   bool m_cancelled = false;
     74   std::string m_status_text;
     75   u32 m_progress_range = 1;
     76   u32 m_progress_value = 0;
     77 
     78   u32 m_base_progress_value = 0;
     79 
     80   std::unique_ptr<State> m_saved_state;
     81 
     82 public:
     83   static ProgressCallback* NullProgressCallback;
     84 };