Fcitx
inputmethodgroup.cpp
1 /*
2  * SPDX-FileCopyrightText: 2016-2016 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 
8 #include "inputmethodgroup.h"
9 #include <algorithm>
10 #include <memory>
11 #include <string>
12 #include <utility>
13 #include <vector>
14 #include "fcitx-utils/log.h"
15 #include "fcitx-utils/macros.h"
16 
17 namespace fcitx {
18 
19 namespace {
20 const std::string emptyString;
21 }
22 
24 public:
25  InputMethodGroupItemPrivate(std::string name) : name_(std::move(name)) {}
26  FCITX_INLINE_DEFINE_DEFAULT_DTOR_COPY_AND_MOVE_WITHOUT_SPEC(
28 
29  std::string name_;
30  std::string layout_;
31 };
32 
34 public:
35  InputMethodGroupPrivate(std::string name) : name_(std::move(name)) {}
36 
37  std::string name_;
38  std::vector<InputMethodGroupItem> inputMethodList_;
39  std::string defaultInputMethod_;
40  std::string defaultLayout_;
41 };
42 
43 InputMethodGroupItem::InputMethodGroupItem(const std::string &name)
44  : d_ptr(std::make_unique<InputMethodGroupItemPrivate>(name)) {}
45 
46 FCITX_DEFINE_DPTR_COPY_AND_DEFAULT_DTOR_AND_MOVE(InputMethodGroupItem);
47 
48 const std::string &InputMethodGroupItem::name() const {
49  FCITX_D();
50  return d->name_;
51 }
52 
53 const std::string &InputMethodGroupItem::layout() const {
54  FCITX_D();
55  return d->layout_;
56 }
57 
58 LogMessageBuilder &operator<<(LogMessageBuilder &builder,
59  const InputMethodGroupItem &groupItem) {
60  builder << "InputMethodGroupItem(" << groupItem.name()
61  << ",layout=" << groupItem.layout() << ")";
62  return builder;
63 }
64 
66 InputMethodGroupItem::setLayout(const std::string &layout) {
67  FCITX_D();
68  d->layout_ = layout;
69  return *this;
70 }
71 
72 InputMethodGroup::InputMethodGroup(const std::string &name)
73  : d_ptr(std::make_unique<InputMethodGroupPrivate>(name)) {}
74 
75 FCITX_DEFINE_DPTR_COPY_AND_DEFAULT_DTOR_AND_MOVE(InputMethodGroup);
76 
77 const std::string &InputMethodGroup::name() const {
78  FCITX_D();
79  return d->name_;
80 }
81 
82 std::vector<InputMethodGroupItem> &InputMethodGroup::inputMethodList() {
83  FCITX_D();
84  return d->inputMethodList_;
85 }
86 
87 const std::vector<InputMethodGroupItem> &
88 InputMethodGroup::inputMethodList() const {
89  FCITX_D();
90  return d->inputMethodList_;
91 }
92 
93 void InputMethodGroup::setDefaultInputMethod(const std::string &im) {
94  FCITX_D();
95  if (std::ranges::any_of(d->inputMethodList_,
96  [&im](const InputMethodGroupItem &item) {
97  return item.name() == im;
98  })) {
99  if (d->inputMethodList_.size() > 1 &&
100  d->inputMethodList_[0].name() == im) {
101  d->defaultInputMethod_ = d->inputMethodList_[1].name();
102  } else {
103  d->defaultInputMethod_ = im;
104  }
105  } else {
106  if (d->inputMethodList_.size() > 1) {
107  d->defaultInputMethod_ = d->inputMethodList_[1].name();
108  } else {
109  d->defaultInputMethod_ = d->inputMethodList_.empty()
110  ? ""
111  : d->inputMethodList_[0].name();
112  }
113  }
114 }
115 
116 const std::string &InputMethodGroup::layoutFor(const std::string &im) const {
117  FCITX_D();
118  auto iter = std::ranges::find_if(
119  d->inputMethodList_,
120  [&im](const InputMethodGroupItem &item) { return item.name() == im; });
121  if (iter != d->inputMethodList_.end()) {
122  return iter->layout();
123  }
124  return emptyString;
125 }
126 
127 const std::string &InputMethodGroup::defaultInputMethod() const {
128  FCITX_D();
129  return d->defaultInputMethod_;
130 }
131 
132 void InputMethodGroup::setDefaultLayout(const std::string &layout) {
133  FCITX_D();
134  d->defaultLayout_ = layout;
135 }
136 
137 const std::string &InputMethodGroup::defaultLayout() const {
138  FCITX_D();
139  return d->defaultLayout_;
140 }
141 } // namespace fcitx
Definition: action.cpp:17
Log utilities.