fix: make SetWindowButtonVisibility work for customButtonsOnHover (#27073)
This commit is contained in:
parent
5e7e0a4c7e
commit
c3091c3a70
9 changed files with 87 additions and 52 deletions
|
@ -1598,8 +1598,6 @@ Changes window icon.
|
|||
|
||||
Sets whether the window traffic light buttons should be visible.
|
||||
|
||||
This cannot be called when `titleBarStyle` is set to `customButtonsOnHover`.
|
||||
|
||||
#### `win.setAutoHideMenuBar(hide)`
|
||||
|
||||
* `hide` Boolean
|
||||
|
|
|
@ -845,6 +845,14 @@ void BaseWindow::SetVibrancy(v8::Isolate* isolate, v8::Local<v8::Value> value) {
|
|||
}
|
||||
|
||||
#if defined(OS_MAC)
|
||||
void BaseWindow::SetWindowButtonVisibility(bool visible) {
|
||||
window_->SetWindowButtonVisibility(visible);
|
||||
}
|
||||
|
||||
bool BaseWindow::GetWindowButtonVisibility() const {
|
||||
return window_->GetWindowButtonVisibility();
|
||||
}
|
||||
|
||||
void BaseWindow::SetTrafficLightPosition(const gfx::Point& position) {
|
||||
// For backward compatibility we treat (0, 0) as reseting to default.
|
||||
if (position.IsOrigin())
|
||||
|
@ -898,13 +906,6 @@ void BaseWindow::AddTabbedWindow(NativeWindow* window,
|
|||
args->ThrowError("AddTabbedWindow cannot be called by a window on itself.");
|
||||
}
|
||||
|
||||
void BaseWindow::SetWindowButtonVisibility(bool visible,
|
||||
gin_helper::Arguments* args) {
|
||||
if (!window_->SetWindowButtonVisibility(visible)) {
|
||||
args->ThrowError("Not supported for this window");
|
||||
}
|
||||
}
|
||||
|
||||
void BaseWindow::SetAutoHideMenuBar(bool auto_hide) {
|
||||
window_->SetAutoHideMenuBar(auto_hide);
|
||||
}
|
||||
|
@ -1243,6 +1244,8 @@ void BaseWindow::BuildPrototype(v8::Isolate* isolate,
|
|||
.SetMethod("addTabbedWindow", &BaseWindow::AddTabbedWindow)
|
||||
.SetMethod("setWindowButtonVisibility",
|
||||
&BaseWindow::SetWindowButtonVisibility)
|
||||
.SetMethod("_getWindowButtonVisibility",
|
||||
&BaseWindow::GetWindowButtonVisibility)
|
||||
.SetProperty("excludedFromShownWindowsMenu",
|
||||
&BaseWindow::IsExcludedFromShownWindowsMenu,
|
||||
&BaseWindow::SetExcludedFromShownWindowsMenu)
|
||||
|
|
|
@ -189,6 +189,8 @@ class BaseWindow : public gin_helper::TrackableObject<BaseWindow>,
|
|||
virtual void SetVibrancy(v8::Isolate* isolate, v8::Local<v8::Value> value);
|
||||
|
||||
#if defined(OS_MAC)
|
||||
void SetWindowButtonVisibility(bool visible);
|
||||
bool GetWindowButtonVisibility() const;
|
||||
void SetTrafficLightPosition(const gfx::Point& position);
|
||||
gfx::Point GetTrafficLightPosition() const;
|
||||
#endif
|
||||
|
@ -202,7 +204,6 @@ class BaseWindow : public gin_helper::TrackableObject<BaseWindow>,
|
|||
void MoveTabToNewWindow();
|
||||
void ToggleTabBar();
|
||||
void AddTabbedWindow(NativeWindow* window, gin_helper::Arguments* args);
|
||||
void SetWindowButtonVisibility(bool visible, gin_helper::Arguments* args);
|
||||
void SetAutoHideMenuBar(bool auto_hide);
|
||||
bool IsMenuBarAutoHide();
|
||||
void SetMenuBarVisibility(bool visible);
|
||||
|
|
|
@ -372,10 +372,6 @@ bool NativeWindow::IsMenuBarVisible() {
|
|||
return true;
|
||||
}
|
||||
|
||||
bool NativeWindow::SetWindowButtonVisibility(bool visible) {
|
||||
return false;
|
||||
}
|
||||
|
||||
double NativeWindow::GetAspectRatio() {
|
||||
return aspect_ratio_;
|
||||
}
|
||||
|
|
|
@ -199,6 +199,8 @@ class NativeWindow : public base::SupportsUserData,
|
|||
|
||||
// Traffic Light API
|
||||
#if defined(OS_MAC)
|
||||
virtual void SetWindowButtonVisibility(bool visible) = 0;
|
||||
virtual bool GetWindowButtonVisibility() const = 0;
|
||||
virtual void SetTrafficLightPosition(base::Optional<gfx::Point> position) = 0;
|
||||
virtual base::Optional<gfx::Point> GetTrafficLightPosition() const = 0;
|
||||
virtual void RedrawTrafficLights() = 0;
|
||||
|
@ -217,9 +219,6 @@ class NativeWindow : public base::SupportsUserData,
|
|||
virtual void ToggleTabBar();
|
||||
virtual bool AddTabbedWindow(NativeWindow* window);
|
||||
|
||||
// Returns false if unsupported.
|
||||
virtual bool SetWindowButtonVisibility(bool visible);
|
||||
|
||||
// Toggle the menu bar.
|
||||
virtual void SetAutoHideMenuBar(bool auto_hide);
|
||||
virtual bool IsMenuBarAutoHide();
|
||||
|
|
|
@ -117,6 +117,8 @@ class NativeWindowMac : public NativeWindow, public ui::NativeThemeObserver {
|
|||
bool IsVisibleOnAllWorkspaces() override;
|
||||
void SetAutoHideCursor(bool auto_hide) override;
|
||||
void SetVibrancy(const std::string& type) override;
|
||||
void SetWindowButtonVisibility(bool visible) override;
|
||||
bool GetWindowButtonVisibility() const override;
|
||||
void SetTrafficLightPosition(base::Optional<gfx::Point> position) override;
|
||||
base::Optional<gfx::Point> GetTrafficLightPosition() const override;
|
||||
void RedrawTrafficLights() override;
|
||||
|
@ -130,7 +132,6 @@ class NativeWindowMac : public NativeWindow, public ui::NativeThemeObserver {
|
|||
void MoveTabToNewWindow() override;
|
||||
void ToggleTabBar() override;
|
||||
bool AddTabbedWindow(NativeWindow* window) override;
|
||||
bool SetWindowButtonVisibility(bool visible) override;
|
||||
void SetAspectRatio(double aspect_ratio,
|
||||
const gfx::Size& extra_size) override;
|
||||
void PreviewFile(const std::string& path,
|
||||
|
@ -187,6 +188,7 @@ class NativeWindowMac : public NativeWindow, public ui::NativeThemeObserver {
|
|||
// Add custom layers to the content view.
|
||||
void AddContentViewLayers(bool minimizable, bool closable);
|
||||
|
||||
void InternalSetWindowButtonVisibility(bool visible);
|
||||
void InternalSetParentWindow(NativeWindow* parent, bool attach);
|
||||
void SetForwardMouseMessages(bool forward);
|
||||
|
||||
|
|
|
@ -1014,9 +1014,7 @@ void NativeWindowMac::SetSimpleFullScreen(bool simple_fullscreen) {
|
|||
} else if (!window_button_visibility_.has_value()) {
|
||||
// Lets keep previous behaviour - hide window controls in titled
|
||||
// fullscreen mode when not specified otherwise.
|
||||
[[window standardWindowButton:NSWindowZoomButton] setHidden:YES];
|
||||
[[window standardWindowButton:NSWindowMiniaturizeButton] setHidden:YES];
|
||||
[[window standardWindowButton:NSWindowCloseButton] setHidden:YES];
|
||||
InternalSetWindowButtonVisibility(false);
|
||||
}
|
||||
|
||||
[window setFrame:fullscreenFrame display:YES animate:YES];
|
||||
|
@ -1034,15 +1032,15 @@ void NativeWindowMac::SetSimpleFullScreen(bool simple_fullscreen) {
|
|||
SetStyleMask(true, NSWindowStyleMaskTitled);
|
||||
}
|
||||
|
||||
// Restore window controls visibility state
|
||||
const bool window_button_hidden =
|
||||
!window_button_visibility_.value_or(true) || !has_frame();
|
||||
[[window standardWindowButton:NSWindowZoomButton]
|
||||
setHidden:window_button_hidden];
|
||||
[[window standardWindowButton:NSWindowMiniaturizeButton]
|
||||
setHidden:window_button_hidden];
|
||||
[[window standardWindowButton:NSWindowCloseButton]
|
||||
setHidden:window_button_hidden];
|
||||
// Restore default window controls visibility state.
|
||||
if (!window_button_visibility_.has_value()) {
|
||||
bool visibility;
|
||||
if (has_frame())
|
||||
visibility = true;
|
||||
else
|
||||
visibility = title_bar_style_ != TitleBarStyle::kNormal;
|
||||
InternalSetWindowButtonVisibility(visibility);
|
||||
}
|
||||
|
||||
[window setFrame:original_frame_ display:YES animate:YES];
|
||||
window.level = original_level_;
|
||||
|
@ -1435,6 +1433,20 @@ void NativeWindowMac::SetVibrancy(const std::string& type) {
|
|||
[effect_view setMaterial:vibrancyType];
|
||||
}
|
||||
|
||||
void NativeWindowMac::SetWindowButtonVisibility(bool visible) {
|
||||
window_button_visibility_ = visible;
|
||||
InternalSetWindowButtonVisibility(visible);
|
||||
}
|
||||
|
||||
bool NativeWindowMac::GetWindowButtonVisibility() const {
|
||||
if (buttons_view_)
|
||||
return ![buttons_view_ isHidden];
|
||||
else
|
||||
return ![window_ standardWindowButton:NSWindowZoomButton].hidden ||
|
||||
![window_ standardWindowButton:NSWindowMiniaturizeButton].hidden ||
|
||||
![window_ standardWindowButton:NSWindowCloseButton].hidden;
|
||||
}
|
||||
|
||||
void NativeWindowMac::SetTrafficLightPosition(
|
||||
base::Optional<gfx::Point> position) {
|
||||
traffic_light_position_ = std::move(position);
|
||||
|
@ -1579,19 +1591,6 @@ bool NativeWindowMac::AddTabbedWindow(NativeWindow* window) {
|
|||
return true;
|
||||
}
|
||||
|
||||
bool NativeWindowMac::SetWindowButtonVisibility(bool visible) {
|
||||
if (title_bar_style_ == TitleBarStyle::kCustomButtonsOnHover) {
|
||||
return false;
|
||||
}
|
||||
|
||||
window_button_visibility_ = visible;
|
||||
|
||||
[[window_ standardWindowButton:NSWindowCloseButton] setHidden:!visible];
|
||||
[[window_ standardWindowButton:NSWindowMiniaturizeButton] setHidden:!visible];
|
||||
[[window_ standardWindowButton:NSWindowZoomButton] setHidden:!visible];
|
||||
return true;
|
||||
}
|
||||
|
||||
void NativeWindowMac::SetAspectRatio(double aspect_ratio,
|
||||
const gfx::Size& extra_size) {
|
||||
NativeWindow::SetAspectRatio(aspect_ratio, extra_size);
|
||||
|
@ -1774,6 +1773,17 @@ void NativeWindowMac::AddContentViewLayers(bool minimizable, bool closable) {
|
|||
}
|
||||
}
|
||||
|
||||
void NativeWindowMac::InternalSetWindowButtonVisibility(bool visible) {
|
||||
if (buttons_view_) {
|
||||
[buttons_view_ setHidden:!visible];
|
||||
} else {
|
||||
[[window_ standardWindowButton:NSWindowCloseButton] setHidden:!visible];
|
||||
[[window_ standardWindowButton:NSWindowMiniaturizeButton]
|
||||
setHidden:!visible];
|
||||
[[window_ standardWindowButton:NSWindowZoomButton] setHidden:!visible];
|
||||
}
|
||||
}
|
||||
|
||||
void NativeWindowMac::InternalSetParentWindow(NativeWindow* parent,
|
||||
bool attach) {
|
||||
if (is_modal())
|
||||
|
|
|
@ -1517,15 +1517,40 @@ describe('BrowserWindow module', () => {
|
|||
}).to.not.throw();
|
||||
});
|
||||
|
||||
it('throws with custom title bar buttons', () => {
|
||||
expect(() => {
|
||||
const w = new BrowserWindow({
|
||||
show: false,
|
||||
titleBarStyle: 'customButtonsOnHover',
|
||||
frame: false
|
||||
});
|
||||
it('changes window button visibility for normal window', () => {
|
||||
const w = new BrowserWindow({ show: false });
|
||||
expect(w._getWindowButtonVisibility()).to.equal(true);
|
||||
w.setWindowButtonVisibility(false);
|
||||
expect(w._getWindowButtonVisibility()).to.equal(false);
|
||||
w.setWindowButtonVisibility(true);
|
||||
}).to.throw('Not supported for this window');
|
||||
expect(w._getWindowButtonVisibility()).to.equal(true);
|
||||
});
|
||||
|
||||
it('changes window button visibility for frameless window', () => {
|
||||
const w = new BrowserWindow({ show: false, frame: false });
|
||||
expect(w._getWindowButtonVisibility()).to.equal(false);
|
||||
w.setWindowButtonVisibility(true);
|
||||
expect(w._getWindowButtonVisibility()).to.equal(true);
|
||||
w.setWindowButtonVisibility(false);
|
||||
expect(w._getWindowButtonVisibility()).to.equal(false);
|
||||
});
|
||||
|
||||
it('changes window button visibility for hiddenInset window', () => {
|
||||
const w = new BrowserWindow({ show: false, frame: false, titleBarStyle: 'hiddenInset' });
|
||||
expect(w._getWindowButtonVisibility()).to.equal(true);
|
||||
w.setWindowButtonVisibility(false);
|
||||
expect(w._getWindowButtonVisibility()).to.equal(false);
|
||||
w.setWindowButtonVisibility(true);
|
||||
expect(w._getWindowButtonVisibility()).to.equal(true);
|
||||
});
|
||||
|
||||
it('changes window button visibility for customButtonsOnHover window', () => {
|
||||
const w = new BrowserWindow({ show: false, frame: false, titleBarStyle: 'customButtonsOnHover' });
|
||||
expect(w._getWindowButtonVisibility()).to.equal(true);
|
||||
w.setWindowButtonVisibility(false);
|
||||
expect(w._getWindowButtonVisibility()).to.equal(false);
|
||||
w.setWindowButtonVisibility(true);
|
||||
expect(w._getWindowButtonVisibility()).to.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
1
typings/internal-electron.d.ts
vendored
1
typings/internal-electron.d.ts
vendored
|
@ -31,6 +31,7 @@ declare namespace Electron {
|
|||
_setTouchBarItems: (items: TouchBarItemType[]) => void;
|
||||
_setEscapeTouchBarItem: (item: TouchBarItemType | {}) => void;
|
||||
_refreshTouchBarItem: (itemID: string) => void;
|
||||
_getWindowButtonVisibility: () => boolean;
|
||||
frameName: string;
|
||||
on(event: '-touch-bar-interaction', listener: (event: Event, itemID: string, details: any) => void): this;
|
||||
removeListener(event: '-touch-bar-interaction', listener: (event: Event, itemID: string, details: any) => void): this;
|
||||
|
|
Loading…
Reference in a new issue