Skip to content

Commit a2f3d82

Browse files
trop[bot]ckerr
andauthored
refactor: simplify some BaseWindow JS getters (#46564)
* refactor: return a std::array<int 2> from BaseWindow::GetMaximumSize() Co-authored-by: Charles Kerr <charles@charleskerr.com> * refactor: return a std::array<int 2> from BaseWindow::GetMinimumSize() Co-authored-by: Charles Kerr <charles@charleskerr.com> * refactor: return a std::array<int 2> from BaseWindow::GetPosition() Co-authored-by: Charles Kerr <charles@charleskerr.com> * refactor: return a std::array<int 2> from BaseWindow::GetSize() Co-authored-by: Charles Kerr <charles@charleskerr.com> * refactor: return a std::array<int 2> from BaseWindow::GetContentSize() Co-authored-by: Charles Kerr <charles@charleskerr.com> * refactor: extract helper method ToArray(const gfx::Size) Co-authored-by: Charles Kerr <charles@charleskerr.com> * refactor: #include correctness Co-authored-by: Charles Kerr <charles@charleskerr.com> --------- Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr <charles@charleskerr.com>
1 parent 7886cc3 commit a2f3d82

File tree

3 files changed

+25
-35
lines changed

3 files changed

+25
-35
lines changed

shell/browser/api/electron_api_base_window.cc

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ v8::Local<v8::Value> ToBuffer(v8::Isolate* isolate, void* val, int size) {
7979
return buffer.ToLocalChecked();
8080
}
8181

82+
[[nodiscard]] constexpr std::array<int, 2U> ToArray(const gfx::Size size) {
83+
return {size.width(), size.height()};
84+
}
85+
86+
[[nodiscard]] constexpr std::array<int, 2U> ToArray(const gfx::Point point) {
87+
return {point.x(), point.y()};
88+
}
89+
8290
} // namespace
8391

8492
BaseWindow::BaseWindow(v8::Isolate* isolate,
@@ -467,12 +475,8 @@ void BaseWindow::SetSize(int width, int height, gin_helper::Arguments* args) {
467475
window_->SetSize(size, animate);
468476
}
469477

470-
std::vector<int> BaseWindow::GetSize() const {
471-
std::vector<int> result(2);
472-
gfx::Size size = window_->GetSize();
473-
result[0] = size.width();
474-
result[1] = size.height();
475-
return result;
478+
std::array<int, 2U> BaseWindow::GetSize() const {
479+
return ToArray(window_->GetSize());
476480
}
477481

478482
void BaseWindow::SetContentSize(int width,
@@ -483,36 +487,24 @@ void BaseWindow::SetContentSize(int width,
483487
window_->SetContentSize(gfx::Size(width, height), animate);
484488
}
485489

486-
std::vector<int> BaseWindow::GetContentSize() const {
487-
std::vector<int> result(2);
488-
gfx::Size size = window_->GetContentSize();
489-
result[0] = size.width();
490-
result[1] = size.height();
491-
return result;
490+
std::array<int, 2U> BaseWindow::GetContentSize() const {
491+
return ToArray(window_->GetContentSize());
492492
}
493493

494494
void BaseWindow::SetMinimumSize(int width, int height) {
495495
window_->SetMinimumSize(gfx::Size(width, height));
496496
}
497497

498-
std::vector<int> BaseWindow::GetMinimumSize() const {
499-
std::vector<int> result(2);
500-
gfx::Size size = window_->GetMinimumSize();
501-
result[0] = size.width();
502-
result[1] = size.height();
503-
return result;
498+
std::array<int, 2U> BaseWindow::GetMinimumSize() const {
499+
return ToArray(window_->GetMinimumSize());
504500
}
505501

506502
void BaseWindow::SetMaximumSize(int width, int height) {
507503
window_->SetMaximumSize(gfx::Size(width, height));
508504
}
509505

510-
std::vector<int> BaseWindow::GetMaximumSize() const {
511-
std::vector<int> result(2);
512-
gfx::Size size = window_->GetMaximumSize();
513-
result[0] = size.width();
514-
result[1] = size.height();
515-
return result;
506+
std::array<int, 2U> BaseWindow::GetMaximumSize() const {
507+
return ToArray(window_->GetMaximumSize());
516508
}
517509

518510
void BaseWindow::SetSheetOffset(double offsetY, gin_helper::Arguments* args) {
@@ -594,12 +586,8 @@ void BaseWindow::SetPosition(int x, int y, gin_helper::Arguments* args) {
594586
window_->SetPosition(gfx::Point(x, y), animate);
595587
}
596588

597-
std::vector<int> BaseWindow::GetPosition() const {
598-
std::vector<int> result(2);
599-
gfx::Point pos = window_->GetPosition();
600-
result[0] = pos.x();
601-
result[1] = pos.y();
602-
return result;
589+
std::array<int, 2U> BaseWindow::GetPosition() const {
590+
return ToArray(window_->GetPosition());
603591
}
604592
void BaseWindow::MoveAbove(const std::string& sourceId,
605593
gin_helper::Arguments* args) {

shell/browser/api/electron_api_base_window.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef ELECTRON_SHELL_BROWSER_API_ELECTRON_API_BASE_WINDOW_H_
66
#define ELECTRON_SHELL_BROWSER_API_ELECTRON_API_BASE_WINDOW_H_
77

8+
#include <array>
89
#include <map>
910
#include <memory>
1011
#include <optional>
@@ -119,17 +120,17 @@ class BaseWindow : public gin_helper::TrackableObject<BaseWindow>,
119120
void SetBounds(const gfx::Rect& bounds, gin_helper::Arguments* args);
120121
gfx::Rect GetBounds() const;
121122
void SetSize(int width, int height, gin_helper::Arguments* args);
122-
std::vector<int> GetSize() const;
123+
std::array<int, 2U> GetSize() const;
123124
void SetContentSize(int width, int height, gin_helper::Arguments* args);
124-
std::vector<int> GetContentSize() const;
125+
std::array<int, 2U> GetContentSize() const;
125126
void SetContentBounds(const gfx::Rect& bounds, gin_helper::Arguments* args);
126127
gfx::Rect GetContentBounds() const;
127128
bool IsNormal() const;
128129
gfx::Rect GetNormalBounds() const;
129130
void SetMinimumSize(int width, int height);
130-
std::vector<int> GetMinimumSize() const;
131+
std::array<int, 2U> GetMinimumSize() const;
131132
void SetMaximumSize(int width, int height);
132-
std::vector<int> GetMaximumSize() const;
133+
std::array<int, 2U> GetMaximumSize() const;
133134
void SetSheetOffset(double offsetY, gin_helper::Arguments* args);
134135
void SetResizable(bool resizable);
135136
bool IsResizable() const;
@@ -149,7 +150,7 @@ class BaseWindow : public gin_helper::TrackableObject<BaseWindow>,
149150
bool IsAlwaysOnTop() const;
150151
void Center();
151152
void SetPosition(int x, int y, gin_helper::Arguments* args);
152-
std::vector<int> GetPosition() const;
153+
std::array<int, 2U> GetPosition() const;
153154
void SetTitle(const std::string& title);
154155
std::string GetTitle() const;
155156
void SetAccessibleTitle(const std::string& title);

shell/common/gin_converters/std_converter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef ELECTRON_SHELL_COMMON_GIN_CONVERTERS_STD_CONVERTER_H_
66
#define ELECTRON_SHELL_COMMON_GIN_CONVERTERS_STD_CONVERTER_H_
77

8+
#include <array>
89
#include <cstddef>
910
#include <functional>
1011
#include <map>

0 commit comments

Comments
 (0)