Skip to content

Commit 595ea97

Browse files
committed
karm-ui: Allow focusable to steal focus.
1 parent 41bd1cd commit 595ea97

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/karm-ui/focus.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,23 @@ export struct FocusListener {
4444
}
4545
};
4646

47+
struct FocusableProps {
48+
bool visual = true;
49+
bool steal = false;
50+
};
51+
4752
struct Focusable : ProxyNode<Focusable> {
4853
bool _focused = false;
54+
FocusableProps _props;
4955

50-
Focusable(Ui::Child child)
51-
: ProxyNode<Focusable>(std::move(child)) {
56+
Focusable(Ui::Child child, FocusableProps props)
57+
: ProxyNode<Focusable>(std::move(child)), _props(props) {
5258
}
5359

5460
void paint(Gfx::Canvas& g, Math::Recti r) override {
5561
ProxyNode<Focusable>::paint(g, r);
5662

57-
if (_focused) {
63+
if (_props.visual and _focused) {
5864
g.strokeStyle(Gfx::stroke(ACCENT500).withWidth(2).withAlign(Gfx::INSIDE_ALIGN));
5965
g.stroke(bound().cast<f64>(), 4);
6066
}
@@ -93,17 +99,23 @@ struct Focusable : ProxyNode<Focusable> {
9399
}
94100

95101
if (_focused or passthrough)
96-
ProxyNode<Focusable>::event(e);
102+
ProxyNode::event(e);
103+
104+
if (e.is<RebuiltEvent>() and
105+
_props.steal and
106+
not _focused) {
107+
_stealFocus();
108+
}
97109
}
98110
};
99111

100-
export Ui::Child focusable(Ui::Child child) {
101-
return makeRc<Focusable>(std::move(child));
112+
export Ui::Child focusable(Ui::Child child, FocusableProps props = {}) {
113+
return makeRc<Focusable>(std::move(child), props);
102114
}
103115

104-
export auto focusable() {
105-
return [](Ui::Child child) {
106-
return focusable(std::move(child));
116+
export auto focusable(FocusableProps props = {}) {
117+
return [props](Ui::Child child) {
118+
return focusable(std::move(child), props);
107119
};
108120
}
109121

0 commit comments

Comments
 (0)