fix: make SetWindowButtonVisibility work for customButtonsOnHover (#27073)

This commit is contained in:
Cheng Zhao 2021-01-19 19:12:09 +09:00 коммит произвёл GitHub
Родитель 5e7e0a4c7e
Коммит c3091c3a70
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
9 изменённых файлов: 87 добавлений и 52 удалений

Просмотреть файл

@ -1598,8 +1598,6 @@ Changes window icon.
Sets whether the window traffic light buttons should be visible. Sets whether the window traffic light buttons should be visible.
This cannot be called when `titleBarStyle` is set to `customButtonsOnHover`.
#### `win.setAutoHideMenuBar(hide)` #### `win.setAutoHideMenuBar(hide)`
* `hide` Boolean * `hide` Boolean

Просмотреть файл

@ -845,6 +845,14 @@ void BaseWindow::SetVibrancy(v8::Isolate* isolate, v8::Local<v8::Value> value) {
} }
#if defined(OS_MAC) #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) { void BaseWindow::SetTrafficLightPosition(const gfx::Point& position) {
// For backward compatibility we treat (0, 0) as reseting to default. // For backward compatibility we treat (0, 0) as reseting to default.
if (position.IsOrigin()) if (position.IsOrigin())
@ -898,13 +906,6 @@ void BaseWindow::AddTabbedWindow(NativeWindow* window,
args->ThrowError("AddTabbedWindow cannot be called by a window on itself."); 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) { void BaseWindow::SetAutoHideMenuBar(bool auto_hide) {
window_->SetAutoHideMenuBar(auto_hide); window_->SetAutoHideMenuBar(auto_hide);
} }
@ -1243,6 +1244,8 @@ void BaseWindow::BuildPrototype(v8::Isolate* isolate,
.SetMethod("addTabbedWindow", &BaseWindow::AddTabbedWindow) .SetMethod("addTabbedWindow", &BaseWindow::AddTabbedWindow)
.SetMethod("setWindowButtonVisibility", .SetMethod("setWindowButtonVisibility",
&BaseWindow::SetWindowButtonVisibility) &BaseWindow::SetWindowButtonVisibility)
.SetMethod("_getWindowButtonVisibility",
&BaseWindow::GetWindowButtonVisibility)
.SetProperty("excludedFromShownWindowsMenu", .SetProperty("excludedFromShownWindowsMenu",
&BaseWindow::IsExcludedFromShownWindowsMenu, &BaseWindow::IsExcludedFromShownWindowsMenu,
&BaseWindow::SetExcludedFromShownWindowsMenu) &BaseWindow::SetExcludedFromShownWindowsMenu)

Просмотреть файл

@ -189,6 +189,8 @@ class BaseWindow : public gin_helper::TrackableObject<BaseWindow>,
virtual void SetVibrancy(v8::Isolate* isolate, v8::Local<v8::Value> value); virtual void SetVibrancy(v8::Isolate* isolate, v8::Local<v8::Value> value);
#if defined(OS_MAC) #if defined(OS_MAC)
void SetWindowButtonVisibility(bool visible);
bool GetWindowButtonVisibility() const;
void SetTrafficLightPosition(const gfx::Point& position); void SetTrafficLightPosition(const gfx::Point& position);
gfx::Point GetTrafficLightPosition() const; gfx::Point GetTrafficLightPosition() const;
#endif #endif
@ -202,7 +204,6 @@ class BaseWindow : public gin_helper::TrackableObject<BaseWindow>,
void MoveTabToNewWindow(); void MoveTabToNewWindow();
void ToggleTabBar(); void ToggleTabBar();
void AddTabbedWindow(NativeWindow* window, gin_helper::Arguments* args); void AddTabbedWindow(NativeWindow* window, gin_helper::Arguments* args);
void SetWindowButtonVisibility(bool visible, gin_helper::Arguments* args);
void SetAutoHideMenuBar(bool auto_hide); void SetAutoHideMenuBar(bool auto_hide);
bool IsMenuBarAutoHide(); bool IsMenuBarAutoHide();
void SetMenuBarVisibility(bool visible); void SetMenuBarVisibility(bool visible);

Просмотреть файл

@ -372,10 +372,6 @@ bool NativeWindow::IsMenuBarVisible() {
return true; return true;
} }
bool NativeWindow::SetWindowButtonVisibility(bool visible) {
return false;
}
double NativeWindow::GetAspectRatio() { double NativeWindow::GetAspectRatio() {
return aspect_ratio_; return aspect_ratio_;
} }

