Skip to content

Commit

Permalink
address feedback from @jkleinsc
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Aug 9, 2019
1 parent afefc76 commit 624a259
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 68 deletions.
5 changes: 2 additions & 3 deletions docs/api/web-contents.md
Expand Up @@ -1264,9 +1264,8 @@ Returns [`PrinterInfo[]`](structures/printer-info.md).
* `dpi` Object (optional)
* `horizontal` Number (optional) - The horizontal dpi.
* `vertical` Number (optional) - The vertical dpi.
* `headerFooterInfo` Object (optional)
* `header` String - String to be printed as page header.
* `footer` String - String to be printed as page footer.
* `header` String - String to be printed as page header.
* `footer` String - String to be printed as page footer.
* `callback` Function (optional)
* `success` Boolean - Indicates success of the print call.
* `failureReason` String - Called back if the print fails; can be `cancelled` or `failed`.
Expand Down
131 changes: 66 additions & 65 deletions shell/browser/api/atom_api_web_contents.cc
Expand Up @@ -1674,78 +1674,79 @@ void WebContents::Print(mate::Arguments* args) {
settings.SetInteger(printing::kSettingCopies, copies);

// Strings to be printed as headers and footers if requested by the user.
mate::Dictionary header_footer;
if (options.Get("headerFooterInfo", &header_footer)) {
settings.SetBoolean(printing::kSettingHeaderFooterEnabled, true);
std::string header;
options.Get("header", &header);
std::string footer;
options.Get("footer", &footer);

std::string header;
header_footer.Get("header", &header);
std::string footer;
header_footer.Get("footer", &footer);
if (!(header.empty() && footer.empty())) {
settings.SetBoolean(printing::kSettingHeaderFooterEnabled, true);

settings.SetString(printing::kSettingHeaderFooterTitle, header);
settings.SetString(printing::kSettingHeaderFooterURL, footer);
}
}

// We don't want to allow the user to enable these settings
// but we need to set them or a CHECK is hit.
settings.SetBoolean(printing::kSettingPrintToPDF, false);
settings.SetBoolean(printing::kSettingCloudPrintDialog, false);
settings.SetBoolean(printing::kSettingPrintWithPrivet, false);
settings.SetBoolean(printing::kSettingShouldPrintSelectionOnly, false);
settings.SetBoolean(printing::kSettingPrintWithExtension, false);
settings.SetBoolean(printing::kSettingRasterizePdf, false);

// Set custom page ranges to print
std::vector<mate::Dictionary> page_ranges;
if (options.Get("pageRanges", &page_ranges)) {
std::unique_ptr<base::ListValue> page_range_list(new base::ListValue());
for (size_t i = 0; i < page_ranges.size(); ++i) {
int from, to;
if (page_ranges[i].Get("from", &from) && page_ranges[i].Get("to", &to)) {
std::unique_ptr<base::DictionaryValue> range(
new base::DictionaryValue());
range->SetInteger(printing::kSettingPageRangeFrom, from);
range->SetInteger(printing::kSettingPageRangeTo, to);
page_range_list->Append(std::move(range));
} else {
continue;
}
// We don't want to allow the user to enable these settings
// but we need to set them or a CHECK is hit.
settings.SetBoolean(printing::kSettingPrintToPDF, false);
settings.SetBoolean(printing::kSettingCloudPrintDialog, false);
settings.SetBoolean(printing::kSettingPrintWithPrivet, false);
settings.SetBoolean(printing::kSettingShouldPrintSelectionOnly, false);
settings.SetBoolean(printing::kSettingPrintWithExtension, false);
settings.SetBoolean(printing::kSettingRasterizePdf, false);

// Set custom page ranges to print
std::vector<mate::Dictionary> page_ranges;
if (options.Get("pageRanges", &page_ranges)) {
std::unique_ptr<base::ListValue> page_range_list(new base::ListValue());
for (size_t i = 0; i < page_ranges.size(); ++i) {
int from, to;
if (page_ranges[i].Get("from", &from) && page_ranges[i].Get("to", &to)) {
std::unique_ptr<base::DictionaryValue> range(new base::DictionaryValue());
range->SetInteger(printing::kSettingPageRangeFrom, from);
range->SetInteger(printing::kSettingPageRangeTo, to);
page_range_list->Append(std::move(range));
} else {
continue;
}
if (page_range_list->GetSize() > 0)
settings.SetList(printing::kSettingPageRange, std::move(page_range_list));
}

// Duplex type user wants to use.
printing::DuplexMode duplex_mode;
options.Get("duplexMode", &duplex_mode);
settings.SetInteger(printing::kSettingDuplexMode, duplex_mode);

// Set custom dots per inch (dpi)
mate::Dictionary dpi_settings;
int dpi = 72;
if (options.Get("dpi", &dpi_settings)) {
int horizontal = 72;
dpi_settings.Get("horizontal", &horizontal);
settings.SetInteger(printing::kSettingDpiHorizontal, horizontal);
int vertical = 72;
dpi_settings.Get("vertical", &vertical);
settings.SetInteger(printing::kSettingDpiVertical, vertical);
} else {
settings.SetInteger(printing::kSettingDpiHorizontal, dpi);
settings.SetInteger(printing::kSettingDpiVertical, dpi);
}

auto* print_view_manager =
printing::PrintViewManagerBasic::FromWebContents(web_contents());
auto* focused_frame = web_contents()->GetFocusedFrame();
auto* rfh = focused_frame && focused_frame->HasSelection()
? focused_frame
: web_contents()->GetMainFrame();
print_view_manager->PrintNow(rfh,
std::make_unique<PrintMsg_PrintPages>(
rfh->GetRoutingID(), silent, settings),
std::move(callback));
if (page_range_list->GetSize() > 0)
settings.SetList(printing::kSettingPageRange, std::move(page_range_list));
}

// Duplex type user wants to use.
printing::DuplexMode duplex_mode;
options.Get("duplexMode", &duplex_mode);
settings.SetInteger(printing::kSettingDuplexMode, duplex_mode);

// Set custom dots per inch (dpi)
mate::Dictionary dpi_settings;
int dpi = 72;
if (options.Get("dpi", &dpi_settings)) {
int horizontal = 72;
dpi_settings.Get("horizontal", &horizontal);
settings.SetInteger(printing::kSettingDpiHorizontal, horizontal);
int vertical = 72;
dpi_settings.Get("vertical", &vertical);
settings.SetInteger(printing::kSettingDpiVertical, vertical);
} else {
settings.SetInteger(printing::kSettingDpiHorizontal, dpi);
settings.SetInteger(printing::kSettingDpiVertical, dpi);
}

auto* print_view_manager =
printing::PrintViewManagerBasic::FromWebContents(web_contents());
auto* focused_frame = web_contents() -> GetFocusedFrame();
auto* rfh = focused_frame && focused_frame -> HasSelection()
? focused_frame
: web_contents() -> GetMainFrame();
print_view_manager->PrintNow(
rfh,
std::make_unique<PrintMsg_PrintPages>(rfh->GetRoutingID(),
silent,
settings),
std::move(callback));
}

std::vector<printing::PrinterBasicInfo> WebContents::GetPrinterList() {
Expand Down Expand Up @@ -2545,7 +2546,7 @@ mate::Handle<WebContents> WebContents::FromOrCreate(
return mate::CreateHandle(isolate, new WebContents(isolate, web_contents));
}

} // namespace api
} // namespace electron

} // namespace electron

Expand Down

0 comments on commit 624a259

Please sign in to comment.