blob: afb41109b96c60a7eaf07c88ce9da25a50820231 [file] [log] [blame]
Adedeji Adebisi12c5f112021-07-22 18:07:52 +00001// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16#include "main.hpp"
17
18#include <ncurses.h>
19
20#include <string>
21#include <vector>
22class DBusTopWindow;
23class ArrowKeyNavigationMenu
24{
25 public:
26 explicit ArrowKeyNavigationMenu(WINDOW* win) :
27 win_(win), h_padding_(2), col_width_(15), h_spacing_(2), idx0_(INVALID),
28 idx1_(INVALID), choice_(INVALID), parent_(nullptr)
29 {}
30 explicit ArrowKeyNavigationMenu(DBusTopWindow* view);
31 void LoadDummyValues()
32 {
33 items_.clear();
34 items_.push_back("Sender");
35 items_.push_back("Destination");
36 items_.push_back("Interface");
37 items_.push_back("Path");
38 items_.push_back("Member");
39 }
40
41 void OnKeyDown(const std::string& key);
42 void Render();
43 void MoveCursorAlongSecondaryAxis(int delta);
44 void MoveCursorAlongPrimaryAxis(int delta);
45 int DispEntriesPerRow()
46 {
47 int ncols = 0;
48 while (true)
49 {
50 int next = ncols + 1;
51 int w = 2 * h_padding_ + col_width_ * next;
52 if (next > 1)
53 w += (next - 1) * h_spacing_;
54 if (w <= rect_.w - 2)
55 {
56 ncols = next;
57 }
58 else
59 {
60 break;
61 }
62 }
63 return ncols;
64 }
65
66 int DispEntriesPerColumn()
67 {
68 return rect_.h;
69 }
70
71 void SetRect(const Rect& rect)
72 {
73 rect_ = rect;
74 }
75
76 enum Order
77 {
78 ColumnMajor,
79 RowMajor,
80 };
81
82 void SetOrder(Order o)
83 {
84 order = o;
85 }
86
87 int Choice()
88 {
89 return choice_;
90 }
91
92 void Deselect()
93 {
94 choice_ = INVALID;
95 }
96
97 bool Empty()
98 {
99 return items_.empty();
100 }
101
102 void SetChoiceAndConstrain(int c);
103 Rect rect_;
104 void AddItem(const std::string& s);
105 bool RemoveHighlightedItem(std::string* ret); // returns true if successful
106 std::vector<std::string> Items()
107 {
108 return items_;
109 }
110
111 void do_Render(bool is_column_major);
112 std::vector<std::string> items_;
113 WINDOW* win_;
114 int h_padding_;
115 int col_width_;
116 int h_spacing_;
117 int idx0_, idx1_;
118 int choice_;
119 DBusTopWindow* parent_;
120 Order order;
121};
122