Skip to content

Commit b5a112f

Browse files
committed
Refactor image handling and tray icon conversion
Improves code formatting and consistency across Linux, macOS, and Windows image implementations. Adds a Windows-specific helper to convert Image objects to HICON, and updates tray icon logic to use this conversion for proper system tray icon sizing. Minor code cleanups and reordering of includes for clarity.
1 parent 0eba255 commit b5a112f

File tree

5 files changed

+137
-62
lines changed

5 files changed

+137
-62
lines changed

src/capi/tray_manager_c.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ bool native_tray_manager_is_supported(void) {
1616

1717
native_tray_icon_t native_tray_manager_get(native_tray_icon_id_t tray_icon_id) {
1818
try {
19-
auto tray_icon = TrayManager::GetInstance().Get(static_cast<TrayIconId>(tray_icon_id));
19+
auto tray_icon =
20+
TrayManager::GetInstance().Get(static_cast<TrayIconId>(tray_icon_id));
2021
return tray_icon ? static_cast<native_tray_icon_t>(tray_icon.get())
2122
: nullptr;
2223
} catch (...) {

src/platform/linux/image_linux.cpp

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#include <gtk/gtk.h>
21
#include <gdk-pixbuf/gdk-pixbuf.h>
32
#include <glib.h>
3+
#include <gtk/gtk.h>
44
#include <cstring>
55
#include <memory>
66
#include <string>
@@ -27,7 +27,10 @@ class Image::Impl {
2727
}
2828

2929
Impl(const Impl& other)
30-
: pixbuf_(nullptr), source_(other.source_), size_(other.size_), format_(other.format_) {
30+
: pixbuf_(nullptr),
31+
source_(other.source_),
32+
size_(other.size_),
33+
format_(other.format_) {
3134
if (other.pixbuf_) {
3235
pixbuf_ = gdk_pixbuf_copy(other.pixbuf_);
3336
}
@@ -54,7 +57,8 @@ Image::Image() : pimpl_(std::make_unique<Impl>()) {}
5457

5558
Image::~Image() = default;
5659

57-
Image::Image(const Image& other) : pimpl_(std::make_unique<Impl>(*other.pimpl_)) {}
60+
Image::Image(const Image& other)
61+
: pimpl_(std::make_unique<Impl>(*other.pimpl_)) {}
5862

5963
Image::Image(Image&& other) noexcept : pimpl_(std::move(other.pimpl_)) {}
6064

@@ -71,7 +75,8 @@ std::shared_ptr<Image> Image::FromFile(const std::string& file_path) {
7175
// Get actual image size
7276
int width = gdk_pixbuf_get_width(pixbuf);
7377
int height = gdk_pixbuf_get_height(pixbuf);
74-
image->pimpl_->size_ = {static_cast<double>(width), static_cast<double>(height)};
78+
image->pimpl_->size_ = {static_cast<double>(width),
79+
static_cast<double>(height)};
7580

7681
// Determine format from file extension
7782
size_t dotPos = file_path.find_last_of('.');
@@ -115,15 +120,15 @@ std::shared_ptr<Image> Image::FromFile(const std::string& file_path) {
115120
// Helper function to decode base64
116121
static std::vector<unsigned char> DecodeBase64(const std::string& base64_data) {
117122
std::vector<unsigned char> result;
118-
123+
119124
gsize out_len = 0;
120125
guchar* decoded = g_base64_decode(base64_data.c_str(), &out_len);
121-
126+
122127
if (decoded && out_len > 0) {
123128
result.assign(decoded, decoded + out_len);
124129
g_free(decoded);
125130
}
126-
131+
127132
return result;
128133
}
129134

@@ -139,15 +144,15 @@ std::shared_ptr<Image> Image::FromBase64(const std::string& base64_data) {
139144

140145
// Decode base64
141146
std::vector<unsigned char> imageData = DecodeBase64(cleanBase64);
142-
147+
143148
if (!imageData.empty()) {
144149
GError* error = nullptr;
145150
GInputStream* stream = g_memory_input_stream_new_from_data(
146151
imageData.data(), imageData.size(), nullptr);
147-
152+
148153
GdkPixbuf* pixbuf = gdk_pixbuf_new_from_stream(stream, nullptr, &error);
149154
g_object_unref(stream);
150-
155+
151156
if (pixbuf) {
152157
image->pimpl_->pixbuf_ = pixbuf;
153158
image->pimpl_->source_ = base64_data;
@@ -189,15 +194,16 @@ std::shared_ptr<Image> Image::FromSystemIcon(const std::string& icon_name) {
189194
if (icon_info) {
190195
GError* error = nullptr;
191196
GdkPixbuf* pixbuf = gtk_icon_info_load_icon(icon_info, &error);
192-
197+
193198
if (pixbuf) {
194199
image->pimpl_->pixbuf_ = pixbuf;
195200
image->pimpl_->source_ = icon_name;
196201

197202
// Get actual image size
198203
int width = gdk_pixbuf_get_width(pixbuf);
199204
int height = gdk_pixbuf_get_height(pixbuf);
200-
image->pimpl_->size_ = {static_cast<double>(width), static_cast<double>(height)};
205+
image->pimpl_->size_ = {static_cast<double>(width),
206+
static_cast<double>(height)};
201207
image->pimpl_->format_ = "System";
202208
} else {
203209
if (error) {
@@ -206,7 +212,7 @@ std::shared_ptr<Image> Image::FromSystemIcon(const std::string& icon_name) {
206212
g_object_unref(icon_info);
207213
return nullptr;
208214
}
209-
215+
210216
g_object_unref(icon_info);
211217
} else {
212218
return nullptr;
@@ -255,7 +261,8 @@ std::string Image::ToBase64() const {
255261
}
256262

257263
// Convert to base64
258-
std::string base64String = EncodeBase64(reinterpret_cast<unsigned char*>(buffer), buffer_size);
264+
std::string base64String =
265+
EncodeBase64(reinterpret_cast<unsigned char*>(buffer), buffer_size);
259266
g_free(buffer);
260267

261268
return "data:image/png;base64," + base64String;
@@ -269,14 +276,14 @@ bool Image::SaveToFile(const std::string& file_path) const {
269276
// Determine file type from extension
270277
size_t dotPos = file_path.find_last_of('.');
271278
std::string type = "png"; // default
272-
279+
273280
if (dotPos != std::string::npos) {
274281
std::string extension = file_path.substr(dotPos + 1);
275282
// Convert to lowercase
276283
for (auto& c : extension) {
277284
c = std::tolower(c);
278285
}
279-
286+
280287
if (extension == "jpg" || extension == "jpeg") {
281288
type = "jpeg";
282289
} else if (extension == "png") {
@@ -292,7 +299,7 @@ bool Image::SaveToFile(const std::string& file_path) const {
292299

293300
GError* error = nullptr;
294301
gboolean success = FALSE;
295-
302+
296303
if (type == "jpeg") {
297304
// For JPEG, specify quality
298305
success = gdk_pixbuf_save(pimpl_->pixbuf_, file_path.c_str(), type.c_str(),
@@ -314,4 +321,3 @@ void* Image::GetNativeObjectInternal() const {
314321
}
315322

316323
} // namespace nativeapi
317-

src/platform/macos/image_macos.mm

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919

2020
Impl() : ns_image_(nil), size_({0, 0}), format_("Unknown") {}
2121

22-
~Impl() {
23-
}
22+
~Impl() {}
2423

2524
Impl(const Impl& other)
2625
: ns_image_(nil), source_(other.source_), size_(other.size_), format_(other.format_) {

0 commit comments

Comments
 (0)