throw an error for out of bounds window levels

This commit is contained in:
leethomas 2017-01-24 20:08:08 -08:00
parent fb741285c6
commit 1f5518b91e
6 changed files with 20 additions and 8 deletions

View file

@ -513,9 +513,16 @@ bool Window::IsClosable() {
void Window::SetAlwaysOnTop(bool top, mate::Arguments* args) {
std::string level = "floating";
int relativeLevel = 0;
std::string error;
args->GetNext(&level);
args->GetNext(&relativeLevel);
window_->SetAlwaysOnTop(top, level, relativeLevel);
window_->SetAlwaysOnTop(top, level, relativeLevel, &error);
if (!error.empty()) {
args->ThrowError(error);
}
}
bool Window::IsAlwaysOnTop() {

View file

@ -120,7 +120,8 @@ class NativeWindow : public base::SupportsUserData,
virtual bool IsClosable() = 0;
virtual void SetAlwaysOnTop(bool top,
const std::string& level = "floating",
int relativeLevel = 0) = 0;
int relativeLevel = 0,
std::string* error = nullptr) = 0;
virtual bool IsAlwaysOnTop() = 0;
virtual void Center() = 0;
virtual void SetTitle(const std::string& title) = 0;

View file

@ -68,7 +68,7 @@ class NativeWindowMac : public NativeWindow,
void SetClosable(bool closable) override;
bool IsClosable() override;
void SetAlwaysOnTop(bool top, const std::string& level,
int relativeLevel) override;
int relativeLevel, std::string* error) override;
bool IsAlwaysOnTop() override;
void Center() override;
void SetTitle(const std::string& title) override;

View file

@ -1056,8 +1056,10 @@ bool NativeWindowMac::IsClosable() {
return [window_ styleMask] & NSClosableWindowMask;
}
void NativeWindowMac::SetAlwaysOnTop(bool top, const std::string& level, int relativeLevel) {
void NativeWindowMac::SetAlwaysOnTop(bool top, const std::string& level, int relativeLevel,
std::string* error) {
int windowLevel = NSNormalWindowLevel;
CGWindowLevel maxWindowLevel = CGWindowLevelForKey(kCGMaximumWindowLevelKey);
if (top) {
if (level == "floating") {
@ -1081,10 +1083,12 @@ void NativeWindowMac::SetAlwaysOnTop(bool top, const std::string& level, int rel
}
NSInteger newLevel = windowLevel + relativeLevel;
if (newLevel >= 0 && newLevel < CGWindowLevelForKey(kCGMaximumWindowLevelKey)) {
if (newLevel >= 0 && newLevel <= maxWindowLevel) {
[window_ setLevel:newLevel];
} else {
[window_ setLevel:windowLevel];
*error = std::string([[NSString stringWithFormat:
@"relativeLevel must be between 0 and %d", maxWindowLevel] UTF8String]);
}
}

View file

@ -683,7 +683,7 @@ bool NativeWindowViews::IsClosable() {
}
void NativeWindowViews::SetAlwaysOnTop(bool top, const std::string& level,
int relativeLevel) {
int relativeLevel, std::string* error) {
window_->SetAlwaysOnTop(top);
}

View file

@ -87,7 +87,7 @@ class NativeWindowViews : public NativeWindow,
void SetClosable(bool closable) override;
bool IsClosable() override;
void SetAlwaysOnTop(bool top, const std::string& level,
int relativeLevel) override;
int relativeLevel, std::string& error) override;
bool IsAlwaysOnTop() override;
void Center() override;
void SetTitle(const std::string& title) override;