refactor: add gin_helper::Dictionary::ValueOrDefault() (#46939)

* feat: add gin_helper::Dictionary::ValueOrDefault()

A convenience function for using a default value if the
specified key isn't present in the dictionary.

* refactor: use ValueOrDefault() in native_window.cc

* refactor: use ValueOrDefault() in native_window_mac.mm

* refactor: use ValueOrDefault() in native_window_views.cc

* refactor: use ValueOrDefault() in electron_api_native_image.cc
This commit is contained in:
Charles Kerr 2025-05-06 15:20:12 -05:00 committed by GitHub
commit b7ae162716
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 48 additions and 54 deletions

View file

@ -382,12 +382,9 @@ gin::Handle<NativeImage> NativeImage::Crop(v8::Isolate* isolate,
}
void NativeImage::AddRepresentation(const gin_helper::Dictionary& options) {
int width = 0;
int height = 0;
float scale_factor = 1.0f;
options.Get("width", &width);
options.Get("height", &height);
options.Get("scaleFactor", &scale_factor);
const int width = options.ValueOrDefault("width", 0);
const int height = options.ValueOrDefault("height", 0);
const float scale_factor = options.ValueOrDefault("scaleFactor", 1.0F);
bool skia_rep_added = false;
gfx::ImageSkia image_skia = image_.AsImageSkia();
@ -515,8 +512,7 @@ gin::Handle<NativeImage> NativeImage::CreateFromBitmap(
bitmap.allocN32Pixels(width, height, false);
bitmap.writePixels({info, buffer_data.data(), bitmap.rowBytes()});
float scale_factor = 1.0F;
options.Get("scaleFactor", &scale_factor);
const float scale_factor = options.ValueOrDefault("scaleFactor", 1.0F);
gfx::ImageSkia image_skia =
gfx::ImageSkia::CreateFromBitmap(bitmap, scale_factor);