Skip to content

Commit 6f26bdf

Browse files
committed
Detect dark mode in iOS
1 parent 1fd9117 commit 6f26bdf

File tree

5 files changed

+31
-0
lines changed

5 files changed

+31
-0
lines changed

src/core/include/growl/core/api/api_internal.h

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class SystemAPIInternal : public SystemAPI, public APIInternal {
2626
virtual ~SystemAPIInternal() {}
2727
// Used in push-style touch input e.g. on iOS.
2828
virtual void onTouch(InputTouchEvent touch) {}
29+
virtual void setDarkMode(bool dark_mode) {}
2930
};
3031

3132
class GraphicsAPIInternal : public GraphicsAPI, public APIInternal {

src/core/include/growl/core/api/system_api.h

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ class SystemAPI {
4545

4646
virtual void setLogLevel(LogLevel log_level) = 0;
4747

48+
virtual bool isDarkMode() {
49+
return false;
50+
}
51+
4852
virtual Result<std::unique_ptr<File>>
4953
openFile(std::string path, size_t start = 0, size_t end = 0) = 0;
5054

src/platforms/ios/src/view_controller.mm

+13
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ - (void)viewDidLoad {
4747
<< std::endl;
4848
exit(1);
4949
}
50+
51+
systemInternal.setDarkMode(
52+
self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark);
53+
5054
if (auto err = graphicsInternal.init()) {
5155
api->system().log(
5256
Growl::LogLevel::Fatal, "ViewController",
@@ -159,4 +163,13 @@ - (void)viewWillAppear:(BOOL)animated {
159163
game->resize(w, h);
160164
}
161165

166+
- (void)willTransitionToTraitCollection:(UITraitCollection*)newCollection
167+
withTransitionCoordinator:
168+
(id<UIViewControllerTransitionCoordinator>)coordinator {
169+
auto& systemInternal =
170+
static_cast<Growl::SystemAPIInternal&>(api->system());
171+
systemInternal.setDarkMode(
172+
newCollection.userInterfaceStyle == UIUserInterfaceStyleDark);
173+
}
174+
162175
@end

src/plugins/ios/src/ios_system.h

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class IOSSystemAPI : public SystemAPIInternal {
1717
bool isRunning() override {
1818
return running;
1919
}
20+
21+
void setDarkMode(bool dark_mode) override;
22+
bool isDarkMode() override;
23+
2024
virtual Result<std::unique_ptr<Window>>
2125
createWindow(const Config& config) override;
2226
void setLogLevel(LogLevel log_level) override;
@@ -36,6 +40,7 @@ class IOSSystemAPI : public SystemAPIInternal {
3640

3741
API& api;
3842
bool running;
43+
bool dark_mode;
3944
GCController* controller = nullptr;
4045
id game_controller_connect_observer;
4146
id game_controller_disconnect_observer;

src/plugins/ios/src/ios_system.mm

+8
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@
9393
OS_LOG_DEFAULT, OS_LOG_TYPE_DEBUG, "[%s] %s", tag.c_str(), msg.c_str());
9494
}
9595

96+
void IOSSystemAPI::setDarkMode(bool dark_mode) {
97+
this->dark_mode = dark_mode;
98+
}
99+
100+
bool IOSSystemAPI::isDarkMode() {
101+
return dark_mode;
102+
}
103+
96104
Result<std::unique_ptr<File>>
97105
IOSSystemAPI::openFile(std::string path, size_t start, size_t end) {
98106
auto full_path =

0 commit comments

Comments
 (0)