LCOV - code coverage report
Current view: top level - src - ui.hh (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 7 7 100.0 %
Date: 2022-06-15 20:16:21 Functions: 0 0 -
Branches: 12 14 85.7 %

           Branch data     Line data    Source code
       1                 :            : #ifndef bqtTermUI_HH
       2                 :            : #define bqtTermUI_HH
       3                 :            : /** @file ui.hh
       4                 :            :  * @brief User interface.
       5                 :            :  */
       6                 :            : 
       7                 :            : #include <string_view>
       8                 :            : #include <cstdint>
       9                 :            : #include <utility>
      10                 :            : #include <variant>
      11                 :            : 
      12                 :            : #include "keysym.hh"
      13                 :            : 
      14                 :            : /** User interface class. */
      15                 :            : class UI
      16                 :            : {
      17                 :            :     // Font geometry in pixels
      18                 :            :     unsigned VidCellWidth  = 8;
      19                 :            :     unsigned VidCellHeight = 16;
      20                 :            :     // Window geometry in cells
      21                 :            :     unsigned WindowWidth  = 80;
      22                 :            :     unsigned WindowHeight = 25;
      23                 :            :     /// Headless? Disables window creation (useless without autoinput & video recording)
      24                 :            :     bool Headless = false;
      25                 :            : 
      26                 :            :     /// Allow windows bigger than desktop? Setting this "true"
      27                 :            :     /// also disables reacting to window resizes.
      28                 :            :     bool Allow_Windows_Bigger_Than_Desktop = false;
      29                 :            : public:
      30                 :            :     /** Constructor */
      31                 :            :     UI();
      32                 :            :     /** Destructor */
      33                 :            :     ~UI();
      34                 :            : 
      35                 :            :     /** @returns currently configured font cell size */
      36                 :          8 :     std::pair<unsigned,unsigned> GetCellSize() const
      37                 :            :     {
      38         [ +  + ]:          8 :         return {VidCellWidth,VidCellHeight};
      39                 :            :     }
      40                 :            :     /** @returns currently configured window size in cells */
      41                 :          2 :     std::pair<unsigned,unsigned> GetWindowSize() const
      42                 :            :     {
      43         [ +  - ]:          2 :         return {WindowWidth,WindowHeight};
      44                 :            :     }
      45                 :            :     /** @returns true if the terminal is started in headless mode */
      46                 :       4866 :     bool IsHeadless() const
      47                 :            :     {
      48   [ +  +  +  +  :       4866 :         return Headless;
             +  +  +  + ]
      49                 :            :     }
      50                 :            : 
      51                 :            :     /** Attempts to resize the terminal window.
      52                 :            :      * @param cellx New font cell width
      53                 :            :      * @param celly New font cell height
      54                 :            :      * @param width New window width in cells
      55                 :            :      * @param height New window height in cells
      56                 :            :      */
      57                 :            :     void ResizeTo(unsigned cellx,unsigned celly, unsigned width,unsigned height);
      58                 :            : 
      59                 :            :     /** Changes the window title to @param str new window title. */
      60                 :            :     void SetWindowTitle(std::string_view str);
      61                 :            :     /** Changes the icon name. Unimplemented. @param str new icon name. */
      62                 :            :     void SetIconName(std::string_view str);
      63                 :            :     /** Issues a short beep, if supported. */
      64                 :            :     void BeepOn();
      65                 :            : 
      66                 :            :     /** Updates the graphics within the window to the contents of the supplied buffer.
      67                 :            :      * @param pixels RGB pixels. The buffer size must be at least cellx*celly*winx*winy units.
      68                 :            :      */
      69                 :            :     void PresentGraphics(const std::uint32_t* pixels);
      70                 :            : 
      71                 :            :     /** Possible kinds of resize, @see EventType */
      72                 :            :     enum resizetype { cellx,celly,winx,winy };
      73                 :            : 
      74                 :            :     /** The return value of HandleEvents.
      75                 :            :      * The first element is text input (string).
      76                 :            :      * The second element is one of the following:
      77                 :            :      *   std::pair<int,int>, expressing new window size in pixels if the window has been resized from GUI.<br>
      78                 :            :      *   std::pair<int,resizetype>, expressing the user's wish to perform a change in rendering proportions.<br>
      79                 :            :      *   bool, true if the terminal should be terminated; false if none of above happened.
      80                 :            :      */
      81                 :            :     using EventType = std::pair<
      82                 :            :         std::string,                    ///< text input
      83                 :            :         std::variant<
      84                 :            :             std::pair<int,int>,         ///< window resized from GUI to these pixel sizes
      85                 :            :             std::pair<int, resizetype>, ///< size changed: -1=dec, 1=inc, 0= nothing
      86                 :            :             bool                        ///< quit flag
      87                 :            :         >>;
      88                 :            : 
      89                 :            :     /** Polls (non-blocking) for UI events. @see EventType for possible events.
      90                 :            :      * @param permit_text_input If this parameter is false, text input from keyboard is not accepted.
      91                 :            :      */
      92                 :            :     EventType HandleEvents(bool permit_text_input);
      93                 :            : 
      94                 :            :     /** Change headless state. Note: Once a window has been opened, changing this setting does not close it. */
      95         [ +  - ]:          2 :     void SetHeadless(bool state) { Headless = state; }
      96                 :            : 
      97                 :            : private:
      98                 :            :     // Delete copies
      99                 :            :     UI(const UI&) = delete;
     100                 :            :     UI(UI&&) = delete;
     101                 :            :     UI& operator=(const UI&) = delete;
     102                 :            :     UI& operator=(UI&&) = delete;
     103                 :            : };
     104                 :            : 
     105                 :            : /** UI sentinel object. */
     106                 :            : extern UI ui;
     107                 :            : 
     108                 :            : #endif

Generated by: LCOV version 1.16