/
switches.h
86 lines (64 loc) · 1.96 KB
/
switches.h
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
85
86
#ifndef RIME_SWITCHES_H_
#define RIME_SWITCHES_H_
#include <rime/common.h>
namespace rime {
class Config;
class ConfigItemRef;
class ConfigMap;
class ConfigValue;
struct StringSlice {
const char* str;
size_t length;
operator string() const {
return str && length ? string(str, length) : string();
}
};
class Switches {
public:
explicit Switches(Config* config) : config_(config) {}
enum SwitchType {
kToggleOption,
kRadioGroup,
};
struct SwitchOption {
an<ConfigMap> the_switch = nullptr;
SwitchType type = kToggleOption;
string option_name;
// reset state value on initialize. -1 if unspecified.
int reset_value = -1;
// index of the switch configuration.
size_t switch_index = 0;
// the index of the option in the radio group.
size_t option_index = 0;
bool found() const {
return bool(the_switch);
}
};
enum FindResult {
kContinue,
kFound,
};
SwitchOption FindOption(function<FindResult (SwitchOption option)> callback);
SwitchOption OptionByName(const string& option_name);
// Returns the switch option defined at switch_index.
// If the swtich is a radio group, return the first option in the group.
SwitchOption ByIndex(size_t switch_index);
static SwitchOption Cycle(const SwitchOption& option);
static SwitchOption Reset(const SwitchOption& option);
static SwitchOption FindRadioGroupOption(
an<ConfigMap> the_switch,
function<FindResult (SwitchOption option)> callback);
static StringSlice GetStateLabel(
an<ConfigMap> the_switch, size_t state_index, bool abbreviated);
StringSlice GetStateLabel(const string& option_name,
int state,
bool abbreviated);
private:
SwitchOption FindOptionFromConfigItem(
ConfigItemRef& item,
size_t switch_index,
function<FindResult (SwitchOption option)> callback);
Config* config_;
};
} // namespace rime
#endif // RIME_SWITCHES_H_