summaryrefslogtreecommitdiff
path: root/src/helpers/AsyncDialogBox.hpp
blob: d3cc242c5725193be49eab3aac5b3d4df13d5ed4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#pragma once

#include "../macros.hpp"
#include "./memory/Memory.hpp"

#include <vector>
#include <functional>

#include <hyprutils/os/Process.hpp>
#include <hyprutils/os/FileDescriptor.hpp>

struct wl_event_source;

class CAsyncDialogBox {
  public:
    static SP<CAsyncDialogBox> create(const std::string& title, const std::string& description, std::vector<std::string> buttons);

    CAsyncDialogBox(const CAsyncDialogBox&)            = delete;
    CAsyncDialogBox(CAsyncDialogBox&&)                 = delete;
    CAsyncDialogBox& operator=(const CAsyncDialogBox&) = delete;
    CAsyncDialogBox& operator=(CAsyncDialogBox&&)      = delete;

    void             open(std::function<void(std::string)> onResolution);
    void             kill();
    bool             isRunning() const;

    void             onWrite(int fd, uint32_t mask);

  private:
    CAsyncDialogBox(const std::string& title, const std::string& description, std::vector<std::string> buttons);

    pid_t                            m_dialogPid       = 0;
    wl_event_source*                 m_readEventSource = nullptr;
    std::function<void(std::string)> m_onResolution;
    Hyprutils::OS::CFileDescriptor   m_pipeReadFd;
    std::string                      m_stdout = "";

    const std::string                m_title;
    const std::string                m_description;
    const std::vector<std::string>   m_buttons;

    // WARNING: cyclic reference. This will be removed once the event source is removed to avoid dangling pointers
    SP<CAsyncDialogBox> m_selfReference;
    WP<CAsyncDialogBox> m_selfWeakReference;
};