HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
audio_device_widget.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
9#pragma once
10
11#include "selection_widget.hpp"
12#include "grid_widget.hpp"
13#include "../GUI/module.hpp"
14#include "../audio/audio_system.hpp"
15#include "../audio/audio_device.hpp"
16#include "../audio/audio_direction.hpp"
17#include "../label.hpp"
18#include <memory>
19#include <string>
20#include <array>
21#include <optional>
22#include <future>
23
24namespace hi { inline namespace v1 {
25
29template<fixed_string Name = "">
30class audio_device_widget final : public widget {
31public:
32 using super = widget;
33 constexpr static auto prefix = Name / "audio-device";
34
37 observer<std::string> device_id;
38
41 observer<audio_direction> direction = audio_direction::bidirectional;
42
43 audio_device_widget(widget *parent, hi::audio_system& audio_system) noexcept :
44 super(parent), _audio_system(&audio_system)
45 {
46 _grid_widget = std::make_unique<grid_widget<prefix>>(this);
47 _device_selection_widget = &_grid_widget->make_widget<selection_widget<prefix>>("A1", device_id, _device_list);
48
49 _sync_device_list_task = sync_device_list();
50 }
51
53 [[nodiscard]] generator<widget const&> children(bool include_invisible) const noexcept override
54 {
55 co_yield *_grid_widget;
56 }
57
58 [[nodiscard]] box_constraints update_constraints() noexcept override
59 {
60 _grid_constraints = _grid_widget->update_constraints();
61 return _grid_constraints;
62 }
63
64 void set_layout(widget_layout const& context) noexcept override
65 {
66 if (compare_store(layout, context)) {
67 hilet grid_rectangle = context.rectangle();
68 _grid_shape = {_grid_constraints, grid_rectangle, theme<prefix>.cap_height(this)};
69 }
70
71 _grid_widget->set_layout(context.transform(_grid_shape));
72 }
73
74 void draw(widget_draw_context& context) noexcept override
75 {
77 _grid_widget->draw(context);
78 }
79 }
80
81 hitbox hitbox_test(point2 position) const noexcept override
82 {
83 if (*mode >= widget_mode::partial) {
84 auto r = hitbox{};
85 r = _grid_widget->hitbox_test_from_parent(position, r);
86 return r;
87 } else {
88 return hitbox{};
89 }
90 }
91
92 [[nodiscard]] bool accepts_keyboard_focus(keyboard_focus_group group) const noexcept override
93 {
94 if (*mode >= widget_mode::partial) {
95 return _grid_widget->accepts_keyboard_focus(group);
96 } else {
97 return false;
98 }
99 }
100
102private:
103 hi::audio_system *_audio_system;
104
108 box_constraints _grid_constraints;
109 box_shape _grid_shape;
110
113 selection_widget<prefix> *_device_selection_widget = nullptr;
114
115 observer<std::vector<std::pair<std::string, label>>> _device_list;
116
117 hi::scoped_task<> _sync_device_list_task;
118
119 [[nodiscard]] hi::scoped_task<> sync_device_list() noexcept
120 {
121 while (true) {
122 {
123 auto proxy = _device_list.copy();
124 proxy->clear();
125 for (auto& device : _audio_system->devices()) {
126 if (device.state() == hi::audio_device_state::active and to_bool(device.direction() & *direction)) {
127 proxy->emplace_back(device.id(), device.label());
128 }
129 }
130 }
131
132 co_await when_any(*_audio_system, direction);
133 }
134 }
135};
136
137}} // namespace hi::v1
Functionality for labels, text and icons.
Defines selection_widget.
Defines grid_widget.
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
@ partial
A widget is partially enabled.
@ invisible
The widget is invisible.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
bool compare_store(T &lhs, U &&rhs) noexcept
Compare then store if there was a change.
Definition utility.hpp:212
Definition widget.hpp:26
widget * parent
Pointer to the parent widget.
Definition widget.hpp:40
observer< widget_mode > mode
The widget mode.
Definition widget.hpp:49
2D constraints.
Definition box_constraints.hpp:22
Audio device configuration widget.
Definition audio_device_widget.hpp:30
observer< std::string > device_id
The audio device this widget has selected and is configuring.
Definition audio_device_widget.hpp:37
observer< audio_direction > direction
The audio direction (input or output) of devices is should show.
Definition audio_device_widget.hpp:41
A graphical control element that allows the user to choose only one of a predefined set of mutually e...
Definition selection_widget.hpp:45