Просмотреть файл

@ -199,6 +199,8 @@ class NativeWindow : public base::SupportsUserData,
// Traffic Light API // Traffic Light API
#if defined(OS_MAC) #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 void SetTrafficLightPosition(base::Optional<gfx::Point> position) = 0;
virtual base::Optional<gfx::Point> GetTrafficLightPosition() const = 0; virtual base::Optional<gfx::Point> GetTrafficLightPosition() const = 0;
virtual void RedrawTrafficLights() = 0; virtual void RedrawTrafficLights() = 0;
@ -217,9 +219,6 @@ class NativeWindow : public base::SupportsUserData,
virtual void ToggleTabBar(); virtual void ToggleTabBar();
virtual bool AddTabbedWindow(NativeWindow* window); virtual bool AddTabbedWindow(NativeWindow* window);
// Returns false if unsupported.
virtual bool SetWindowButtonVisibility(bool visible);
// Toggle the menu bar. // Toggle the menu bar.
virtual void SetAutoHideMenuBar(bool auto_hide); virtual void SetAutoHideMenuBar(bool auto_hide);
virtual bool IsMenuBarAutoHide(); virtual bool IsMenuBarAutoHide();

Просмотреть файл

@ -117,6 +117,8 @@ class NativeWindowMac : public NativeWindow, public ui::NativeThemeObserver {
bool IsVisibleOnAllWorkspaces() override; bool IsVisibleOnAllWorkspaces() override;
void SetAutoHideCursor(bool auto_hide) override; void SetAutoHideCursor(bool auto_hide) override;
void SetVibrancy(const std::string& type) 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; void SetTrafficLightPosition(base::Optional<gfx::Point> position) override;
base::Optional<gfx::Point> GetTrafficLightPosition() const override; base::Optional<gfx::Point> GetTrafficLightPosition() const override;
void RedrawTrafficLights() override; void RedrawTrafficLights() override;
@ -130,7 +132,6 @@ class NativeWindowMac : public NativeWindow, public ui::NativeThemeObserver {
void MoveTabToNewWindow() override; void MoveTabToNewWindow() override;
void ToggleTabBar() override; void ToggleTabBar() override;
bool AddTabbedWindow(NativeWindow* window) override; bool AddTabbedWindow(NativeWindow* window) override;
bool SetWindowButtonVisibility(bool visible) override;
void SetAspectRatio(double aspect_ratio, void SetAspectRatio(double aspect_ratio,
const gfx::Size& extra_size) override; const gfx::Size& extra_size) override;
void PreviewFile(const std::string& path, void PreviewFile(const std::string& path,
@ -187,6 +188,7 @@ class NativeWindowMac : public NativeWindow, public ui::NativeThemeObserver {
// Add custom layers to the content view. // Add custom layers to the content view.
void AddContentViewLayers(bool minimizable, bool closable); void AddContentViewLayers(bool minimizable, bool closable);
void InternalSetWindowButtonVisibility(bool visible);
void InternalSetParentWindow(NativeWindow* parent, bool attach); void InternalSetParentWindow(NativeWindow* parent, bool attach);
void SetForwardMouseMessages(bool forward); void SetForwardMouseMessages(bool forward);

Просмотреть файл

@ -1014,9 +1014,7 @@ void NativeWindowMac::SetSimpleFullScreen(bool simple_fullscreen) {
} else if (!window_button_visibility_.has_value()) { } else if (!window_button_visibility_.has_value()) {
// Lets keep previous behaviour - hide window controls in titled // Lets keep previous behaviour - hide window controls in titled
// fullscreen mode when not specified otherwise. // fullscreen mode when not specified otherwise.
[[window standardWindowButton:NSWindowZoomButton] setHidden:YES]; InternalSetWindowButtonVisibility(false);
[[window standardWindowButton:NSWindowMiniaturizeButton] setHidden:YES];
[[window standardWindowButton:NSWindowCloseButton] setHidden:YES];
} }
[window setFrame:fullscreenFrame display:YES animate:YES]; [window setFrame:fullscreenFrame display:YES animate:YES];
@ -1034,15 +1032,15 @@ void NativeWindowMac::SetSimpleFullScreen(bool simple_fullscreen) {
SetStyleMask(true, NSWindowStyleMaskTitled); SetStyleMask(true, NSWindowStyleMaskTitled);
} }
// Restore window controls visibility state // Restore default window controls visibility state.
const bool window_button_hidden = if (!window_button_visibility_.has_value()) {
!window_button_visibility_.value_or(true) || !has_frame(); bool visibility;
[[window standardWindowButton:NSWindowZoomButton] if (has_frame())
setHidden:window_button_hidden]; visibility = true;
[[window standardWindowButton:NSWindowMiniaturizeButton] else
setHidden:window_button_hidden]; visibility = title_bar_style_ != TitleBarStyle::kNormal;
[[window standardWindowButton:NSWindowCloseButton] InternalSetWindowButtonVisibility(visibility);
setHidden:window_button_hidden]; }
[window setFrame:original_frame_ display:YES animate:YES]; [window setFrame:original_frame_ display:YES animate:YES];
window.level = original_level_; window.level = original_level_;
@ -1435,6 +1433,20 @@ void NativeWindowMac::SetVibrancy(const std::string& type) {
[effect_view setMaterial:vibrancyType]; [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( void NativeWindowMac::SetTrafficLightPosition(
base::Optional<gfx::Point> position) { base::Optional<gfx::Point> position) {
traffic_light_position_ = std::move(position); traffic_light_position_ = std::move(position);
@ -1579,19 +1591,6 @@ bool NativeWindowMac::AddTabbedWindow(NativeWindow* window) {
return true; 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, void NativeWindowMac::SetAspectRatio(double aspect_ratio,
const gfx::Size& extra_size) { const gfx::Size& extra_size) {
NativeWindow::SetAspectRatio(aspect_ratio, 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, void NativeWindowMac::InternalSetParentWindow(NativeWindow* parent,
bool attach) { bool attach) {
if (is_modal()) if (is_modal())

Просмотреть файл

@ -1517,15 +1517,40 @@ describe('BrowserWindow module', () => {
}).to.not.throw(); }).to.not.throw();
}); });
it('throws with custom title bar buttons', () => { it('changes window button visibility for normal window', () => {
expect(() => { const w = new BrowserWindow({ show: false });
const w = new BrowserWindow({ expect(w._getWindowButtonVisibility()).to.equal(true);
show: false, w.setWindowButtonVisibility(false);
titleBarStyle: 'customButtonsOnHover', expect(w._getWindowButtonVisibility()).to.equal(false);
frame: false w.setWindowButtonVisibility(true);
}); expect(w._getWindowButtonVisibility()).to.equal(true);
w.setWindowButtonVisibility(true); });
}).to.throw('Not supported for this window');
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 поставляемый
Просмотреть файл

@ -31,6 +31,7 @@ declare namespace Electron {
_setTouchBarItems: (items: TouchBarItemType[]) => void; _setTouchBarItems: (items: TouchBarItemType[]) => void;
_setEscapeTouchBarItem: (item: TouchBarItemType | {}) => void; _setEscapeTouchBarItem: (item: TouchBarItemType | {}) => void;
_refreshTouchBarItem: (itemID: string) => void; _refreshTouchBarItem: (itemID: string) => void;
_getWindowButtonVisibility: () => boolean;
frameName: string; frameName: string;
on(event: '-touch-bar-interaction', listener: (event: Event, itemID: string, details: any) => void): this; 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; removeListener(event: '-touch-bar-interaction', listener: (event: Event, itemID: string, details: any) => void): this;