Skip to content

Commit c0d35c2

Browse files
committed
rocket: auto rotate on pogo keyboard connect
1 parent 54d778d commit c0d35c2

5 files changed

Lines changed: 55 additions & 10 deletions

File tree

apps/rocket/AppWidgets.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <UI.h>
4+
#include <UI/Rotate.h>
45

56
#include "App.h"
67

@@ -12,10 +13,12 @@ class RunningAppWidget : public rmlib::StatelessWidget<RunningAppWidget> {
1213
RunningAppWidget(const App& app,
1314
rmlib::Callback onTap,
1415
rmlib::Callback onKill,
15-
bool isCurrent)
16+
bool isCurrent,
17+
rmlib::Rotation rotation)
1618
: app(app)
1719
, onTap(std::move(onTap))
1820
, onKill(std::move(onKill))
21+
, rotation(rotation)
1922
, isCurrent(isCurrent) {}
2023

2124
auto build(rmlib::AppContext& /*unused*/,
@@ -26,7 +29,7 @@ class RunningAppWidget : public rmlib::StatelessWidget<RunningAppWidget> {
2629
: getMissingImage().canvas;
2730

2831
return container(
29-
Column(GestureDetector(Sized(Image(canvas), 234, 300),
32+
Column(GestureDetector(Rotated(rotation, Sized(Image(canvas), 234, 300)),
3033
Gestures{}.onTap(onTap)),
3134
Row(Text(app.description().name), Button("X", onKill))),
3235
Insets::all(isCurrent ? 1 : 2),
@@ -38,6 +41,7 @@ class RunningAppWidget : public rmlib::StatelessWidget<RunningAppWidget> {
3841
const App& app;
3942
rmlib::Callback onTap;
4043
rmlib::Callback onKill;
44+
rmlib::Rotation rotation;
4145
bool isCurrent;
4246
};
4347

apps/rocket/Launcher.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ LauncherState
109109
LauncherWidget::createState() {
110110
return LauncherState{};
111111
}
112+
112113
void
113114
LauncherState::init(rmlib::AppContext& context,
114115
const rmlib::BuildContext& /*unused*/) {
@@ -139,6 +140,10 @@ LauncherState::init(rmlib::AppContext& context,
139140
}
140141
},
141142
std::chrono::minutes(1));
143+
144+
updateRotation(context);
145+
context.onDeviceUpdate(
146+
[this, &context] { modify().updateRotation(context); });
142147
}
143148

144149
bool
@@ -378,3 +383,10 @@ void
378383
LauncherState::resetInactivity() const {
379384
inactivityCountdown = default_inactivity_timeout;
380385
}
386+
387+
void
388+
LauncherState::updateRotation(rmlib::AppContext& ctx) {
389+
rotation = ctx.getInputManager().getBaseDevices().pogoKeyboard != nullptr
390+
? Rotation::Clockwise
391+
: Rotation::None;
392+
}

apps/rocket/Launcher.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "AppWidgets.h"
55

66
#include <UI.h>
7+
#include <UI/Rotate.h>
78

89
class LauncherState;
910

@@ -75,7 +76,8 @@ class LauncherState : public rmlib::StateBase<LauncherWidget> {
7576
self.stopTimer();
7677
});
7778
},
78-
app.description().path == currentAppPath);
79+
app.description().path == currentAppPath,
80+
invert(rotation));
7981
}
8082
}
8183
return Wrap(widgets);
@@ -119,16 +121,18 @@ class LauncherState : public rmlib::StateBase<LauncherWidget> {
119121

120122
auto ui = [&]() -> DynamicWidget {
121123
if (visible) {
122-
return launcher(context);
124+
return Rotated(rotation, launcher(context));
123125
}
124126

125127
if (background == nullptr) {
126128
return Colored(white);
127129
}
128130

129131
if (backgroundSize.has_value()) {
130-
return Center(Sized(
131-
Image(*background), backgroundSize->width, backgroundSize->height));
132+
return Center(Rotated(rotation,
133+
Sized(Image(*background),
134+
backgroundSize->width,
135+
backgroundSize->height)));
132136
}
133137
return Image(*background);
134138
}();
@@ -168,6 +172,8 @@ class LauncherState : public rmlib::StateBase<LauncherWidget> {
168172

169173
void resetInactivity() const;
170174

175+
void updateRotation(rmlib::AppContext& ctx);
176+
171177
std::vector<App> apps;
172178
std::string currentAppPath;
173179

@@ -179,6 +185,8 @@ class LauncherState : public rmlib::StateBase<LauncherWidget> {
179185
const rmlib::Canvas* fbCanvas = nullptr;
180186
rmlib::input::InputDeviceBase* touchDevice = nullptr;
181187

188+
rmlib::Rotation rotation = rmlib::Rotation::None;
189+
182190
int sleepCountdown = -1;
183191
mutable int inactivityCountdown = default_inactivity_timeout;
184192
bool visible = true;

test/unit/TestRocket.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ TEST_CASE("AppWidget", "[rocket]") {
166166

167167
bool current = GENERATE(true, false);
168168

169-
ctx.pumpWidget(Center(
170-
RunningAppWidget(app, [&] { tapped++; }, [&] { killed++; }, current)));
169+
ctx.pumpWidget(Center(RunningAppWidget(
170+
app, [&] { tapped++; }, [&] { killed++; }, current, Rotation::None)));
171171

172172
auto appWidget = ctx.findByType<RunningAppWidget>();
173173
REQUIRE_THAT(
@@ -183,12 +183,12 @@ TEST_CASE("AppWidget", "[rocket]") {
183183
}
184184
}
185185

186-
TEST_CASE("Launcher", "[rocket]") {
186+
TemporaryDirectory
187+
makeDrafts() {
187188
TemporaryDirectory tmp;
188189

189190
const auto configPath = tmp.dir / ".config" / "draft";
190191
REQUIRE_NOTHROW(std::filesystem::create_directories(configPath));
191-
setenv("HOME", tmp.dir.c_str(), true);
192192

193193
writeFile(configPath / "a.draft", R"(
194194
name=a
@@ -202,6 +202,24 @@ name=b
202202
call=yes
203203
)");
204204

205+
return tmp;
206+
}
207+
208+
TEST_CASE("Landscape", "[rocket]") {
209+
auto tmp = makeDrafts();
210+
setenv("HOME", tmp.dir.c_str(), true);
211+
212+
auto ctx = TestContext::make(/*keyboardAttached=*/true);
213+
ctx.pumpWidget(Center(LauncherWidget()));
214+
auto launcher = ctx.findByType<LauncherWidget>();
215+
216+
REQUIRE_THAT(launcher, ctx.matchesGolden("rocket-landscape.png"));
217+
}
218+
219+
TEST_CASE("Launcher", "[rocket]") {
220+
auto tmp = makeDrafts();
221+
setenv("HOME", tmp.dir.c_str(), true);
222+
205223
auto ctx = TestContext::make();
206224
ctx.pumpWidget(Center(LauncherWidget()));
207225
auto launcher = ctx.findByType<LauncherWidget>();
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)