Skip to content

Commit

Permalink
feat: add win.getBackgroundColor() for macOS (#21448)
Browse files Browse the repository at this point in the history
thanks @vbennich - great work on this 🌟
  • Loading branch information
vbennich authored and codebytere committed Dec 13, 2019
1 parent c1210f8 commit cf833a7
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/api/browser-window.md
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,11 @@ console.log(win.getBounds())

Returns [`Rectangle`](structures/rectangle.md) - The `bounds` of the window as `Object`.

#### `win.getBackgroundColor()`

Returns `String` - Gets the background color of the window. See [Setting
`backgroundColor`](#setting-backgroundcolor).

#### `win.setContentBounds(bounds[, animate])`

* `bounds` [Rectangle](structures/rectangle.md)
Expand Down
1 change: 0 additions & 1 deletion shell/browser/api/atom_api_browser_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class BrowserView : public gin_helper::TrackableObject<BrowserView>,
void SetBounds(const gfx::Rect& bounds);
gfx::Rect GetBounds();
void SetBackgroundColor(const std::string& color_name);

v8::Local<v8::Value> GetWebContents();

v8::Global<v8::Value> web_contents_;
Expand Down
5 changes: 5 additions & 0 deletions shell/browser/api/atom_api_top_level_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,10 @@ void TopLevelWindow::SetBackgroundColor(const std::string& color_name) {
window_->SetBackgroundColor(color);
}

std::string TopLevelWindow::GetBackgroundColor() {
return ToRGBHex(window_->GetBackgroundColor());
}

void TopLevelWindow::SetHasShadow(bool has_shadow) {
window_->SetHasShadow(has_shadow);
}
Expand Down Expand Up @@ -1145,6 +1149,7 @@ void TopLevelWindow::BuildPrototype(v8::Isolate* isolate,
.SetMethod("setKiosk", &TopLevelWindow::SetKiosk)
.SetMethod("isKiosk", &TopLevelWindow::IsKiosk)
.SetMethod("setBackgroundColor", &TopLevelWindow::SetBackgroundColor)
.SetMethod("getBackgroundColor", &TopLevelWindow::GetBackgroundColor)
.SetMethod("setHasShadow", &TopLevelWindow::SetHasShadow)
.SetMethod("hasShadow", &TopLevelWindow::HasShadow)
.SetMethod("setOpacity", &TopLevelWindow::SetOpacity)
Expand Down
1 change: 1 addition & 0 deletions shell/browser/api/atom_api_top_level_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class TopLevelWindow : public gin_helper::TrackableObject<TopLevelWindow>,
void SetKiosk(bool kiosk);
bool IsKiosk();
virtual void SetBackgroundColor(const std::string& color_name);
std::string GetBackgroundColor();
void SetHasShadow(bool has_shadow);
bool HasShadow();
void SetOpacity(const double opacity);
Expand Down
1 change: 1 addition & 0 deletions shell/browser/native_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class NativeWindow : public base::SupportsUserData,
virtual void SetKiosk(bool kiosk) = 0;
virtual bool IsKiosk() = 0;
virtual void SetBackgroundColor(SkColor color) = 0;
virtual SkColor GetBackgroundColor() = 0;
virtual void SetHasShadow(bool has_shadow) = 0;
virtual bool HasShadow() = 0;
virtual void SetOpacity(const double opacity) = 0;
Expand Down
1 change: 1 addition & 0 deletions shell/browser/native_window_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class NativeWindowMac : public NativeWindow {
void SetKiosk(bool kiosk) override;
bool IsKiosk() override;
void SetBackgroundColor(SkColor color) override;
SkColor GetBackgroundColor() override;
void SetHasShadow(bool has_shadow) override;
bool HasShadow() override;
void SetOpacity(const double opacity) override;
Expand Down
5 changes: 5 additions & 0 deletions shell/browser/native_window_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,11 @@ void ViewDidMoveToSuperview(NSView* self, SEL _cmd) {
[[[window_ contentView] layer] setBackgroundColor:cgcolor];
}

SkColor NativeWindowMac::GetBackgroundColor() {
return skia::CGColorRefToSkColor(
[[[window_ contentView] layer] backgroundColor]);
}

void NativeWindowMac::SetHasShadow(bool has_shadow) {
[window_ setHasShadow:has_shadow];
}
Expand Down
4 changes: 4 additions & 0 deletions shell/browser/native_window_views.cc
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,10 @@ bool NativeWindowViews::IsKiosk() {
return IsFullscreen();
}

SkColor NativeWindowViews::GetBackgroundColor() {
return root_view_->background()->get_color();
}

void NativeWindowViews::SetBackgroundColor(SkColor background_color) {
// web views' background color.
root_view_->SetBackground(views::CreateSolidBackground(background_color));
Expand Down
1 change: 1 addition & 0 deletions shell/browser/native_window_views.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class NativeWindowViews : public NativeWindow,
gfx::Rect GetContentBounds() override;
gfx::Size GetContentSize() override;
gfx::Rect GetNormalBounds() override;
SkColor GetBackgroundColor() override;
void SetContentSizeConstraints(
const extensions::SizeConstraints& size_constraints) override;
void SetResizable(bool resizable) override;
Expand Down
23 changes: 23 additions & 0 deletions spec-main/api-browser-window-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,29 @@ describe('BrowserWindow module', () => {
})
})

describe('BrowserWindow.getBackgroundColor()', () => {
it('returns default value if no backgroundColor is set', () => {
w.destroy()
w = new BrowserWindow({})
expect(w.getBackgroundColor()).to.equal('#FFFFFF')
})
it('returns correct value if backgroundColor is set', () => {
const backgroundColor = '#BBAAFF'
w.destroy()
w = new BrowserWindow({
backgroundColor: backgroundColor
})
expect(w.getBackgroundColor()).to.equal(backgroundColor)
})
it('returns correct value from setBackgroundColor()', () => {
const backgroundColor = '#AABBFF'
w.destroy()
w = new BrowserWindow({})
w.setBackgroundColor(backgroundColor)
expect(w.getBackgroundColor()).to.equal(backgroundColor)
})
})

describe(`BrowserWindow.getNormalBounds()`, () => {
describe(`Normal state`, () => {
it(`checks normal bounds after resize`, (done) => {
Expand Down

0 comments on commit cf833a7

Please sign in to comment.