summaryrefslogtreecommitdiff
path: root/src/ui/include/screen.hpp
blob: ac7b19f8acc0945dfb6c578901d10cdfbaa93ba9 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * Copyright 2023 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#pragma once

#include <memory>
#include <optional>
#include <vector>

#include "bindey/binding.h"
#include "core/lv_group.h"
#include "core/lv_obj.h"
#include "core/lv_obj_tree.h"
#include "event_binding.hpp"
#include "lvgl.h"
#include "nod/nod.hpp"
#include "widget_top_bar.hpp"

namespace ui {

/*
 * Base class for ever discrete screen in the app. Provides a consistent
 * interface that can be used for transitioning between screens, adding them to
 * back stacks, etc.
 */
class Screen {
 public:
  Screen();
  virtual ~Screen();

  /*
   * Called periodically to allow the screen to update itself, e.g. to handle
   * std::futures that are still loading in.
   */
  virtual auto Tick() -> void {}

  auto UpdateTopBar(const widgets::TopBar::State& state) -> void;

  auto root() -> lv_obj_t* { return root_; }
  auto content() -> lv_obj_t* { return content_; }

  auto modal_content() -> lv_obj_t* { return modal_content_; }
  auto modal_group(lv_group_t* g) -> void { modal_group_ = g; }
  auto group() -> lv_group_t* {
    if (modal_group_) {
      return modal_group_;
    }
    return group_;
  }

 protected:
  auto CreateTopBar(lv_obj_t* parent, const widgets::TopBar::Configuration&)
      -> widgets::TopBar*;

  std::pmr::vector<bindey::scoped_binding> data_bindings_;
  std::pmr::vector<std::unique_ptr<EventBinding>> event_bindings_;

  template <typename T>
  auto lv_bind(lv_obj_t* obj, lv_event_code_t ev, T fn) -> void {
    auto binding = std::make_unique<EventBinding>(obj, ev);
    binding->signal().connect(fn);
    event_bindings_.push_back(std::move(binding));
  }

  lv_obj_t* const root_;
  lv_obj_t* content_;
  lv_obj_t* modal_content_;

  lv_group_t* const group_;
  lv_group_t* modal_group_;

 private:
  std::unique_ptr<widgets::TopBar> top_bar_;
};

class MenuScreen : public Screen {
 public:
  MenuScreen(const std::pmr::string& title, bool show_back_button = true);
};

}  // namespace ui