Skip to content

Commit 8c5bb59

Browse files
authored
Merge pull request electron#10667 from dittos/window-opacity
Add window opacity support
2 parents a337b12 + 7570ec9 commit 8c5bb59

12 files changed

+92
-0
lines changed

atom/browser/api/atom_api_window.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,14 @@ bool Window::HasShadow() {
631631
return window_->HasShadow();
632632
}
633633

634+
void Window::SetOpacity(const double opacity) {
635+
window_->SetOpacity(opacity);
636+
}
637+
638+
double Window::GetOpacity() {
639+
return window_->GetOpacity();
640+
}
641+
634642
void Window::FocusOnWebView() {
635643
window_->FocusOnWebView();
636644
}
@@ -1060,6 +1068,8 @@ void Window::BuildPrototype(v8::Isolate* isolate,
10601068
.SetMethod("setBackgroundColor", &Window::SetBackgroundColor)
10611069
.SetMethod("setHasShadow", &Window::SetHasShadow)
10621070
.SetMethod("hasShadow", &Window::HasShadow)
1071+
.SetMethod("setOpacity", &Window::SetOpacity)
1072+
.SetMethod("getOpacity", &Window::GetOpacity)
10631073
.SetMethod("setRepresentedFilename", &Window::SetRepresentedFilename)
10641074
.SetMethod("getRepresentedFilename", &Window::GetRepresentedFilename)
10651075
.SetMethod("setDocumentEdited", &Window::SetDocumentEdited)

atom/browser/api/atom_api_window.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ class Window : public mate::TrackableObject<Window>,
161161
void SetBackgroundColor(const std::string& color_name);
162162
void SetHasShadow(bool has_shadow);
163163
bool HasShadow();
164+
void SetOpacity(const double opacity);
165+
double GetOpacity();
164166
void FocusOnWebView();
165167
void BlurWebView();
166168
bool IsWebViewFocused();

atom/browser/native_window.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ void NativeWindow::InitFromOptions(const mate::Dictionary& options) {
159159
if (options.Get(options::kHasShadow, &has_shadow)) {
160160
SetHasShadow(has_shadow);
161161
}
162+
double opacity;
163+
if (options.Get(options::kOpacity, &opacity)) {
164+
SetOpacity(opacity);
165+
}
162166
bool top;
163167
if (options.Get(options::kAlwaysOnTop, &top) && top) {
164168
SetAlwaysOnTop(true);

atom/browser/native_window.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ class NativeWindow : public base::SupportsUserData,
141141
virtual void SetBackgroundColor(const std::string& color_name) = 0;
142142
virtual void SetHasShadow(bool has_shadow) = 0;
143143
virtual bool HasShadow() = 0;
144+
virtual void SetOpacity(const double opacity) = 0;
145+
virtual double GetOpacity() = 0;
144146
virtual void SetRepresentedFilename(const std::string& filename);
145147
virtual std::string GetRepresentedFilename();
146148
virtual void SetDocumentEdited(bool edited);

atom/browser/native_window_mac.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ class NativeWindowMac : public NativeWindow,
8383
void SetBackgroundColor(const std::string& color_name) override;
8484
void SetHasShadow(bool has_shadow) override;
8585
bool HasShadow() override;
86+
void SetOpacity(const double opacity) override;
87+
double GetOpacity() override;
8688
void SetRepresentedFilename(const std::string& filename) override;
8789
std::string GetRepresentedFilename() override;
8890
void SetDocumentEdited(bool edited) override;

atom/browser/native_window_mac.mm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,14 @@ static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
15041504
return [window_ hasShadow];
15051505
}
15061506

1507+
void NativeWindowMac::SetOpacity(const double opacity) {
1508+
[window_ setAlphaValue:opacity];
1509+
}
1510+
1511+
double NativeWindowMac::GetOpacity() {
1512+
return [window_ alphaValue];
1513+
}
1514+
15071515
void NativeWindowMac::SetRepresentedFilename(const std::string& filename) {
15081516
[window_ setRepresentedFilename:base::SysUTF8ToNSString(filename)];
15091517
}

atom/browser/native_window_views.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,13 +814,33 @@ bool NativeWindowViews::HasShadow() {
814814
!= wm::ShadowElevation::NONE;
815815
}
816816

817+
void NativeWindowViews::SetOpacity(const double opacity) {
818+
#if defined(OS_WIN)
819+
HWND hwnd = GetAcceleratedWidget();
820+
if (!layered_) {
821+
LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
822+
ex_style |= WS_EX_LAYERED;
823+
::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style);
824+
layered_ = true;
825+
}
826+
::SetLayeredWindowAttributes(hwnd, 0, opacity * 255, LWA_ALPHA);
827+
#endif
828+
opacity_ = opacity;
829+
}
830+
831+
double NativeWindowViews::GetOpacity() {
832+
return opacity_;
833+
}
834+
817835
void NativeWindowViews::SetIgnoreMouseEvents(bool ignore, bool forward) {
818836
#if defined(OS_WIN)
819837
LONG ex_style = ::GetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE);
820838
if (ignore)
821839
ex_style |= (WS_EX_TRANSPARENT | WS_EX_LAYERED);
822840
else
823841
ex_style &= ~(WS_EX_TRANSPARENT | WS_EX_LAYERED);
842+
if (layered_)
843+
ex_style |= WS_EX_LAYERED;
824844
::SetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE, ex_style);
825845

826846
// Forwarding is always disabled when not ignoring mouse messages.

atom/browser/native_window_views.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ class NativeWindowViews : public NativeWindow,
104104
void SetBackgroundColor(const std::string& color_name) override;
105105
void SetHasShadow(bool has_shadow) override;
106106
bool HasShadow() override;
107+
void SetOpacity(const double opacity) override;
108+
double GetOpacity() override;
107109
void SetIgnoreMouseEvents(bool ignore, bool forward) override;
108110
void SetContentProtection(bool enable) override;
109111
void SetFocusable(bool focusable) override;
@@ -274,6 +276,7 @@ class NativeWindowViews : public NativeWindow,
274276
static HHOOK mouse_hook_;
275277
bool forwarding_mouse_messages_ = false;
276278
HWND legacy_window_ = NULL;
279+
bool layered_ = false;
277280
#endif
278281

279282
// Handles unhandled keyboard messages coming back from the renderer process.
@@ -293,6 +296,7 @@ class NativeWindowViews : public NativeWindow,
293296
bool fullscreenable_;
294297
std::string title_;
295298
gfx::Size widget_size_;
299+
double opacity_ = 1.0;
296300

297301
DISALLOW_COPY_AND_ASSIGN(NativeWindowViews);
298302
};

atom/common/options_switches.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ const char kBackgroundColor[] = "backgroundColor";
8686
// Whether the window should have a shadow.
8787
const char kHasShadow[] = "hasShadow";
8888

89+
// Browser window opacity
90+
const char kOpacity[] = "opacity";
91+
8992
// Whether the window can be activated.
9093
const char kFocusable[] = "focusable";
9194

atom/common/options_switches.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ extern const char kDisableAutoHideCursor[];
4848
extern const char kStandardWindow[];
4949
extern const char kBackgroundColor[];
5050
extern const char kHasShadow[];
51+
extern const char kOpacity[];
5152
extern const char kFocusable[];
5253
extern const char kWebPreferences[];
5354
extern const char kVibrancyType[];

0 commit comments

Comments
 (0)