Skip to content

Commit

Permalink
fix(desktop/main.cc): check for invalid values for the application wi…
Browse files Browse the repository at this point in the history
…ndow sizes
  • Loading branch information
chicoxyzzy committed Aug 9, 2023
1 parent 9726c77 commit b7c137f
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions src/desktop/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ MAIN {
}

auto screen = currentWindow->getScreenSize();

options.width = message.get("width").size() ? currentWindow->getSizeInPixels(message.get("width"), screen.width) : 0;
options.height = message.get("height").size() ? currentWindow->getSizeInPixels(message.get("height"), screen.height) : 0;

Expand Down Expand Up @@ -953,20 +953,46 @@ MAIN {

app.onExit = shutdownHandler;

String defaultWidth = app.appData["window_width"].size() > 0 ? app.appData["window_width"] : "100%";
String defaultHeight = app.appData["window_height"].size() > 0 ? app.appData["window_height"] : "100%";
String defaultMinWidth = app.appData["window_min_width"].size() > 0 ? app.appData["window_min_width"] : "0";
String defaultMinHeight = app.appData["window_min_height"].size() > 0 ? app.appData["window_min_height"] : "0";
String defaultMaxWidth = app.appData["window_max_width"].size() > 0 ? app.appData["window_max_width"] : "100%";
String defaultMaxHeight = app.appData["window_max_height"].size() > 0 ? app.appData["window_max_height"] : "100%";
Vector<String> properties = {
"window_width", "window_height",
"window_min_width", "window_min_height",
"window_max_width", "window_max_height"
};

auto setDefaultValue = [&](String property) {
// for min values set 0
if (property.find("min") != -1) {
return "0";
// for max other values set 100%
} else {
return "100%";
}
};

// Regular expression to match a float number or a percentage
std::regex validPattern("^\\d*\\.?\\d+%?$");

for (const auto& property : properties) {
if (app.appData[property].size() > 0) {
auto value = app.appData[property];
if (!std::regex_match(value, validPattern)) {
stdWrite("Invalid value for " + property + ": " + value, true);
app.appData[property] = setDefaultValue(property);
stdWrite("Setting default value: " + app.appData[property], true);
}
// set default values
} else {
app.appData[property] = setDefaultValue(property);
}
}

windowManager.configure(WindowManagerOptions {
.defaultHeight = defaultHeight,
.defaultWidth = defaultWidth,
.defaultMinWidth = defaultMinWidth,
.defaultMinHeight = defaultMinHeight,
.defaultMaxWidth = defaultMaxWidth,
.defaultMaxHeight = defaultMaxHeight,
.defaultHeight = app.appData["window_height"],
.defaultWidth = app.appData["window_width"],
.defaultMinWidth = app.appData["window_min_width"],
.defaultMinHeight = app.appData["window_min_height"],
.defaultMaxWidth = app.appData["window_max_width"],
.defaultMaxHeight = app.appData["window_max_height"],
.headless = isHeadless,
.isTest = isTest,
.argv = argvArray.str(),
Expand Down

0 comments on commit b7c137f

Please sign in to comment.