refactor: simplify some BaseWindow JS getters (#46542)

* refactor: return a std::array<int 2> from BaseWindow::GetMaximumSize()

* refactor: return a std::array<int 2> from BaseWindow::GetMinimumSize()

* refactor: return a std::array<int 2> from BaseWindow::GetPosition()

* refactor: return a std::array<int 2> from BaseWindow::GetSize()

* refactor: return a std::array<int 2> from BaseWindow::GetContentSize()

* refactor: extract helper method ToArray(const gfx::Size)

* refactor: #include correctness
This commit is contained in:
Charles Kerr 2025-04-08 10:58:12 -05:00 committed by GitHub
parent 1a6de10da8
commit 636cbc19ac
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 25 additions and 35 deletions

View file

@ -79,6 +79,14 @@ v8::Local<v8::Value> ToBuffer(v8::Isolate* isolate, void* val, int size) {
return buffer.ToLocalChecked();
}
[[nodiscard]] constexpr std::array<int, 2U> ToArray(const gfx::Size size) {
return {size.width(), size.height()};
}
[[nodiscard]] constexpr std::array<int, 2U> ToArray(const gfx::Point point) {
return {point.x(), point.y()};
}
} // namespace
BaseWindow::BaseWindow(v8::Isolate* isolate,
@ -467,12 +475,8 @@ void BaseWindow::SetSize(int width, int height, gin_helper::Arguments* args) {
window_->SetSize(size, animate);
}
std::vector<int> BaseWindow::GetSize() const {
std::vector<int> result(2);
gfx::Size size = window_->GetSize();
result[0] = size.width();
result[1] = size.height();
return result;
std::array<int, 2U> BaseWindow::GetSize() const {
return ToArray(window_->GetSize());
}
void BaseWindow::SetContentSize(int width,
@ -483,36 +487,24 @@ void BaseWindow::SetContentSize(int width,
window_->SetContentSize(gfx::Size(width, height), animate);
}
std::vector<int> BaseWindow::GetContentSize() const {
std::vector<int> result(2);
gfx::Size size = window_->GetContentSize();
result[0] = size.width();
result[1] = size.height();
return result;
std::array<int, 2U> BaseWindow::GetContentSize() const {
return ToArray(window_->GetContentSize());
}
void BaseWindow::SetMinimumSize(int width, int height) {
window_->SetMinimumSize(gfx::Size(width, height));
}
std::vector<int> BaseWindow::GetMinimumSize() const {
std::vector<int> result(2);
gfx::Size size = window_->GetMinimumSize();
result[0] = size.width();
result[1] = size.height();
return result;
std::array<int, 2U> BaseWindow::GetMinimumSize() const {
return ToArray(window_->GetMinimumSize());
}
void BaseWindow::SetMaximumSize(int width, int height) {
window_->SetMaximumSize(gfx::Size(width, height));
}
std::vector<int> BaseWindow::GetMaximumSize() const {
std::vector<int> result(2);
gfx::Size size = window_->GetMaximumSize();
result[0] = size.width();
result[1] = size.height();
return result;
std::array<int, 2U> BaseWindow::GetMaximumSize() const {
return ToArray(window_->GetMaximumSize());
}
void BaseWindow::SetSheetOffset(double offsetY, gin_helper::Arguments* args) {
@ -594,12 +586,8 @@ void BaseWindow::SetPosition(int x, int y, gin_helper::Arguments* args) {
window_->SetPosition(gfx::Point(x, y), animate);
}
std::vector<int> BaseWindow::GetPosition() const {
std::vector<int> result(2);
gfx::Point pos = window_->GetPosition();
result[0] = pos.x();
result[1] = pos.y();
return result;
std::array<int, 2U> BaseWindow::GetPosition() const {
return ToArray(window_->GetPosition());
}
void BaseWindow::MoveAbove(const std::string& sourceId,
gin_helper::Arguments* args) {

View file

@ -5,6 +5,7 @@
#ifndef ELECTRON_SHELL_BROWSER_API_ELECTRON_API_BASE_WINDOW_H_
#define ELECTRON_SHELL_BROWSER_API_ELECTRON_API_BASE_WINDOW_H_
#include <array>
#include <map>
#include <memory>
#include <optional>
@ -119,17 +120,17 @@ class BaseWindow : public gin_helper::TrackableObject<BaseWindow>,
void SetBounds(const gfx::Rect& bounds, gin_helper::Arguments* args);
gfx::Rect GetBounds() const;
void SetSize(int width, int height, gin_helper::Arguments* args);
std::vector<int> GetSize() const;
std::array<int, 2U> GetSize() const;
void SetContentSize(int width, int height, gin_helper::Arguments* args);
std::vector<int> GetContentSize() const;
std::array<int, 2U> GetContentSize() const;
void SetContentBounds(const gfx::Rect& bounds, gin_helper::Arguments* args);
gfx::Rect GetContentBounds() const;
bool IsNormal() const;
gfx::Rect GetNormalBounds() const;
void SetMinimumSize(int width, int height);
std::vector<int> GetMinimumSize() const;
std::array<int, 2U> GetMinimumSize() const;
void SetMaximumSize(int width, int height);
std::vector<int> GetMaximumSize() const;
std::array<int, 2U> GetMaximumSize() const;
void SetSheetOffset(double offsetY, gin_helper::Arguments* args);
void SetResizable(bool resizable);
bool IsResizable() const;
@ -149,7 +150,7 @@ class BaseWindow : public gin_helper::TrackableObject<BaseWindow>,
bool IsAlwaysOnTop() const;
void Center();
void SetPosition(int x, int y, gin_helper::Arguments* args);
std::vector<int> GetPosition() const;
std::array<int, 2U> GetPosition() const;
void SetTitle(const std::string& title);
std::string GetTitle() const;
void SetAccessibleTitle(const std::string& title);

View file

@ -5,6 +5,7 @@
#ifndef ELECTRON_SHELL_COMMON_GIN_CONVERTERS_STD_CONVERTER_H_
#define ELECTRON_SHELL_COMMON_GIN_CONVERTERS_STD_CONVERTER_H_
#include <array>
#include <cstddef>
#include <functional>
#include <map>